public function addCollection(MethodCollection $collection) { // we need to remove all routes with the same names first because just replacing them // would not place the new route at the end of the merged array foreach ($collection->all() as $name => $route) { unset($this->routes[$name]); $this->routes[$name] = $route; } $this->resources = array_merge($this->resources, $collection->getResources()); }
/** * Loads from annotations from a file. * * @param string $file A PHP file path * @param string|null $type The resource type * * @return MethodCollection A RouteCollection instance * * @throws \InvalidArgumentException When the file does not exist or its routes cannot be parsed */ public function load($file, $type = null) { $path = $this->getLocator()->locate($file); $collection = new MethodCollection(); if ($class = $this->findClass($path)) { $collection->addResource(new FileResource($path)); $collection->addCollection($this->loader->load($class, $type)); } if (PHP_VERSION_ID >= 70000) { // PHP 7 memory manager will not release after token_get_all(), see https://bugs.php.net/70098 gc_mem_caches(); } return $collection; }
/** * {@inheritdoc} */ public function load($file, $type = null) { $path = $this->getLocator()->locate($file); $collection = new MethodCollection(); $collection->addResource(new DirectoryResource($path)); foreach (scandir($path) as $dir) { if ('.' !== $dir[0]) { $this->setCurrentDir($path); $subPath = $path . '/' . $dir; $subType = null; if (is_dir($subPath)) { $subPath .= '/'; $subType = 'directory'; } $subCollection = $this->import($subPath, $subType, false, $path); $collection->addCollection($subCollection); } } return $collection; }
/** * Loads from annotations from a directory. * * @param string $path A directory path * @param string|null $type The resource type * * @return MethodCollection A RouteCollection instance * * @throws \InvalidArgumentException When the directory does not exist or its routes cannot be parsed */ public function load($path, $type = null) { $dir = $this->getLocator()->locate($path); $collection = new MethodCollection(); $collection->addResource(new DirectoryResource($dir, '/\\.php$/')); /** @var \SplFileInfo[] $files */ $files = iterator_to_array(new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($dir), \RecursiveIteratorIterator::LEAVES_ONLY)); usort($files, function (\SplFileInfo $a, \SplFileInfo $b) { return (string) $a > (string) $b ? 1 : -1; }); foreach ($files as $file) { if (!$file->isFile() || '.php' !== substr($file->getFilename(), -4)) { continue; } if ($class = $this->findClass($file)) { $refl = new \ReflectionClass($class); if ($refl->isAbstract()) { continue; } $collection->addCollection($this->getLoader()->load($class, $type)); } } return $collection; }
/** * Parses a route and adds it to the RouteCollection. * * @param MethodCollection $collection A RouteCollection instance * @param string $name Route name * @param array $config Route definition * @param string $path Full path of the YAML file being processed */ protected function parseRoute(MethodCollection $collection, $name, array $config, $path) { $context = array_key_exists('context', $config) ? (array) $config['context'] : []; $method = array_key_exists('method', $config) ? $config['method'] : $name; $inherit = array_key_exists('inherit', $config) ? $config['inherit'] : true; $route = new Route($method, $config['controller'], $context, array_key_exists('default_context', $config) ? $config['default_context'] : true, $inherit); $collection->add($name, $route); }
protected function addRoute(MethodCollection $collection, Method $annot, array $parents, \ReflectionClass $class, \ReflectionMethod $method) { $collection->add($annot->getMethod(), new Route($annot->getMethod(), $class->getName() . '::' . $method->getName(), array_merge($parents['context'], $annot->getContext()), $parents['default_context'] && $annot->isDefaultContext(), $annot->isInherit())); }