Beispiel #1
0
 /**
  * @params ModuleHookModel $hook
  * @depends testToggleActivation
  */
 public function testUpdate(ModuleHookModel $moduleHook)
 {
     $event = new ModuleHookUpdateEvent($moduleHook);
     $event->setHookId($moduleHook->getHookId())->setClassname($moduleHook->getClassname())->setMethod($moduleHook->getMethod())->setActive(true)->setDispatcher($this->dispatcher);
     $this->action->updateModuleHook($event);
     $updatedModuleHook = $event->getModuleHook();
     $this->assertEquals($event->getHookId(), $updatedModuleHook->getHookId());
     $this->assertEquals($event->getClassname(), $updatedModuleHook->getClassname());
     $this->assertEquals($event->getMethod(), $updatedModuleHook->getMethod());
     $this->assertEquals($event->getActive(), $updatedModuleHook->getActive());
     return $updatedModuleHook;
 }
 /**
  * Hydrate the update form for this object, before passing it to the update template
  *
  * @param ModuleHook $object
  * @return ModuleHookModificationForm
  */
 protected function hydrateObjectForm($object)
 {
     $data = ['id' => $object->getId(), 'hook_id' => $object->getHookId(), 'module_id' => $object->getModuleId(), 'classname' => $object->getClassname(), 'method' => $object->getMethod(), 'active' => $object->getActive()];
     return new ModuleHookModificationForm($this->getRequest(), 'form', $data);
 }
 /**
  * Create a new hook if the hook definition is valid.
  *
  * @param string               $class  the namespace of the class
  * @param \Thelia\Model\Module $module the module
  * @param string               $id     the service (hook) id
  * @param array                $event  the event attributes
  *
  * @throws \InvalidArgumentException
  */
 protected function registerHook($class, $module, $id, $event)
 {
     $active = isset($event['active']) ? intval($event['active']) : 1;
     $active = 1 === $active;
     if (!isset($event['event'])) {
         throw new \InvalidArgumentException(sprintf('Service "%s" must define the "event" attribute on "hook.event_listener" tags.', $id));
     }
     $type = isset($event['type']) ? $this->getHookType($event['type']) : TemplateDefinition::FRONT_OFFICE;
     $hook = HookQuery::create()->filterByCode($event['event'])->filterByType($type)->findOne();
     if (null === $hook) {
         Tlog::getInstance()->addAlert(sprintf("Hook %s is unknown.", $event['event']));
         return;
     }
     if (!$hook->getActivate()) {
         Tlog::getInstance()->addAlert(sprintf("Hook %s is not activated.", $event['event']));
         return;
     }
     if (!isset($event['method'])) {
         $callback = function ($matches) {
             return strtoupper($matches[0]);
         };
         $event['method'] = 'on' . preg_replace_callback(array('/(?<=\\b)[a-z]/i', '/[^a-z0-9]/i'), $callback, $event['event']);
         $event['method'] = preg_replace('/[^a-z0-9]/i', '', $event['method']);
     }
     // test if method exists
     if (!$this->isValidHookMethod($class, $event['method'], $hook->getBlock())) {
         return;
     }
     // test if hook is already registered in ModuleHook
     $moduleHook = ModuleHookQuery::create()->filterByModuleId($module)->filterByHook($hook)->filterByMethod($event['method'])->findOne();
     if (null === $moduleHook) {
         // Assign the module to the hook only if it has not been "deleted"
         $ignoreCount = IgnoredModuleHookQuery::create()->filterByHook($hook)->filterByModuleId($module)->count();
         if (0 === $ignoreCount) {
             // hook for module doesn't exist, we add it with default registered values
             $moduleHook = new ModuleHook();
             $moduleHook->setHook($hook)->setModuleId($module)->setClassname($id)->setMethod($event['method'])->setActive($active)->setHookActive(true)->setModuleActive(true)->setPosition(ModuleHook::MAX_POSITION)->save();
         }
     } else {
         // Update hook if id was changed in the definition
         if ($moduleHook->getClassname() != $id) {
             $moduleHook->setClassname($id)->save();
         }
     }
 }
 /**
  * Create a new hook if the hook definition is valid.
  *
  * @param string               $class  the namespace of the class
  * @param \Thelia\Model\Module $module the module
  * @param string               $id     the service (hook) id
  * @param array                $attributes  the hook attributes
  *
  * @throws \InvalidArgumentException
  */
 protected function registerHook($class, $module, $id, $attributes)
 {
     if (!isset($attributes['event'])) {
         throw new \InvalidArgumentException(sprintf('Service "%s" must define the "event" attribute on "hook.event_listener" tags.', $id));
     }
     $active = isset($attributes['active']) ? intval($attributes['active']) : 1;
     $attributes['active'] = 1 === $active;
     $attributes['templates'] = isset($attributes['templates']) ? strval($attributes['templates']) : '';
     $attributes['type'] = isset($attributes['type']) ? $this->getHookType($attributes['type']) : TemplateDefinition::FRONT_OFFICE;
     if (null === ($hook = $this->getHook($attributes['event'], $attributes['type']))) {
         return;
     }
     $attributes = $this->getMethodName($attributes);
     // test if method exists
     $validMethod = true;
     if (!$this->isValidHookMethod($class, $attributes['method'], $hook->getBlock())) {
         $validMethod = false;
     }
     // test if hook is already registered in ModuleHook
     $moduleHook = ModuleHookQuery::create()->filterByModuleId($module->getId())->filterByHook($hook)->filterByMethod($attributes['method'])->findOne();
     if (null === $moduleHook) {
         if (!$validMethod) {
             Tlog::getInstance()->addAlert(sprintf("Module [%s] could not be registered hook [%s], method [%s] is not reachable.", $module->getCode(), $attributes['event'], $attributes['method']));
             return;
         }
         // Assign the module to the hook only if it has not been "deleted"
         $ignoreCount = IgnoredModuleHookQuery::create()->filterByHook($hook)->filterByModuleId($module->getId())->count();
         if (0 === $ignoreCount) {
             // hook for module doesn't exist, we add it with default registered values
             $moduleHook = new ModuleHook();
             $moduleHook->setHook($hook)->setModuleId($module->getId())->setClassname($id)->setMethod($attributes['method'])->setActive($active)->setHookActive(true)->setModuleActive(true)->setPosition(ModuleHook::MAX_POSITION);
             if (isset($attributes['templates'])) {
                 $moduleHook->setTemplates($attributes['templates']);
             }
             $moduleHook->save();
         }
     } else {
         if (!$validMethod) {
             Tlog::getInstance()->addAlert(sprintf("Module [%s] could not use hook [%s], method [%s] is not reachable anymore.", $module->getCode(), $attributes['event'], $attributes['method']));
             $moduleHook->setHookActive(false)->save();
         } else {
             //$moduleHook->setTemplates($attributes['templates']);
             // Update hook if id was changed in the definition
             if ($moduleHook->getClassname() != $id) {
                 $moduleHook->setClassname($id);
             }
             $moduleHook->save();
         }
     }
 }
 /**
  * Hydrate the update form for this object, before passing it to the update template
  *
  * @param ModuleHook $object
  * @return ModuleHookModificationForm
  */
 protected function hydrateObjectForm($object)
 {
     $data = ['id' => $object->getId(), 'hook_id' => $object->getHookId(), 'module_id' => $object->getModuleId(), 'classname' => $object->getClassname(), 'method' => $object->getMethod(), 'active' => $object->getActive()];
     return $this->createForm(AdminForm::MODULE_HOOK_MODIFICATION, 'form', $data);
 }