1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Sylius package. |
5
|
|
|
* |
6
|
|
|
* (c) Paweł Jędrzejewski |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Sylius\Bundle\ThemeBundle\Locator; |
13
|
|
|
|
14
|
|
|
use Sylius\Bundle\ThemeBundle\Factory\FinderFactoryInterface; |
15
|
|
|
use Symfony\Component\Finder\Finder; |
16
|
|
|
use Symfony\Component\Finder\SplFileInfo; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @author Kamil Kokot <[email protected]> |
20
|
|
|
*/ |
21
|
|
|
final class RecursiveFileLocator implements FileLocatorInterface |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @var FinderFactoryInterface |
25
|
|
|
*/ |
26
|
|
|
private $finderFactory; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var array |
30
|
|
|
*/ |
31
|
|
|
private $paths; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @param FinderFactoryInterface $finderFactory |
35
|
|
|
* @param array $paths An array of paths where to look for resources |
36
|
|
|
*/ |
37
|
|
|
public function __construct(FinderFactoryInterface $finderFactory, array $paths) |
38
|
|
|
{ |
39
|
|
|
$this->finderFactory = $finderFactory; |
40
|
|
|
$this->paths = $paths; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* {@inheritdoc} |
45
|
|
|
*/ |
46
|
|
|
public function locateFileNamed($name) |
47
|
|
|
{ |
48
|
|
|
return $this->doLocateFilesNamed($name)->current(); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* {@inheritdoc} |
53
|
|
|
*/ |
54
|
|
|
public function locateFilesNamed($name) |
55
|
|
|
{ |
56
|
|
|
return iterator_to_array($this->doLocateFilesNamed($name)); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @param string $name |
61
|
|
|
* |
62
|
|
|
* @return \Generator |
63
|
|
|
*/ |
64
|
|
|
private function doLocateFilesNamed($name) |
65
|
|
|
{ |
66
|
|
|
$this->assertNameIsNotEmpty($name); |
67
|
|
|
|
68
|
|
|
$found = false; |
69
|
|
|
foreach ($this->paths as $path) { |
70
|
|
|
try { |
71
|
|
|
$finder = $this->finderFactory->create(); |
72
|
|
|
$finder |
73
|
|
|
->files() |
74
|
|
|
->name($name) |
75
|
|
|
->ignoreUnreadableDirs() |
76
|
|
|
->in($path); |
77
|
|
|
|
78
|
|
|
/** @var SplFileInfo $file */ |
79
|
|
|
foreach ($finder as $file) { |
80
|
|
|
$found = true; |
81
|
|
|
|
82
|
|
|
yield $file->getPathname(); |
83
|
|
|
} |
84
|
|
|
} catch (\InvalidArgumentException $exception) { |
|
|
|
|
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
if (false === $found) { |
89
|
|
|
throw new \InvalidArgumentException(sprintf( |
90
|
|
|
'The file "%s" does not exist (searched in the following directories: %s).', |
91
|
|
|
$name, |
92
|
|
|
implode(', ', $this->paths) |
93
|
|
|
)); |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @param string $name |
99
|
|
|
*/ |
100
|
|
|
private function assertNameIsNotEmpty($name) |
101
|
|
|
{ |
102
|
|
|
if (null === $name || '' === $name) { |
103
|
|
|
throw new \InvalidArgumentException( |
104
|
|
|
'An empty file name is not valid to be located.' |
105
|
|
|
); |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|