|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Tworzenieweb\SqlProvisioner; |
|
4
|
|
|
|
|
5
|
|
|
use RuntimeException; |
|
6
|
|
|
use Symfony\Component\Config\FileLocator; |
|
7
|
|
|
use Symfony\Component\Console\Command\Command; |
|
8
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
|
9
|
|
|
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* @author Luke Adamczewski |
|
13
|
|
|
* @package Tworzenieweb\SqlProvisioner |
|
14
|
|
|
*/ |
|
15
|
|
|
class Application extends \Symfony\Component\Console\Application |
|
16
|
|
|
{ |
|
17
|
|
|
/** @var ContainerBuilder */ |
|
18
|
|
|
private $container; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @param string $name |
|
22
|
|
|
* @param string $version |
|
23
|
|
|
*/ |
|
24
|
|
|
public function __construct($name = 'UNKNOWN', $version = 'UNKNOWN') |
|
25
|
|
|
{ |
|
26
|
|
|
parent::__construct($name, $version); |
|
27
|
|
|
$this->boot(); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
|
|
31
|
|
|
|
|
32
|
|
|
private function boot() |
|
33
|
|
|
{ |
|
34
|
|
|
$this->container = new ContainerBuilder(); |
|
35
|
|
|
$this->container->setParameter('sql_provisioner.composer_bin_path', $this->getComposerBinPath()); |
|
36
|
|
|
|
|
37
|
|
|
$loader = new XmlFileLoader($this->container, new FileLocator($this->getConfigPath())); |
|
38
|
|
|
$loader->load('services.xml'); |
|
39
|
|
|
$this->registerCommands(); |
|
40
|
|
|
$this->registerChecks(); |
|
41
|
|
|
$this->container->compile(); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
protected function registerCommands() |
|
45
|
|
|
{ |
|
46
|
|
|
foreach ($this->container->findTaggedServiceIds('console.command') as $commandId => $command) { |
|
47
|
|
|
$this->add($this->getCommandForId($commandId)); |
|
|
|
|
|
|
48
|
|
|
} |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
|
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @return string |
|
55
|
|
|
*/ |
|
56
|
|
|
private function getConfigPath() |
|
57
|
|
|
{ |
|
58
|
|
|
return __DIR__ . '/../config'; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @return string |
|
63
|
|
|
*/ |
|
64
|
|
|
private function getComposerBinPath() |
|
65
|
|
|
{ |
|
66
|
|
|
return __DIR__ . '/../vendor/bin'; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
|
|
70
|
|
|
|
|
71
|
|
|
private function registerChecks() |
|
72
|
|
|
{ |
|
73
|
|
|
foreach ($this->container->findTaggedServiceIds('provision.check') as $serviceId => $command) { |
|
74
|
|
|
$this->container->get('processor.candidate')->addCheck($this->container->get($serviceId)); |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* @param string $commandId |
|
80
|
|
|
* @return Command |
|
81
|
|
|
*/ |
|
82
|
|
|
protected function getCommandForId($commandId) |
|
83
|
|
|
{ |
|
84
|
|
|
if (!$this->container->has($commandId)) { |
|
85
|
|
|
throw new RuntimeException(sprintf('There is no command class for id %s', $commandId)); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
return $this->container->get($commandId); |
|
89
|
|
|
} |
|
90
|
|
|
} |
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: