BaseBuilder::getGenerator()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Admingenerator\GeneratorBundle\Builder;
4
5
use Symfony\Component\Templating\TemplateNameParser;
6
use Symfony\Component\Config\FileLocator;
7
use Symfony\Bundle\FrameworkBundle\Templating\Loader\TemplateLocator;
8
use Symfony\Bundle\TwigBundle\Loader\FilesystemLoader;
9
use Symfony\Component\HttpFoundation\ParameterBag;
10
use TwigGenerator\Builder\BaseBuilder as GenericBaseBuilder;
11
use TwigGenerator\Builder\Generator as GenericBaseGenerator;
12
13
abstract class BaseBuilder extends GenericBaseBuilder
14
{
15
    /**
16
     * @var \Admingenerator\GeneratorBundle\Builder\Generator    The generator.
17
     */
18
    protected $generator;
19
20
    /**
21
     * @var array
22
     */
23
    protected $templatesToGenerate = array();
24
25
    /**
26
     * @var \Symfony\Component\HttpFoundation\ParameterBag
27
     */
28
    protected $variables;
29
30
    public function __construct()
31
    {
32
        parent::__construct();
33
        $this->variables = new ParameterBag(array());
34
        $this->twigFilters[] = '\\Doctrine\\Common\\Util\\Inflector::classify';
35
    }
36
37
    /**
38
     * Set files to generate
39
     *
40
     * @param array $templatesToGenerate (key => template file; value => output file name)
41
     */
42
    public function setTemplatesToGenerate(array $templatesToGenerate)
43
    {
44
        $this->templatesToGenerate = $templatesToGenerate;
45
    }
46
47
    /**
48
     * Add a file to generate
49
     *
50
     * @param string $template
51
     * @param string $outputName
52
     */
53
    public function addTemplateToGenerate($template, $outputName)
54
    {
55
        $this->templatesToGenerate[$template] = $outputName;
56
    }
57
58
    /**
59
     * Retrieve files to generate.
60
     *
61
     * @return array
62
     */
63
    public function getTemplatesToGenerate()
64
    {
65
        return $this->templatesToGenerate;
66
    }
67
68
    /**
69
     * Check if builder must generate multiple files
70
     * based on templatesToGenerate property.
71
     *
72
     * @return boolean
73
     */
74
    public function isMultiTemplatesBuilder()
75
    {
76
        $tmp = $this->getTemplatesToGenerate();
77
78
        return !empty($tmp);
79
    }
80
81
    /**
82
     * (non-PHPdoc)
83
     * @see \TwigGenerator\Builder\BaseBuilder::writeOnDisk()
84
     */
85
    public function writeOnDisk($outputDirectory)
86
    {
87
        if ($this->isMultiTemplatesBuilder()) {
88
            foreach ($this->getTemplatesToGenerate() as $templateName => $outputName) {
89
                $this->setOutputName($outputName);
90
                $this->setTemplateName($templateName);
91
                parent::writeOnDisk($outputDirectory);
92
            }
93
        } else {
94
            parent::writeOnDisk($outputDirectory);
95
        }
96
    }
97
98
    protected function getTwigEnvironment()
99
    {
100
        $locator = new TemplateLocator(new FileLocator($this->getTemplateDirs()));
101
        $templateNameParser = new TemplateNameParser();
102
        $loader = new FilesystemLoader($locator, $templateNameParser);
103
        $twig = new \Twig_Environment($loader, array(
0 ignored issues
show
Deprecated Code introduced by
The class Twig_Environment has been deprecated with message: since Twig 2.7, use "Twig\Environment" instead

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
104
            'autoescape' => false,
105
            'strict_variables' => true,
106
            'debug' => true,
107
            'cache' => $this->getGenerator()->getTempDir(),
108
        ));
109
110
        $this->loadTwigExtensions($twig);
111
        $this->loadTwigFilters($twig);
112
113
        return $twig;
114
    }
115
116
    /**
117
     * @return string the YamlKey
118
     */
119
    public function getYamlKey()
120
    {
121
        return $this->getSimpleClassName();
122
    }
123
124
    public function setVariables(array $variables)
125
    {
126
        $variables = new ParameterBag($variables);
127
        $this->variables = $variables;
128
    }
129
130
    /**
131
     * (non-PHPdoc)
132
     * @see Builder/Admingenerator\GeneratorBundle\Builder.BuilderInterface::getVariables()
133
     */
134
    public function getVariables()
135
    {
136
        return $this->variables->all();
137
    }
138
139
    /**
140
     * (non-PHPdoc)
141
     * @see Builder/Admingenerator\GeneratorBundle\Builder.BuilderInterface::hasVariable()
142
     * @param string $key
143
     * @return bool
144
     */
145
    public function hasVariable($key)
146
    {
147
        return $this->variables->has($key);
148
    }
149
150
    /**
151
     * (non-PHPdoc)
152
     * @see Builder/Admingenerator\GeneratorBundle\Builder.BuilderInterface::getVariable()
153
     */
154
    public function getVariable($key, $default = null, $deep = false)
155
    {
156
        return $this->variables->get($key, $default, $deep);
0 ignored issues
show
Unused Code introduced by
The call to ParameterBag::get() has too many arguments starting with $deep.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
157
    }
158
159
    /**
160
     * Get model class from model param
161
     * @return string
162
     */
163
    public function getModelClass()
164
    {
165
        return $this->getSimpleClassName($this->getVariable('model'));
166
    }
167
168
    /**
169
     * Set the generator.
170
     *
171
     * @param \TwigGenerator\Builder\Generator $generator A generator.
172
     */
173
    public function setGenerator(GenericBaseGenerator $generator)
174
    {
175
        if (!$generator instanceof Generator) {
176
            throw new \LogicException(
177
                '$generator must be an instance of Admingenerator\GeneratorBundle\Builder\Generator, '
178
               .'other instances are not supported.'
179
            );
180
        }
181
182
        $this->generator = $generator;
183
    }
184
185
    /**
186
     * Return the generator.
187
     *
188
     * @return \Admingenerator\GeneratorBundle\Builder\Generator    The generator.
189
     */
190
    public function getGenerator()
191
    {
192
        return $this->generator;
193
    }
194
}
195