1111namespace Wikimedia \Composer ;
1212
1313use Composer \Composer ;
14- use Composer \Config ;
14+ use Composer \DependencyResolver \ Operation \ InstallOperation ;
1515use Composer \EventDispatcher \EventSubscriberInterface ;
1616use Composer \Factory ;
1717use Composer \Installer ;
@@ -115,10 +115,26 @@ class MergePlugin implements PluginInterface, EventSubscriberInterface
115115 protected $ loadedFiles = array ();
116116
117117 /**
118+ * Is this the first time that our plugin has been installed?
119+ *
118120 * @var bool $pluginFirstInstall
119121 */
120122 protected $ pluginFirstInstall ;
121123
124+ /**
125+ * Is the autoloader file supposed to be written out?
126+ *
127+ * @var bool $dumpAutoloader
128+ */
129+ protected $ dumpAutoloader ;
130+
131+ /**
132+ * Is the autoloader file supposed to be optimized?
133+ *
134+ * @var bool $optimizeAutoloader
135+ */
136+ protected $ optimizeAutoloader ;
137+
122138 /**
123139 * {@inheritdoc}
124140 */
@@ -136,23 +152,23 @@ public static function getSubscribedEvents()
136152 {
137153 return array (
138154 InstallerEvents::PRE_DEPENDENCIES_SOLVING => 'onDependencySolve ' ,
139- ScriptEvents::PRE_INSTALL_CMD => 'onInstallOrUpdate ' ,
140- ScriptEvents::PRE_UPDATE_CMD => 'onInstallOrUpdate ' ,
141- ScriptEvents::PRE_AUTOLOAD_DUMP => 'onInstallOrUpdate ' ,
142155 PackageEvents::POST_PACKAGE_INSTALL => 'onPostPackageInstall ' ,
143156 ScriptEvents::POST_INSTALL_CMD => 'onPostInstallOrUpdate ' ,
144157 ScriptEvents::POST_UPDATE_CMD => 'onPostInstallOrUpdate ' ,
158+ ScriptEvents::PRE_AUTOLOAD_DUMP => 'onInstallUpdateOrDump ' ,
159+ ScriptEvents::PRE_INSTALL_CMD => 'onInstallUpdateOrDump ' ,
160+ ScriptEvents::PRE_UPDATE_CMD => 'onInstallUpdateOrDump ' ,
145161 );
146162 }
147163
148164 /**
149- * Handle an event callback for an install or update command by checking
150- * for "merge-patterns" in the "extra" data and merging package contents
151- * if found.
165+ * Handle an event callback for an install, update or dump command by
166+ * checking for "merge-patterns" in the "extra" data and merging package
167+ * contents if found.
152168 *
153169 * @param Event $event
154170 */
155- public function onInstallOrUpdate (Event $ event )
171+ public function onInstallUpdateOrDump (Event $ event )
156172 {
157173 $ config = $ this ->readConfig ($ this ->getRootPackage ());
158174 if (isset ($ config ['recurse ' ])) {
@@ -167,6 +183,14 @@ public function onInstallOrUpdate(Event $event)
167183 $ this ->devMode = $ event ->isDevMode ();
168184 $ this ->mergePackages ($ config );
169185 }
186+
187+ if ($ event ->getName () === ScriptEvents::PRE_AUTOLOAD_DUMP ) {
188+ $ this ->dumpAutoloader = true ;
189+ $ flags = $ event ->getFlags ();
190+ if (isset ($ flags ['optimize ' ])) {
191+ $ this ->optimizeAutoloader = $ flags ['optimize ' ];
192+ }
193+ }
170194 }
171195
172196 /**
@@ -432,7 +456,14 @@ protected function mergeLinks(array $origin, array $merge, array &$dups)
432456 public function onDependencySolve (InstallerEvent $ event )
433457 {
434458 if (empty ($ this ->duplicateLinks )) {
459+ // @codeCoverageIgnoreStart
460+ // We shouldn't really ever be able to get here as this event is
461+ // triggered inside Composer\Installer and should have been
462+ // preceded by a pre-install or pre-update event but better to
463+ // have an unneeded check than to break with some future change in
464+ // the event system.
435465 return ;
466+ // @codeCoverageIgnoreEnd
436467 }
437468
438469 $ request = $ event ->getRequest ();
@@ -456,10 +487,13 @@ public function onDependencySolve(InstallerEvent $event)
456487 */
457488 public function onPostPackageInstall (PackageEvent $ event )
458489 {
459- $ package = $ event ->getOperation ()->getPackage ()->getName ();
460- if ($ package === self ::PACKAGE_NAME ) {
461- $ this ->debug ('composer-merge-plugin installed ' );
462- $ this ->pluginFirstInstall = true ;
490+ $ op = $ event ->getOperation ();
491+ if ($ op instanceof InstallOperation) {
492+ $ package = $ op ->getPackage ()->getName ();
493+ if ($ package === self ::PACKAGE_NAME ) {
494+ $ this ->debug ('composer-merge-plugin installed ' );
495+ $ this ->pluginFirstInstall = true ;
496+ }
463497 }
464498 }
465499
@@ -490,19 +524,28 @@ public function onPostInstallOrUpdate(Event $event)
490524 '</comment> '
491525 );
492526
527+ $ config = $ this ->composer ->getConfig ();
528+
529+ $ preferSource = $ config ->get ('preferred-install ' ) == 'source ' ;
530+ $ preferDist = $ config ->get ('preferred-install ' ) == 'dist ' ;
531+
493532 $ installer = Installer::create (
494533 $ event ->getIO (),
495534 // Create a new Composer instance to ensure full processing of
496535 // the merged files.
497536 Factory::create ($ event ->getIO (), null , false )
498537 );
499538
539+ $ installer ->setPreferSource ($ preferSource );
540+ $ installer ->setPreferDist ($ preferDist );
541+ $ installer ->setDevMode ($ event ->isDevMode ());
542+ $ installer ->setDumpAutoloader ($ this ->dumpAutoloader );
543+ $ installer ->setOptimizeAutoloader ($ this ->optimizeAutoloader );
544+
500545 // Force update mode so that new packages are processed rather
501546 // than just telling the user that composer.json and composer.lock
502547 // don't match.
503548 $ installer ->setUpdate (true );
504- $ installer ->setDevMode ($ event ->isDevMode ());
505- // TODO: can we set more flags to match the current run?
506549
507550 $ installer ->run ();
508551 }
0 commit comments