Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Plugin.php
Go to the documentation of this file.
1 <?php
6 declare(strict_types=1);
7 
9 
10 use Composer\Autoload\ClassLoader;
11 use Composer\Composer;
12 use Composer\DependencyResolver\Operation\InstallOperation;
13 use Composer\DependencyResolver\Operation\UpdateOperation;
14 use Composer\EventDispatcher\EventSubscriberInterface;
15 use Composer\Installer\PackageEvent;
16 use Composer\IO\IOInterface;
17 use Composer\Plugin\PluginInterface;
22 
23 class Plugin implements PluginInterface, EventSubscriberInterface
24 {
28  private $composer;
32  private $io;
33 
37  private $moduleDeployment;
38 
39  public function activate(Composer $composer, IOInterface $io)
40  {
41  $this->composer = $composer;
42  $this->io = $io;
43  }
44 
45  public static function getSubscribedEvents()
46  {
47  return [
48  'pre-install-cmd' => 'initModuleDeployment',
49  'pre-update-cmd' => 'initModuleDeployment',
50  'post-package-install' => 'onPackageChange',
51  'post-package-update' => 'onPackageChange',
52  ];
53  }
54 
55  public function initModuleDeployment(): void
56  {
57  try {
58  if (!defined('BP')) {
59  define('BP', getcwd());
60  }
62 
63  $configurator = (new InventoryConfiguratorFactory($this->io))->createConfigurator(BP);
64  } catch (\Throwable $e) {
65  $configurator = new NoChangesConfigurator($this->io);
66  }
67 
68  $this->moduleDeployment = new InventoryModuleDeployment($configurator, $this->io);
69  }
70 
71  public function onPackageChange(PackageEvent $event): void
72  {
73  $operation = $event->getOperation();
74  if ($operation instanceof InstallOperation) {
75  $package = $operation->getPackage();
76  } elseif ($operation instanceof UpdateOperation) {
77  $package = $operation->getTargetPackage();
78  } else {
79  return;
80  }
81 
82  $this->getModuleDeployment()->deploy($package);
83  }
84 
85  private function getModuleDeployment(): InventoryModuleDeployment
86  {
87  if (!isset($this->moduleDeployment)) {
88  $this->initModuleDeployment();
89  }
90  return $this->moduleDeployment;
91  }
92 }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
onPackageChange(PackageEvent $event)
Definition: Plugin.php:71
static registerAutoloader(AutoloaderInterface $newAutoloader)
activate(Composer $composer, IOInterface $io)
Definition: Plugin.php:39
const BP
Definition: autoload.php:14