-
Notifications
You must be signed in to change notification settings - Fork 15
/
Application.php
209 lines (188 loc) · 7.68 KB
/
Application.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
<?php
namespace Amp;
use Amp\Util\Process;
use LesserEvil\ShellVerbosityIsEvil;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\Console\Input\ArgvInput;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\ConsoleOutput;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Config\Loader\LoaderResolver;
use Symfony\Component\Config\Loader\DelegatingLoader;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
use Symfony\Component\Filesystem\Filesystem;
class Application extends \Symfony\Component\Console\Application {
/**
* @var string the base location for writable data
*/
private $appDir;
/**
* @var array of string places to search for config files
*/
private $configDirectories;
/**
* @param $binDir
* @return \Amp\Application
*/
public static function create($binDir) {
if (getenv('AMPHOME')) {
$appDir = getenv('AMPHOME');
}
else {
$appDir = getenv('HOME') . DIRECTORY_SEPARATOR . '.amp';
}
$configDirectories = array(
dirname($binDir) . '/app/defaults',
$appDir,
);
$application = new Application('amp', '@package_version@', $appDir, $configDirectories);
return $application;
}
/**
* Primary entry point for execution of the standalone command.
*
* @param string $binDir
*
* @throws \Exception
*/
public static function main($binDir) {
$application = self::create($binDir);
$application->setCatchExceptions(FALSE);
$application->upgradeIfNeeded();
$application->run();
}
/**
* @var \Symfony\Component\DependencyInjection\ContainerInterface
*/
private $container;
public function __construct($name, $version, $appDir, $configDirectories) {
parent::__construct($name, $version);
$this->appDir = $appDir;
$this->configDirectories = $configDirectories;
$this->loadContainer();
$this->addCommands($this->createCommands());
}
public function loadContainer() {
if (empty($this->appDir) || empty($this->configDirectories)) {
throw new \Exception(__CLASS__ . ': Missing required properties (appDir, configDirectories)');
}
$container = new ContainerBuilder();
$container->setParameter('app_dir', $this->appDir);
$container->setParameter('amp_src_dir', dirname(__DIR__));
$container->setParameter('log_dir', $this->appDir . DIRECTORY_SEPARATOR . 'log');
$container->setParameter('my_cnf_dir', $this->appDir . DIRECTORY_SEPARATOR . 'my.cnf.d');
$container->setParameter('apache_dir', $this->appDir . DIRECTORY_SEPARATOR . 'apache.d');
//$container->setParameter('apache24_dir', $this->appDir . DIRECTORY_SEPARATOR . 'apache.d');
$container->setParameter('apache_tpl', implode(DIRECTORY_SEPARATOR, array(
__DIR__,
'Templates',
'apache-vhost.php',
)));
$container->setParameter('apache24_tpl', implode(DIRECTORY_SEPARATOR, array(
__DIR__,
'Templates',
'apache24-vhost.php',
)));
$container->setParameter('nginx_dir', $this->appDir . DIRECTORY_SEPARATOR . 'nginx.d');
$container->setParameter('nginx_tpl', implode(DIRECTORY_SEPARATOR, array(
__DIR__,
'Templates',
'nginx-vhost.php',
)));
$container->setParameter('instances_yml', $this->appDir . DIRECTORY_SEPARATOR . 'instances.yml');
$container->setParameter('config_yml', $this->appDir . DIRECTORY_SEPARATOR . 'services.yml');
$container->setParameter('ram_disk_dir', $this->appDir . DIRECTORY_SEPARATOR . 'ram_disk');
$fs = new Filesystem();
$fs->mkdir(array(
$this->appDir,
$container->getParameter('log_dir'),
$container->getParameter('my_cnf_dir'),
$container->getParameter('apache_dir'),
//$container->getParameter('apache24_dir'),
$container->getParameter('nginx_dir'),
));
$locator = new FileLocator($this->configDirectories);
$loaderResolver = new LoaderResolver(array(
new YamlFileLoader($container, $locator),
));
$delegatingLoader = new DelegatingLoader($loaderResolver);
foreach (array('services.yml') as $file) {
$yamlUserFiles = $locator->locate($file, NULL, FALSE);
foreach ($yamlUserFiles as $file) {
$delegatingLoader->load($file);
}
}
// renamed mysql_ram_server_port to mysqld_port
if (!$container->hasParameter('mysqld_port')) {
if ($container->hasParameter('mysql_ram_server_port')) {
$container->setParameter('mysqld_port', $container->getParameter('mysql_ram_server_port'));
}
else {
$container->setParameter('mysqld_port', 3307);
}
}
if (getenv('AMP_INSTANCES_TIMEOUT')) {
$container->setParameter('instances_timeout', getenv('AMP_INSTANCES_TIMEOUT'));
}
// TODO: If we load default configuration first, the version paramter will always be here.
$dbParam = $container->hasParameter('db_type') ? 'db_type' : 'mysql_type';
$container->setAlias('db', 'db.' . $container->getParameter($dbParam));
$container->setAlias('httpd', 'httpd.' . $container->getParameter('httpd_type'));
$container->setAlias('perm', 'perm.' . $container->getParameter('perm_type'));
$container->setAlias('ram_disk', 'ram_disk.' . $container->getParameter('ram_disk_type'));
$container->setAlias('hosts', 'hosts.' . $container->getParameter('hosts_type'));
$this->container = $container;
}
/**
* @return \Symfony\Component\DependencyInjection\ContainerInterface
*/
public function getContainer() {
return $this->container;
}
/**
* Construct command objects
*
* FIXME: Move to services.yml
*
* @return array of Symfony Command objects
*/
public function createCommands() {
$commands = array();
$commands[] = new \Amp\Command\ConfigCommand($this, NULL, $this->getContainer()->get('config.repository'));
$commands[] = new \Amp\Command\ConfigGetCommand($this, NULL, $this->getContainer()->get('config.repository'));
$commands[] = new \Amp\Command\ConfigSetCommand($this, NULL, $this->getContainer()->get('config.repository'));
$commands[] = new \Amp\Command\ConfigResetCommand($this, NULL, $this->getContainer()->get('config.repository'));
$commands[] = new \Amp\Command\ConfigUpgradeCommand($this, NULL, $this->getContainer()->get('config.repository'));
$commands[] = new \Amp\Command\DatadirCommand($this, NULL);
$commands[] = new \Amp\Command\TestCommand($this, NULL);
$commands[] = new \Amp\Command\CreateCommand($this, NULL);
$commands[] = new \Amp\Command\ShowCommand($this, NULL);
$commands[] = new \Amp\Command\ExportCommand($this, NULL);
$commands[] = new \Amp\Command\SqlCommand($this, NULL);
$commands[] = new \Amp\Command\SqlDumpCommand($this, NULL);
$commands[] = new \Amp\Command\DestroyCommand($this, NULL);
$commands[] = new \Amp\Command\CleanupCommand($this, NULL);
$commands[] = new \Amp\Command\MysqlStartCommand($this, NULL);
$commands[] = new \Amp\Command\MysqlStopCommand($this, NULL);
return $commands;
}
/**
* Upgrade the configuration
*/
public function upgradeIfNeeded() {
// TODO: Figure out how to get the version number of .amp/services.yml.
$command = $this->get('config:upgrade');
global $argv;
$newArgv = array($argv[0], 'config:upgrade');
$exitCode = $this->doRunCommand($command, new ArgvInput($newArgv), new ConsoleOutput());
return $exitCode;
}
protected function configureIO(InputInterface $input, OutputInterface $output) {
ShellVerbosityIsEvil::doWithoutEvil(function() use ($input, $output) {
parent::configureIO($input, $output);
});
}
}