GenrbCompiler.php 1.9 KB

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