/** * @param \Thelia\Core\Event\Module\ModuleInstallEvent $event * * @throws \Exception * @throws \Symfony\Component\Filesystem\Exception\IOException * @throws \Exception */ public function install(ModuleInstallEvent $event) { $moduleDefinition = $event->getModuleDefinition(); $oldModule = ModuleQuery::create()->findOneByFullNamespace($moduleDefinition->getNamespace()); $dispatcher = $event->getDispatcher(); $fs = new Filesystem(); $activated = false; // check existing module if (null !== $oldModule) { $activated = $oldModule->getActivate(); if ($activated) { // deactivate $toggleEvent = new ModuleToggleActivationEvent($oldModule); // disable the check of the module because it's already done $toggleEvent->setNoCheck(true); $toggleEvent->setDispatcher($dispatcher); $dispatcher->dispatch(TheliaEvents::MODULE_TOGGLE_ACTIVATION, $toggleEvent); } // delete $modulePath = $oldModule->getAbsoluteBaseDir(); $deleteEvent = new ModuleDeleteEvent($oldModule); $deleteEvent->setDispatcher($dispatcher); try { $dispatcher->dispatch(TheliaEvents::MODULE_DELETE, $deleteEvent); } catch (Exception $ex) { // if module has not been deleted if ($fs->exists($modulePath)) { throw $ex; } } } // move new module $modulePath = sprintf('%s%s', THELIA_MODULE_DIR, $event->getModuleDefinition()->getCode()); try { $fs->mirror($event->getModulePath(), $modulePath); } catch (IOException $ex) { if (!$fs->exists($modulePath)) { throw $ex; } } // Update the module $moduleDescriptorFile = sprintf('%s%s%s%s%s', $modulePath, DS, 'Config', DS, 'module.xml'); $moduleManagement = new ModuleManagement(); $file = new SplFileInfo($moduleDescriptorFile); $module = $moduleManagement->updateModule($file, $this->container); // activate if old was activated if ($activated) { $toggleEvent = new ModuleToggleActivationEvent($module->getId()); $toggleEvent->setNoCheck(true); $toggleEvent->setDispatcher($dispatcher); $dispatcher->dispatch(TheliaEvents::MODULE_TOGGLE_ACTIVATION, $toggleEvent); } $event->setModule($module); }