Skip to content

Commit

Permalink
Merge pull request #2 from mbed67/examples
Browse files Browse the repository at this point in the history
corrections in specifications, added examples and integration tests
  • Loading branch information
mvriel committed Aug 18, 2015
2 parents f7c0c22 + abe889a commit 47e2171
Show file tree
Hide file tree
Showing 13 changed files with 285 additions and 9 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"minimum-stability": "stable",
"require-dev": {
"phpunit/phpunit": "^4.0",
"mockery/mockery": "~0.9@dev"
"mockery/mockery": "~0.9@dev",
"league/flysystem-memory": "^1.0"
}
}
51 changes: 50 additions & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 31 additions & 0 deletions examples/01-find-hidden-files.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php
require_once(__DIR__ . '/../vendor/autoload.php');

use League\Flysystem\Filesystem;
use League\Flysystem\Memory\MemoryAdapter as Adapter;
use Flyfinder\Finder;
use Flyfinder\Specification\IsHidden;

/*
* First create a new Filesystem and add the FlySystem plugin
* In this example we are using a filesystem with the memory adapter
*/
$filesystem = new Filesystem(new Adapter());
$filesystem->addPlugin(new Finder());

// Create some demo files
$filesystem->write('test.txt', 'test');
$filesystem->write('.hiddendir/.test.txt', 'test');

//In order to tell FlyFinder what to find, you need to give it a specification
//In this example the specification will be satisfied by files and directories that are hidden
$specification = new IsHidden();

//FlyFinder will yield a generator object with the files that are found
$generator = $filesystem->find($specification);

$result = [];

foreach ($generator as $value) {
$result[] = $value;
}
42 changes: 42 additions & 0 deletions examples/02-find-on-multiple-criteria.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php
require_once(__DIR__ . '/../vendor/autoload.php');

use League\Flysystem\Filesystem;
use League\Flysystem\Memory\MemoryAdapter as Adapter;
use Flyfinder\Finder;
use Flyfinder\Path;
use Flyfinder\Specification\IsHidden;
use Flyfinder\Specification\HasExtension;
use Flyfinder\Specification\InPath;

/*
* First create a new Filesystem and add the FlySystem plugin
* In this example we are using a filesystem with the memory adapter
*/
$filesystem = new Filesystem(new Adapter());
$filesystem->addPlugin(new Finder());

// Create some demo files
$filesystem->write('test.txt', 'test');
$filesystem->write('.hiddendir/.test.txt', 'test');
$filesystem->write('.hiddendir/found.txt', 'test');
$filesystem->write('.hiddendir/normaldir/example.txt', 'test');

/*
* In order to tell FlyFinder what to find, you need to give it a specification
* In this example the specification will be satisfied by *.txt files
* within the .hidden directory and its subdirectories that are not hidden
*/
$isHidden = new IsHidden();
$hasExtension = new HasExtension(['txt']);
$inPath = new InPath(new Path('.hiddendir'));
$specification = $inPath->andSpecification($hasExtension)->andSpecification($isHidden->notSpecification());

//FlyFinder will yield a generator object with the files that are found
$generator = $filesystem->find($specification);

$result = [];

foreach ($generator as $value) {
$result[] = $value;
}
4 changes: 4 additions & 0 deletions src/Finder.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ public function setFilesystem(FilesystemInterface $filesystem)
}

/**
* Find the specified files
*
* @param SpecificationInterface $specification
* @return Generator
*/
Expand All @@ -58,6 +60,8 @@ public function handle(SpecificationInterface $specification)
}

/**
* Recursively yield files that meet the specification
*
* @param SpecificationInterface $specification
* @param string $path
* @return Generator
Expand Down
2 changes: 2 additions & 0 deletions src/Specification/AndSpecification.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ final class AndSpecification extends CompositeSpecification implements Specifica
private $other;

/**
* Initializes the AndSpecification object
*
* @param CompositeSpecification $one
* @param CompositeSpecification $other
*/
Expand Down
9 changes: 9 additions & 0 deletions src/Specification/CompositeSpecification.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
abstract class CompositeSpecification
{
/**
* Returns a specification that satisfies the original specification
* as well as the other specification
*
* @param CompositeSpecification $other
* @return AndSpecification
*/
Expand All @@ -28,6 +31,9 @@ public function andSpecification(CompositeSpecification $other)
}

