Completed
Pull Request — master (#333)
by Nikola
02:33
created

CacheWarmer::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 5
ccs 0
cts 5
cp 0
rs 9.4285
cc 2
eloc 3
nc 2
nop 2
crap 6
1
<?php
2
/*
3
 * Go! AOP framework
4
 *
5
 * @copyright Copyright 2013, 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\Instrument\ClassLoading;
12
13
use Go\Core\AspectKernel;
14
use Go\Instrument\FileSystem\Enumerator;
15
use Go\Instrument\Transformer\FilterInjectorTransformer;
16
use Symfony\Component\Console\Output\NullOutput;
17
use Symfony\Component\Console\Output\OutputInterface;
18
19
/**
20
 * Warms up the cache
21
 */
22
class CacheWarmer
23
{
24
    /**
25
     * @var AspectKernel
26
     */
27
    protected $aspectKernel;
28
29
    /**
30
     * @var OutputInterface
31
     */
32
    protected $output;
33
34
    /**
35
     * CacheWarmer constructor.
36
     *
37
     * @param AspectKernel $aspectKernel A kernel
38
     * @param OutputInterface $output Optional output to log current status.
39
     */
40
    public function __construct(AspectKernel $aspectKernel, OutputInterface $output = null)
41
    {
42
        $this->aspectKernel = $aspectKernel;
43
        $this->output       = $output !== null ? $output : new NullOutput();
44
    }
45
46
    /**
47
     * Warms up cache
48
     */
49
    public function warmUp()
50
    {
51
        $options = $this->aspectKernel->getOptions();
52
53
        if (empty($options['cacheDir'])) {
54
            throw new \InvalidArgumentException('Cache warmer require the `cacheDir` options to be configured');
55
        }
56
57
        $enumerator = new Enumerator($options['appDir'], $options['includePaths'], $options['excludePaths']);
58
        $iterator   = $enumerator->enumerate();
59
        $total      = iterator_count($iterator);
60
61
        $this->output->writeln(sprintf('Total <info>%s</info> files to process.', $total));
62
        $this->output->writeln('');
63
        $iterator->rewind();
64
65
        set_error_handler(function ($errno, $errstr, $errfile, $errline) {
66
            throw new \ErrorException($errstr, $errno, 0, $errfile, $errline);
67
        });
68
69
        $errors       = [];
70
        $displayException = \Closure::bind(function ($exception, $path) use (&$errors) {
71
            $this->output->writeln(sprintf('<fg=white;bg=red;options=bold>[ERR]</>: %s', $path));
72
            $errors[$path] = $exception->getMessage();
73
        }, $this);
74
75
        foreach ($iterator as $file) {
76
            $path = $file->getRealPath();
77
78
            try {
79
                // This will trigger creation of cache
80
                file_get_contents(FilterInjectorTransformer::PHP_FILTER_READ .
81
                    SourceTransformingLoader::FILTER_IDENTIFIER .
82
                    '/resource=' . $path
83
                );
84
85
                $this->output->writeln(sprintf('<fg=green;options=bold>[OK]</>: <comment>%s</comment>', $path));
86
            } catch (\Throwable $e) {
0 ignored issues
show
Bug introduced by
The class Throwable does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
87
                $displayException($e, $path);
88
            } catch (\Exception $e) {
89
                $displayException($e, $path);
90
            }
91
        }
92
93
        restore_error_handler();
94
95
        if ($this->output->isVerbose()) {
96
            foreach ($errors as $path => $error) {
97
                $this->output->writeln(sprintf('<fg=white;bg=red;options=bold>[ERR]</>: File "%s" is not processed correctly due to exception: "%s".', $path, $error));
98
            }
99
        }
100
101
        $this->output->writeln('');
102
        $this->output->writeln(sprintf('<fg=green;>[DONE]</>: Total processed %s, %s errors.', $total, count($errors)));
103
    }
104
}
105