|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
/** |
|
5
|
|
|
* This file is part of phpDocumentor. |
|
6
|
|
|
* |
|
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
8
|
|
|
* file that was distributed with this source code. |
|
9
|
|
|
* |
|
10
|
|
|
* @copyright 2010-2018 Mike van Riel<[email protected]> |
|
11
|
|
|
* @license http://www.opensource.org/licenses/mit-license.php MIT |
|
12
|
|
|
* @link http://phpdoc.org |
|
13
|
|
|
*/ |
|
14
|
|
|
|
|
15
|
|
|
namespace Flyfinder; |
|
16
|
|
|
|
|
17
|
|
|
use Flyfinder\Specification\SpecificationInterface; |
|
18
|
|
|
use Generator; |
|
19
|
|
|
use League\Flysystem\FilesystemInterface; |
|
20
|
|
|
use League\Flysystem\PluginInterface; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Flysystem plugin to add file finding capabilities to the filesystem entity. |
|
24
|
|
|
* |
|
25
|
|
|
* Note that found *directories* are **not** returned... only found *files*. |
|
26
|
|
|
*/ |
|
27
|
|
|
class Finder implements PluginInterface |
|
28
|
|
|
{ |
|
29
|
|
|
const ALGORITHM_LEGACY = 0; |
|
30
|
|
|
const ALGORITHM_OPTIMIZED = 1; |
|
31
|
|
|
|
|
32
|
|
|
/** @var FilesystemInterface */ |
|
33
|
|
|
private $filesystem; |
|
34
|
|
|
|
|
35
|
1 |
|
/** @var int */ |
|
36
|
|
|
private $algorithm = self::ALGORITHM_LEGACY; |
|
37
|
1 |
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Get the method name. |
|
40
|
|
|
*/ |
|
41
|
|
|
public function getMethod(): string |
|
42
|
|
|
{ |
|
43
|
1 |
|
return 'find'; |
|
44
|
|
|
} |
|
45
|
1 |
|
|
|
46
|
1 |
|
/** |
|
47
|
|
|
* Set the Filesystem object. |
|
48
|
|
|
*/ |
|
49
|
|
|
public function setFilesystem(FilesystemInterface $filesystem): void |
|
50
|
|
|
{ |
|
51
|
|
|
$this->filesystem = $filesystem; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
1 |
|
/** |
|
55
|
|
|
* Find the specified files |
|
56
|
1 |
|
* |
|
57
|
1 |
|
* Note that only found *files* are yielded at this level, |
|
58
|
1 |
|
* which go back to the caller. |
|
59
|
|
|
*/ |
|
60
|
|
|
public function handle(SpecificationInterface $specification): Generator |
|
61
|
1 |
|
{ |
|
62
|
|
|
foreach ($this->yieldFilesInPath($specification, '') as $path) { |
|
63
|
|
|
if (isset($path['type']) && $path['type'] === 'file') { |
|
64
|
|
|
yield $path; |
|
65
|
|
|
} |
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Recursively yield files that meet the specification |
|
71
|
1 |
|
* |
|
72
|
|
|
* Note that directories are also yielded at this level, |
|
73
|
1 |
|
* since they have to be recursed into. Yielded directories |
|
74
|
1 |
|
* will not make their way back to the caller, as they are filtered out |
|
75
|
1 |
|
* by {@link handle()}. |
|
76
|
1 |
|
* @param SpecificationInterface $specification |
|
77
|
|
|
* @param string $path |
|
78
|
|
|
* @return Generator |
|
79
|
1 |
|
*/ |
|
80
|
1 |
|
private function yieldFilesInPath(SpecificationInterface $specification, string $path): Generator |
|
81
|
1 |
|
{ |
|
82
|
|
|
$listContents = $this->filesystem->listContents($path); |
|
83
|
|
|
foreach ($listContents as $location) { |
|
84
|
|
|
if ($specification->isSatisfiedBy($location)) { |
|
85
|
1 |
|
yield $location; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
if ($location['type'] === 'dir') { |
|
89
|
|
|
if ( |
|
90
|
|
|
self::ALGORITHM_OPTIMIZED === $this->algorithm |
|
91
|
|
|
&& !$specification->canBeSatisfiedByAnythingBelow($location) |
|
92
|
|
|
) { |
|
93
|
|
|
continue; |
|
94
|
|
|
} |
|
95
|
|
|
foreach ($this->yieldFilesInPath($specification, $location['path']) as $returnedLocation) { |
|
96
|
|
|
yield $returnedLocation; |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* @return int |
|
104
|
|
|
*/ |
|
105
|
|
|
public function getAlgorithm(): int |
|
106
|
|
|
{ |
|
107
|
|
|
return $this->algorithm; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* @param int $algorithm |
|
112
|
|
|
*/ |
|
113
|
|
|
public function setAlgorithm(int $algorithm): void |
|
114
|
|
|
{ |
|
115
|
|
|
$this->algorithm = self::ALGORITHM_OPTIMIZED === $algorithm ? $algorithm : self::ALGORITHM_LEGACY; |
|
116
|
|
|
} |
|
117
|
|
|
} |
|
118
|
|
|
|