14
14
use Composer \Composer ;
15
15
use Composer \Config ;
16
16
use Composer \IO \IOInterface ;
17
+ use Composer \Json \JsonFile ;
17
18
use Composer \Package \AliasPackage ;
18
19
use Composer \Package \PackageInterface ;
19
20
use Composer \Script \Event ;
21
+ use Composer \Util \RemoteFilesystem ;
20
22
use Exception ;
21
23
use RuntimeException ;
22
24
use Symfony \Component \Filesystem \Filesystem ;
@@ -103,11 +105,6 @@ class PuliPluginImpl
103
105
*/
104
106
private $ initialized = false ;
105
107
106
- /**
107
- * @var string
108
- */
109
- private $ autoloadFile ;
110
-
111
108
public function __construct (Event $ event , PuliRunner $ puliRunner = null )
112
109
{
113
110
$ this ->composer = $ event ->getComposer ();
@@ -116,38 +113,19 @@ public function __construct(Event $event, PuliRunner $puliRunner = null)
116
113
$ this ->isDev = $ event ->isDevMode ();
117
114
$ this ->puliRunner = $ puliRunner ;
118
115
$ this ->rootDir = Path::normalize (getcwd ());
119
-
120
- $ vendorDir = $ this ->config ->get ('vendor-dir ' );
121
-
122
- // On TravisCI, $vendorDir is a relative path. Probably an old Composer
123
- // build or something. Usually, $vendorDir should be absolute already.
124
- $ vendorDir = Path::makeAbsolute ($ vendorDir , $ this ->rootDir );
125
-
126
- $ this ->autoloadFile = $ vendorDir .'/autoload.php ' ;
127
116
}
128
117
129
118
public function preAutoloadDump ()
130
119
{
131
- if (!$ this ->initialized ) {
132
- $ this ->initialize ();
133
- }
134
-
135
- // This method is called twice. Run it only once.
120
+ // This method is called multiple times. Run it only once.
136
121
if (!$ this ->runPreAutoloadDump ) {
137
122
return ;
138
123
}
139
124
140
125
$ this ->runPreAutoloadDump = false ;
141
126
142
- try {
143
- $ factoryClass = $ this ->getConfigKey ('factory.in.class ' );
144
- $ factoryFile = $ this ->getConfigKey ('factory.in.file ' );
145
- } catch (PuliRunnerException $ e ) {
146
- $ this ->printWarning ('Could not load Puli configuration ' , $ e );
147
-
148
- return ;
149
- }
150
-
127
+ $ factoryClass = $ this ->getConfigKey ('factory.in.class ' );
128
+ $ factoryFile = $ this ->getConfigKey ('factory.in.file ' );
151
129
$ factoryFile = Path::makeAbsolute ($ factoryFile , $ this ->rootDir );
152
130
153
131
$ autoload = $ this ->composer ->getPackage ()->getAutoload ();
@@ -178,7 +156,7 @@ public function postAutoloadDump()
178
156
$ this ->initialize ();
179
157
}
180
158
181
- // This method is called twice . Run it only once.
159
+ // This method is called multiple times . Run it only once.
182
160
if (!$ this ->runPostAutoloadDump ) {
183
161
return ;
184
162
}
@@ -193,8 +171,15 @@ public function postAutoloadDump()
193
171
return ;
194
172
}
195
173
196
- $ this ->insertFactoryClassConstant ($ this ->autoloadFile , $ factoryClass );
197
- $ this ->setBootstrapFile ($ this ->autoloadFile );
174
+ $ vendorDir = $ this ->config ->get ('vendor-dir ' );
175
+
176
+ // On TravisCI, $vendorDir is a relative path. Probably an old Composer
177
+ // build or something. Usually, $vendorDir should be absolute already.
178
+ $ vendorDir = Path::makeAbsolute ($ vendorDir , $ this ->rootDir );
179
+
180
+ $ autoloadFile = $ vendorDir .'/autoload.php ' ;
181
+ $ this ->insertFactoryClassConstant ($ autoloadFile , $ factoryClass );
182
+ $ this ->setBootstrapFile ($ autoloadFile );
198
183
}
199
184
200
185
/**
@@ -206,7 +191,7 @@ public function postInstall()
206
191
$ this ->initialize ();
207
192
}
208
193
209
- // This method is called twice . Run it only once.
194
+ // This method is called multiple times . Run it only once.
210
195
if (!$ this ->runPostInstall ) {
211
196
return ;
212
197
}
@@ -251,13 +236,6 @@ public function postInstall()
251
236
252
237
private function initialize ()
253
238
{
254
- if (!file_exists ($ this ->autoloadFile )) {
255
- $ filesystem = new Filesystem ();
256
- // Avoid problems if using the runner before autoload.php has been
257
- // generated
258
- $ filesystem ->dumpFile ($ this ->autoloadFile , '' );
259
- }
260
-
261
239
$ this ->initialized = true ;
262
240
263
241
// Keep the manually set runner
@@ -535,6 +513,33 @@ private function loadComposerPackages()
535
513
536
514
private function getConfigKey ($ key )
537
515
{
516
+ if (null === $ this ->puliRunner ) {
517
+ $ puliJsonPath = realpath (substr ($ this ->config ->get ('vendor-dir ' ), 0 , -strlen ($ this ->config ->get ('vendor-dir ' , Config::RELATIVE_PATHS )))) . DIRECTORY_SEPARATOR . 'puli.json ' ;
518
+ if (file_exists ($ puliJsonPath )) {
519
+ $ puliJsonFile = new JsonFile ($ puliJsonPath , new RemoteFilesystem ($ this ->io ));
520
+ $ puliConfig = $ puliJsonFile ->read ();
521
+ if (empty ($ puliConfig ['version ' ]) || '1.0 ' !== $ puliConfig ['version ' ]) {
522
+ throw new \RuntimeException ('Invalid configuration version schema! ' );
523
+ }
524
+ }
525
+ switch ($ key ) {
526
+ case 'factory.in.file ' :
527
+ if (empty ($ puliConfig ) || empty ($ puliConfig ['config ' ]['factory ' ]['in ' ]['file ' ])) {
528
+ return '.puli/GeneratedPuliFactory ' ;
529
+ }
530
+ return $ puliConfig ['config ' ]['factory ' ]['in ' ]['file ' ];
531
+ break ;
532
+ case 'factory.in.class ' :
533
+ if (empty ($ puliConfig ) || empty ($ puliConfig ['config ' ]['factory ' ]['in ' ]['class ' ])) {
534
+ return 'Puli \\GeneratedPuliFactory ' ;
535
+ }
536
+ return $ puliConfig ['config ' ]['factory ' ]['in ' ]['class ' ];
537
+ break ;
538
+ default :
539
+ throw new \RuntimeException (sprintf ('Cannot extract key "%s" from config! ' , $ key ));
540
+ }
541
+ }
542
+
538
543
$ value = trim ($ this ->puliRunner ->run ('config %key% --parsed ' , array (
539
544
'key ' => $ key ,
540
545
)));
0 commit comments