8000 [Routing] Ignore hidden directories when loading routes from annotations by jakzal · Pull Request #21832 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Routing] Ignore hidden directories when loading routes from annotations #21832

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
[Routing] Ignore hidden directories when loading routes from annotations
  • Loading branch information
jakzal committed Mar 2, 2017
commit ce9df0237c57f45316f417869d85697128f8b4ae
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,15 @@ public function load($path, $type = null)

$collection = new RouteCollection();
$collection->addResource(new DirectoryResource($dir, '/\.php$/'));
$files = iterator_to_array(new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($dir), \RecursiveIteratorIterator::LEAVES_ONLY));
$files = iterator_to_array(new \RecursiveIteratorIterator(
new RecursiveCallbackFilterIterator(
new \RecursiveDirectoryIterator($dir),
function (\SplFileInfo $current) {
return '.' !== substr($current->getBasename(), 0, 1);
}
),
\RecursiveIteratorIterator::LEAVES_ONLY
));
usort($files, function (\SplFileInfo $a, \SplFileInfo $b) {
return (string) $a > (string) $b ? 1 : -1;
});
Expand Down Expand Up @@ -79,3 +87,34 @@ public func 8000 tion supports($resource, $type = null)
return is_dir($path) && (!$type || 'annotation' === $type);
}
}

/**
* @internal To be removed as RecursiveCallbackFilterIterator is available since PHP 5.4
*/
class RecursiveCallbackFilterIterator extends \FilterIterator implements \RecursiveIterator
{
private $iterator;
private $callback;

public function __construct(\RecursiveIterator $iterator, $callback)
{
$this->iterator = $iterator;
$this->callback = $callback;
parent::__construct($iterator);
}

public function accept()
{
return call_user_func($this->callback, $this->current(), $this->key(), $this->iterator);
}

public function hasChildren()
{
return $this->iterator->hasChildren();
}

public function getChildren()
{
return new static($this->iterator->getChildren(), $this->callback);
}
}
8000
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,22 @@ public function testLoad()
$this->loader->load(__DIR__.'/../Fixtures/AnnotatedClasses');
}

public function testLoadIgnoresHiddenDirectories()
{
$this->expectAnnotationsToBeReadFrom(array(
'Symfony\Component\Routing\Tests\Fixtures\AnnotatedClasses\BarClass',
'Symfony\Component\Routing\Tests\Fixtures\AnnotatedClasses\FooClass',
));

$this->reader
->expects($this->any())
->method('getMethodAnnotations')
->will($this->returnValue(array()))
;

$this->loader->load(__DIR__.'/../Fixtures/AnnotatedClasses');
}

public function testSupports()
{
$fixturesDir = __DIR__.'/../Fixtures';
Expand All @@ -50,4 +66,13 @@ public function testSupports()
$this->assertTrue($this->loader->supports($fixturesDir, 'annotation'), '->supports() checks the resource type if specified');
$this->assertFalse($this->loader->supports($fixturesDir, 'foo'), '->supports() checks the resource type if specified');
}

private function expectAnnotationsToBeReadFrom(array $classes)
{
$this->reader->expects($this->exactly(count($classes)))
->method('getClassAnnotation')
->with($this->callback(function (\ReflectionClass $class) use ($classes) {
return in_array($class->getName(), $classes);
}));
}
}
0