GenrbCompiler.php 1.7 KB

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