123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462 |
- <?php
- /**
- *
- * Cube Framework $Id$ QKN6Rvf+edw/vrqXp1EGojRwRQibhhzdK1RxTFUrfAY=
- *
- * @link http://codecu.be/framework
- * @copyright Copyright (c) 2017 CodeCube SRL
- * @license http://codecu.be/framework/license Commercial License
- *
- * @version 1.10 [rev.1.10.01]
- */
- /**
- * modules manager class
- */
- namespace Cube;
- use Cube\Controller\Request,
- Cube\Loader\Autoloader;
- class ModuleManager
- {
- /**
- * uri delimiter
- */
- const URI_DELIMITER = '/';
- /**
- * name and location of a module config file
- */
- const MODULE_CONFIG = 'config/*.config.php';
- /**
- * location of a module's classes
- */
- const MODULE_FILES = 'src';
- /**
- *
- * location of the application's modules
- */
- const MODULES_PATH = 'module';
- /**
- *
- * holds an instance of the object
- *
- * @var \Cube\ModuleManager
- */
- private static $_instance;
- /**
- *
- * an array that holds the names of the modules defined in the application
- *
- * @var array
- */
- protected $_modules = array();
- /**
- *
- * get the paths of the application modules
- *
- * @var array
- */
- protected $_paths = array();
- /**
- *
- * holds the route objects that are generated from the modules configs
- * the first active route in the stack will give the active module
- *
- * @var array
- */
- protected $_routes = array();
- /**
- *
- * the request uri, used for matching route objects and retrieving the active module
- *
- * @var string
- */
- protected $_requestUri;
- /**
- *
- * get active module from routing the request
- *
- * @var string
- */
- protected $_activeModule;
- /**
- *
- * class to be use for routing
- * must implement \Cube\Controller\Router\Route\RouteInterface
- *
- * @var string
- */
- protected $_routeClass = '\Cube\Controller\Router\Route\Rewrite';
- /**
- *
- * class constructor
- */
- protected function __construct()
- {
- $this->addPath('', self::MODULES_PATH);
- }
- /**
- *
- * returns an instance of the object and creates it if it wasnt instantiated yet
- *
- * @return \Cube\ModuleManager
- */
- public static function getInstance()
- {
- if (!self::$_instance instanceof self) {
- self::$_instance = new self();
- }
- return self::$_instance;
- }
- /**
- *
- * reset all module properties
- *
- * @return $this
- */
- public function resetProperties()
- {
- $this->_modules = array();
- $this->_paths = array();
- $this->_routes = array();
- $this->_requestUri = '';
- $this->_activeModule = '';
- return $this;
- }
- /**
- *
- * get modules paths
- *
- * @return array
- */
- public function getPaths()
- {
- return $this->_paths;
- }
- /**
- *
- * add single module path
- *
- * @param string $key
- * @param string $path
- *
- * @return $this
- */
- public function addPath($key, $path)
- {
- if (!in_array($path, $this->_paths)) {
- $this->_paths[$key] = $path;
- }
- return $this;
- }
- /**
- *
- * get the names of the registered modules
- *
- * @return array
- * @throws \DomainException
- */
- public function getModules()
- {
- if (empty($this->_modules)) {
- throw new \DomainException("No modules have been defined for the application.");
- }
- return $this->_modules;
- }
- /**
- *
- * add multiple modules
- *
- * @param array $modules
- *
- * @return $this
- */
- public function setModules($modules = array())
- {
- if (!empty($modules)) {
- foreach ($modules as $module) {
- $this->setModule($module);
- }
- }
- return $this;
- }
- /**
- *
- * load module config files
- * @1.9: all files that are placed in the config folder and end in .config.php will be included
- *
- * @param string $module the name of the module
- *
- * @return array returns the module config array or an empty array if the file doesnt exist
- * @throws \OutOfRangeException
- */
- public function getConfig($module)
- {
- $result = array();
- if (!in_array($module, $this->_modules)) {
- throw new \OutOfRangeException(
- sprintf("Cannot load the config file for module '%s' because the module was not initialized.", $module));
- }
- $files = array_merge(
- glob(__DIR__
- . '/../../'
- . self::MODULES_PATH . DIRECTORY_SEPARATOR
- . $module . DIRECTORY_SEPARATOR
- . self::MODULE_CONFIG),
- glob(__DIR__
- . '/../../'
- . Autoloader::getInstance()->getModsPath() . DIRECTORY_SEPARATOR
- . self::MODULES_PATH . DIRECTORY_SEPARATOR
- . $module . DIRECTORY_SEPARATOR
- . self::MODULE_CONFIG));
- foreach ($files as $file) {
- if (file_exists($file)) {
- $data = (array)include $file;
- $result = array_replace_recursive(
- array_merge_recursive($result, $data), $data);
- }
- }
- return $result;
- }
- /**
- *
- * add a single module to the array, and save its config array as well
- *
- * @param string $module
- *
- * @return $this
- */
- public function setModule($module)
- {
- if (!in_array($module, $this->_modules) && !empty($module)) {
- array_push($this->_modules, $module);
- $config = $this->getConfig($module);
- if (isset($config['routes'])) {
- $this->setRoutes($config['routes'], $module);
- }
- $this->addPath(
- $module,
- self::MODULES_PATH . DIRECTORY_SEPARATOR
- . $module . DIRECTORY_SEPARATOR
- . self::MODULE_FILES);
- }
- return $this;
- }
- /**
- *
- * get the active module based the request uri and the route objects defined
- *
- * @return string the name of the active module
- */
- public function getActiveModule()
- {
- if (!isset($this->_activeModule)) {
- foreach ($this->_routes as $route) {
- /** @var \Cube\Controller\Router\Route\AbstractRoute $route */
- if ($route->match($this->getRequestUri()) !== false) {
- $this->_activeModule = $route->getModule();
- return $this->_activeModule;
- }
- }
- $split = explode(self::URI_DELIMITER, preg_replace('/^\//', '', $this->getRequestUri()));
- $moduleName = ucfirst($split[0]);
- $modules = $this->getModules();
- if (in_array($moduleName, $modules)) {
- $this->_activeModule = $moduleName;
- }
- else {
- // we init a standard request object and get the module variable if available
- $request = new Request\Standard();
- if ($module = $request->getParam('module')) {
- $this->_activeModule = $request->normalize($module);
- }
- else {
- $this->_activeModule = $modules[0];
- }
- }
- }
- return $this->_activeModule;
- }
- /**
- *
- * override the active module parameter
- *
- * @param string $module
- *
- * @return $this
- */
- public function setActiveModule($module)
- {
- if (in_array($module, $this->_modules)) {
- $this->_activeModule = $module;
- }
- return $this;
- }
- /**
- *
- * clear active module variable
- *
- * @return $this
- */
- public function clearActiveModule()
- {
- $this->_activeModule = null;
- return $this;
- }
- /**
- *
- * set route class
- *
- * @param string $routeClass
- *
- * @return $this
- */
- public function setRouteClass($routeClass)
- {
- if (class_exists($routeClass)) {
- $this->_routeClass = $routeClass;
- }
- return $this;
- }
- /**
- *
- * get route class
- *
- * @return string
- */
- public function getRouteClass()
- {
- return $this->_routeClass;
- }
- /**
- *
- * return the routes array
- *
- * @return array
- */
- public function getRoutes()
- {
- return $this->_routes;
- }
- /**
- *
- * create route objects from the arrays defined in the module config files,
- * and match routes to the request uri as well
- * we get the request uri by creating a new request object
- *
- * @param array $routes
- * @param string $module the name of the module the routes belong to
- *
- * @return $this
- */
- public function setRoutes(array $routes, $module)
- {
- foreach ($routes as $name => $options) {
- $path = (isset($options[0])) ? $options[0] : null;
- $defaults = (isset($options[1])) ? (array)$options[1] : array();
- $conditions = (isset($options[2])) ? (array)$options[2] : array();
- /** @var \Cube\Controller\Router\Route\AbstractRoute $route */
- $route = new $this->_routeClass($path, $defaults, $conditions);
- $route->setName($name)
- ->setModule($module);
- array_push($this->_routes, $route);
- }
- return $this;
- }
- /**
- *
- * get the formatted request uri, set if its not already set
- *
- * @return string
- */
- public function getRequestUri()
- {
- if (empty($this->_requestUri)) {
- $this->setRequestUri();
- }
- return $this->_requestUri;
- }
- /**
- *
- * set the request uri and format it so that it can be used for matching route objects
- * (remove the base url of the application from the uri and remove any GET variables as well)
- *
- * @param string $requestUri
- *
- * @return $this
- */
- public function setRequestUri($requestUri = null)
- {
- if ($requestUri === null) {
- $request = new Request();
- $requestUri = $request->getRequestUri();
- }
- $this->_requestUri = $requestUri;
- return $this;
- }
- }
|