PhpBundleWriter.php 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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($path, $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 \(/', 'array(', $data);
  39. $data = preg_replace('/\n {1,10}array\(/', 'array(', $data);
  40. $data = preg_replace('/ /', ' ', $data);
  41. $data = sprintf($template, $data);
  42. file_put_contents($path.'/'.$locale.'.php', $data);
  43. }
  44. }