|
- <?php
-
-
- namespace yii\base;
-
- use Yii;
-
-
- class Controller extends Component implements ViewContextInterface
- {
-
-
- const EVENT_BEFORE_ACTION = 'beforeAction';
-
-
- const EVENT_AFTER_ACTION = 'afterAction';
-
-
-
- public $id;
-
-
- public $module;
-
-
- public $defaultAction = 'index';
-
-
- public $layout;
-
-
- public $action;
-
-
-
- private $_view;
-
-
- private $_viewPath;
-
-
-
-
- public function __construct($id, $module, $config = [])
- {
- $this->id = $id;
- $this->module = $module;
- parent::__construct($config);
- }
-
-
-
- public function actions()
- {
- return [];
- }
-
-
-
- public function runAction($id, $params = [])
- {
- $action = $this->createAction($id);
- if ($action === null) {
- throw new InvalidRouteException('Unable to resolve the request: ' . $this->getUniqueId() . '/' . $id);
- }
-
- Yii::trace('Route to run: ' . $action->getUniqueId(), __METHOD__);
-
- if (Yii::$app->requestedAction === null) {
- Yii::$app->requestedAction = $action;
- }
-
- $oldAction = $this->action;
- $this->action = $action;
-
- $modules = [];
- $runAction = true;
-
-
- foreach ($this->getModules() as $module) {
- if ($module->beforeAction($action)) {
- array_unshift($modules, $module);
- } else {
- $runAction = false;
- break;
- }
- }
-
- $result = null;
-
- if ($runAction && $this->beforeAction($action)) {
-
- $result = $action->runWithParams($params);
-
- $result = $this->afterAction($action, $result);
-
-
- foreach ($modules as $module) {
-
- $result = $module->afterAction($action, $result);
- }
- }
-
- $this->action = $oldAction;
-
- return $result;
- }
-
-
-
- public function run($route, $params = [])
- {
- $pos = strpos($route, '/');
- if ($pos === false) {
- return $this->runAction($route, $params);
- } elseif ($pos > 0) {
- return $this->module->runAction($route, $params);
- } else {
- return Yii::$app->runAction(ltrim($route, '/'), $params);
- }
- }
-
-
-
- public function bindActionParams($action, $params)
- {
- return [];
- }
-
-
-
- public function createAction($id)
- {
- if ($id === '') {
- $id = $this->defaultAction;
- }
-
- $actionMap = $this->actions();
- if (isset($actionMap[$id])) {
- return Yii::createObject($actionMap[$id], [$id, $this]);
- } elseif (preg_match('/^[a-z0-9\\-_]+$/', $id) && strpos($id, '--') === false && trim($id, '-') === $id) {
- $methodName = 'action' . str_replace(' ', '', ucwords(implode(' ', explode('-', $id))));
- if (method_exists($this, $methodName)) {
- $method = new \ReflectionMethod($this, $methodName);
- if ($method->isPublic() && $method->getName() === $methodName) {
- return new InlineAction($id, $this, $methodName);
- }
- }
- }
-
- return null;
- }
-
-
-
- public function beforeAction($action)
- {
- $event = new ActionEvent($action);
- $this->trigger(self::EVENT_BEFORE_ACTION, $event);
- return $event->isValid;
- }
-
-
-
- public function afterAction($action, $result)
- {
- $event = new ActionEvent($action);
- $event->result = $result;
- $this->trigger(self::EVENT_AFTER_ACTION, $event);
- return $event->result;
- }
-
-
-
- public function getModules()
- {
- $modules = [$this->module];
- $module = $this->module;
- while ($module->module !== null) {
- array_unshift($modules, $module->module);
- $module = $module->module;
- }
- return $modules;
- }
-
-
-
- public function getUniqueId()
- {
- return $this->module instanceof Application ? $this->id : $this->module->getUniqueId() . '/' . $this->id;
- }
-
-
-
- public function getRoute()
- {
- return $this->action !== null ? $this->action->getUniqueId() : $this->getUniqueId();
- }
-
-
-
- public function render($view, $params = [])
- {
- $content = $this->getView()->render($view, $params, $this);
- return $this->renderContent($content);
- }
-
-
-
- public function renderContent($content)
- {
- $layoutFile = $this->findLayoutFile($this->getView());
- if ($layoutFile !== false) {
- return $this->getView()->renderFile($layoutFile, ['content' => $content], $this);
- } else {
- return $content;
- }
- }
-
-
-
- public function renderPartial($view, $params = [])
- {
- return $this->getView()->render($view, $params, $this);
- }
-
-
-
- public function renderFile($file, $params = [])
- {
- return $this->getView()->renderFile($file, $params, $this);
- }
-
-
-
- public function getView()
- {
- if ($this->_view === null) {
- $this->_view = Yii::$app->getView();
- }
- return $this->_view;
- }
-
-
-
- public function setView($view)
- {
- $this->_view = $view;
- }
-
-
-
- public function getViewPath()
- {
- if ($this->_viewPath === null) {
- $this->_viewPath = $this->module->getViewPath() . DIRECTORY_SEPARATOR . $this->id;
- }
- return $this->_viewPath;
- }
-
-
-
- public function setViewPath($path)
- {
- $this->_viewPath = Yii::getAlias($path);
- }
-
-
-
- public function findLayoutFile($view)
- {
- $module = $this->module;
- if (is_string($this->layout)) {
- $layout = $this->layout;
- } elseif ($this->layout === null) {
- while ($module !== null && $module->layout === null) {
- $module = $module->module;
- }
- if ($module !== null && is_string($module->layout)) {
- $layout = $module->layout;
- }
- }
-
- if (!isset($layout)) {
- return false;
- }
-
- if (strncmp($layout, '@', 1) === 0) {
- $file = Yii::getAlias($layout);
- } elseif (strncmp($layout, '/', 1) === 0) {
- $file = Yii::$app->getLayoutPath() . DIRECTORY_SEPARATOR . substr($layout, 1);
- } else {
- $file = $module->getLayoutPath() . DIRECTORY_SEPARATOR . $layout;
- }
-
- if (pathinfo($file, PATHINFO_EXTENSION) !== '') {
- return $file;
- }
- $path = $file . '.' . $view->defaultExtension;
- if ($view->defaultExtension !== 'php' && !is_file($path)) {
- $path = $file . '.php';
- }
-
- return $path;
- }
- }
|