|
1
|
|
|
<?php namespace Anomaly\Streams\Platform\Support; |
|
2
|
|
|
|
|
3
|
|
|
use Illuminate\Contracts\Config\Repository; |
|
4
|
|
|
use Illuminate\Filesystem\Filesystem; |
|
5
|
|
|
use SplFileInfo; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Class Configurator |
|
9
|
|
|
* |
|
10
|
|
|
* @link http://anomaly.is/streams-platform |
|
11
|
|
|
* @author AnomalyLabs, Inc. <[email protected]> |
|
12
|
|
|
* @author Ryan Thompson <[email protected]> |
|
13
|
|
|
* @package Anomaly\Streams\Platform\Support |
|
14
|
|
|
*/ |
|
15
|
|
|
class Configurator |
|
16
|
|
|
{ |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* The file system. |
|
20
|
|
|
* |
|
21
|
|
|
* @var Filesystem |
|
22
|
|
|
*/ |
|
23
|
|
|
protected $files; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* The config repository. |
|
27
|
|
|
* |
|
28
|
|
|
* @var Repository |
|
29
|
|
|
*/ |
|
30
|
|
|
protected $config; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Create a new Configurator instance. |
|
34
|
|
|
* |
|
35
|
|
|
* @param Filesystem $files |
|
36
|
|
|
* @param Repository $config |
|
37
|
|
|
*/ |
|
38
|
|
|
function __construct(Filesystem $files, Repository $config) |
|
|
|
|
|
|
39
|
|
|
{ |
|
40
|
|
|
$this->files = $files; |
|
41
|
|
|
$this->config = $config; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Add a namespace to configuration. |
|
46
|
|
|
* |
|
47
|
|
|
* @param $namespace |
|
48
|
|
|
* @param $directory |
|
49
|
|
|
*/ |
|
50
|
|
|
public function addNamespace($namespace, $directory) |
|
51
|
|
|
{ |
|
52
|
|
|
if (!$this->files->isDirectory($directory)) { |
|
53
|
|
|
return; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/* @var SplFileInfo $file */ |
|
57
|
|
|
foreach ($this->files->allFiles($directory) as $file) { |
|
58
|
|
|
|
|
59
|
|
|
$key = trim( |
|
60
|
|
|
str_replace( |
|
61
|
|
|
$directory, |
|
62
|
|
|
'', |
|
63
|
|
|
$file->getPath() |
|
64
|
|
|
) . DIRECTORY_SEPARATOR . $file->getBaseName('.php'), |
|
65
|
|
|
DIRECTORY_SEPARATOR |
|
66
|
|
|
); |
|
67
|
|
|
|
|
68
|
|
|
// Normalize key slashes. |
|
69
|
|
|
$key = str_replace('\\', '/', $key); |
|
70
|
|
|
|
|
71
|
|
|
$this->config->set($namespace . '::' . $key, $this->files->getRequire($file->getPathname())); |
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* Add namespace overrides to configuration. |
|
77
|
|
|
* |
|
78
|
|
|
* @param $namespace |
|
79
|
|
|
* @param $directory |
|
80
|
|
|
*/ |
|
81
|
|
|
public function addNamespaceOverrides($namespace, $directory) |
|
82
|
|
|
{ |
|
83
|
|
|
if (!$this->files->isDirectory($directory)) { |
|
84
|
|
|
return; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/* @var SplFileInfo $file */ |
|
88
|
|
|
foreach ($this->files->allFiles($directory) as $file) { |
|
89
|
|
|
|
|
90
|
|
|
$key = trim( |
|
91
|
|
|
str_replace( |
|
92
|
|
|
$directory, |
|
93
|
|
|
'', |
|
94
|
|
|
$file->getPath() |
|
95
|
|
|
) . DIRECTORY_SEPARATOR . $file->getBaseName('.php'), |
|
96
|
|
|
DIRECTORY_SEPARATOR |
|
97
|
|
|
); |
|
98
|
|
|
|
|
99
|
|
|
// Normalize key slashes. |
|
100
|
|
|
$key = str_replace('\\', '/', $key); |
|
101
|
|
|
|
|
102
|
|
|
$this->config->set( |
|
103
|
|
|
$namespace . '::' . $key, |
|
104
|
|
|
array_replace( |
|
105
|
|
|
$this->config->get($namespace . '::' . $key, []), |
|
106
|
|
|
$this->files->getRequire($file->getPathname()) |
|
107
|
|
|
) |
|
108
|
|
|
); |
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
} |
|
112
|
|
|
|
Adding explicit visibility (
private,protected, orpublic) is generally recommend to communicate to other developers how, and from where this method is intended to be used.