|
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 Sunrise\Http\Router\Exception\InvalidLoadResourceException; |
|
18
|
|
|
use Sunrise\Http\Router\RouteCollectionFactory; |
|
19
|
|
|
use Sunrise\Http\Router\RouteCollectionFactoryInterface; |
|
20
|
|
|
use Sunrise\Http\Router\RouteCollectionInterface; |
|
21
|
|
|
use Sunrise\Http\Router\RouteCollector; |
|
22
|
|
|
use Sunrise\Http\Router\RouteFactory; |
|
23
|
|
|
use Sunrise\Http\Router\RouteFactoryInterface; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Import functions |
|
27
|
|
|
*/ |
|
28
|
|
|
use function is_file; |
|
29
|
|
|
use function sprintf; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* CollectableFileLoader |
|
33
|
|
|
*/ |
|
34
|
|
|
class CollectableFileLoader implements LoaderInterface |
|
35
|
|
|
{ |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @var string[] |
|
39
|
|
|
*/ |
|
40
|
|
|
private $resources = []; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @var RouteCollectionFactoryInterface |
|
44
|
|
|
*/ |
|
45
|
|
|
private $collectionFactory; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @var RouteFactoryInterface |
|
49
|
|
|
*/ |
|
50
|
|
|
private $routeFactory; |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @param null|RouteCollectionFactoryInterface $collectionFactory |
|
54
|
|
|
* @param null|RouteFactoryInterface $routeFactory |
|
55
|
|
|
*/ |
|
56
|
3 |
|
public function __construct( |
|
57
|
|
|
RouteCollectionFactoryInterface $collectionFactory = null, |
|
58
|
|
|
RouteFactoryInterface $routeFactory = null |
|
59
|
|
|
) { |
|
60
|
3 |
|
$this->collectionFactory = $collectionFactory ?? new RouteCollectionFactory(); |
|
61
|
3 |
|
$this->routeFactory = $routeFactory ?? new RouteFactory(); |
|
62
|
3 |
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* {@inheritDoc} |
|
66
|
|
|
*/ |
|
67
|
2 |
|
public function attach($resource) : void |
|
68
|
|
|
{ |
|
69
|
2 |
|
if (!is_file($resource)) { |
|
70
|
1 |
|
throw new InvalidLoadResourceException( |
|
71
|
1 |
|
sprintf('The "%s" resource not found.', $resource) |
|
72
|
|
|
); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
1 |
|
$this->resources[] = $resource; |
|
76
|
1 |
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* {@inheritDoc} |
|
80
|
|
|
*/ |
|
81
|
1 |
|
public function load() : RouteCollectionInterface |
|
82
|
|
|
{ |
|
83
|
1 |
|
$collect = new RouteCollector( |
|
84
|
1 |
|
$this->collectionFactory, |
|
85
|
1 |
|
$this->routeFactory |
|
86
|
|
|
); |
|
87
|
|
|
|
|
88
|
1 |
|
foreach ($this->resources as $resource) { |
|
89
|
|
|
(function () use ($resource) { |
|
90
|
1 |
|
require $resource; |
|
91
|
1 |
|
})->call($collect); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
1 |
|
return $collect->getCollection(); |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
|