1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of the DS Framework. |
4
|
|
|
* |
5
|
|
|
* (c) Dan Smith <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
namespace Ds\Router\Loaders; |
11
|
|
|
|
12
|
|
|
use Ds\Router\Exceptions\RouterException; |
13
|
|
|
use Ds\Router\Interfaces\RouteCollectionInterface; |
14
|
|
|
use Ds\Router\Interfaces\RouterInterface; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Class FileLoader |
18
|
|
|
* |
19
|
|
|
* @package Ds\Router\Loaders |
20
|
|
|
* @author Dan Smith <[email protected]> |
21
|
|
|
* @license http://opensource.org/licenses/gpl-license.php GNU Public License |
22
|
|
|
*/ |
23
|
|
|
class FileLoader extends AbstractLoader |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* File Loader Default settings |
27
|
|
|
* |
28
|
|
|
* @var array |
29
|
|
|
*/ |
30
|
|
|
public static $defaults = [ |
31
|
|
|
'vars' => [] |
32
|
|
|
]; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* File Loader Options |
36
|
|
|
* |
37
|
|
|
* @var array |
38
|
|
|
*/ |
39
|
|
|
protected $options; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* FileLoader constructor. |
43
|
|
|
* |
44
|
|
|
* @param RouterInterface $router Routing Component |
45
|
|
|
* @param array $options File Loader Options. |
46
|
|
|
*/ |
47
|
7 |
|
public function __construct(RouterInterface $router, array $options = []) |
48
|
|
|
{ |
49
|
7 |
|
$this->options = \array_replace_recursive(FileLoader::$defaults, $options); |
50
|
7 |
|
$this->router = $router; |
51
|
7 |
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @inheritdoc |
55
|
|
|
*/ |
56
|
3 |
View Code Duplication |
public function loadFiles(array $files) |
|
|
|
|
57
|
|
|
{ |
58
|
3 |
|
if (!$this->router->isCached()) { |
59
|
2 |
|
foreach ((array)$files as $filename) { |
60
|
2 |
|
$collection = $this->loadFile($filename); |
61
|
2 |
|
$this->router = $this->router->mergeCollection($collection); |
62
|
|
|
} |
63
|
|
|
} |
64
|
2 |
|
return $this->router; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @inheritdoc |
69
|
|
|
*/ |
70
|
6 |
|
public function loadFile(string $file) |
71
|
|
|
{ |
72
|
6 |
|
if (!$this->router->isCached()) { |
73
|
5 |
|
foreach ((array)$this->options['vars'] as $key => $item) { |
74
|
5 |
|
if (\is_string($key)) { |
75
|
5 |
|
${$key} = $item; |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|
79
|
5 |
|
if (!\file_exists($file)) { |
80
|
2 |
|
throw new RouterException("Could not locate route file.'{$file}'"); |
81
|
|
|
} |
82
|
|
|
|
83
|
4 |
|
$routeData = require $file; |
84
|
|
|
|
85
|
4 |
|
if ($routeData instanceof RouteCollectionInterface === false) { |
86
|
1 |
|
throw new RouterException("'{$file}' must return an instance of Rs\\Router\\RouteCollectionInterface"); |
87
|
|
|
} |
88
|
|
|
|
89
|
3 |
|
return $routeData; |
90
|
|
|
} |
91
|
1 |
|
return $this->router->getCollection(); |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.