/** * Reads routes for the given module and registers them automatically. * * @param AbstractModule $module Module object. */ public function readModuleRoutes(AbstractModule $module) { $moduleName = $module->getName(); $routesDir = rtrim($module->getModuleDir(), '/') . '/Controllers'; if (!is_dir($routesDir)) { return; } $moduleNamespace = $module->getNamespace() . NS . 'Controllers' . NS; $routesDirLength = strlen($routesDir . '/'); $files = FilesystemUtils::glob($routesDir . '/{,**/}*.php', GLOB_BRACE); foreach ($files as $file) { // remove full path to the routes dir, so we're left only with relative path $file = substr($file, $routesDirLength); // also remove extension $file = substr($file, 0, -4); // build full class name $subNamespace = str_replace('/', NS, $file); $class = $moduleNamespace . $subNamespace; // class_exists autoloads the file if (!class_exists($class)) { continue; } // check if this class can be instantiated, if not, skip it $reflection = new ReflectionClass($class); if (!$reflection->isInstantiable()) { continue; } $this->addRoute($moduleName . ':' . $subNamespace, $class, $moduleName, $module->getUrlPrefix() . $class::_getUrl()); } }