Generator::getCacheKey()   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\Generator;
4
5
use Symfony\Component\Finder\Finder;
6
use Symfony\Component\Routing\RouterInterface;
7
use Symfony\Component\HttpKernel\KernelInterface;
8
use Admingenerator\GeneratorBundle\Validator\ValidatorInterface;
9
use Admingenerator\GeneratorBundle\Builder\Generator as AdminGenerator;
10
use Doctrine\Common\Cache as DoctrineCache;
11
use Twig_Environment as TwigEnvironment;
12
13
abstract class Generator implements GeneratorInterface
14
{
15
    /**
16
     * @var string
17
     */
18
    protected $root_dir;
19
20
    /**
21
     * @var string
22
     */
23
    protected $cache_dir;
24
25
    /**
26
     * @var string
27
     */
28
    protected $generator_yaml;
29
30
    /**
31
     * @var array $bundleConfig Generator bundle config.
32
     */
33
    protected $bundleConfig;
34
35
    /**
36
     * @var object $fieldGuesser The fieldguesser.
37
     */
38
    protected $fieldGuesser;
39
40
    /**
41
     * @var string
42
     */
43
    protected $baseGeneratorName;
44
45
    /**
46
     * @var array
47
     */
48
    protected $validators = array();
49
50
    /**
51
     * @var DoctrineCache\CacheProvider
52
     */
53
    protected $cacheProvider;
54
55
    /**
56
     * @var string
57
     */
58
    protected $cacheSuffix = 'default';
59
60
    /**
61
     * @var array
62
     */
63
    protected $templatesDirectories = array();
64
65
    /**
66
     * @var bool
67
     */
68
    protected $overwriteIfExists = false;
69
70
    /**
71
     * @var RouterInterface
72
     */
73
    protected $router;
74
    
75
    /**
76
     * @var TwigEnvironment
77
     */
78
    protected $twig;
79
80
    /**
81
     * @var KernelInterface
82
     */
83
    protected $kernel;
84
85
    /**
86
     * @param $root_dir
87
     * @param $cache_dir
88
     */
89
    public function __construct($root_dir, $cache_dir)
90
    {
91
        $this->root_dir = $root_dir;
92
        $this->cache_dir = $cache_dir;
93
        $this->cacheProvider = new DoctrineCache\ArrayCache();
94
    }
95
96
    /**
97
     * @param DoctrineCache\CacheProvider $cacheProvider
98
     * @param string $cacheSuffix
99
     * @return void
100
     */
101
    public function setCacheProvider(DoctrineCache\CacheProvider $cacheProvider, $cacheSuffix = 'default')
102
    {
103
        $this->cacheProvider = $cacheProvider;
104
        $this->cacheSuffix = $cacheSuffix;
105
    }
106
107
    /**
108
     * Force overwrite files if exists mode.
109
     * @return void
110
     */
111
    public function forceOverwriteIfExists()
112
    {
113
        $this->overwriteIfExists = true;
114
    }
115
116
    /**
117
     * @param $directory
118
     * @return void
119
     */
120
    public function addTemplatesDirectory($directory)
121
    {
122
        $this->templatesDirectories[] = $directory;
123
    }
124
125
    /**
126
     * @param $yaml_file
127
     * @return void
128
     */
129
    public function setGeneratorYml($yaml_file)
130
    {
131
        $this->generator_yaml = $yaml_file;
132
    }
133
134
    /**
135
     * @return string
136
     */
137
    public function getGeneratorYml()
138
    {
139
        return $this->generator_yaml;
140
    }
141
142
    /**
143
     * @param $baseGeneratorName
144
     * @return void
145
     */
146
    public function setBaseGeneratorName($baseGeneratorName)
147
    {
148
        $this->baseGeneratorName = $baseGeneratorName;
149
    }
150
151
    /**
152
     * @return string
153
     */
154
    protected function getBaseGeneratorName()
155
    {
156
        return $this->baseGeneratorName;
157
    }
158
159
    /**
160
     * (non-PHPdoc)
161
     * @see Generator/Admingenerator\GeneratorBundle\Generator.GeneratorInterface::getCachePath()
162
     */
163
    public function getCachePath($namespace, $bundle_name)
164
    {
165
        return $this->cache_dir.'/Admingenerated/'.str_replace('\\', DIRECTORY_SEPARATOR, $namespace).$bundle_name;
166
    }
167
168
    /**
169
     * (non-PHPdoc)
170
     * @see Generator/Admingenerator\GeneratorBundle\Generator.GeneratorInterface::build()
171
     */
172
    public function build($forceGeneration = false)
173
    {
174
        if (!$forceGeneration && $this->cacheProvider->fetch($this->getCacheKey())) {
175
            return;
176
        }
177
178
        $this->doBuild();
179
        $this->cacheProvider->save($this->getCacheKey(), true);
180
    }
181
182
    /**
183
     * Process build
184
     */
185
    abstract protected function doBuild();
186
187
    /**
188
     * @return string
189
     */
190
    protected function getCacheKey()
191
    {
192
        return sprintf('admingen_isbuilt_%s_%s', $this->getBaseGeneratorName(), $this->cacheSuffix);
193
    }
194
195
    /**
196
     * @param object $fieldGuesser The fieldguesser.
197
     * @return void
198
     */
199
    public function setFieldGuesser($fieldGuesser)
200
    {
201
        $this->fieldGuesser = $fieldGuesser;
202
    }
203
204
    /**
205
     * @return object The fieldguesser.
206
     */
207
    public function getFieldGuesser()
208
    {
209
        return $this->fieldGuesser;
210
    }
211
212
    /**
213
     * Check if we have to build file
214
     */
215
    public function needToOverwrite(AdminGenerator $generator)
216
    {
217
        if ($this->overwriteIfExists) {
218
            return true;
219
        }
220
221
        $cacheDir = $this->getCachePath($generator->getFromYaml('params.namespace_prefix'), $generator->getFromYaml('params.bundle_name'));
222
223
        if (!is_dir($cacheDir)) {
224
            return true;
225
        }
226
227
        $fileInfo = new \SplFileInfo($this->getGeneratorYml());
228
229
        $finder = new Finder();
230
        $files = $finder->files()
231
            ->date('< '.date('Y-m-d H:i:s',$fileInfo->getMTime()))
232
            ->in($cacheDir)
233
            ->count();
234
235
        if ($files > 0) {
236
            return true;
237
        }
238
239
        $finder = new Finder();
240
        foreach ($finder->files()->in($cacheDir) as $file) {
241
            if (false !== strpos(file_get_contents($file), 'AdmingeneratorEmptyBuilderClass')) {
242
                return true;
243
            }
244
        }
245
246
        return false;
247
    }
248
249
    /**
250
     * @return void
251
     */
252
    public function addValidator(ValidatorInterface $validator)
253
    {
254
        $this->validators[] = $validator;
255
    }
256
257
    /**
258
     * @return void
259
     */
260
    public function validateYaml()
261
    {
262
        foreach ($this->validators as $validator) {
263
            $validator->validate($this);
264
        }
265
    }
266
267
    /**
268
     * @param array $bundleConfig
269
     * @return void
270
     */
271
    public function setBundleConfig(array $bundleConfig)
272
    {
273
        $this->bundleConfig = $bundleConfig;
274
    }
275
276
    /**
277
     * @param \Symfony\Component\Routing\RouterInterface $router
278
     * @return void
279
     */
280
    public function setRouter(RouterInterface $router)
281
    {
282
        $this->router = $router;
283
    }
284
    
285
    /**
286
     * @param TwigEnvironment $twig
287
     * @return void
288
     */
289
    public function setTwig(TwigEnvironment $twig)
290
    {
291
        $this->twig = $twig;
292
    }
293
    
294
    /**
295
     * @param KernelInterface $kernel
296
     * @return void
297
     */
298
    public function setKernel(KernelInterface $kernel)
299
    {
300
        $this->kernel = $kernel;
301
    }
302
}
303