Passed
Push — master ( e1642a...3c4285 )
by Anatoly
01:03 queued 12s
created

CollectableFileLoader::getContainer()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 0
crap 1
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\RouteCollectionFactory;
20
use Sunrise\Http\Router\RouteCollectionFactoryInterface;
21
use Sunrise\Http\Router\RouteCollectionInterface;
22
use Sunrise\Http\Router\RouteCollector;
23
use Sunrise\Http\Router\RouteFactory;
24
use Sunrise\Http\Router\RouteFactoryInterface;
25
26
/**
27
 * Import functions
28
 */
29
use function glob;
30
use function is_dir;
31
use function is_file;
32
use function sprintf;
33
34
/**
35
 * CollectableFileLoader
36
 */
37
class CollectableFileLoader implements LoaderInterface
38
{
39
40
    /**
41
     * @var string[]
42
     */
43
    private $resources = [];
44
45
    /**
46
     * @var RouteCollectionFactoryInterface
47
     */
48
    private $collectionFactory;
49
50
    /**
51
     * @var RouteFactoryInterface
52
     */
53
    private $routeFactory;
54
55
    /**
56
     * @var null|ContainerInterface
57
     */
58
    private $container = null;
59
60
    /**
61
     * Constructor of the class
62
     *
63
     * @param null|RouteCollectionFactoryInterface $collectionFactory
64
     * @param null|RouteFactoryInterface $routeFactory
65
     */
66 7
    public function __construct(
67
        ?RouteCollectionFactoryInterface $collectionFactory = null,
68
        ?RouteFactoryInterface $routeFactory = null
69
    ) {
70 7
        $this->collectionFactory = $collectionFactory ?? new RouteCollectionFactory();
71 7
        $this->routeFactory = $routeFactory ?? new RouteFactory();
72 7
    }
73
74
    /**
75
     * Gets the loader container
76
     *
77
     * @return null|ContainerInterface
78
     *
79
     * @since 2.9.0
80
     */
81 1
    public function getContainer() : ?ContainerInterface
82
    {
83 1
        return $this->container;
84
    }
85
86
    /**
87
     * Sets the given container to the loader
88
     *
89
     * @param null|ContainerInterface $container
90
     *
91
     * @return void
92
     *
93
     * @since 2.9.0
94
     */
95 2
    public function setContainer(?ContainerInterface $container) : void
96
    {
97 2
        $this->container = $container;
98 2
    }
99
100
    /**
101
     * {@inheritdoc}
102
     */
103 5
    public function attach($resource) : void
104
    {
105 5
        if (is_dir($resource)) {
106 2
            $resources = glob($resource . '/*.php');
107 2
            foreach ($resources as $resource) {
0 ignored issues
show
introduced by
$resource is overwriting one of the parameters of this function.
Loading history...
108 2
                $this->resources[] = $resource;
109
            }
110
111 2
            return;
112
        }
113
114 3
        if (!is_file($resource)) {
115 1
            throw new InvalidLoaderResourceException(
116 1
                sprintf('The resource "%s" is not found.', $resource)
117
            );
118
        }
119
120 2
        $this->resources[] = $resource;
121 2
    }
122
123
    /**
124
     * {@inheritdoc}
125
     */
126 2
    public function attachArray(array $resources) : void
127
    {
128 2
        foreach ($resources as $resource) {
129 2
            $this->attach($resource);
130
        }
131 2
    }
132
133
    /**
134
     * {@inheritdoc}
135
     */
136 4
    public function load() : RouteCollectionInterface
137
    {
138 4
        $collect = new RouteCollector(
139 4
            $this->collectionFactory,
140 4
            $this->routeFactory
141
        );
142
143 4
        $collect->setContainer($this->container);
144
145 4
        foreach ($this->resources as $resource) {
146 4
            (function () use ($resource) {
147 4
                require $resource;
148 4
            })->call($collect);
149
        }
150
151 4
        return $collect->getCollection();
152
    }
153
}
154