/vendor/symfony/src/Symfony/Component/Translation/Loader/MoFileLoader.php

https://github.com/israelnoguera/parejas · PHP · 169 lines · 93 code · 31 blank · 45 comment · 13 complexity · 73e3ef283f174ad1c3a3bfe4b2ed24ae MD5 · raw file

  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Translation\Loader;
  11. use Symfony\Component\Config\Resource\FileResource;
  12. /**
  13. * @copyright Copyright (c) 2010, Union of RAD http://union-of-rad.org (http://lithify.me/)
  14. */
  15. class MoFileLoader extends ArrayLoader implements LoaderInterface
  16. {
  17. /**
  18. * Magic used for validating the format of a MO file as well as
  19. * detecting if the machine used to create that file was little endian.
  20. *
  21. * @var float
  22. */
  23. const MO_LITTLE_ENDIAN_MAGIC = 0x950412de;
  24. /**
  25. * Magic used for validating the format of a MO file as well as
  26. * detecting if the machine used to create that file was big endian.
  27. *
  28. * @var float
  29. */
  30. const MO_BIG_ENDIAN_MAGIC = 0xde120495;
  31. /**
  32. * The size of the header of a MO file in bytes.
  33. *
  34. * @var integer Number of bytes.
  35. */
  36. const MO_HEADER_SIZE = 28;
  37. public function load($resource, $locale, $domain = 'messages')
  38. {
  39. $messages = $this->parse($resource);
  40. // empty file
  41. if (null === $messages) {
  42. $messages = array();
  43. }
  44. // not an array
  45. if (!is_array($messages)) {
  46. throw new \InvalidArgumentException(sprintf('The file "%s" must contain a valid mo file.', $resource));
  47. }
  48. $catalogue = parent::load($messages, $locale, $domain);
  49. $catalogue->addResource(new FileResource($resource));
  50. return $catalogue;
  51. }
  52. /**
  53. * Parses machine object (MO) format, independent of the machine's endian it
  54. * was created on. Both 32bit and 64bit systems are supported.
  55. *
  56. * @param resource $stream
  57. * @return array
  58. * @throws InvalidArgumentException If stream content has an invalid format.
  59. */
  60. private function parse($resource)
  61. {
  62. $stream = fopen($resource, 'r');
  63. $stat = fstat($stream);
  64. if ($stat['size'] < self::MO_HEADER_SIZE) {
  65. throw new \InvalidArgumentException("MO stream content has an invalid format.");
  66. }
  67. $magic = unpack('V1', fread($stream, 4));
  68. $magic = hexdec(substr(dechex(current($magic)), -8));
  69. if ($magic == self::MO_LITTLE_ENDIAN_MAGIC) {
  70. $isBigEndian = false;
  71. } elseif ($magic == self::MO_BIG_ENDIAN_MAGIC) {
  72. $isBigEndian = true;
  73. } else {
  74. throw new \InvalidArgumentException("MO stream content has an invalid format.");
  75. }
  76. $header = array(
  77. 'formatRevision' => null,
  78. 'count' => null,
  79. 'offsetId' => null,
  80. 'offsetTranslated' => null,
  81. 'sizeHashes' => null,
  82. 'offsetHashes' => null,
  83. );
  84. foreach ($header as &$value) {
  85. $value = $this->readLong($stream, $isBigEndian);
  86. }
  87. extract($header);
  88. $messages = array();
  89. for ($i = 0; $i < $count; $i++) {
  90. $singularId = $pluralId = null;
  91. $translated = null;
  92. fseek($stream, $offsetId + $i * 8);
  93. $length = $this->readLong($stream, $isBigEndian);
  94. $offset = $this->readLong($stream, $isBigEndian);
  95. if ($length < 1) {
  96. continue;
  97. }
  98. fseek($stream, $offset);
  99. $singularId = fread($stream, $length);
  100. if (strpos($singularId, "\000") !== false) {
  101. list($singularId, $pluralId) = explode("\000", $singularId);
  102. }
  103. fseek($stream, $offsetTranslated + $i * 8);
  104. $length = $this->readLong($stream, $isBigEndian);
  105. $offset = $this->readLong($stream, $isBigEndian);
  106. fseek($stream, $offset);
  107. $translated = fread($stream, $length);
  108. if (strpos($translated, "\000") !== false) {
  109. $translated = explode("\000", $translated);
  110. }
  111. $ids = array('singular' => $singularId, 'plural' => $pluralId);
  112. $item = compact('ids', 'translated');
  113. if (is_array($item['translated'])) {
  114. $messages[$item['ids']['singular']] = stripslashes($item['translated'][0]);
  115. if (isset($item['ids']['plural'])) {
  116. $messages[$item['ids']['plural']] = stripslashes(end($item['translated']));
  117. }
  118. } elseif($item['ids']['singular']) {
  119. $messages[$item['ids']['singular']] = stripslashes($item['translated']);
  120. }
  121. }
  122. fclose($stream);
  123. return array_filter($messages);
  124. }
  125. /**
  126. * Reads an unsigned long from stream respecting endianess.
  127. *
  128. * @param resource $stream
  129. * @param boolean $isBigEndian
  130. * @return integer
  131. */
  132. private function readLong($stream, $isBigEndian)
  133. {
  134. $result = unpack($isBigEndian ? 'N1' : 'V1', fread($stream, 4));
  135. $result = current($result);
  136. return (integer) substr($result, -8);
  137. }
  138. }