1515use Composer \Installer \InstallerInterface ;
1616use Composer \Package \PackageInterface ;
1717use MagentoHackathon \Composer \Magento \Deploy \Manager \Entry ;
18+ use React \Promise \PromiseInterface ;
1819
1920/**
2021 * Composer Magento Installer
@@ -99,7 +100,7 @@ class Installer extends LibraryInstaller implements InstallerInterface
99100 * @var bool
100101 */
101102 protected $ appendGitIgnore = false ;
102-
103+
103104 /**
104105 * @var array Path mapping prefixes that need to be translated (i.e. to
105106 * use a public directory as the web server root).
@@ -201,7 +202,7 @@ public function setDeployManager( DeployManager $deployManager)
201202 $ this ->deployManager = $ deployManager ;
202203 }
203204
204-
205+
205206 public function setConfig ( ProjectConfig $ config )
206207 {
207208 $ this ->config = $ config ;
@@ -329,7 +330,7 @@ public function getDeployStrategy(PackageInterface $package, $strategy = null)
329330 }
330331 if ( isset ($ extra ['magento-deploy-ignore ' ][$ package ->getName ()]) ){
331332 $ moduleSpecificDeployIgnores = array_merge (
332- $ moduleSpecificDeployIgnores ,
333+ $ moduleSpecificDeployIgnores ,
333334 $ extra ['magento-deploy-ignore ' ][$ package ->getName ()]
334335 );
335336 }
@@ -394,35 +395,39 @@ public function getTargetDir()
394395 }
395396
396397 /**
397- * Installs specific package
398- *
399- * @param InstalledRepositoryInterface $repo repository in which to check
400- * @param PackageInterface $package package instance
398+ * @inheritdoc
401399 */
402400 public function install (InstalledRepositoryInterface $ repo , PackageInterface $ package )
403401 {
404-
405402 if ($ package ->getType () === 'magento-core ' && !$ this ->preInstallMagentoCore ()) {
406403 return ;
407404 }
408405
409- parent ::install ($ repo , $ package );
406+ $ afterInstall = function () use ($ package ) {
407+ // skip marshal and apply default behavior if extra->map does not exist
408+ if ($ this ->hasExtraMap ($ package )) {
409+ $ strategy = $ this ->getDeployStrategy ($ package );
410+ $ strategy ->setMappings ($ this ->getParser ($ package )->getMappings ());
411+ $ deployManagerEntry = new Entry ();
412+ $ deployManagerEntry ->setPackageName ($ package ->getName ());
413+ $ deployManagerEntry ->setDeployStrategy ($ strategy );
414+ $ this ->deployManager ->addPackage ($ deployManagerEntry );
410415
411- // skip marshal and apply default behavior if extra->map does not exist
412- if (!$ this ->hasExtraMap ($ package )) {
413- return ;
414- }
416+ if ($ this ->appendGitIgnore ) {
417+ $ this ->appendGitIgnore ($ package , $ this ->getGitIgnoreFileLocation ());
418+ }
419+ }
420+ };
415421
416- $ strategy = $ this ->getDeployStrategy ($ package );
417- $ strategy ->setMappings ($ this ->getParser ($ package )->getMappings ());
418- $ deployManagerEntry = new Entry ();
419- $ deployManagerEntry ->setPackageName ($ package ->getName ());
420- $ deployManagerEntry ->setDeployStrategy ($ strategy );
421- $ this ->deployManager ->addPackage ($ deployManagerEntry );
422+ $ promise = parent ::install ($ repo , $ package );
422423
423- if ($ this ->appendGitIgnore ) {
424- $ this ->appendGitIgnore ($ package , $ this ->getGitIgnoreFileLocation ());
424+ // Composer v2 might return a promise here
425+ if ($ promise instanceof PromiseInterface) {
426+ return $ promise ->then ($ afterInstall );
425427 }
428+
429+ // If not, execute the code right away as parent::install executed synchronously (composer v1, or v2 without async)
430+ $ afterInstall ();
426431 }
427432
428433 /**
@@ -464,7 +469,7 @@ public function appendGitIgnore(PackageInterface $package, $ignoreFile)
464469 if ( in_array ($ ignore , $ ignoredMappings ) ){
465470 continue ;
466471 }
467-
472+
468473 $ additions [] = $ ignore ;
469474 }
470475 }
@@ -474,7 +479,7 @@ public function appendGitIgnore(PackageInterface $package, $ignoreFile)
474479 $ contents = array_merge ($ contents , $ additions );
475480 file_put_contents ($ ignoreFile , implode ("\n" , $ contents ));
476481 }
477-
482+
478483 if ($ package ->getType () === 'magento-core ' ) {
479484 $ this ->prepareMagentoCore ();
480485 }
@@ -590,17 +595,10 @@ protected function redeployProject() {
590595 }
591596
592597 /**
593- * Updates specific package
594- *
595- * @param InstalledRepositoryInterface $repo repository in which to check
596- * @param PackageInterface $initial already installed package version
597- * @param PackageInterface $target updated version
598- *
599- * @throws InvalidArgumentException if $from package is not installed
598+ * @inheritdoc
600599 */
601600 public function update (InstalledRepositoryInterface $ repo , PackageInterface $ initial , PackageInterface $ target )
602601 {
603-
604602 if ($ target ->getType () === 'magento-core ' && !$ this ->preUpdateMagentoCore ()) {
605603 return ;
606604 }
@@ -618,25 +616,35 @@ public function update(InstalledRepositoryInterface $repo, PackageInterface $ini
618616 }
619617 }
620618
621- parent ::update ($ repo , $ initial , $ target );
619+ $ afterUpdate = function () use ($ target ) {
620+ // marshal files for new package version if extra->map exist
621+ if ($ this ->hasExtraMap ($ target )) {
622+ $ targetStrategy = $ this ->getDeployStrategy ($ target );
623+ $ targetStrategy ->setMappings ($ this ->getParser ($ target )->getMappings ());
624+ $ deployManagerEntry = new Entry ();
625+ $ deployManagerEntry ->setPackageName ($ target ->getName ());
626+ $ deployManagerEntry ->setDeployStrategy ($ targetStrategy );
627+ $ this ->deployManager ->addPackage ($ deployManagerEntry );
628+ }
622629
623- // marshal files for new package version if extra->map exist
624- if ($ this ->hasExtraMap ($ target )) {
625- $ targetStrategy = $ this ->getDeployStrategy ($ target );
626- $ targetStrategy ->setMappings ($ this ->getParser ($ target )->getMappings ());
627- $ deployManagerEntry = new Entry ();
628- $ deployManagerEntry ->setPackageName ($ target ->getName ());
629- $ deployManagerEntry ->setDeployStrategy ($ targetStrategy );
630- $ this ->deployManager ->addPackage ($ deployManagerEntry );
631- }
630+ if ($ this ->appendGitIgnore ) {
631+ $ this ->appendGitIgnore ($ target , $ this ->getGitIgnoreFileLocation ());
632+ }
632633
633- if ($ this ->appendGitIgnore ) {
634- $ this ->appendGitIgnore ($ target , $ this ->getGitIgnoreFileLocation ());
635- }
634+ if ($ target ->getType () === 'magento-core ' ) {
635+ $ this ->postUpdateMagentoCore ();
636+ }
637+ };
638+
639+ $ promise = parent ::update ($ repo , $ initial , $ target );
636640
637- if ($ target ->getType () === 'magento-core ' ) {
638- $ this ->postUpdateMagentoCore ();
641+ // Composer v2 might return a promise here
642+ if ($ promise instanceof PromiseInterface) {
643+ return $ promise ->then ($ afterUpdate );
639644 }
645+
646+ // If not, execute the code right away as parent::update executed synchronously (composer v1, or v2 without async)
647+ $ afterUpdate ();
640648 }
641649
642650
@@ -707,30 +715,24 @@ public function clearMagentoCache() {
707715 }
708716
709717 /**
710- * Uninstalls specific package.
711- *
712- * @param InstalledRepositoryInterface $repo repository in which to check
713- * @param PackageInterface $package package instance
718+ * @inheritdoc
714719 */
715720 public function uninstall (InstalledRepositoryInterface $ repo , PackageInterface $ package )
716721 {
717722 // skip marshal and apply default behavior if extra->map does not exist
718- if (!$ this ->hasExtraMap ($ package )) {
719- parent ::uninstall ($ repo , $ package );
720- return ;
721- }
722-
723- $ strategy = $ this ->getDeployStrategy ($ package );
724- $ strategy ->setMappings ($ this ->getParser ($ package )->getMappings ());
725- try {
726- $ strategy ->clean ();
727- } catch (\ErrorException $ e ) {
728- if ($ this ->io ->isDebug ()) {
729- $ this ->io ->write ($ e ->getMessage ());
723+ if ($ this ->hasExtraMap ($ package )) {
724+ $ strategy = $ this ->getDeployStrategy ($ package );
725+ $ strategy ->setMappings ($ this ->getParser ($ package )->getMappings ());
726+ try {
727+ $ strategy ->clean ();
728+ } catch (\ErrorException $ e ) {
729+ if ($ this ->io ->isDebug ()) {
730+ $ this ->io ->write ($ e ->getMessage ());
731+ }
730732 }
731733 }
732734
733- parent ::uninstall ($ repo , $ package );
735+ return parent ::uninstall ($ repo , $ package );
734736 }
735737
736738 /**
@@ -792,7 +794,7 @@ public function getInstallPath(PackageInterface $package)
792794
793795 return $ installPath ;
794796 }
795-
797+
796798 public function transformArrayKeysToLowerCase ($ array )
797799 {
798800 $ arrayNew = array ();
@@ -806,7 +808,7 @@ public function transformArrayKeysToLowerCase($array)
806808 * this function is for annoying people with messages.
807809 *
808810 * First usage: get people to vote about the future release of composer so later I can say "you wanted it this way"
809- *
811+ *
810812 * @param IOInterface $io
811813 */
812814 public function annoy (IOInterface $ io )
@@ -819,7 +821,7 @@ public function annoy(IOInterface $io)
819821 $io->write('<comment> time for voting about the future of the #magento #composer installer. </comment>', true);
820822 $io->write('<comment> https://github.com/magento-hackathon/magento-composer-installer/blob/discussion-master/Milestone/2/index.md </comment>', true);
821823 $io->write('<error> For the case you don\'t vote, I will ignore your problems till iam finished with the resulting release. </error>', true);
822- *
824+ *
823825 **/
824826 }
825827
0 commit comments