cache = $cache; if ($callback !== null) { $this->callback = $callback; } if ($folderList !== null) { $this->folderList = $folderList; } if ($attributes !== null) { $this->attributes = $attributes; } else { $this->attributes = [ 'object' => [ Attribute\Object\Route::class ], 'method' => [ Attribute\Method\Route::class ], ]; } } public function addFolder($folder) : void { $this->folderList[] = $folder; } public function setFolderList(array $list) : void { $this->folderList = $list; } public function scan(? array $folders = null) : Generator { foreach($folders ?: $this->folderList as $namespace => $folder) { if ( ! file_exists($folder) ) { throw new RuntimeException(sprintf("Folder `%s` can not be found or scanned", $folder)); } foreach (new DirectoryIterator($folder) as $fileinfo) { if ( ! $fileinfo->isDot() ) { if ( $fileinfo->isDir() ) { foreach($this->scan([ "{$namespace}\\" . $fileinfo->getBasename() => $fileinfo->getPathname() ]) as $ns2 => $fi2) { yield $ns2 => $fi2; } } else { yield $namespace => $fileinfo; } } } } } public function compile(null|array $attributes = null) : array { $attributes ??= $this->attributes; return $this->handleCaching(substr(md5(serialize($attributes)), 0, 7), function() use ($attributes) : array { $list = []; foreach($this->scan() as $namespace => $file) { if ( $file->getExtension() !== "php" ) { continue; } $base = null; $class = $this->generateClassname($file->getBasename(".php"), $namespace); $methods = null; $objectResolver = new ObjectResolver($class, $this->cache); if ( isset($attributes['object']) ) { $objects = $objectResolver->reflectedClass->getClassAttributeListFromClassname($attributes['object'], true, false); foreach($objects as $object) { if ($object->method ?? false ) { $methods ??= (array) $object->method; } $base ??= $object->base ?? null; } } if ( isset($attributes['method']) ) { $routeList = $objectResolver->reflectedClass->getMethodAttributeListFromClassname( $attributes['method'], false, false ); foreach($routeList as $func => $routes) { if (is_array($routes)) { foreach ($routes as $route) { if ($route instanceof \Notes\Route\Attribute\Method\Route) { $route->base = $route->base ?: $base; } $route->class = $class; $route->classMethod = $func; if (false === ($route->methods ?? false)) { $route->methods = $methods ?? $this->defaultMethods; } if (false !== ($this->callback ?? false)) { call_user_func_array($this->callback, [$route]); } $list[] = $route; } } } } } return $list; }); } protected function generateClassname($file, $namespace) { return "\\$namespace\\$file"; } }