1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of DivineNii opensource projects. |
7
|
|
|
* |
8
|
|
|
* PHP version 7.4 and above required |
9
|
|
|
* |
10
|
|
|
* @author Divine Niiquaye Ibok <[email protected]> |
11
|
|
|
* @copyright 2021 DivineNii (https://divinenii.com/) |
12
|
|
|
* @license https://opensource.org/licenses/BSD-3-Clause License |
13
|
|
|
* |
14
|
|
|
* For the full copyright and license information, please view the LICENSE |
15
|
|
|
* file that was distributed with this source code. |
16
|
|
|
*/ |
17
|
|
|
|
18
|
|
|
namespace Rade\DI\Loader; |
19
|
|
|
|
20
|
|
|
use Rade\DI\ContainerBuilder; |
21
|
|
|
use Symfony\Component\Config\Resource\FileExistenceResource; |
22
|
|
|
use Symfony\Component\Config\Resource\GlobResource; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* DirectoryLoader is a recursive loader to go through directories. |
26
|
|
|
* |
27
|
|
|
* @author Divine Niiquaye Ibok <[email protected]> |
28
|
|
|
*/ |
29
|
|
|
class DirectoryLoader extends FileLoader |
30
|
|
|
{ |
31
|
|
|
/** |
32
|
|
|
* {@inheritdoc} |
33
|
|
|
*/ |
34
|
3 |
|
public function load($file, string $type = null): void |
35
|
|
|
{ |
36
|
3 |
|
$file = \rtrim($file, '/'); |
37
|
3 |
|
$path = $this->locator->locate($file); |
38
|
|
|
|
39
|
2 |
|
if ($this->container instanceof ContainerBuilder) { |
40
|
2 |
|
$this->container->addResource(new FileExistenceResource($file)); |
|
|
|
|
41
|
2 |
|
$this->container->addResource(new GlobResource($path, '/*', false)); |
|
|
|
|
42
|
|
|
} |
43
|
|
|
|
44
|
2 |
|
foreach (\scandir($path) as $dir) { |
|
|
|
|
45
|
2 |
|
if ('.' !== $dir[0]) { |
46
|
2 |
|
if (\is_dir($path . '/' . $dir)) { |
|
|
|
|
47
|
1 |
|
$dir .= '/'; // append / to allow recursion |
48
|
|
|
} |
49
|
|
|
|
50
|
2 |
|
$this->setCurrentDir($path); |
|
|
|
|
51
|
2 |
|
$this->import($dir, null, false, $path); |
|
|
|
|
52
|
|
|
} |
53
|
|
|
} |
54
|
2 |
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* {@inheritdoc} |
58
|
|
|
*/ |
59
|
4 |
|
public function supports($resource, string $type = null) |
60
|
|
|
{ |
61
|
4 |
|
if ('directory' === $type) { |
62
|
2 |
|
return true; |
63
|
|
|
} |
64
|
|
|
|
65
|
4 |
|
return null === $type && \is_string($resource) && '/' === \substr($resource, -1); |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|