17
17
*
18
18
* @SuppressWarnings(PHPMD.ExcessiveClassComplexity)
19
19
*/
20
- class DeclarativeSchemaDependencyProvider
20
+ class DeclarativeSchemaDependencyProvider extends DependencyProvider
21
21
{
22
- /**
23
- * Types of dependency between modules.
24
- */
25
- const TYPE_HARD = 'hard ' ;
26
-
27
- /**
28
- * The identifier of dependency for mapping.
29
- */
30
- const MAP_TYPE_DECLARED = 'declared ' ;
31
-
32
- /**
33
- * The identifier of dependency for mapping.
34
- */
35
- const MAP_TYPE_FOUND = 'found ' ;
36
-
37
22
/**
38
23
* Declarative name for table entity of the declarative schema.
39
24
*/
@@ -54,21 +39,11 @@ class DeclarativeSchemaDependencyProvider
54
39
*/
55
40
const SCHEMA_ENTITY_INDEX = 'index ' ;
56
41
57
- /**
58
- * @var array
59
- */
60
- private $ mapDependencies = [];
61
-
62
42
/**
63
43
* @var array
64
44
*/
65
45
private $ dbSchemaDeclaration = [];
66
46
67
- /**
68
- * @var array
69
- */
70
- private $ packageModuleMapping = [];
71
-
72
47
/**
73
48
* @var array
74
49
*/
@@ -145,42 +120,6 @@ private function getSchemaFileNameByModuleName(string $module): string
145
120
return $ this ->moduleSchemaFileMapping [$ module ] ?? '' ;
146
121
}
147
122
148
- /**
149
- * Initialise map of dependencies.
150
- *
151
- * @throws \Exception
152
- */
153
- private function initDeclaredDependencies ()
154
- {
155
- if (empty ($ this ->mapDependencies )) {
156
- $ jsonFiles = Files::init ()->getComposerFiles (ComponentRegistrar::MODULE , false );
157
- foreach ($ jsonFiles as $ file ) {
158
- $ json = new \Magento \Framework \Config \Composer \Package ($ this ->readJsonFile ($ file ));
159
- $ moduleName = $ this ->convertModuleName ($ json ->get ('name ' ));
160
- $ require = array_keys ((array )$ json ->get ('require ' ));
161
- $ this ->presetDependencies ($ moduleName , $ require , self ::TYPE_HARD );
162
- }
163
- }
164
- }
165
-
166
- /**
167
- * Read data from json file.
168
- *
169
- * @param string $file
170
- * @return mixed
171
- * @throws \Exception
172
- */
173
- private function readJsonFile (string $ file , bool $ asArray = false )
174
- {
175
- $ decodedJson = json_decode (file_get_contents ($ file ), $ asArray );
176
- if (null == $ decodedJson ) {
177
- //phpcs:ignore Magento2.Exceptions.DirectThrow
178
- throw new \Exception ("Invalid Json: $ file " );
179
- }
180
-
181
- return $ decodedJson ;
182
- }
183
-
184
123
/**
185
124
* Remove self dependencies.
186
125
*
@@ -629,74 +568,6 @@ private function collectDependency(
629
568
}
630
569
}
631
570
632
- /**
633
- * Add dependencies to dependency list.
634
- *
635
- * @param string $moduleName
636
- * @param array $packageNames
637
- * @param string $type
638
- *
639
- * @return void
640
- * @throws \Exception
641
- */
642
- private function presetDependencies (
643
- string $ moduleName ,
644
- array $ packageNames ,
645
- string $ type
646
- ): void {
647
- $ packageNames = array_filter ($ packageNames , function ($ packageName ) {
648
- return $ this ->getModuleName ($ packageName ) ||
649
- 0 === strpos ($ packageName , 'magento/ ' ) && 'magento/magento-composer-installer ' != $ packageName ;
650
- });
651
-
652
- foreach ($ packageNames as $ packageName ) {
653
- $ this ->addDependencies (
654
- $ moduleName ,
655
- $ type ,
656
- self ::MAP_TYPE_DECLARED ,
657
- [$ this ->convertModuleName ($ packageName )]
658
- );
659
- }
660
- }
661
-
662
- /**
663
- * Returns package name on module name mapping.
664
- *
665
- * @return array
666
- * @throws \Exception
667
- */
668
- private function getPackageModuleMapping (): array
669
- {
670
- if (!$ this ->packageModuleMapping ) {
671
- $ jsonFiles = Files::init ()->getComposerFiles (ComponentRegistrar::MODULE , false );
672
-
673
- $ packageModuleMapping = [];
674
- foreach ($ jsonFiles as $ file ) {
675
- $ moduleXml = simplexml_load_file (dirname ($ file ) . '/etc/module.xml ' );
676
- $ moduleName = str_replace ('_ ' , '\\' , (string )$ moduleXml ->module ->attributes ()->name );
677
- $ composerJson = $ this ->readJsonFile ($ file );
678
- $ packageName = $ composerJson ->name ;
679
- $ packageModuleMapping [$ packageName ] = $ moduleName ;
680
- }
681
-
682
- $ this ->packageModuleMapping = $ packageModuleMapping ;
683
- }
684
-
685
- return $ this ->packageModuleMapping ;
686
- }
687
-
688
- /**
689
- * Retrieve Magento style module name.
690
- *
691
- * @param string $packageName
692
- * @return null|string
693
- * @throws \Exception
694
- */
695
- private function getModuleName (string $ packageName ): ?string
696
- {
697
- return $ this ->getPackageModuleMapping ()[$ packageName ] ?? null ;
698
- }
699
-
700
571
/**
701
572
* Retrieve array of dependency items.
702
573
*
@@ -705,54 +576,8 @@ private function getModuleName(string $packageName): ?string
705
576
* @param $mapType
706
577
* @return array
707
578
*/
708
- private function getDeclaredDependencies (string $ module , string $ type , string $ mapType )
579
+ protected function getDeclaredDependencies (string $ module , string $ type , string $ mapType )
709
580
{
710
581
return $ this ->mapDependencies [$ module ][$ type ][$ mapType ] ?? [];
711
582
}
712
-
713
- /**
714
- * Add dependency map items.
715
- *
716
- * @param $module
717
- * @param $type
718
- * @param $mapType
719
- * @param $dependencies
720
- */
721
- protected function addDependencies (string $ module , string $ type , string $ mapType , array $ dependencies )
722
- {
723
- $ this ->mapDependencies [$ module ][$ type ][$ mapType ] = array_merge_recursive (
724
- $ this ->getDeclaredDependencies ($ module , $ type , $ mapType ),
725
- $ dependencies
726
- );
727
- }
728
-
729
- /**
730
- * Converts a composer json component name into the Magento Module form.
731
- *
732
- * @param string $jsonName The name of a composer json component or dependency e.g. 'magento/module-theme'
733
- * @return string The corresponding Magento Module e.g. 'Magento\Theme'
734
- * @throws \Exception
735
- */
736
- private function convertModuleName (string $ jsonName ): string
737
- {
738
- $ moduleName = $ this ->getModuleName ($ jsonName );
739
- if ($ moduleName ) {
740
- return $ moduleName ;
741
- }
742
-
743
- if (strpos ($ jsonName , 'magento/magento ' ) !== false
744
- || strpos ($ jsonName , 'magento/framework ' ) !== false
745
- ) {
746
- $ moduleName = str_replace ('/ ' , "\t" , $ jsonName );
747
- $ moduleName = str_replace ('framework- ' , "Framework \t" , $ moduleName );
748
- $ moduleName = str_replace ('- ' , ' ' , $ moduleName );
749
- $ moduleName = ucwords ($ moduleName );
750
- $ moduleName = str_replace ("\t" , '\\' , $ moduleName );
751
- $ moduleName = str_replace (' ' , '' , $ moduleName );
752
- } else {
753
- $ moduleName = $ jsonName ;
754
- }
755
-
756
- return $ moduleName ;
757
- }
758
583
}
0 commit comments