BundleCompiler.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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\ResourceBundle\Compiler;
  11. use Symfony\Component\Intl\Exception\RuntimeException;
  12. /**
  13. * Compiles .txt resource bundles to binary .res files.
  14. *
  15. * @author Bernhard Schussek <bschussek@gmail.com>
  16. */
  17. class BundleCompiler implements BundleCompilerInterface
  18. {
  19. /**
  20. * @var string The path to the "genrb" executable.
  21. */
  22. private $genrb;
  23. /**
  24. * Creates a new compiler based on the "genrb" executable.
  25. *
  26. * @param string $genrb Optional. The path to the "genrb" executable.
  27. * @param string $envVars Optional. Environment variables to be loaded when
  28. * running "genrb".
  29. *
  30. * @throws RuntimeException If the "genrb" cannot be found.
  31. */
  32. public function __construct($genrb = 'genrb', $envVars = '')
  33. {
  34. exec('which ' . $genrb, $output, $status);
  35. if (0 !== $status) {
  36. throw new RuntimeException(sprintf(
  37. 'The command "%s" is not installed',
  38. $genrb
  39. ));
  40. }
  41. $this->genrb = ($envVars ? $envVars . ' ' : '') . $genrb;
  42. }
  43. /**
  44. * {@inheritdoc}
  45. */
  46. public function compile($sourcePath, $targetDir)
  47. {
  48. if (is_dir($sourcePath)) {
  49. $sourcePath .= '/*.txt';
  50. }
  51. exec($this->genrb.' --quiet -e UTF-8 -d '.$targetDir.' '.$sourcePath, $output, $status);
  52. if ($status !== 0) {
  53. throw new RuntimeException(sprintf(
  54. 'genrb failed with status %d while compiling %s to %s.',
  55. $status,
  56. $sourcePath,
  57. $targetDir
  58. ));
  59. }
  60. }
  61. }