forked from kherge-archive/console
/
ApplicationCache.php
173 lines (149 loc) · 5.25 KB
/
ApplicationCache.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
<?php
namespace Box\Component\Console;
use Box\Component\Console\Exception\CacheException;
use KHerGe\File\File;
use Symfony\Component\Config\ConfigCache;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Dumper\PhpDumper;
use Symfony\Component\DependencyInjection\Dumper\XmlDumper;
/**
* Manages the loading and saving of the application container cache.
*
* **Note:** The caching methods each accept a `$debug` argument. The argument
* is set to true by default. "Debugging" allows the cache manager to check if
* resources have changed since the container was last built. This includes
* resources such as configuration files and compiler pass classes. If these
* resources have changed and debugging is enabled, the cache will be flagged
* as stale. With debugging disabled, the cache will always be flagged as
* fresh, event when the resources have changed.
*
* While it is recommended that debugging always be enabled, you may disable
* debugging if you are certain that your resources will never change. If they
* do, you will need to manually delete the cache files.
*
* @author Kevin Herrera <kevin@herrera.io>
*/
class ApplicationCache extends Application
{
/**
* Loads a cached application or creates a new one.
*
* @param string $file The path to the cache file.
* @param callable $setup The callback that sets up a new container.
* @param string $class The container class name.
* @param boolean $debug Enable debugging? (see class doc)
*
* @return ApplicationCache The application.
*/
public static function bootstrap(
$file,
callable $setup = null,
$class = 'ConsoleContainer',
$debug = true
) {
try {
$app = self::load($file, $class, $debug);
} catch (CacheException $exception) {
$container = new ContainerBuilder();
$app = new static($container);
if (null !== $setup) {
$setup($container, $app);
}
/** @noinspection PhpUndefinedMethodInspection */
$app
->loadFromExtensions()
->save($file, $class, $debug);
}
return $app;
}
/**
* Loads an application using its cached container.
*
* @param string $file The path to the cache file.
* @param string $class The container class name.
* @param boolean $debug Enable debugging? (see class doc)
*
* @return ApplicationCache The loaded application.
*
* @throws CacheException If the application could not be loaded.
*/
public static function load(
$file,
$class = 'ConsoleContainer',
$debug = true
) {
if (!file_exists($file)) {
throw CacheException::fileNotExist($file); // @codeCoverageIgnore
}
$cache = new ConfigCache($file, $debug);
if (!$cache->isFresh()) {
throw CacheException::cacheStale($file); // @codeCoverageIgnore
}
/** @noinspection PhpIncludeInspection */
require_once $file;
if (!class_exists($class)) {
throw CacheException::classNotExist($class, $file); // @codeCoverageIgnore
}
return new static(new $class());
}
/**
* Saves the application container to a cache file
*
* @param string $file The path to the cache file.
* @param string $class The container class name.
* @param boolean $debug Enable debugging? (see class doc)
*
* @throws CacheException If the application could not be saved.
*/
public function save($file, $class = 'ConsoleContainer', $debug = true)
{
$container = $this->getContainer();
if (!($container instanceof ContainerBuilder)) {
throw CacheException::notBuilder(); // @codeCoverageIgnore
}
$dir = dirname($file);
if (!file_exists($dir)
&& !mkdir($dir, 0755, true)) {
throw CacheException::cannotCreateDir($dir); // @codeCoverageIgnore
}
if (!$container->isFrozen()) {
$container->compile();
}
$this->dumpXmlConfig($container, $file);
$dumper = new PhpDumper($container);
$cache = new ConfigCache($file, $debug);
$cache->write(
$dumper->dump(
array(
'class' => $class
)
),
$container->getResources()
);
}
/**
* Dumps the container configuration as an XML file for rebuilding.
*
* @param ContainerBuilder $container The container builder.
* @param string $file The path to the cache file.
*/
private function dumpXmlConfig(ContainerBuilder $container, $file)
{
$file = sprintf(
'%s%s%s.xml',
dirname($file),
DIRECTORY_SEPARATOR,
pathinfo($file, PATHINFO_FILENAME)
);
$container
->getDefinition(self::getId('helper.container'))
->addMethodCall(
'setFile',
array($file)
)
;
$dumper = new XmlDumper($container);
$writer = new File($file, 'w');
$writer->fwrite($dumper->dump());
}
}