aboutsummaryrefslogtreecommitdiffhomepage
path: root/vendor/symfony/intl/Symfony/Component/Intl/ResourceBundle/Transformer/BundleTransformer.php
blob: 0692d6fe50a05981c611593dded24890dd941340 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
<?php

/*
 * This file is part of the Symfony package.
 *
 * (c) Fabien Potencier <fabien@symfony.com>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Symfony\Component\Intl\ResourceBundle\Transformer;

use Symfony\Component\Intl\Exception\RuntimeException;
use Symfony\Component\Intl\ResourceBundle\Transformer\Rule\TransformationRuleInterface;
use Symfony\Component\Intl\ResourceBundle\Writer\PhpBundleWriter;

/**
 * Compiles a number of resource bundles based on predefined compilation rules.
 *
 * @author Bernhard Schussek <bschussek@gmail.com>
 */
class BundleTransformer
{
    /**
     * @var TransformationRuleInterface[]
     */
    private $rules = array();

    /**
     * Adds a new compilation rule.
     *
     * @param TransformationRuleInterface $rule The compilation rule.
     */
    public function addRule(TransformationRuleInterface $rule)
    {
        $this->rules[] = $rule;
    }

    /**
     * Runs the compilation with the given compilation context.
     *
     * @param CompilationContextInterface $context The context storing information
     *                                             needed to run the compilation.
     *
     * @throws RuntimeException If any of the files to be compiled by the loaded
     *                          compilation rules does not exist.
     */
    public function compileBundles(CompilationContextInterface $context)
    {
        $filesystem = $context->getFilesystem();
        $compiler = $context->getCompiler();

        $filesystem->remove($context->getBinaryDir());
        $filesystem->mkdir($context->getBinaryDir());

        foreach ($this->rules as $rule) {
            $filesystem->mkdir($context->getBinaryDir() . '/' . $rule->getBundleName());

            $resources = (array) $rule->beforeCompile($context);

            foreach ($resources as $resource) {
                if (!file_exists($resource)) {
                    throw new RuntimeException(sprintf(
                        'The file "%s" to be compiled by %s does not exist.',
                        $resource,
                        get_class($rule)
                    ));
                }

                $compiler->compile($resource, $context->getBinaryDir() . '/' . $rule->getBundleName());
            }

            $rule->afterCompile($context);
        }
    }

    public function createStubs(StubbingContextInterface $context)
    {
        $filesystem = $context->getFilesystem();
        $phpWriter = new PhpBundleWriter();

        $filesystem->remove($context->getStubDir());
        $filesystem->mkdir($context->getStubDir());

        foreach ($this->rules as $rule) {
            $filesystem->mkdir($context->getStubDir() . '/' . $rule->getBundleName());

            $data = $rule->beforeCreateStub($context);

            $phpWriter->write($context->getStubDir() . '/' . $rule->getBundleName(), 'en', $data);

            $rule->afterCreateStub($context);
        }
    }
}