Skip to content

[Finder] added scanner adapter #8685

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

Closed
wants to merge 7 commits into from
Closed
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
3 changes: 2 additions & 1 deletion src/Symfony/Component/Finder/Finder.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,9 @@ public function __construct()
$this
->addAdapter(new GnuFindAdapter())
->addAdapter(new BsdFindAdapter())
->addAdapter(new Scanner\Adapter())
->addAdapter(new PhpAdapter(), -50)
->setAdapter('php')
->setAdapter('scanner')
;
}

Expand Down
89 changes: 89 additions & 0 deletions src/Symfony/Component/Finder/Scanner/Adapter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Finder\Scanner;

use Symfony\Component\Finder\Adapter\AbstractAdapter;
use Symfony\Component\Finder\Iterator;

/**
* PHP finder engine implementation.
*
* @author Jean-François Simon <contact@jfsimon.fr>
*/
class Adapter extends AbstractAdapter
{
/**
* {@inheritdoc}
*/
public function searchInDirectory($dir)
{
$builder = new Builder($this->mode, $this->minDepth, $this->maxDepth, $this->exclude);

foreach ($this->notPaths as $value) {
$builder->notPath(new Expression($value));
}

foreach ($this->notNames as $value) {
$builder->notName(new Expression($value));
}

foreach ($this->paths as $value) {
$builder->path(new Expression($value));
}

foreach ($this->names as $value) {
$builder->name(new Expression($value));
}

$scanner = new Scanner($dir, $builder->build(), $this->ignoreUnreadableDirs, $this->followLinks);
$iterator = $scanner->getIterator();

if ($this->sizes) {
$iterator = new Iterator\SizeRangeFilterIterator($iterator, $this->sizes);
}

if ($this->dates) {
$iterator = new Iterator\DateRangeFilterIterator($iterator, $this->dates);
}

if ($this->filters) {
$iterator = new Iterator\CustomFilterIterator($iterator, $this->filters);
}

if ($this->contains || $this->notContains) {
$iterator = new Iterator\FilecontentFilterIterator($iterator, $this->contains, $this->notContains);
}

if ($this->sort) {
$iteratorAggregate = new Iterator\SortableIterator($iterator, $this->sort);
$iterator = $iteratorAggregate->getIterator();
}

return $iterator;
}

/**
* {@inheritdoc}
*/
public function getName()
{
return 'scanner';
}

/**
* {@inheritdoc}
*/
protected function canBeUsed()
{
return true;
}
}
149 changes: 149 additions & 0 deletions src/Symfony/Component/Finder/Scanner/Builder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Finder\Scanner;

/**
* Scanner constraints builder.
*
* @author Jean-François Simon <contact@jfsimon.fr>
*/
class Builder
{
private $type;
private $minDepth;
private $maxDepth;
private $excludedNames;

private $filenameConstraints = array();
private $pathnameConstraints = array();

/**
* Constructor.
*
* @param int $type
* @param int $minDepth
* @param int $maxDepth
* @param array $excludedNames
*/
public function __construct($type = Constraints::TYPE_ALL, $minDepth = 0, $maxDepth = PHP_INT_MAX, array $excludedNames = array())
{
$this->type = $type;
$this->minDepth = $minDepth;
$this->maxDepth = $maxDepth;
$this->excludedNames = array_merge(array('.', '..'), $excludedNames);
}

/**
* Adds name exclusion constraint.
*
* @param Expression $expression
*
* @return Builder
*/
public function notName(Expression $expression)
{
$key = $expression->isRegex() ? 'excluded_patterns' : 'excluded_filenames';

if (!isset($this->filenameConstraints[$key])) {
$this->filenameConstraints[$key] = array();
}

$this->filenameConstraints[$key][] = $expression->getValue();

return $this;
}

/**
* Adds path exclusion constraint.
*
* @param Expression $expression
*
* @return Builder
*/
public function notPath(Expression $expression)
{
$regex = $expression->getRegex();
$key = $regex->isEnding() ? 'excluded_ending_patterns' : 'excluded_patterns';

if (!isset($this->pathnameConstraints[$key])) {
$this->pathnameConstraints[$key] = array();
}

$this->pathnameConstraints[$key][] = $regex;

return $this;
}

/**
* Adds name matching constraint.
*
* @param Expression $expression
*
* @return Builder
*/
public function name(Expression $expression)
{
$key = $expression->isRegex() ? 'included_patterns' : 'included_filenames';

if (!isset($this->filenameConstraints[$key])) {
$this->filenameConstraints[$key] = array();
}

$this->filenameConstraints[$key][] = $expression->getValue();

return $this;
}

/**
* Adds path matching constraint.
*
* @param Expression $expression
*
* @return Builder
*/
public function path(Expression $expression)
{
$regex = $expression->getRegex();
$key = $regex->isEnding() ? 'included_ending_patterns' : 'included_patterns';

if (!isset($this->pathnameConstraints[$key])) {
$this->pathnameConstraints[$key] = array();
}

$this->pathnameConstraints[$key][] = $regex;

return $this;
}

/**
* Builds constraints.
*
* @return Constraints
*/
public function build()
{
foreach ($this->pathnameConstraints as $key => $regexs) {
$this->pathnameConstraints[$key] = array_map(function (Regex $regex) { return (string) $regex; }, Regex::mergeWithOr($regexs));
}

foreach ($this->filenameConstraints as $key => $regexs) {
if (false !== strpos($key, 'patterns')) {
$this->filenameConstraints[$key] = array_map(function (Regex $regex) { return (string) $regex; }, Regex::mergeWithOr($regexs));
}
}

return new Constraints(
$this->type, $this->minDepth, $this->maxDepth,
$this->excludedNames, $this->pathnameConstraints, $this->filenameConstraints
);
}
}
Loading