/** * 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 an import and adds the routes in the resource to the RouteCollection. * * @param MethodCollection $collection A RouteCollection instance * @param array $config Route definition * @param string $path Full path of the YAML file being processed * @param string $file Loaded file name */ protected function parseImport(MethodCollection $collection, array $config, $path, $file) { $type = isset($config['type']) ? $config['type'] : null; $prefix = isset($config['prefix']) ? $config['prefix'] : ''; $this->setCurrentDir(dirname($path)); $subCollection = $this->import($config['resource'], $type, false, $file); /* @var $subCollection MethodCollection */ $subCollection->addPrefix($prefix); if (array_key_exists('context', $config)) { foreach ((array) $config['context'] as $context) { $subCollection->addContext($context); } } $collection->addCollection($subCollection); }