Frontend\Core\Engine\Block\Widget::execute PHP Метод

execute() публичный Метод

Execute the action We will build the class name, require the class and call the execute method.
public execute ( )
    public function execute()
    {
        // build action-class-name
        $actionClass = 'Frontend\\Modules\\' . $this->getModule() . '\\Widgets\\' . $this->getAction();
        if ($this->getModule() == 'Core') {
            $actionClass = 'Frontend\\Core\\Widgets\\' . $this->getAction();
        }
        // validate if class exists (aka has correct name)
        if (!class_exists($actionClass)) {
            throw new FrontendException('The action file ' . $actionClass . ' could not be found.');
        }
        // create action-object
        $this->object = new $actionClass($this->getKernel(), $this->getModule(), $this->getAction(), $this->getData());
        // validate if the execute-method is callable
        if (!is_callable(array($this->object, 'execute'))) {
            throw new FrontendException('The action file should contain a callable method "execute".');
        }
        // call the execute method of the real action (defined in the module)
        $this->object->execute();
        $this->output = $this->render($this->getCustomTemplate());
    }

Usage Example

Пример #1
0
 /**
  * Parse a widget straight from the template, rather than adding it through pages.
  *
  * @internal if your widget outputs random data you should cache it inside the widget
  * Fork checks the output and if the output of the widget is random it will loop until the random data
  * is the same as in the previous iteration
  * @param string $var    The variable.
  * @param string $module The module whose module we want to execute.
  * @param string $action The action to execute.
  * @param string $id     The widget id (saved in data-column).
  * @return string|null
  */
 public static function parseWidget($var, $module, $action, $id = null)
 {
     $data = $id !== null ? serialize(array('id' => $id)) : null;
     // create new widget instance and return parsed content
     $extra = new FrontendBlockWidget(Model::get('kernel'), $module, $action, $data);
     // set parseWidget because we will need it to skip setting headers in the display
     Model::getContainer()->set('parseWidget', true);
     try {
         $extra->execute();
         $content = $extra->getContent();
         Model::getContainer()->set('parseWidget', null);
         return $content;
     } catch (Exception $e) {
         // if we are debugging, we want to see the exception
         if (Model::getContainer()->getParameter('kernel.debug')) {
             throw $e;
         }
         return null;
     }
 }