@@ -34,8 +34,19 @@ protected function execute(InputInterface $input, OutputInterface $output): int
34
34
35
35
$ localConfig = $ composer ->getConfig ();
36
36
37
- $ localPsr4Paths = $ localPackage ->getAutoload ()['psr-4 ' ];
38
- if (count ($ localPsr4Paths ) < 2 ) {
37
+ $ localPsr4Autoload = $ localPackage ->getAutoload ()['psr-4 ' ] ?? [];
38
+ $ hasCacheDirectory = false ;
39
+ foreach ($ localPsr4Autoload as $ directories ) {
40
+ if (!is_array ($ directories )) {
41
+ continue ;
42
+ }
43
+ if (count ($ directories ) < 2 ) {
44
+ continue ;
45
+ }
46
+ $ hasCacheDirectory = true ;
47
+ break ;
48
+ }
49
+ if (!$ hasCacheDirectory ) {
39
50
$ this ->getIO ()->writeError ("<bg=red>You must set cache directory</> " );
40
51
41
52
return 1 ;
@@ -97,55 +108,65 @@ protected function execute(InputInterface $input, OutputInterface $output): int
97
108
98
109
$ filesCount = 0 ;
99
110
100
- $ sourceDirectory = $ filesystem ->normalizePath ($ basePath . '/ ' . $ localPsr4Paths [1 ]);
101
- $ cacheDirectory = $ filesystem ->normalizePath ($ basePath . '/ ' . $ localPsr4Paths [0 ]);
102
- $ filesystem ->emptyDirectory ($ cacheDirectory );
111
+ foreach ($ localPsr4Autoload as $ directories ) {
112
+ if (!is_array ($ directories )) {
113
+ continue ;
114
+ }
103
115
104
- try {
105
- $ result = $ compiler ->compile ($ sourceDirectory );
116
+ if (count ($ directories ) < 2 ) {
117
+ continue ;
118
+ }
106
119
107
- foreach ($ result ->getConcreteClasses () as $ concreteClass ) {
108
- $ genericFilePath = $ classLoader ->findFile ($ concreteClass ->genericFqn );
109
- if (!$ genericFilePath ) {
110
- throw new \RuntimeException (sprintf ('Can \'t find file for class "%s" ' , $ concreteClass ->genericFqn ));
111
- }
120
+ $ sourceDirectory = $ filesystem ->normalizePath ($ basePath . '/ ' . $ directories [1 ]);
121
+ $ cacheDirectory = $ filesystem ->normalizePath ($ basePath . '/ ' . $ directories [0 ]);
122
+ $ filesystem ->emptyDirectory ($ cacheDirectory );
112
123
113
- $ package = self ::findPackageByFilePath ($ genericPackages , $ genericFilePath );
114
- if ($ package === null ) {
115
- throw new \RuntimeException (sprintf ('Can \'t find package for file "%s" ' , $ genericFilePath ));
116
- }
124
+ try {
125
+ $ result = $ compiler ->compile ($ sourceDirectory );
117
126
118
- $ cacheDirectory = $ package ->getCacheDirectory ($ genericFilePath );
127
+ foreach ($ result ->getConcreteClasses () as $ concreteClass ) {
128
+ $ genericFilePath = $ classLoader ->findFile ($ concreteClass ->genericFqn );
129
+ if (!$ genericFilePath ) {
130
+ throw new \RuntimeException (sprintf ('Can \'t find file for class "%s" ' , $ concreteClass ->genericFqn ));
131
+ }
119
132
120
- if ( array_key_exists ( $ cacheDirectory , $ emptiedCacheDirectories )) {
121
- $ filesystem -> emptyDirectory ( $ cacheDirectory );
122
- $ emptiedCacheDirectories [ $ cacheDirectory ] = true ;
123
- }
133
+ $ package = self :: findPackageByFilePath ( $ genericPackages , $ genericFilePath );
134
+ if ( $ package === null ) {
135
+ throw new \ RuntimeException ( sprintf ( ' Can \' t find package for file "%s" ' , $ genericFilePath )) ;
136
+ }
124
137
125
- $ concreteFilePath = rtrim ( $ cacheDirectory, DIRECTORY_SEPARATOR ) . DIRECTORY_SEPARATOR . ltrim ( $ package ->getRelativeFilePathByClassFqn ( $ concreteClass -> fqn ), DIRECTORY_SEPARATOR );
126
- $ filesystem -> ensureDirectoryExists ( dirname ( $ concreteFilePath ));
127
- if (file_put_contents ( $ concreteFilePath , $ printer -> printFile ( $ concreteClass -> ast )) === false ) {
128
- throw new \ RuntimeException ( sprintf ( ' Can \' t write into file "%s" ' , $ concreteFilePath ) );
129
- }
130
- $ filesCount ++;
138
+ $ cacheDirectory = $ package ->getCacheDirectory ( $ genericFilePath );
139
+
140
+ if (array_key_exists ( $ cacheDirectory , $ emptiedCacheDirectories ) ) {
141
+ $ filesystem -> emptyDirectory ( $ cacheDirectory );
142
+ $ emptiedCacheDirectories [ $ cacheDirectory ] = true ;
143
+ }
131
144
132
- if ($ this ->getIO ()->isVerbose ()) {
133
- $ line = sprintf (' - %s ' , $ concreteClass ->fqn );
134
- if ($ this ->getIO ()->isVeryVerbose ()) {
135
- $ line .= sprintf (' <comment>%s</comment> ' , $ concreteFilePath );
145
+ $ concreteFilePath = rtrim ($ cacheDirectory , DIRECTORY_SEPARATOR ) . DIRECTORY_SEPARATOR . ltrim ($ package ->getRelativeFilePathByClassFqn ($ concreteClass ->fqn ), DIRECTORY_SEPARATOR );
146
+ $ filesystem ->ensureDirectoryExists (dirname ($ concreteFilePath ));
147
+ if (file_put_contents ($ concreteFilePath , $ printer ->printFile ($ concreteClass ->ast )) === false ) {
148
+ throw new \RuntimeException (sprintf ('Can \'t write into file "%s" ' , $ concreteFilePath ));
149
+ }
150
+ $ filesCount ++;
151
+
152
+ if ($ this ->getIO ()->isVerbose ()) {
153
+ $ line = sprintf (' - %s ' , $ concreteClass ->fqn );
154
+ if ($ this ->getIO ()->isVeryVerbose ()) {
155
+ $ line .= sprintf (' <comment>%s</comment> ' , $ concreteFilePath );
156
+ }
157
+ $ this ->getIO ()->write ($ line );
136
158
}
137
- $ this ->getIO ()->write ($ line );
138
159
}
139
- }
140
160
141
- } catch (\Exception $ exception ) {
142
- $ this ->getIO ()->writeError (sprintf ("<bg=red>%s</> " , $ exception ->getMessage ()));
143
- if ($ exception ->getPrevious () !== null ) {
144
- $ this ->getIO ()->writeError (sprintf ("<bg=red>%s</> " , $ exception ->getPrevious ()->getMessage ()));
145
- }
146
- $ this ->getIO ()->writeError (sprintf ("<bg=red>%s</> " , $ exception ->getTraceAsString ()));
161
+ } catch (\Exception $ exception ) {
162
+ $ this ->getIO ()->writeError (sprintf ("<bg=red>%s</> " , $ exception ->getMessage ()));
163
+ if ($ exception ->getPrevious () !== null ) {
164
+ $ this ->getIO ()->writeError (sprintf ("<bg=red>%s</> " , $ exception ->getPrevious ()->getMessage ()));
165
+ }
166
+ $ this ->getIO ()->writeError (sprintf ("<bg=red>%s</> " , $ exception ->getTraceAsString ()));
147
167
148
- return 1 ;
168
+ return 1 ;
169
+ }
149
170
}
150
171
151
172
$ timeFin = microtime (true );
0 commit comments