Example #1
0
 /**
  * Use auto route
  *
  * @param callable|bool $closure
  * @param string        $index
  * @return Router|mixed
  */
 public function autoRoute($closure, $index = 'Index')
 {
     if ($closure instanceof \Closure) {
         return $this->router->automatic = $closure;
     } elseif ($closure === true || is_string($closure)) {
         $_cli = $this->injectors['cli'];
         // Set route use default automatic
         return $this->router->inject('automatic', function ($path) use($closure, $_cli, $index) {
             $parts = array();
             // Split rule
             if (!$_cli && $path !== '/') {
                 $_paths = explode('/', ltrim($path, '/'));
             } else {
                 if ($_cli && $path !== '') {
                     $_ = explode(' ', $path);
                     $_paths = explode(':', array_shift($_));
                 }
             }
             // Process parts
             if (isset($_paths)) {
                 foreach ($_paths as $_path) {
                     $parts[] = ucfirst(strtolower($_path));
                 }
             }
             // Set default namespace
             if ($closure !== true) {
                 array_unshift($parts, $closure);
             }
             // Index
             if (!$parts) {
                 return $index;
             }
             // Generate class name
             $class = join('\\', $parts);
             // Try to lookup class and with it's index
             return array($class, $class . '\\' . $index);
         });
     }
     return false;
 }