<?php defined('SYSPATH') or die('No direct script access.');
/**
* Words aren't enough sometimes, you need some sarcasm to do the trick
*
* @since 1.0
* @package Commoneer
* @subpackage Exception
*/
class Exception_Sarcasm extends Commoneer_Exception
{
/**
* @var int Sarcasm error code
*/
protected $_code = 666;
/**
* Construct a new Exception_Sarcasm
*
* @param string $message
* @param array $variables
* @param int $code
*/
public function __construct($message = 'Oh? Did you honestly just try to do that?', array $variables = array(), $code = 666)
{
parent::__construct($message, $variables, $code);
}
}
public class ArrayUtil {
private static final Object[] EMPTY = new Object[0]
;
public static Object[] createArray() {
return EMPTY;
}
public static Object[] createArray(Object arg0) {
return new Object[]{
arg0};
}
public static Object[] createArray(Object arg0, Object arg1) {
return new Object[]{
arg0, arg1};
}
public static Object[] createArray(Object arg0, Object arg1, Object arg2) {
return new Object[]{
arg0, arg1, arg2};
}
public static Object[] createArray(Object arg0, Object arg1, Object arg2, Object arg3) {
return new Object[]{
arg0, arg1, arg2, arg3};
}
ИМХО таких вещей не должно быть в исходном коде, они должны создаваться генератором во время сборки.Вообще, там коммент сверху поясняет, что это внутренняя оптимизация, и почему так лучше.
public function changeToSubclass($subclass) {
// ugly hack. Nope... two ugly hacks
class_exists($subclass);
if (get_class($this) == $subclass) {
return $this;
}
${'th'.'is'} = unserialize(
preg_replace(
'/^O:[0-9]+:"[^"]+":/',
'O:' . strlen($subclass) . ':"' . $subclass . '":',
serialize($this)
)
);
return $this;
}
public function load($locale, $filename)
{
if (!is_file($filename) || !is_readable($filename)) {
throw new Exception\InvalidArgumentException(sprintf(
'Could not open file %s for reading',
$filename
));
}
$textDomain = new TextDomain();
ErrorHandler::start();
$this->file = fopen($filename, 'rb');
$error = ErrorHandler::stop();
if (false === $this->file) {
throw new Exception\InvalidArgumentException(sprintf(
'Could not open file %s for reading',
$filename
), 0, $error);
}
// Verify magic number
$magic = fread($this->file, 4);
if ($magic == "\x95\x04\x12\xde") {
$this->littleEndian = false;
} elseif ($magic == "\xde\x12\x04\x95") {
$this->littleEndian = true;
} else {
fclose($this->file);
throw new Exception\InvalidArgumentException(sprintf(
'%s is not a valid gettext file',
$filename
));
}
// Verify major revision (only 0 and 1 supported)
$majorRevision = ($this->readInteger() >> 16);
if ($majorRevision !== 0 && $majorRevision !== 1) {
fclose($this->file);
throw new Exception\InvalidArgumentException(sprintf(
'%s has an unknown major revision',
$filename
));
}
// Gather main information
$numStrings = $this->readInteger();
$originalStringTableOffset = $this->readInteger();
$translationStringTableOffset = $this->readInteger();
// Usually there follow size and offset of the hash table, but we have
// no need for it, so we skip them.
fseek($this->file, $originalStringTableOffset);
$originalStringTable = $this->readIntegerList(2 * $numStrings);
fseek($this->file, $translationStringTableOffset);
$translationStringTable = $this->readIntegerList(2 * $numStrings);
// Read in all translations
for ($current = 0; $current < $numStrings; $current++) {
$sizeKey = $current * 2 + 1;
$offsetKey = $current * 2 + 2;
$originalStringSize = $originalStringTable[$sizeKey];
$originalStringOffset = $originalStringTable[$offsetKey];
$translationStringSize = $translationStringTable[$sizeKey];
$translationStringOffset = $translationStringTable[$offsetKey];
$originalString = array('');
if ($originalStringSize > 0) {
fseek($this->file, $originalStringOffset);
$originalString = explode("\0", fread($this->file, $originalStringSize));
}
if ($translationStringSize > 0) {
fseek($this->file, $translationStringOffset);
$translationString = explode("\0", fread($this->file, $translationStringSize));
if (count($originalString) > 1 && count($translationString) > 1) {
$textDomain[$originalString[0]] = $translationString;
array_shift($originalString);
foreach ($originalString as $string) {
$textDomain[$string] = '';
}
} else {
$textDomain[$originalString[0]] = $translationString[0];
}
}
}
// Read header entries
if (array_key_exists('', $textDomain)) {
$rawHeaders = explode("\n", trim($textDomain['']));
foreach ($rawHeaders as $rawHeader) {
list($header, $content) = explode(':', $rawHeader, 2);
if (trim(strtolower($header)) === 'plural-forms') {
$textDomain->setPluralRule(PluralRule::fromString($content));
}
}
unset($textDomain['']);
}
fclose($this->file);
return $textDomain;
}