-
-
Notifications
You must be signed in to change notification settings - Fork 163
/
CacheWarmer.php
105 lines (87 loc) · 3.27 KB
/
CacheWarmer.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
<?php
/*
* Go! AOP framework
*
* @copyright Copyright 2013, Lisachenko Alexander <lisachenko.it@gmail.com>
*
* This source file is subject to the license that is bundled
* with this source code in the file LICENSE.
*/
namespace Go\Instrument\ClassLoading;
use Go\Core\AspectKernel;
use Go\Instrument\FileSystem\Enumerator;
use Go\Instrument\Transformer\FilterInjectorTransformer;
use Symfony\Component\Console\Output\NullOutput;
use Symfony\Component\Console\Output\OutputInterface;
/**
* Warms up the cache
*/
class CacheWarmer
{
/**
* @var AspectKernel
*/
protected $aspectKernel;
/**
* @var OutputInterface
*/
protected $output;
/**
* CacheWarmer constructor.
*
* @param AspectKernel $aspectKernel A kernel
* @param OutputInterface $output Optional output to log current status.
*/
public function __construct(AspectKernel $aspectKernel, OutputInterface $output = null)
{
$this->aspectKernel = $aspectKernel;
$this->output = $output !== null ? $output : new NullOutput();
}
/**
* Warms up cache
*/
public function warmUp()
{
$options = $this->aspectKernel->getOptions();
if (empty($options['cacheDir'])) {
throw new \InvalidArgumentException('Cache warmer require the `cacheDir` options to be configured');
}
$enumerator = new Enumerator($options['appDir'], $options['includePaths'], $options['excludePaths']);
$iterator = $enumerator->enumerate();
$total = iterator_count($iterator);
$this->output->writeln(sprintf('Total <info>%s</info> files to process.', $total));
$this->output->writeln('');
$iterator->rewind();
set_error_handler(function ($errno, $errstr, $errfile, $errline) {
throw new \ErrorException($errstr, $errno, 0, $errfile, $errline);
});
$errors = [];
$displayException = function (\Throwable $exception, $path) use (&$errors) {
$this->output->writeln(sprintf('<fg=white;bg=red;options=bold>[ERR]</>: %s', $path));
$errors[$path] = $exception->getMessage();
};
foreach ($iterator as $file) {
$path = $file->getRealPath();
try {
// This will trigger creation of cache
file_get_contents(FilterInjectorTransformer::PHP_FILTER_READ .
SourceTransformingLoader::FILTER_IDENTIFIER .
'/resource=' . $path
);
$this->output->writeln(sprintf('<fg=green;options=bold>[OK]</>: <comment>%s</comment>', $path));
} catch (\Throwable $e) {
$displayException($e, $path);
} catch (\Exception $e) {
$displayException($e, $path);
}
}
restore_error_handler();
if ($this->output->isVerbose()) {
foreach ($errors as $path => $error) {
$this->output->writeln(sprintf('<fg=white;bg=red;options=bold>[ERR]</>: File "%s" is not processed correctly due to exception: "%s".', $path, $error));
}
}
$this->output->writeln('');
$this->output->writeln(sprintf('<fg=green;>[DONE]</>: Total processed %s, %s errors.', $total, count($errors)));
}
}