-
Notifications
You must be signed in to change notification settings - Fork 82
/
Copy pathContainer.php
134 lines (116 loc) · 3.86 KB
/
Container.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
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\MagentoCloud\App;
use Composer\Composer;
use Composer\Factory;
use Composer\IO\BufferIO;
use Magento\MagentoCloud\ExtensionRegistrar;
use Magento\MagentoCloud\Filesystem\SystemList;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
use Exception;
/**
* @inheritdoc
* @codeCoverageIgnore
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class Container implements ContainerInterface
{
/**
* @var \Symfony\Component\DependencyInjection\Container
*/
private $container;
/**
* @param string $toolsBasePath
* @param string $magentoBasePath
* @throws ContainerException
*/
public function __construct(string $toolsBasePath, string $magentoBasePath)
{
$containerBuilder = new ContainerBuilder();
$containerBuilder->set('container', $this);
$containerBuilder->setDefinition('container', new Definition(__CLASS__))
->setArguments([$toolsBasePath, $magentoBasePath]);
$systemList = new SystemList($toolsBasePath, $magentoBasePath);
$containerBuilder->set(SystemList::class, $systemList);
$containerBuilder->setDefinition(SystemList::class, new Definition(SystemList::class));
$containerBuilder->set(Composer::class, $this->createComposerInstance($systemList));
$containerBuilder->setDefinition(Composer::class, new Definition(Composer::class));
try {
$loader = new XmlFileLoader($containerBuilder, new FileLocator($toolsBasePath . '/config/'));
$loader->load('services.xml');
foreach (ExtensionRegistrar::getPaths() as $extensionPath) {
if (file_exists($extensionPath . '/config/services.xml')) {
$loader = new XmlFileLoader($containerBuilder, new FileLocator($extensionPath . '/config/'));
$loader->load('services.xml');
}
}
} catch (Exception $exception) {
throw new ContainerException($exception->getMessage(), $exception->getCode(), $exception);
}
$containerBuilder->compile();
$this->container = $containerBuilder;
}
/**
* @param SystemList $systemList
* @return Composer
*/
private function createComposerInstance(SystemList $systemList): Composer
{
$composerFactory = new Factory();
$composerFile = file_exists($systemList->getMagentoRoot() . '/composer.json')
? $systemList->getMagentoRoot() . '/composer.json'
: $systemList->getRoot() . '/composer.json';
return $composerFactory->createComposer(
new BufferIO(),
$composerFile,
false,
$systemList->getMagentoRoot()
);
}
/**
* @inheritDoc
*/
public function get($id)
{
try {
return $this->container->get($id);
} catch (Exception $exception) {
throw new ContainerException(
$exception->getMessage(),
$exception->getCode(),
$exception
);
}
}
/**
* @inheritdoc
*/
public function has($id): bool
{
return $this->container->has($id);
}
/**
* @inheritdoc
*/
public function set(string $id, $service): void
{
$this->container->set($id, $service);
}
/**
* @inheritdoc
*/
public function create(string $abstract, array $params = [])
{
if (empty($params) && $this->has($abstract)) {
return $this->get($abstract);
}
return new $abstract(...array_values($params));
}
}