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\OutputInterface; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Warms up the cache |
20
|
|
|
*/ |
21
|
|
|
class CacheWarmer |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @var AspectKernel |
25
|
|
|
*/ |
26
|
|
|
protected $aspectKernel; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var OutputInterface |
30
|
|
|
*/ |
31
|
|
|
protected $output; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* CacheWarmer constructor. |
35
|
|
|
* |
36
|
|
|
* @param AspectKernel $aspectKernel A kernel |
37
|
|
|
* @param OutputInterface $output Optional output to log current status. |
38
|
|
|
*/ |
39
|
|
|
public function __construct(AspectKernel $aspectKernel, OutputInterface $output = null) |
40
|
|
|
{ |
41
|
|
|
$this->aspectKernel = $aspectKernel; |
42
|
|
|
$this->output = $output; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Warms up cache |
47
|
|
|
*/ |
48
|
|
|
public function warmUp() |
49
|
|
|
{ |
50
|
|
|
$options = $this->aspectKernel->getOptions(); |
51
|
|
|
|
52
|
|
|
if (empty($options['cacheDir'])) { |
53
|
|
|
throw new \InvalidArgumentException('Cache warmer require the `cacheDir` options to be configured'); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
$enumerator = new Enumerator($options['appDir'], $options['includePaths'], $options['excludePaths']); |
57
|
|
|
$iterator = $enumerator->enumerate(); |
58
|
|
|
$total = iterator_count($iterator); |
59
|
|
|
|
60
|
|
|
$this->writeln(sprintf('Total <info>%s</info> files to process.', $total)); |
61
|
|
|
$this->newline(); |
62
|
|
|
$iterator->rewind(); |
63
|
|
|
|
64
|
|
|
set_error_handler(function($errno, $errstr, $errfile, $errline) { |
|
|
|
|
65
|
|
|
throw new \ErrorException($errstr, $errno, 0, $errfile, $errline); |
66
|
|
|
}); |
67
|
|
|
|
68
|
|
|
$errors = []; |
69
|
|
|
|
70
|
|
|
foreach ($iterator as $file) { |
71
|
|
|
$path = $file->getRealPath(); |
72
|
|
|
|
73
|
|
|
try { |
74
|
|
|
// This will trigger creation of cache |
75
|
|
|
file_get_contents(FilterInjectorTransformer::PHP_FILTER_READ . |
76
|
|
|
SourceTransformingLoader::FILTER_IDENTIFIER . |
77
|
|
|
'/resource=' . $path |
78
|
|
|
); |
79
|
|
|
|
80
|
|
|
$this->writeln(sprintf('<fg=green;options=bold>[OK]</>: <comment>%s</comment>', $path)); |
81
|
|
|
} catch (\Exception $e) { |
82
|
|
|
$this->writeln(sprintf('<fg=white;bg=red;options=bold>[ERR]</>: %s', $path)); |
83
|
|
|
$errors[$path] = $e->getMessage(); |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
restore_error_handler(); |
88
|
|
|
|
89
|
|
|
if ($this->isVerbose()) { |
90
|
|
|
foreach ($errors as $path => $error) { |
91
|
|
|
$this->writeln(sprintf('<fg=white;bg=red;options=bold>[ERR]</>: File "%s" is not processed correctly due to exception: "%s".', $path, $error)); |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
$this->newline(); |
96
|
|
|
$this->writeln(sprintf('<fg=green;>[DONE]</>: Total processed %s, %s errors.', $total, count($errors))); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Proxy to OutputInterface->writeln() method dumping new line. |
101
|
|
|
*/ |
102
|
|
|
private function newline() |
103
|
|
|
{ |
104
|
|
|
if (null === $this->output) { |
105
|
|
|
return; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
$this->output->writeln(''); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Proxy to OutputInterface->writeln() method. |
113
|
|
|
* |
114
|
|
|
* @param string $message Message text. |
115
|
|
|
*/ |
116
|
|
|
private function writeln($message) |
117
|
|
|
{ |
118
|
|
|
if (null === $this->output) { |
119
|
|
|
return; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
$this->output->writeln($message); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Check if output verbosity is verbose. |
127
|
|
|
* |
128
|
|
|
* @return bool |
129
|
|
|
*/ |
130
|
|
|
private function isVerbose() |
131
|
|
|
{ |
132
|
|
|
if (null === $this->output) { |
133
|
|
|
return false; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
return $this->output->isVerbose(); |
137
|
|
|
} |
138
|
|
|
} |