PhpBundleWriter.php 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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\Intl\Data\Bundle\Writer;
  11. /**
  12. * Writes .php resource bundles.
  13. *
  14. * @author Bernhard Schussek <bschussek@gmail.com>
  15. *
  16. * @internal
  17. */
  18. class PhpBundleWriter implements BundleWriterInterface
  19. {
  20. /**
  21. * {@inheritdoc}
  22. */
  23. public function write(string $path, string $locale, $data)
  24. {
  25. $template = <<<'TEMPLATE'
  26. <?php
  27. return %s;
  28. TEMPLATE;
  29. if ($data instanceof \Traversable) {
  30. $data = iterator_to_array($data);
  31. }
  32. array_walk_recursive($data, function (&$value) {
  33. if ($value instanceof \Traversable) {
  34. $value = iterator_to_array($value);
  35. }
  36. });
  37. $data = var_export($data, true);
  38. $data = preg_replace('/array \(/', '[', $data);
  39. $data = preg_replace('/\n {1,10}\[/', '[', $data);
  40. $data = preg_replace('/ /', ' ', $data);
  41. $data = preg_replace('/\),$/m', '],', $data);
  42. $data = preg_replace('/\)$/', ']', $data);
  43. $data = sprintf($template, $data);
  44. file_put_contents($path.'/'.$locale.'.php', $data);
  45. }
  46. }