Backend\Core\Engine\Base\Cronjob::setAction PHP Method

setAction() public method

We can't rely on the parent setModule function, because a cronjob requires no login
public setAction ( string $action, string $module = null )
$action string The action to load.
$module string The module to load.
    public function setAction($action, $module = null)
    {
        // set module
        if ($module !== null) {
            $this->setModule($module);
        }
        // check if module is set
        if ($this->getModule() === null) {
            throw new BackendException('Module has not yet been set.');
        }
        // path to look for actions based on the module
        if ($this->getModule() == 'Core') {
            $path = BACKEND_CORE_PATH . '/Cronjobs';
        } else {
            $path = BACKEND_MODULES_PATH . '/' . $this->getModule() . '/Cronjobs';
        }
        // check if file exists
        if (!is_file($path . '/' . $action . '.php')) {
            header('HTTP/1.1 403 Forbidden');
            throw new BackendException('Action not allowed.');
        }
        // set property
        $this->action = (string) $action;
    }

Usage Example

示例#1
0
 /**
  * Execute the action
  * We will build the classname, require the class and call the execute method.
  */
 protected function execute()
 {
     if (extension_loaded('newrelic')) {
         newrelic_background_job();
     }
     $this->loadConfig();
     // build action-class-name
     $actionClass = 'Backend\\Modules\\' . $this->getModule() . '\\Cronjobs\\' . $this->getAction();
     if ($this->getModule() == 'Core') {
         $actionClass = 'Backend\\Core\\Cronjobs\\' . $this->getAction();
     }
     // validate if class exists (aka has correct name)
     if (!class_exists($actionClass)) {
         // set correct headers
         \SpoonHTTP::setHeadersByCode(500);
         // throw exception
         throw new Exception('The cronjobfile is present, but the classname should be: ' . $actionClass . '.');
     }
     // create action-object
     $this->cronjob = new $actionClass($this->getKernel());
     $this->cronjob->setModule($this->getModule());
     $this->cronjob->setAction($this->getAction());
     if (extension_loaded('newrelic')) {
         newrelic_name_transaction('cronjob::' . $this->getModule() . '::' . $this->getAction());
     }
 }
All Usage Examples Of Backend\Core\Engine\Base\Cronjob::setAction