4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Intl\ResourceBundle\Transformer
;
14 use Symfony\Component\Intl\Exception\RuntimeException
;
15 use Symfony\Component\Intl\ResourceBundle\Transformer\Rule\TransformationRuleInterface
;
16 use Symfony\Component\Intl\ResourceBundle\Writer\PhpBundleWriter
;
19 * Compiles a number of resource bundles based on predefined compilation rules.
21 * @author Bernhard Schussek <bschussek@gmail.com>
23 class BundleTransformer
26 * @var TransformationRuleInterface[]
28 private $rules = array();
31 * Adds a new compilation rule.
33 * @param TransformationRuleInterface $rule The compilation rule.
35 public function addRule(TransformationRuleInterface
$rule)
37 $this->rules
[] = $rule;
41 * Runs the compilation with the given compilation context.
43 * @param CompilationContextInterface $context The context storing information
44 * needed to run the compilation.
46 * @throws RuntimeException If any of the files to be compiled by the loaded
47 * compilation rules does not exist.
49 public function compileBundles(CompilationContextInterface
$context)
51 $filesystem = $context->getFilesystem();
52 $compiler = $context->getCompiler();
54 $filesystem->remove($context->getBinaryDir());
55 $filesystem->mkdir($context->getBinaryDir());
57 foreach ($this->rules
as $rule) {
58 $filesystem->mkdir($context->getBinaryDir() . '/' . $rule->getBundleName());
60 $resources = (array) $rule->beforeCompile($context);
62 foreach ($resources as $resource) {
63 if (!file_exists($resource)) {
64 throw new RuntimeException(sprintf(
65 'The file "%s" to be compiled by %s does not exist.',
71 $compiler->compile($resource, $context->getBinaryDir() . '/' . $rule->getBundleName());
74 $rule->afterCompile($context);
78 public function createStubs(StubbingContextInterface
$context)
80 $filesystem = $context->getFilesystem();
81 $phpWriter = new PhpBundleWriter();
83 $filesystem->remove($context->getStubDir());
84 $filesystem->mkdir($context->getStubDir());
86 foreach ($this->rules
as $rule) {
87 $filesystem->mkdir($context->getStubDir() . '/' . $rule->getBundleName());
89 $data = $rule->beforeCreateStub($context);
91 $phpWriter->write($context->getStubDir() . '/' . $rule->getBundleName(), 'en', $data);
93 $rule->afterCreateStub($context);