-
Notifications
You must be signed in to change notification settings - Fork 51
Expand file tree
/
Copy pathApplication.php
More file actions
156 lines (129 loc) · 4.67 KB
/
Copy pathApplication.php
File metadata and controls
156 lines (129 loc) · 4.67 KB
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
<?php
/**
* (c) shopware AG <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace ShopwareCli;
use Composer\Autoload\ClassLoader;
use ShopwareCli\Application\DependencyInjection;
use ShopwareCli\Application\ExtensionManager;
use ShopwareCli\Services\PathProvider\PathProvider;
use Symfony\Component\Console\Application as SymfonyApplication;
use Symfony\Component\Console\Input\InputDefinition;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Main application of the cli tools
*/
class Application extends SymfonyApplication
{
public const NAME = 'sw-cli-tools';
public const VERSION = '__VERSION__';
/**
* @var ClassLoader
*/
private $loader;
/**
* @var ContainerInterface
*/
private $container;
public function __construct(ClassLoader $loader)
{
$this->loader = $loader;
parent::__construct(static::NAME, static::VERSION);
$this->container = DependencyInjection::createContainer(\dirname(__DIR__));
}
/**
* {@inheritdoc}
*/
public function doRun(InputInterface $input, OutputInterface $output)
{
$this->createContainer($input, $output);
$this->checkDirectories();
$noExtensions = $input->hasParameterOption('--no-extensions');
$this->loadExtensions($noExtensions);
// Compile the container after the plugins did their container extensions
$this->container->compile();
$this->addCommands($this->container->get('command_manager')->getCommands());
$this->container->get('plugin_provider')->setRepositories($this->container->get('repository_manager')->getRepositories());
return parent::doRun($input, $output);
}
public function getContainer(): ContainerInterface
{
return $this->container;
}
/**
* Add global "--no-extensions" option
*
* @return InputDefinition
*/
protected function getDefaultInputDefinition()
{
$inputDefinitions = parent::getDefaultInputDefinition();
$inputDefinitions->addOption(
new InputOption('--no-extensions', null, InputOption::VALUE_NONE, 'Don\'t load 3rd party extensions.')
);
return $inputDefinitions;
}
/**
* Creates the container and sets some services which are only synthetic in the container
*/
protected function createContainer(InputInterface $input, OutputInterface $output): ContainerBuilder
{
$questionHelper = $this->getHelperSet()->get('question');
$this->container->set('output_interface', $output);
$this->container->set('input_interface', $input);
$this->container->set('question_helper', $questionHelper);
$this->container->set('helper_set', $this->getHelperSet());
$this->container->set('autoloader', $this->loader);
return $this->container;
}
/**
* Make sure, that the required directories do actually exist
*
* @throws \RuntimeException
*/
protected function checkDirectories(): void
{
/** @var PathProvider $pathProvider */
$pathProvider = $this->container->get('path_provider');
$paths = [
$pathProvider->getAssetsPath(),
$pathProvider->getCachePath(),
$pathProvider->getExtensionPath(),
$pathProvider->getConfigPath(),
];
foreach ($paths as $dir) {
if (\is_dir($dir)) {
continue;
}
if (!\is_dir($dir) && !\mkdir($dir, 0777, true) && !\is_dir($dir)) {
throw new \RuntimeException("Could not find / create $dir");
}
}
}
/**
* Load extensions. The default extensions are always loaded,
* 3rd party extensions depending on $noExtensions
*
* Default extensions are loaded first
*
* @param bool $noExtensions
*/
protected function loadExtensions($noExtensions): void
{
$paths = [$this->container->get('path_provider')->getCliToolPath() . '/src/Extensions'];
if (!$noExtensions) {
$paths[] = $this->container->get('path_provider')->getExtensionPath();
}
/** @var ExtensionManager $extensionManager */
$extensionManager = $this->container->get('extension_manager');
$extensionManager->discoverExtensions($paths);
$extensionManager->injectContainer($this->container);
}
}