Skip to content

Commit 47e2171

Browse files
committed
Merge pull request #2 from mbed67/examples
corrections in specifications, added examples and integration tests
2 parents f7c0c22 + abe889a commit 47e2171

File tree

13 files changed

+285
-9
lines changed

13 files changed

+285
-9
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
"minimum-stability": "stable",
2222
"require-dev": {
2323
"phpunit/phpunit": "^4.0",
24-
"mockery/mockery": "~0.9@dev"
24+
"mockery/mockery": "~0.9@dev",
25+
"league/flysystem-memory": "^1.0"
2526
}
2627
}

composer.lock

Lines changed: 50 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/01-find-hidden-files.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
require_once(__DIR__ . '/../vendor/autoload.php');
3+
4+
use League\Flysystem\Filesystem;
5+
use League\Flysystem\Memory\MemoryAdapter as Adapter;
6+
use Flyfinder\Finder;
7+
use Flyfinder\Specification\IsHidden;
8+
9+
/*
10+
* First create a new Filesystem and add the FlySystem plugin
11+
* In this example we are using a filesystem with the memory adapter
12+
*/
13+
$filesystem = new Filesystem(new Adapter());
14+
$filesystem->addPlugin(new Finder());
15+
16+
// Create some demo files
17+
$filesystem->write('test.txt', 'test');
18+
$filesystem->write('.hiddendir/.test.txt', 'test');
19+
20+
//In order to tell FlyFinder what to find, you need to give it a specification
21+
//In this example the specification will be satisfied by files and directories that are hidden
22+
$specification = new IsHidden();
23+
24+
//FlyFinder will yield a generator object with the files that are found
25+
$generator = $filesystem->find($specification);
26+
27+
$result = [];
28+
29+
foreach ($generator as $value) {
30+
$result[] = $value;
31+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
require_once(__DIR__ . '/../vendor/autoload.php');
3+
4+
use League\Flysystem\Filesystem;
5+
use League\Flysystem\Memory\MemoryAdapter as Adapter;
6+
use Flyfinder\Finder;
7+
use Flyfinder\Path;
8+
use Flyfinder\Specification\IsHidden;
9+
use Flyfinder\Specification\HasExtension;
10+
use Flyfinder\Specification\InPath;
11+
12+
/*
13+
* First create a new Filesystem and add the FlySystem plugin
14+
* In this example we are using a filesystem with the memory adapter
15+
*/
16+
$filesystem = new Filesystem(new Adapter());
17+
$filesystem->addPlugin(new Finder());
18+
19+
// Create some demo files
20+
$filesystem->write('test.txt', 'test');
21+
$filesystem->write('.hiddendir/.test.txt', 'test');
22+
$filesystem->write('.hiddendir/found.txt', 'test');
23+
$filesystem->write('.hiddendir/normaldir/example.txt', 'test');
24+
25+
/*
26+
* In order to tell FlyFinder what to find, you need to give it a specification
27+
* In this example the specification will be satisfied by *.txt files
28+
* within the .hidden directory and its subdirectories that are not hidden
29+
*/
30+
$isHidden = new IsHidden();
31+
$hasExtension = new HasExtension(['txt']);
32+
$inPath = new InPath(new Path('.hiddendir'));
33+
$specification = $inPath->andSpecification($hasExtension)->andSpecification($isHidden->notSpecification());
34+
35+
//FlyFinder will yield a generator object with the files that are found
36+
$generator = $filesystem->find($specification);
37+
38+
$result = [];
39+
40+
foreach ($generator as $value) {
41+
$result[] = $value;
42+
}

src/Finder.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ public function setFilesystem(FilesystemInterface $filesystem)
4747
}
4848

4949
/**
50+
* Find the specified files
51+
*
5052
* @param SpecificationInterface $specification
5153
* @return Generator
5254
*/
@@ -58,6 +60,8 @@ public function handle(SpecificationInterface $specification)
5860
}
5961

6062
/**
63+
* Recursively yield files that meet the specification
64+
*
6165
* @param SpecificationInterface $specification
6266
* @param string $path
6367
* @return Generator

src/Specification/AndSpecification.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ final class AndSpecification extends CompositeSpecification implements Specifica
2828
private $other;
2929

3030
/**
31+
* Initializes the AndSpecification object
32+
*
3133
* @param CompositeSpecification $one
3234
* @param CompositeSpecification $other
3335
*/

src/Specification/CompositeSpecification.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919
abstract class CompositeSpecification
2020
{
2121
/**
22+
* Returns a specification that satisfies the original specification
23+
* as well as the other specification
24+
*
2225
* @param CompositeSpecification $other
2326
* @return AndSpecification
2427
*/
@@ -28,6 +31,9 @@ public function andSpecification(CompositeSpecification $other)
2831
}
2932

3033
/**
34+
* Returns a specification that satisfies the original specification
35+
* or the other specification
36+
*
3137
* @param CompositeSpecification $other
3238
* @return OrSpecification
3339
*/
@@ -37,6 +43,9 @@ public function orSpecification(CompositeSpecification $other)
3743
}
3844

3945
/**
46+
* Returns a specification that is the inverse of the original specification
47+
* i.e. does not meet the original criteria
48+
*
4049
* @return NotSpecification
4150
*/
4251
public function notSpecification()

src/Specification/InPath.php

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,12 @@ class InPath extends CompositeSpecification implements SpecificationInterface
2626
private $path;
2727

2828
/**
29+
* Initializes the InPath specification
30+
*
2931
* @param Path $path
3032
*/
3133
public function __construct(Path $path)
3234
{
33-
3435
$this->path = $path;
3536
}
3637

@@ -42,6 +43,40 @@ public function __construct(Path $path)
4243
*/
4344
public function isSatisfiedBy(array $value)
4445
{
45-
return isset($value['dirname']) && $value['dirname'] === (string)$this->path ? true : false;
46+
if (isset($value['dirname'])) {
47+
$path = $this->cleanPath((string) $this->path);
48+
49+
$validChars = '[a-zA-Z0-9\\\/\.\<\>\,\|\:\(\)\&\;\#]';
50+
51+
$pattern = '(^(?!\/)'
52+
. str_replace(['?', '*'], [$validChars . '{1}', $validChars . '*'], $path)
53+
. $validChars . '*)';
54+
55+
if (preg_match($pattern, $value['dirname'] . '/')) {
56+
return true;
57+
}
58+
return false;
59+
}
60+
return false;
61+
}
62+
63+
/**
64+
* If a path is given with a leading ./ this will be removed
65+
* If a path doesn't have a trailing /, a slash will be added
66+
*
67+
* @param string $path
68+
* @return string
69+
*/
70+
private function cleanPath($path)
71+
{
72+
if (substr($path, 0, 2) === './') {
73+
$path = substr($path, 1);
74+
}
75+
76+
if (substr($path, -1) !== '/') {
77+
$path = $path . '/';
78+
}
79+
80+
return $path;
4681
}
4782
}

src/Specification/NotSpecification.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ final class NotSpecification extends CompositeSpecification implements Specifica
2323
private $wrapped;
2424

2525
/**
26+
* Initializes the NotSpecification object
27+
*
2628
* @param CompositeSpecification $wrapped
2729
*/
2830
public function __construct(CompositeSpecification $wrapped)

src/Specification/OrSpecification.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ final class OrSpecification extends CompositeSpecification implements Specificat
2828
private $other;
2929

3030
/**
31+
* Initializes the OrSpecification object
32+
*
3133
* @param CompositeSpecification $one
3234
* @param CompositeSpecification $other
3335
*/

0 commit comments

Comments
 (0)