AspectCacheWarmer   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 4
dl 0
loc 77
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A isOptional() 0 4 1
A warmUp() 0 32 3
1
<?php
2
/**
3
 * Go! AOP framework
4
 *
5
 * @copyright Copyright 2015, Lisachenko Alexander <[email protected]>
6
 *
7
 * This source file is subject to the license that is bundled
8
 * with this source code in the file LICENSE.
9
 */
10
11
namespace Go\Symfony\GoAopBundle\CacheWarmer;
12
13
14
use Go\Core\AspectKernel;
15
use Go\Instrument\ClassLoading\CachePathManager;
16
use Go\Instrument\ClassLoading\SourceTransformingLoader;
17
use Go\Instrument\FileSystem\Enumerator;
18
use Go\Instrument\Transformer\FilterInjectorTransformer;
19
use Symfony\Component\HttpKernel\CacheWarmer\CacheWarmer;
20
21
/**
22
 * Warming the cache with injected advices
23
 *
24
 * NB: in some cases hierarchy analysis can trigger "Fatal Error: class XXX not found". This is means, that there is
25
 * some class with unresolved parent classes. To avoid this issue, just exclude bad classes from analysis via
26
 * 'excludePaths' configuration option.
27
 */
28
class AspectCacheWarmer extends CacheWarmer
29
{
30
    /**
31
     * Instance of aspect kernel
32
     *
33
     * @var AspectKernel
34
     */
35
    private $aspectKernel;
36
37
    /**
38
     * @var CachePathManager
39
     */
40
    private $cachePathManager;
41
42
    /**
43
     * @param AspectKernel $aspectKernel
44
     * @param CachePathManager $cachePathManager
45
     */
46
    public function __construct(AspectKernel $aspectKernel, CachePathManager $cachePathManager)
47
    {
48
        $this->aspectKernel     = $aspectKernel;
49
        $this->cachePathManager = $cachePathManager;
50
    }
51
52
    /**
53
     * Checks whether this warmer is optional or not.
54
     *
55
     * Optional warmers can be ignored on certain conditions.
56
     *
57
     * A warmer should return true if the cache can be
58
     * generated incrementally and on-demand.
59
     *
60
     * @return bool true if the warmer is optional, false otherwise
61
     */
62
    public function isOptional()
63
    {
64
        return false;
65
    }
66
67
    /**
68
     * Warms up the cache.
69
     *
70
     * @param string $cacheDir The cache directory
71
     */
72
    public function warmUp($cacheDir)
73
    {
74
        $options     = $this->aspectKernel->getOptions();
75
        $oldCacheDir = $this->cachePathManager->getCacheDir();
76
77
        $this->cachePathManager->setCacheDir($cacheDir.'/aspect');
78
79
        $enumerator = new Enumerator($options['appDir'], $options['includePaths'], $options['excludePaths']);
80
        $iterator   = $enumerator->enumerate();
81
82
        set_error_handler(function ($errno, $errstr, $errfile, $errline) {
83
            throw new \ErrorException($errstr, $errno, 0, $errfile, $errline);
84
        });
85
86
        foreach ($iterator as $file) {
87
            $realPath = $file->getRealPath();
88
            try {
89
                // This will trigger creation of cache
90
                file_get_contents(
91
                    FilterInjectorTransformer::PHP_FILTER_READ.
92
                    SourceTransformingLoader::FILTER_IDENTIFIER.
93
                    "/resource=" . $realPath
94
                );
95
            } catch (\Exception $e) {
96
                /* noop */
97
            }
98
        }
99
100
        restore_error_handler();
101
        $this->cachePathManager->flushCacheState();
102
        $this->cachePathManager->setCacheDir($oldCacheDir);
103
    }
104
}
105