composer package updates
[openemr.git] / vendor / symfony / translation / Loader / MoFileLoader.php
blob928cc9dfd592cde1d76acb8c4f492f7a2a7332c6
1 <?php
3 /*
4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Translation\Loader;
14 use Symfony\Component\Translation\Exception\InvalidResourceException;
16 /**
17 * @copyright Copyright (c) 2010, Union of RAD http://union-of-rad.org (http://lithify.me/)
19 class MoFileLoader extends FileLoader
21 /**
22 * Magic used for validating the format of a MO file as well as
23 * detecting if the machine used to create that file was little endian.
25 * @var float
27 const MO_LITTLE_ENDIAN_MAGIC = 0x950412de;
29 /**
30 * Magic used for validating the format of a MO file as well as
31 * detecting if the machine used to create that file was big endian.
33 * @var float
35 const MO_BIG_ENDIAN_MAGIC = 0xde120495;
37 /**
38 * The size of the header of a MO file in bytes.
40 * @var int Number of bytes
42 const MO_HEADER_SIZE = 28;
44 /**
45 * Parses machine object (MO) format, independent of the machine's endian it
46 * was created on. Both 32bit and 64bit systems are supported.
48 * {@inheritdoc}
50 protected function loadResource($resource)
52 $stream = fopen($resource, 'r');
54 $stat = fstat($stream);
56 if ($stat['size'] < self::MO_HEADER_SIZE) {
57 throw new InvalidResourceException('MO stream content has an invalid format.');
59 $magic = unpack('V1', fread($stream, 4));
60 $magic = hexdec(substr(dechex(current($magic)), -8));
62 if ($magic == self::MO_LITTLE_ENDIAN_MAGIC) {
63 $isBigEndian = false;
64 } elseif ($magic == self::MO_BIG_ENDIAN_MAGIC) {
65 $isBigEndian = true;
66 } else {
67 throw new InvalidResourceException('MO stream content has an invalid format.');
70 // formatRevision
71 $this->readLong($stream, $isBigEndian);
72 $count = $this->readLong($stream, $isBigEndian);
73 $offsetId = $this->readLong($stream, $isBigEndian);
74 $offsetTranslated = $this->readLong($stream, $isBigEndian);
75 // sizeHashes
76 $this->readLong($stream, $isBigEndian);
77 // offsetHashes
78 $this->readLong($stream, $isBigEndian);
80 $messages = array();
82 for ($i = 0; $i < $count; ++$i) {
83 $pluralId = null;
84 $translated = null;
86 fseek($stream, $offsetId + $i * 8);
88 $length = $this->readLong($stream, $isBigEndian);
89 $offset = $this->readLong($stream, $isBigEndian);
91 if ($length < 1) {
92 continue;
95 fseek($stream, $offset);
96 $singularId = fread($stream, $length);
98 if (strpos($singularId, "\000") !== false) {
99 list($singularId, $pluralId) = explode("\000", $singularId);
102 fseek($stream, $offsetTranslated + $i * 8);
103 $length = $this->readLong($stream, $isBigEndian);
104 $offset = $this->readLong($stream, $isBigEndian);
106 if ($length < 1) {
107 continue;
110 fseek($stream, $offset);
111 $translated = fread($stream, $length);
113 if (strpos($translated, "\000") !== false) {
114 $translated = explode("\000", $translated);
117 $ids = array('singular' => $singularId, 'plural' => $pluralId);
118 $item = compact('ids', 'translated');
120 if (is_array($item['translated'])) {
121 $messages[$item['ids']['singular']] = stripcslashes($item['translated'][0]);
122 if (isset($item['ids']['plural'])) {
123 $plurals = array();
124 foreach ($item['translated'] as $plural => $translated) {
125 $plurals[] = sprintf('{%d} %s', $plural, $translated);
127 $messages[$item['ids']['plural']] = stripcslashes(implode('|', $plurals));
129 } elseif (!empty($item['ids']['singular'])) {
130 $messages[$item['ids']['singular']] = stripcslashes($item['translated']);
134 fclose($stream);
136 return array_filter($messages);
140 * Reads an unsigned long from stream respecting endianness.
142 * @param resource $stream
143 * @param bool $isBigEndian
145 * @return int
147 private function readLong($stream, $isBigEndian)
149 $result = unpack($isBigEndian ? 'N1' : 'V1', fread($stream, 4));
150 $result = current($result);
152 return (int) substr($result, -8);