1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Openl10n\Cli; |
4
|
|
|
|
5
|
|
|
use Openl10n\Cli\ServiceContainer\Configuration\ConfigurationLoader; |
6
|
|
|
use Openl10n\Cli\ServiceContainer\Exception\ConfigurationLoadingException; |
7
|
|
|
use Openl10n\Cli\ServiceContainer\ExtensionManager; |
8
|
|
|
use Symfony\Component\Console\Application as BaseApplication; |
9
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
10
|
|
|
|
11
|
|
|
class Application extends BaseApplication |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var ConfigurationLoader |
15
|
|
|
*/ |
16
|
|
|
protected $configurationLoader; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var ExtensionManager |
20
|
|
|
*/ |
21
|
|
|
protected $extensionManager; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var ContainerBuilder |
25
|
|
|
*/ |
26
|
|
|
protected $container; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var boolean |
30
|
|
|
*/ |
31
|
|
|
protected $ignoreMissingConfiguration; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @param string $name Application name |
35
|
|
|
* @param string $version Application version |
36
|
|
|
* @param ConfigurationLoader $configurationLoader Configuration loader |
37
|
|
|
* @param ExtensionManager $extensionManager Extension manager |
38
|
|
|
*/ |
39
|
|
|
public function __construct($name, $version, ConfigurationLoader $configurationLoader, ExtensionManager $extensionManager) |
40
|
|
|
{ |
41
|
|
|
parent::__construct($name, $version); |
42
|
|
|
|
43
|
|
|
$this->configurationLoader = $configurationLoader; |
44
|
|
|
$this->extensionManager = $extensionManager; |
45
|
|
|
|
46
|
|
|
$this->ignoreMissingConfiguration = false; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @return ContainerInterface The container |
51
|
|
|
*/ |
52
|
|
|
public function getContainer() |
53
|
|
|
{ |
54
|
|
|
if (null === $this->container) { |
55
|
|
|
$this->container = $this->createContainer(); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
return $this->container; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Force container to be recreated. |
63
|
|
|
*/ |
64
|
|
|
public function destroyContainer() |
65
|
|
|
{ |
66
|
|
|
$this->container = null; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Ignore when the configuration file is missing. |
71
|
|
|
* |
72
|
|
|
* Useful for commands which don't need the configurated services |
73
|
|
|
* such as the `init` command. |
74
|
|
|
* |
75
|
|
|
* @param boolean $value |
76
|
|
|
*/ |
77
|
|
|
public function ignoreMissingConfiguration($value = true) |
78
|
|
|
{ |
79
|
|
|
$this->ignoreMissingConfiguration = (bool) $value; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Create a new container. |
84
|
|
|
* |
85
|
|
|
* @return ContainerInterface |
86
|
|
|
*/ |
87
|
|
|
protected function createContainer() |
88
|
|
|
{ |
89
|
|
|
$container = new ContainerBuilder(); |
90
|
|
|
|
91
|
|
|
// Default configuration. |
92
|
|
|
$container->set('application', $this); |
93
|
|
|
$container->set('configuration.loader', $this->configurationLoader); |
94
|
|
|
$container->set('extension_manager', $this->extensionManager); |
95
|
|
|
|
96
|
|
|
// Initialize extensions. |
97
|
|
|
$this->extensionManager->initialize($container); |
98
|
|
|
|
99
|
|
|
// Load configuration. |
100
|
|
|
try { |
101
|
|
|
$rawConfigs = $this->configurationLoader->loadConfiguration(); |
102
|
|
|
|
103
|
|
|
$this->extensionManager->load($rawConfigs, $container); |
|
|
|
|
104
|
|
|
} catch (ConfigurationLoadingException $e) { |
105
|
|
|
// No configuration file found. |
106
|
|
|
// Continue with restricted services if `ignoreMissingConfiguration` |
107
|
|
|
// has been specified. |
108
|
|
|
if (!$this->ignoreMissingConfiguration) { |
109
|
|
|
throw $e; |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
// Process compiler pass & compile container |
114
|
|
|
$container->compile(); |
115
|
|
|
|
116
|
|
|
return $container; |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.