GeneratorCacheWarmer   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 90
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 3
dl 0
loc 90
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A warmUp() 0 24 5
A isOptional() 0 4 1
A buildFromYaml() 0 15 2
A propelInit() 0 7 2
1
<?php
2
3
namespace Admingenerator\GeneratorBundle\CacheWarmer;
4
5
use Symfony\Component\Yaml\Yaml;
6
use Symfony\Component\HttpKernel\CacheWarmer\CacheWarmerInterface;
7
use Symfony\Component\DependencyInjection\ContainerInterface;
8
use Admingenerator\GeneratorBundle\Filesystem\GeneratorsFinder;
9
10
/**
11
 * Generate all admingenerated bundle on warmup
12
 *
13
 * @author Cedric LOMBARDOT
14
 */
15
class GeneratorCacheWarmer implements CacheWarmerInterface
16
{
17
    /**
18
     * @var ContainerInterface
19
     */
20
    protected $container;
21
22
    /**
23
     * @var GeneratorsFinder
24
     */
25
    protected $finder;
26
27
    /**
28
     * Constructor.
29
     *
30
     * @param ContainerInterface $container The dependency injection container
31
     */
32
    public function __construct(ContainerInterface $container)
33
    {
34
        $this->container = $container;
35
        $this->finder = new GeneratorsFinder($container->get('kernel'));
36
    }
37
38
    /**
39
     * Warms up the cache.
40
     *
41
     * @param string $cacheDir The cache directory
42
     */
43
    public function warmUp($cacheDir)
44
    {
45
        if ($this->container->has('admingenerator.generator.propel')) {
46
            $this->propelInit();
47
        }
48
49
        foreach ($this->finder->findAll() as $yaml) {
50
            try {
51
                $this->buildFromYaml($yaml);
52
            } catch (\Exception $e) {
53
                while ($e->getPrevious()) {
54
                    $e = $e->getPrevious();
55
                }
56
                echo ">> Skip warmup ".
57
                    $e->getMessage().
58
                    ".\nIn file ".
59
                    $e->getFile().
60
                    " on line ".
61
                    $e->getLine().
62
                    ".\nBacktrace:\n".
63
                    $e->getTraceAsString();
64
            }
65
        }
66
    }
67
68
    /**
69
     * Checks whether this warmer is optional or not.
70
     *
71
     * @return Boolean always false
72
     */
73
    public function isOptional()
74
    {
75
        return false;
76
    }
77
78
    protected function buildFromYaml($file)
79
    {
80
        $generatorConfiguration = Yaml::parse(file_get_contents($file));
81
        $generator = $this->container->get($generatorConfiguration['generator']);
82
        $generator->setGeneratorYml($file);
83
84
        // windows support too
85
        if (preg_match('/(?:\/|\\\\)([^\/\\\\]+?)-generator.yml$/', $file, $matches)) {
86
            $generator->setBaseGeneratorName(ucfirst($matches[1]));
87
        } else {
88
            $generator->setBaseGeneratorName('');
89
        }
90
91
        $generator->build(true);
92
    }
93
94
    /**
95
     * Force Propel boot before cache warmup
96
     */
97
    protected function propelInit()
98
    {
99
        if (!\Propel::isInit()) {
100
            \Propel::setConfiguration($this->container->get('propel.configuration'));
101
            \Propel::initialize();
102
        }
103
    }
104
}
105