|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* @author Jared King <[email protected]> |
|
5
|
|
|
* |
|
6
|
|
|
* @link http://jaredtking.com |
|
7
|
|
|
* |
|
8
|
|
|
* @copyright 2015 Jared King |
|
9
|
|
|
* @license MIT |
|
10
|
|
|
*/ |
|
11
|
|
|
namespace Infuse\Console; |
|
12
|
|
|
|
|
13
|
|
|
use Infuse\HasApp; |
|
14
|
|
|
use Symfony\Component\Console\Command\Command; |
|
15
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
16
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
17
|
|
|
|
|
18
|
|
|
class OptimizeCommand extends Command |
|
19
|
|
|
{ |
|
20
|
|
|
use HasApp; |
|
21
|
|
|
|
|
22
|
|
|
protected function configure() |
|
23
|
|
|
{ |
|
24
|
|
|
$this |
|
25
|
|
|
->setName('optimize') |
|
26
|
|
|
->setDescription('Optimizes the app'); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
|
30
|
|
|
{ |
|
31
|
|
|
$output->writeln('Optimizing app'); |
|
32
|
|
|
|
|
33
|
|
|
if ($this->app['config']->get('router.cacheFile')) { |
|
34
|
|
|
$this->cacheRouteTable($output); |
|
35
|
|
|
} else { |
|
36
|
|
|
$output->writeln('The route table could be cached with the router.cacheFile setting'); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
$this->cacheConfig($output); |
|
40
|
|
|
|
|
41
|
|
|
return 0; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
private function cacheRouteTable(OutputInterface $output) |
|
45
|
|
|
{ |
|
46
|
|
|
$output->writeln('-- Caching route table'); |
|
47
|
|
|
|
|
48
|
|
|
$cacheFile = $this->app['config']->get('router.cacheFile'); |
|
49
|
|
|
@unlink($cacheFile); |
|
|
|
|
|
|
50
|
|
|
|
|
51
|
|
|
$this->app['router']->getDispatcher(); |
|
52
|
|
|
|
|
53
|
|
|
$output->writeln('Success!'); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
private function cacheConfig(OutputInterface $output) |
|
57
|
|
|
{ |
|
58
|
|
|
$output->writeln('-- Caching configuration'); |
|
59
|
|
|
|
|
60
|
|
|
$configFile = INFUSE_BASE_DIR.'/config.php'; |
|
61
|
|
|
$originalConfigFile = INFUSE_BASE_DIR.'/config.original.php'; |
|
62
|
|
|
|
|
63
|
|
|
// we are going to cache the already loaded config, |
|
64
|
|
|
// unless the loaded config comes from a cached version |
|
65
|
|
|
$settings = $this->app['config']->all(); |
|
66
|
|
|
if (file_exists($originalConfigFile)) { |
|
67
|
|
|
$settings = include $originalConfigFile; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
// build a hard-coded version of the application configuration |
|
71
|
|
|
// that does not include any configuration building logic |
|
72
|
|
|
$configStr = "<?php\n// THIS FILE IS AUTO-GENERATED\n"; |
|
73
|
|
|
$configStr .= 'return '; |
|
74
|
|
|
$configStr .= var_export($settings, true); |
|
75
|
|
|
$configStr .= ';'; |
|
76
|
|
|
|
|
77
|
|
|
// move the original config file to a backup |
|
78
|
|
|
if (file_exists($configFile) && !file_exists($originalConfigFile)) { |
|
79
|
|
|
rename($configFile, $originalConfigFile); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
// flush out |
|
83
|
|
|
if (file_put_contents($configFile, $configStr)) { |
|
84
|
|
|
$output->writeln('Success!'); |
|
85
|
|
|
} else { |
|
86
|
|
|
$output->writeln('Could not write config.php'); |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
|
If you suppress an error, we recommend checking for the error condition explicitly: