6 declare(strict_types=1);
31 private $schemaConfig;
36 private $componentRegistrar;
41 private $jsonPersistor;
46 private $readerComposite;
51 private $primaryDbSchema;
56 private $elementNameResolver;
61 private $deploymentConfig;
80 $this->jsonPersistor = $jsonPersistor;
81 $this->schemaConfig = $schemaConfig;
82 $this->readerComposite = $readerComposite;
83 $this->elementNameResolver = $elementNameResolver;
95 $this->checkMagentoInstallation();
96 $schema = $this->schemaConfig->getDeclarationConfig();
98 foreach (array_keys($this->componentRegistrar->getPaths(
'module')) as $moduleName) {
99 $this->persistModule(
$schema, $moduleName);
102 $this->persistModule(
$schema, $moduleName);
111 private function checkMagentoInstallation()
114 if ($tablePrefixLength) {
116 __(
'Magento was installed with a table prefix. Please re-install without prefix.')
128 private function persistModule(Schema
$schema,
string $moduleName)
131 $modulePath = $this->componentRegistrar->getPath(
'module', $moduleName);
132 $whiteListFileName = $modulePath
133 . DIRECTORY_SEPARATOR
135 . DIRECTORY_SEPARATOR
139 if (file_exists($whiteListFileName)) {
143 $data = $this->filterPrimaryTables($this->readerComposite->read($moduleName));
144 if (!empty(
$data[
'table'])) {
149 [
$tableName => $this->getElementsWithFixedName($tabledata)],
150 [
$tableName => $this->getElementsWithAutogeneratedName(
158 $this->jsonPersistor->persist(
$content, $whiteListFileName);
169 private function getElementsWithFixedName(array $tableData): array
171 $declaredStructure = [];
172 if (!empty($tableData[
'column'])) {
173 $declaredColumns = array_keys($tableData[
'column']);
174 $declaredStructure[
'column'] = array_fill_keys($declaredColumns,
true);
176 return $declaredStructure;
187 private function getElementsWithAutogeneratedName(Schema
$schema,
string $tableName, array $tableData) : array
189 $declaredStructure = [];
192 $elementType =
'index';
193 if (!empty($tableData[$elementType])) {
194 foreach ($tableData[$elementType] as $tableElementData) {
195 $indexName = $this->elementNameResolver->getFullIndexName(
197 $tableElementData[
'column'],
198 $tableElementData[
'indexType'] ??
null 200 $declaredStructure[$elementType][$indexName] =
true;
204 $elementType =
'constraint';
205 if (!empty($tableData[$elementType])) {
206 foreach ($tableData[$elementType] as $tableElementData) {
207 if ($tableElementData[
'type'] ===
'foreign') {
208 $referenceTable =
$schema->getTableByName($tableElementData[
'referenceTable']);
209 $constraintName = $this->elementNameResolver->getFullFKName(
211 $table->getColumnByName($tableElementData[
'column']),
213 $referenceTable->getColumnByName($tableElementData[
'referenceColumn'])
216 $constraintName = $this->elementNameResolver->getFullIndexName(
218 $tableElementData[
'column'],
219 $tableElementData[
'type']
222 $declaredStructure[$elementType][$constraintName] =
true;
226 return $declaredStructure;
234 private function getPrimaryDbSchema(): array
236 if (!$this->primaryDbSchema) {
237 $this->primaryDbSchema = $this->readerComposite->read(
'primary');
239 return $this->primaryDbSchema;
248 private function filterPrimaryTables(array $moduleDbSchema): array
250 $primaryDbSchema = $this->getPrimaryDbSchema();
251 if (isset($moduleDbSchema[
'table']) && isset($primaryDbSchema[
'table'])) {
252 foreach (array_keys($primaryDbSchema[
'table']) as $tableNameKey) {
253 unset($moduleDbSchema[
'table'][$tableNameKey]);
256 return $moduleDbSchema;
const GENERATED_WHITELIST_FILE_NAME
const CONFIG_PATH_DB_PREFIX
__construct(ComponentRegistrar $componentRegistrar, JsonPersistor $jsonPersistor, SchemaConfig $schemaConfig, ReaderComposite $readerComposite, ElementNameResolver $elementNameResolver, DeploymentConfig $deploymentConfig)
generate(string $moduleName)