|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* It's free open-source software released under the MIT License. |
|
5
|
|
|
* |
|
6
|
|
|
* @author Anatoly Fenric <[email protected]> |
|
7
|
|
|
* @copyright Copyright (c) 2018, Anatoly Fenric |
|
8
|
|
|
* @license https://github.com/sunrise-php/http-router/blob/master/LICENSE |
|
9
|
|
|
* @link https://github.com/sunrise-php/http-router |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Sunrise\Http\Router\Loader; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Import classes |
|
16
|
|
|
*/ |
|
17
|
|
|
use Psr\Container\ContainerInterface; |
|
18
|
|
|
use Sunrise\Http\Router\Exception\InvalidLoaderResourceException; |
|
19
|
|
|
use Sunrise\Http\Router\ReferenceResolver; |
|
20
|
|
|
use Sunrise\Http\Router\ReferenceResolverInterface; |
|
21
|
|
|
use Sunrise\Http\Router\RouteCollectionFactory; |
|
22
|
|
|
use Sunrise\Http\Router\RouteCollectionFactoryInterface; |
|
23
|
|
|
use Sunrise\Http\Router\RouteCollectionInterface; |
|
24
|
|
|
use Sunrise\Http\Router\RouteCollector; |
|
25
|
|
|
use Sunrise\Http\Router\RouteFactory; |
|
26
|
|
|
use Sunrise\Http\Router\RouteFactoryInterface; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Import functions |
|
30
|
|
|
*/ |
|
31
|
|
|
use function glob; |
|
32
|
|
|
use function is_dir; |
|
33
|
|
|
use function is_file; |
|
34
|
|
|
use function sprintf; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* ConfigLoader |
|
38
|
|
|
*/ |
|
39
|
|
|
class ConfigLoader implements LoaderInterface |
|
40
|
|
|
{ |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @var string[] |
|
44
|
|
|
*/ |
|
45
|
|
|
private $resources = []; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @var RouteCollectionFactoryInterface |
|
49
|
|
|
*/ |
|
50
|
|
|
private $collectionFactory; |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @var RouteFactoryInterface |
|
54
|
|
|
*/ |
|
55
|
|
|
private $routeFactory; |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @var ReferenceResolverInterface |
|
59
|
|
|
*/ |
|
60
|
|
|
private $referenceResolver; |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Constructor of the class |
|
64
|
|
|
* |
|
65
|
|
|
* @param RouteCollectionFactoryInterface|null $collectionFactory |
|
66
|
|
|
* @param RouteFactoryInterface|null $routeFactory |
|
67
|
|
|
* @param ReferenceResolverInterface|null $referenceResolver |
|
68
|
|
|
*/ |
|
69
|
8 |
|
public function __construct( |
|
70
|
|
|
?RouteCollectionFactoryInterface $collectionFactory = null, |
|
71
|
|
|
?RouteFactoryInterface $routeFactory = null, |
|
72
|
|
|
?ReferenceResolverInterface $referenceResolver = null |
|
73
|
|
|
) { |
|
74
|
8 |
|
$this->collectionFactory = $collectionFactory ?? new RouteCollectionFactory(); |
|
75
|
8 |
|
$this->routeFactory = $routeFactory ?? new RouteFactory(); |
|
76
|
8 |
|
$this->referenceResolver = $referenceResolver ?? new ReferenceResolver(); |
|
77
|
8 |
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Gets the loader container |
|
81
|
|
|
* |
|
82
|
|
|
* @return ContainerInterface|null |
|
83
|
|
|
* |
|
84
|
|
|
* @since 2.9.0 |
|
85
|
|
|
*/ |
|
86
|
1 |
|
public function getContainer() : ?ContainerInterface |
|
87
|
|
|
{ |
|
88
|
1 |
|
return $this->referenceResolver->getContainer(); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Sets the given container to the loader |
|
93
|
|
|
* |
|
94
|
|
|
* @param ContainerInterface|null $container |
|
95
|
|
|
* |
|
96
|
|
|
* @return void |
|
97
|
|
|
* |
|
98
|
|
|
* @since 2.9.0 |
|
99
|
|
|
*/ |
|
100
|
2 |
|
public function setContainer(?ContainerInterface $container) : void |
|
101
|
|
|
{ |
|
102
|
2 |
|
$this->referenceResolver->setContainer($container); |
|
103
|
2 |
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* {@inheritdoc} |
|
107
|
|
|
*/ |
|
108
|
6 |
|
public function attach($resource) : void |
|
109
|
|
|
{ |
|
110
|
6 |
|
if (is_dir($resource)) { |
|
111
|
1 |
|
$resources = glob($resource . '/*.php'); |
|
112
|
1 |
|
foreach ($resources as $resource) { |
|
|
|
|
|
|
113
|
1 |
|
$this->resources[] = $resource; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
1 |
|
return; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
5 |
|
if (!is_file($resource)) { |
|
120
|
2 |
|
throw new InvalidLoaderResourceException(sprintf( |
|
121
|
2 |
|
'The resource "%s" is not found.', |
|
122
|
2 |
|
$resource |
|
123
|
|
|
)); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
3 |
|
$this->resources[] = $resource; |
|
127
|
3 |
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* {@inheritdoc} |
|
131
|
|
|
*/ |
|
132
|
3 |
|
public function attachArray(array $resources) : void |
|
133
|
|
|
{ |
|
134
|
3 |
|
foreach ($resources as $resource) { |
|
135
|
3 |
|
$this->attach($resource); |
|
136
|
|
|
} |
|
137
|
2 |
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* {@inheritdoc} |
|
141
|
|
|
*/ |
|
142
|
4 |
|
public function load() : RouteCollectionInterface |
|
143
|
|
|
{ |
|
144
|
4 |
|
$collector = new RouteCollector( |
|
145
|
4 |
|
$this->collectionFactory, |
|
146
|
4 |
|
$this->routeFactory, |
|
147
|
4 |
|
$this->referenceResolver |
|
148
|
|
|
); |
|
149
|
|
|
|
|
150
|
4 |
|
foreach ($this->resources as $resource) { |
|
151
|
4 |
|
(function () use ($resource) { |
|
152
|
4 |
|
require $resource; |
|
153
|
4 |
|
})->call($collector); |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
4 |
|
return $collector->getCollection(); |
|
157
|
|
|
} |
|
158
|
|
|
} |
|
159
|
|
|
|