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
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\InventoryComposerInstaller;
use Composer\IO\IOInterface;
use Magento\Framework\App\DeploymentConfig;
use Magento\Framework\App\Filesystem\DirectoryList;
use Magento\Framework\Config\File\ConfigFilePool;
use Magento\Framework\Filesystem;
use Magento\Framework\Filesystem\DriverPool;
class InventoryConfiguratorFactory
{
/**
* @var DeploymentConfig;
*/
private $deploymentConfig;
/**
* @var IOInterface
*/
private $io;
public function __construct(IOInterface $io)
{
$this->io = $io;
}
public function createConfigurator(string $projectDir): InventoryConfiguratorInterface
{
$deploymentConfig = $this->getDeploymentConfig($projectDir);
if ($this->isModulesConfigured($deploymentConfig)
&& !$this->isInventoryAlreadyEnabled($deploymentConfig)
) {
$deploymentConfigWriter = $this->createDeploymentConfigWriter($projectDir);
return new DisabledInventoryConfiguration(
$deploymentConfig,
$deploymentConfigWriter,
$this->io
);
} else {
return new NoChangesConfigurator($this->io);
}
}
private function isModulesConfigured(DeploymentConfig $deploymentConfig): bool
{
return is_array($deploymentConfig->get('modules'));
}
private function getDeploymentConfig(string $projectDir): DeploymentConfig
{
$cacheKey = realpath($projectDir);
if (!isset($this->deploymentConfig[$cacheKey])) {
$this->deploymentConfig[$cacheKey] = $this->createDeploymentConfig($projectDir);
}
return $this->deploymentConfig[$cacheKey];
}
private function createDeploymentConfig(string $projectDir): DeploymentConfig
{
$deploymentConfigReader = $this->createDeploymentConfigReader($projectDir);
$deploymentConfig = new DeploymentConfig($deploymentConfigReader);
return $deploymentConfig;
}
private function createDeploymentConfigReader(string $projectDir): DeploymentConfig\Reader
{
$dirList = $this->createDirectoryList($projectDir);
$filesystemDriverPool = $this->createFilesystemDriverPool();
$configFilePool = $this->createConfigFilePool();
$reader = new DeploymentConfig\Reader(
$dirList,
$filesystemDriverPool,
$configFilePool
);
return $reader;
}
private function createDeploymentConfigWriter(string $projectDir): DeploymentConfig\Writer
{
$deploymentConfigReader = $this->createDeploymentConfigReader($projectDir);
$configFilePool = $this->createConfigFilePool();
$deploymentConfig = $this->createDeploymentConfig($projectDir);
$dirList = $this->createDirectoryList($projectDir);
$filesystemDriverPool = $this->createFilesystemDriverPool();
$filesystemReader = new Filesystem\Directory\ReadFactory($filesystemDriverPool);
$filesystemWriter = new Filesystem\Directory\WriteFactory($filesystemDriverPool);
$filesystem = new Filesystem(
$dirList,
$filesystemReader,
$filesystemWriter
);
$deploymentConfigWriter = new DeploymentConfig\Writer(
$deploymentConfigReader,
$filesystem,
$configFilePool,
$deploymentConfig
);
return $deploymentConfigWriter;
}
private function createDirectoryList(string $projectDir): DirectoryList
{
$dirsConfig = DirectoryList::getDefaultConfig();
$dirsConfig[DirectoryList::ROOT] = $projectDir;
$dirList = new DirectoryList($projectDir, $dirsConfig);
return $dirList;
}
private function createFilesystemDriverPool(): DriverPool
{
$driverPool = new DriverPool();
return $driverPool;
}
private function createConfigFilePool(): ConfigFilePool
{
$configFilePool = new ConfigFilePool();
return $configFilePool;
}
private function isInventoryAlreadyEnabled(DeploymentConfig $deploymentConfig): bool
{
$modulesConfig = $deploymentConfig->get('modules');
foreach ($modulesConfig as $module => $enabled) {
if ($enabled && strpos($module, 'Magento_Inventory') !== false) {
return true;
}
}
return false;
}
}