Esempio n. 1
0
 /**
  * @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);
 }
Esempio n. 2
0
 public function installAction()
 {
     if (null !== ($response = $this->checkAuth(AdminResources::MODULE, array(), AccessManager::CREATE))) {
         return $response;
     }
     $newModule = null;
     $moduleDefinition = null;
     /** @var ModuleInstallForm $moduleInstall */
     $moduleInstall = $this->createForm(AdminForm::MODULE_INSTALL);
     try {
         $this->validateForm($moduleInstall, "post");
         $moduleDefinition = $moduleInstall->getModuleDefinition();
         $modulePath = $moduleInstall->getModulePath();
         $moduleInstallEvent = new ModuleInstallEvent();
         $moduleInstallEvent->setModulePath($modulePath)->setModuleDefinition($moduleDefinition);
         $this->dispatch(TheliaEvents::MODULE_INSTALL, $moduleInstallEvent);
         $newModule = $moduleInstallEvent->getModule();
         if (null !== $newModule) {
             $this->getSession()->getFlashBag()->add('module-installed', $this->getTranslator()->trans('The module %module has been installed successfully.', ['%module' => $moduleDefinition->getCode()]));
             return $this->generateRedirectFromRoute('admin.module');
         } else {
             $message = $this->getTranslator()->trans("Sorry, an error occured.");
         }
     } catch (FormValidationException $e) {
         $message = $e->getMessage();
     } catch (\Exception $e) {
         $message = $this->getTranslator()->trans("Sorry, an error occured: %s", ['%s' => $e->getMessage()]);
     }
     Tlog::getInstance()->error(sprintf("Error during module installation process. Exception was %s", $message));
     $moduleInstall->setErrorMessage($message);
     $this->getParserContext()->addForm($moduleInstall)->setGeneralError($message);
     return $this->render("modules");
 }