3 namespace Robo\ClassDiscovery;
5 use Symfony\Component\Finder\Finder;
6 use Composer\Autoload\ClassLoader;
9 * Class RelativeNamespaceDiscovery
11 * @package Robo\Plugin\ClassDiscovery
13 class RelativeNamespaceDiscovery extends AbstractClassDiscovery
16 * @var \Composer\Autoload\ClassLoader
18 protected $classLoader;
23 protected $relativeNamespace = '';
26 * RelativeNamespaceDiscovery constructor.
28 * @param \Composer\Autoload\ClassLoader $classLoader
30 public function __construct(ClassLoader $classLoader)
32 $this->classLoader = $classLoader;
36 * @param string $relativeNamespace
38 * @return RelativeNamespaceDiscovery
40 public function setRelativeNamespace($relativeNamespace)
42 $this->relativeNamespace = $relativeNamespace;
50 public function getClasses()
53 $relativePath = $this->convertNamespaceToPath($this->relativeNamespace);
55 foreach ($this->classLoader->getPrefixesPsr4() as $baseNamespace => $directories) {
56 $directories = array_filter(array_map(function ($directory) use ($relativePath) {
57 return $directory.$relativePath;
58 }, $directories), 'is_dir');
61 foreach ($this->search($directories, $this->searchPattern) as $file) {
62 $relativePathName = $file->getRelativePathname();
63 $classes[] = $baseNamespace.$this->convertPathToNamespace($relativePath.'/'.$relativePathName);
74 public function getFile($class)
76 return $this->classLoader->findFile($class);
83 * @return \Symfony\Component\Finder\Finder
85 protected function search($directories, $pattern)
87 $finder = new Finder();
100 protected function convertPathToNamespace($path)
102 return str_replace(['/', '.php'], ['\\', ''], trim($path, '/'));
108 public function convertNamespaceToPath($namespace)
110 return '/'.str_replace("\\", '/', trim($namespace, '\\'));