1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* Copyright (c) 2011-2015, Celestino Diaz <[email protected]> |
5
|
|
|
* |
6
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
7
|
|
|
* of this software and associated documentation files (the "Software"), to deal |
8
|
|
|
* in the Software without restriction, including without limitation the rights |
9
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
10
|
|
|
* copies of the Software, and to permit persons to whom the Software is |
11
|
|
|
* furnished to do so, subject to the following conditions: |
12
|
|
|
* |
13
|
|
|
* The above copyright notice and this permission notice shall be included in |
14
|
|
|
* all copies or substantial portions of the Software. |
15
|
|
|
* |
16
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
17
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
18
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
19
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
20
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
21
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
22
|
|
|
* THE SOFTWARE. |
23
|
|
|
*/ |
24
|
|
|
|
25
|
|
|
namespace Brickoo\Component\Routing\Route\Collector; |
26
|
|
|
|
27
|
|
|
use ArrayIterator; |
28
|
|
|
use Brickoo\Component\Common\Collection; |
29
|
|
|
use Brickoo\Component\Routing\Route\RouteCollection; |
30
|
|
|
use Brickoo\Component\Common\Assert; |
31
|
|
|
use DirectoryIterator; |
32
|
|
|
use FilesystemIterator; |
33
|
|
|
use InvalidArgumentException; |
34
|
|
|
use RecursiveDirectoryIterator; |
35
|
|
|
use RecursiveIteratorIterator; |
36
|
|
|
use RegexIterator; |
37
|
|
|
use SplFileInfo; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* FileCollector |
41
|
|
|
* |
42
|
|
|
* Implements a route collector based on one or many files |
43
|
|
|
* which have to return a route collection. |
44
|
|
|
* @author Celestino Diaz <[email protected]> |
45
|
|
|
*/ |
46
|
|
|
class FileRouteCollector implements RouteCollector { |
47
|
|
|
|
48
|
|
|
/** @var string */ |
49
|
|
|
private $routingPath; |
50
|
|
|
|
51
|
|
|
/** @var string */ |
52
|
|
|
private $routingFilename; |
53
|
|
|
|
54
|
|
|
/** @var boolean */ |
55
|
|
|
private $searchRecursively; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Class constructor. |
59
|
|
|
* @param string $routingPath the routing directory path |
60
|
|
|
* @param string $routingFilename the route filename to look for |
61
|
|
|
* @param boolean $searchRecursively flag to search recursively |
62
|
|
|
* @throws \InvalidArgumentException if an argument is not valid |
63
|
|
|
*/ |
64
|
3 |
|
public function __construct($routingPath, $routingFilename, $searchRecursively = false) { |
65
|
3 |
|
Assert::isString($routingPath); |
66
|
3 |
|
Assert::isString($routingFilename); |
67
|
3 |
|
Assert::isBoolean($searchRecursively); |
68
|
|
|
|
69
|
3 |
|
if (empty($routingPath)) { |
70
|
1 |
|
throw new InvalidArgumentException("The routing path cannot be empty."); |
71
|
|
|
} |
72
|
|
|
|
73
|
2 |
|
if (empty($routingFilename)) { |
74
|
1 |
|
throw new InvalidArgumentException("The routing filename cannot be empty."); |
75
|
|
|
} |
76
|
|
|
|
77
|
1 |
|
$this->routingPath = $routingPath; |
78
|
1 |
|
$this->routingFilename = $routingFilename; |
79
|
1 |
|
$this->searchRecursively = $searchRecursively; |
80
|
1 |
|
} |
81
|
|
|
|
82
|
|
|
/** {@inheritDoc} */ |
83
|
2 |
|
public function collect() { |
84
|
2 |
|
$filePaths = $this->collectRouteCollectionsFilePaths(); |
85
|
|
|
|
86
|
2 |
|
$collection = new Collection(); |
87
|
2 |
|
foreach ($filePaths as $filePath) { |
88
|
2 |
|
if (($routeCollection = $this->getRouteCollectionFromFilePath($filePath)) |
89
|
2 |
|
&& $routeCollection instanceof RouteCollection) { |
90
|
2 |
|
$collection->add($routeCollection); |
91
|
2 |
|
} |
92
|
2 |
|
} |
93
|
2 |
|
return $collection; |
94
|
|
|
} |
95
|
|
|
|
96
|
2 |
|
private function getRouteCollectionFromFilePath($filePath) { |
97
|
2 |
|
return include $filePath; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Return the collected route collections file paths. |
102
|
|
|
* @return array the collected collection file paths |
103
|
|
|
*/ |
104
|
2 |
|
private function collectRouteCollectionsFilePaths() { |
105
|
2 |
|
return $this->searchRecursively ? $this->getRecursiveFilePaths() : $this->getFilePaths(); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Returns the file paths of the available route collections. |
110
|
|
|
* @return array the matching file paths |
111
|
|
|
*/ |
112
|
1 |
|
private function getFilePaths() { |
113
|
1 |
|
$collectedFilePaths = []; |
114
|
|
|
|
115
|
1 |
|
foreach (new DirectoryIterator($this->routingPath) as $splFileInfo) { |
116
|
1 |
|
if ($splFileInfo->isFile() |
117
|
1 |
|
&& (strpos($splFileInfo->getFilename(), $this->routingFilename) !== false)) { |
118
|
1 |
|
$collectedFilePaths[] = $splFileInfo->getRealPath(); |
119
|
1 |
|
} |
120
|
1 |
|
} |
121
|
|
|
|
122
|
1 |
|
return $collectedFilePaths; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Returns the file paths of the route collections recursively collected. |
127
|
|
|
* @return array the available file paths |
128
|
|
|
*/ |
129
|
1 |
|
private function getRecursiveFilePaths() { |
130
|
1 |
|
$collectedFilePaths = []; |
131
|
|
|
|
132
|
1 |
|
$directory = new RecursiveDirectoryIterator($this->routingPath, FilesystemIterator::FOLLOW_SYMLINKS); |
133
|
1 |
|
$iterator = new RecursiveIteratorIterator($directory); |
134
|
|
|
|
135
|
1 |
|
$array = iterator_to_array($iterator); |
136
|
1 |
|
usort($array, function(SplFileInfo $aFile, SplFileInfo $bFile) { |
137
|
1 |
|
return $aFile->getPathname() < $bFile->getPathname(); |
138
|
1 |
|
}); |
139
|
1 |
|
$orderedIterator = new ArrayIterator($array); |
140
|
|
|
|
141
|
1 |
|
foreach (new RegexIterator($orderedIterator, "~".$this->routingFilename."$~i", RegexIterator::MATCH) as $splFileInfo) { |
142
|
1 |
|
$collectedFilePaths[] = $splFileInfo->getRealPath(); |
143
|
1 |
|
} |
144
|
|
|
|
145
|
1 |
|
return $collectedFilePaths; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
} |
149
|
|
|
|