/**
* Returns a specification that satisfies the original specification
* or the other specification
*
* @param CompositeSpecification $other
* @return OrSpecification
*/
Expand All @@ -37,6 +43,9 @@ public function orSpecification(CompositeSpecification $other)
}

/**
* Returns a specification that is the inverse of the original specification
* i.e. does not meet the original criteria
*
* @return NotSpecification
*/
public function notSpecification()
Expand Down
39 changes: 37 additions & 2 deletions src/Specification/InPath.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,12 @@ class InPath extends CompositeSpecification implements SpecificationInterface
private $path;

/**
* Initializes the InPath specification
*
* @param Path $path
*/
public function __construct(Path $path)
{

$this->path = $path;
}

Expand All @@ -42,6 +43,40 @@ public function __construct(Path $path)
*/
public function isSatisfiedBy(array $value)
{
return isset($value['dirname']) && $value['dirname'] === (string)$this->path ? true : false;
if (isset($value['dirname'])) {
$path = $this->cleanPath((string) $this->path);

$validChars = '[a-zA-Z0-9\\\/\.\<\>\,\|\:\(\)\&\;\#]';

$pattern = '(^(?!\/)'
. str_replace(['?', '*'], [$validChars . '{1}', $validChars . '*'], $path)
. $validChars . '*)';

if (preg_match($pattern, $value['dirname'] . '/')) {
return true;
}
return false;
}
return false;
}

/**
* If a path is given with a leading ./ this will be removed
* If a path doesn't have a trailing /, a slash will be added
*
* @param string $path
* @return string
*/
private function cleanPath($path)
{
if (substr($path, 0, 2) === './') {
$path = substr($path, 1);
}

if (substr($path, -1) !== '/') {
$path = $path . '/';
}

return $path;
}
}
2 changes: 2 additions & 0 deletions src/Specification/NotSpecification.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ final class NotSpecification extends CompositeSpecification implements Specifica
private $wrapped;

/**
* Initializes the NotSpecification object
*
* @param CompositeSpecification $wrapped
*/
public function __construct(CompositeSpecification $wrapped)
Expand Down
2 changes: 2 additions & 0 deletions src/Specification/OrSpecification.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ final class OrSpecification extends CompositeSpecification implements Specificat
private $other;

/**
* Initializes the OrSpecification object
*
* @param CompositeSpecification $one
* @param CompositeSpecification $other
*/
Expand Down
31 changes: 31 additions & 0 deletions tests/integration/FindHiddenFilesTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php
/**
* This file is part of phpDocumentor.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @copyright 2010-2015 Mike van Riel<mike@phpdoc.org>
* @license http://www.opensource.org/licenses/mit-license.php MIT
* @link http://phpdoc.org
*/

namespace Flyfinder;

/**
* Integration test against examples/01-find-hidden-files.php
* @coversNothing
*/
class FindHiddenFilesTest extends \PHPUnit_Framework_TestCase
{
/**
* @var string[] $result
*/
public function testFindingHiddenFiles()
{
include(__DIR__ . '/../../examples/01-find-hidden-files.php');

$this->assertEquals(2, count($result));
$this->assertEquals(".test.txt", $result[1]['basename']);
}
}
32 changes: 32 additions & 0 deletions tests/integration/FindOnMultipleCriteriaTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php
/**
* This file is part of phpDocumentor.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @copyright 2010-2015 Mike van Riel<mike@phpdoc.org>
* @license http://www.opensource.org/licenses/mit-license.php MIT
* @link http://phpdoc.org
*/

namespace Flyfinder;

/**
* Integration test against examples/02-find-on-multiple-criteria.php
* @coversNothing
*/
class FindOnMultipleCriteriaTest extends \PHPUnit_Framework_TestCase
{
/**
* @var string[] $result
*/
public function testFindingFilesOnMultipleCriteria()
{
include(__DIR__ . '/../../examples/02-find-on-multiple-criteria.php');

$this->assertEquals(2, count($result));
$this->assertEquals("found.txt", $result[0]['basename']);
$this->assertEquals("example.txt", $result[1]['basename']);
}
}
Loading

0 comments on commit 47e2171

Please sign in to comment.