/** {@inheritdoc} */ public function load($class, $type = null) { if (!class_exists($class)) { throw new \InvalidArgumentException(sprintf('Class "%s" does not exist.', $class)); } $class = new \ReflectionClass($class); if ($class->isAbstract()) { throw new \InvalidArgumentException(sprintf('Annotations from class "%s" cannot be read as it is abstract.', $class->getName())); } $parents = $this->getParentAnnotations($class); $collection = new MethodCollection(); $collection->addResource(new FileResource($class->getFileName())); foreach ($class->getMethods() as $method) { if (!$method->isPublic()) { continue; } foreach ($this->reader->getMethodAnnotations($method) as $annot) { if ($annot instanceof Method) { $this->addRoute($collection, $annot, $parents, $class, $method); } } } $collection->addPrefix($parents['method']); return $collection; }
/** * 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 a resource. * * @param mixed $resource The resource * @param string|null $type The resource type or null if unknown * * @return MethodCollection * @throws \Exception If something went wrong */ public function load($resource, $type = null) { $path = $this->getLocator()->locate($resource); if (!stream_is_local($path)) { throw new \InvalidArgumentException(sprintf('This is not a local file "%s".', $path)); } if (!file_exists($path)) { throw new \InvalidArgumentException(sprintf('File "%s" not found.', $path)); } if (null === $this->parser) { $this->parser = new Parser(); } try { $parsedConfig = $this->parser->parse(file_get_contents($path)); } catch (ParseException $e) { throw new \InvalidArgumentException(sprintf('The file "%s" does not contain valid YAML.', $path), 0, $e); } $collection = new MethodCollection(); $collection->addResource(new FileResource($path)); // empty file if (null === $parsedConfig) { return $collection; } // not an array if (!is_array($parsedConfig)) { throw new \InvalidArgumentException(sprintf('The file "%s" must contain a YAML array.', $path)); } foreach ($parsedConfig as $name => $config) { $this->validate($config, $name, $path); if (isset($config['resource'])) { $this->parseImport($collection, $config, $path, $resource); } else { $this->parseRoute($collection, $name, $config, $path); } } 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; }