|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640 |
- <?php
-
-
- namespace yii\base;
-
- use Yii;
- use yii\di\ServiceLocator;
-
-
- class Module extends ServiceLocator
- {
-
-
- const EVENT_BEFORE_ACTION = 'beforeAction';
-
-
- const EVENT_AFTER_ACTION = 'afterAction';
-
-
-
- public $params = [];
-
-
- public $id;
-
-
- public $module;
-
-
- public $layout;
-
-
- public $controllerMap = [];
-
-
- public $controllerNamespace;
-
-
- public $defaultRoute = 'default';
-
-
-
- private $_basePath;
-
-
- private $_viewPath;
-
-
- private $_layoutPath;
-
-
- private $_modules = [];
-
-
-
-
- public function __construct($id, $parent = null, $config = [])
- {
- $this->id = $id;
- $this->module = $parent;
- parent::__construct($config);
- }
-
-
-
- public static function getInstance()
- {
- $class = get_called_class();
- return isset(Yii::$app->loadedModules[$class]) ? Yii::$app->loadedModules[$class] : null;
- }
-
-
-
- public static function setInstance($instance)
- {
- if ($instance === null) {
- unset(Yii::$app->loadedModules[get_called_class()]);
- } else {
- Yii::$app->loadedModules[get_class($instance)] = $instance;
- }
- }
-
-
-
- public function init()
- {
- if ($this->controllerNamespace === null) {
- $class = get_class($this);
- if (($pos = strrpos($class, '\\')) !== false) {
- $this->controllerNamespace = substr($class, 0, $pos) . '\\controllers';
- }
- }
- }
-
-
-
- public function getUniqueId()
- {
- return $this->module ? ltrim($this->module->getUniqueId() . '/' . $this->id, '/') : $this->id;
- }
-
-
-
- public function getBasePath()
- {
- if ($this->_basePath === null) {
- $class = new \ReflectionClass($this);
- $this->_basePath = dirname($class->getFileName());
- }
-
- return $this->_basePath;
- }
-
-
-
- public function setBasePath($path)
- {
- $path = Yii::getAlias($path);
- $p = strncmp($path, 'phar://', 7) === 0 ? $path : realpath($path);
- if ($p !== false && is_dir($p)) {
- $this->_basePath = $p;
- } else {
- throw new InvalidParamException("The directory does not exist: $path");
- }
- }
-
-
-
- public function getControllerPath()
- {
- return Yii::getAlias('@' . str_replace('\\', '/', $this->controllerNamespace));
- }
-
-
-
- public function getViewPath()
- {
- if ($this->_viewPath === null) {
- $this->_viewPath = $this->getBasePath() . DIRECTORY_SEPARATOR . 'views';
- }
- return $this->_viewPath;
- }
-
-
-
- public function setViewPath($path)
- {
- $this->_viewPath = Yii::getAlias($path);
- }
-
-
-
- public function getLayoutPath()
- {
- if ($this->_layoutPath === null) {
- $this->_layoutPath = $this->getViewPath() . DIRECTORY_SEPARATOR . 'layouts';
- }
-
- return $this->_layoutPath;
- }
-
-
-
- public function setLayoutPath($path)
- {
- $this->_layoutPath = Yii::getAlias($path);
- }
-
-
-
- public function setAliases($aliases)
- {
- foreach ($aliases as $name => $alias) {
- Yii::setAlias($name, $alias);
- }
- }
-
-
-
- public function hasModule($id)
- {
- if (($pos = strpos($id, '/')) !== false) {
-
- $module = $this->getModule(substr($id, 0, $pos));
-
- return $module === null ? false : $module->hasModule(substr($id, $pos + 1));
- } else {
- return isset($this->_modules[$id]);
- }
- }
-
-
-
- public function getModule($id, $load = true)
- {
- if (($pos = strpos($id, '/')) !== false) {
-
- $module = $this->getModule(substr($id, 0, $pos));
-
- return $module === null ? null : $module->getModule(substr($id, $pos + 1), $load);
- }
-
- if (isset($this->_modules[$id])) {
- if ($this->_modules[$id] instanceof Module) {
- return $this->_modules[$id];
- } elseif ($load) {
- Yii::trace("Loading module: $id", __METHOD__);
-
- $module = Yii::createObject($this->_modules[$id], [$id, $this]);
- $module->setInstance($module);
- return $this->_modules[$id] = $module;
- }
- }
-
- return null;
- }
-
-
-
- public function setModule($id, $module)
- {
- if ($module === null) {
- unset($this->_modules[$id]);
- } else {
- $this->_modules[$id] = $module;
- }
- }
-
-
-
- public function getModules($loadedOnly = false)
- {
- if ($loadedOnly) {
- $modules = [];
- foreach ($this->_modules as $module) {
- if ($module instanceof Module) {
- $modules[] = $module;
- }
- }
-
- return $modules;
- } else {
- return $this->_modules;
- }
- }
-
-
-
- public function setModules($modules)
- {
- foreach ($modules as $id => $module) {
- $this->_modules[$id] = $module;
- }
- }
-
-
-
- public function runAction($route, $params = [])
- {
- $parts = $this->createController($route);
- if (is_array($parts)) {
-
- list($controller, $actionID) = $parts;
- $oldController = Yii::$app->controller;
- Yii::$app->controller = $controller;
- $result = $controller->runAction($actionID, $params);
- if ($oldController !== null) {
- Yii::$app->controller = $oldController;
- }
-
- return $result;
- } else {
- $id = $this->getUniqueId();
- throw new InvalidRouteException('Unable to resolve the request "' . ($id === '' ? $route : $id . '/' . $route) . '".');
- }
- }
-
-
-
- public function createController($route)
- {
- if ($route === '') {
- $route = $this->defaultRoute;
- }
-
-
- $route = trim($route, '/');
- if (strpos($route, '//') !== false) {
- return false;
- }
-
- if (strpos($route, '/') !== false) {
- list ($id, $route) = explode('/', $route, 2);
- } else {
- $id = $route;
- $route = '';
- }
-
-
- if (isset($this->controllerMap[$id])) {
- $controller = Yii::createObject($this->controllerMap[$id], [$id, $this]);
- return [$controller, $route];
- }
- $module = $this->getModule($id);
- if ($module !== null) {
- return $module->createController($route);
- }
-
- if (($pos = strrpos($route, '/')) !== false) {
- $id .= '/' . substr($route, 0, $pos);
- $route = substr($route, $pos + 1);
- }
-
- $controller = $this->createControllerByID($id);
- if ($controller === null && $route !== '') {
- $controller = $this->createControllerByID($id . '/' . $route);
- $route = '';
- }
-
- return $controller === null ? false : [$controller, $route];
- }
-
-
-
- public function createControllerByID($id)
- {
- $pos = strrpos($id, '/');
- if ($pos === false) {
- $prefix = '';
- $className = $id;
- } else {
- $prefix = substr($id, 0, $pos + 1);
- $className = substr($id, $pos + 1);
- }
-
- if (!preg_match('%^[a-z][a-z0-9\\-_]*$%', $className)) {
- return null;
- }
- if ($prefix !== '' && !preg_match('%^[a-z0-9_/]+$%i', $prefix)) {
- return null;
- }
-
- $className = str_replace(' ', '', ucwords(str_replace('-', ' ', $className))) . 'Controller';
- $className = ltrim($this->controllerNamespace . '\\' . str_replace('/', '\\', $prefix) . $className, '\\');
- if (strpos($className, '-') !== false || !class_exists($className)) {
- return null;
- }
-
- if (is_subclass_of($className, 'yii\base\Controller')) {
- $controller = Yii::createObject($className, [$id, $this]);
- return get_class($controller) === $className ? $controller : null;
- } elseif (YII_DEBUG) {
- throw new InvalidConfigException("Controller class must extend from \\yii\\base\\Controller.");
- } else {
- 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;
- }
- }
|