public function runController($controllerName, $controllerNamespace) { \ifw::trace("run controller {$controllerName} in namespace {$controllerNamespace}"); $className = $this->getClassNamespace() . '\\' . $controllerNamespace . '\\' . $controllerName . 'Controller'; $this->dispatchEvent(self::EVENT_BEFORE_CONTROLLER); $this->response = \ifw::createObject($className, ['module' => $this, 'id' => $controllerName]); $this->dispatchEvent(self::EVENT_AFTER_CONTROLLER); return $this->response; }
private function createModel($row) { if ($this->_asArray) { return $row; } $obj = \ifw::createObject($this->modelClass); $obj->setAsUpdateRecord(); $obj->attributes = $row; return $obj; }
public function runAction($action) { Ifw::trace("run action '{$action}'"); $request = Ifw::$app->request; $this->dispatchEvent(self::EVENT_BEFORE_ACTION); $actions = $this->actions(); if (array_key_exists($action, $actions)) { $obj = \ifw::createObject($actions[$action], ['controller' => $this, 'id' => $action]); if (!$obj instanceof \ifw\core\Action) { throw new \ifw\core\Exception("The requested action class must be an instance of ifw\\core\\Action."); } $this->response = $obj->run(); } else { $actionName = 'action' . ucfirst($action); if (!$this->hasMethod($actionName)) { throw new \Exception("The requested action {$actionName} does not exists."); } $params = []; // @todo should implement the request of data inside the specific controller (cli/web) if (!$request->isCli()) { $actionTypes = $this->actionTypes(); $dataType = array_key_exists($action, $actionTypes) ? $actionTypes[$action] : $request::TYPE_GET; $data = $request->getData($dataType); } else { $data = $request->getParams(); } foreach ($this->getMethodArguments($actionName) as $arg) { if (array_key_exists($arg->name, $data)) { $value = $data[$arg->name]; if ($arg->isArray) { if (!is_array($value)) { throw new Exception("The paremeter '{$arg->name}' for action '{$actionName}' must be an array!"); } } } else { if (!$arg->isOptional) { throw new Exception("The action '{$actionName}' requires the {$dataType} parameter '{$arg->name}'."); } $value = $arg->defaultValue; } $params[] = $value; } $this->response = call_user_func_array([$this, $actionName], $params); } $this->dispatchEvent(self::EVENT_AFTER_ACTION); return $this->response; }
private function callHandler($handler, $context) { if (is_object($handler) && !$handler instanceof \Closure) { return $handler->handler($context); } if (is_callable($handler)) { return $handler($context); } if (is_array($handler) && isset($handler[0]) && is_object($handler[0]) && isset($handler[1]) && is_string($handler[1])) { return call_user_func_array([$handler[0], $handler[1]], []); } if (is_string($handler)) { $object = \ifw::createObject($handler); if ($object instanceof \ifw\core\Event) { $object->setContext($context); return $object->handler(); } return $object->handler($context); } throw new \Exception('The registered event handler is not valid.'); }