Ejemplo n.º 1
0
 /**
  * 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());
     }
 }
Ejemplo n.º 2
0
 /**
  * Searches the given module for any commands that could be automatically registered.
  * 
  * @param AbstractModule $module
  */
 protected function readModuleCommands(AbstractModule $module)
 {
     $name = $module->getName();
     $commandsDir = $module->getModuleDir() . '/Commands';
     if (!is_dir($commandsDir)) {
         return;
     }
     $moduleNamespace = $module->getNamespace() . NS . 'Commands' . NS;
     $console = $this;
     // register a closure so we can recursively scan the routes directory
     $scan = function ($dir, $namespace, $self) use($name, $moduleNamespace, $module, $console) {
         $namespace = $namespace ? trim($namespace, NS) . NS : '';
         $files = scandir($dir);
         foreach ($files as $file) {
             // ignore . and ..
             if (in_array($file, array('.', '..'))) {
                 continue;
             }
             // if directory then go recursively
             if (is_dir($dir . DS . $file)) {
                 $self($dir . DS . $file, $namespace . $file, $self);
                 continue;
             }
             $file = explode('.', $file);
             $rawClass = $file[0];
             $class = $moduleNamespace . $namespace . $rawClass;
             // class_exists autoloads a file
             if (class_exists($class)) {
                 $name = $class::getName();
                 $name = trim($name);
                 if (empty($name)) {
                     throw new InvalidCommandException('Command "' . $class . '" has to have a name. Please set the static property $name.');
                 }
                 $name = ($module->getCommandNamespace() ? $module->getCommandNamespace() . ':' : '') . $name;
                 $console->addCommand($name, $class);
             }
         }
     };
     // scan the module
     $scan($commandsDir, '', $scan);
 }