1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Arcanedev\Support\Providers\Concerns; |
6
|
|
|
|
7
|
|
|
use Illuminate\Support\Str; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Trait HasConfig |
11
|
|
|
* |
12
|
|
|
* @author ARCANEDEV <[email protected]> |
13
|
|
|
*/ |
14
|
|
|
trait HasConfig |
15
|
|
|
{ |
16
|
|
|
/* ----------------------------------------------------------------- |
17
|
|
|
| Properties |
18
|
|
|
| ----------------------------------------------------------------- |
19
|
|
|
*/ |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Merge multiple config files into one instance (package name as root key) |
23
|
|
|
* |
24
|
|
|
* @var bool |
25
|
|
|
*/ |
26
|
|
|
protected $multiConfigs = false; |
27
|
|
|
|
28
|
|
|
/* ----------------------------------------------------------------- |
29
|
|
|
| Main Methods |
30
|
|
|
| ----------------------------------------------------------------- |
31
|
|
|
*/ |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Get config folder. |
35
|
|
|
* |
36
|
|
|
* @return string |
37
|
|
|
*/ |
38
|
|
|
protected function getConfigFolder(): string |
39
|
|
|
{ |
40
|
|
|
return realpath($this->getBasePath().DIRECTORY_SEPARATOR.'config'); |
|
|
|
|
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Get config key. |
45
|
|
|
* |
46
|
|
|
* @param bool $withVendor |
47
|
|
|
* @param string $separator |
48
|
|
|
* |
49
|
|
|
* @return string |
50
|
|
|
*/ |
51
|
18 |
|
protected function getConfigKey(bool $withVendor = false, string $separator = '.'): string |
52
|
|
|
{ |
53
|
18 |
|
$package = Str::slug($this->getPackageName()); |
|
|
|
|
54
|
|
|
|
55
|
18 |
|
return $withVendor |
56
|
|
|
? Str::slug($this->getVendorName()).$separator.$package |
|
|
|
|
57
|
18 |
|
: $package; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Get config file path. |
62
|
|
|
* |
63
|
|
|
* @return string |
64
|
|
|
*/ |
65
|
18 |
|
protected function getConfigFile(): string |
66
|
|
|
{ |
67
|
18 |
|
return $this->getConfigFolder().DIRECTORY_SEPARATOR."{$this->getPackageName()}.php"; |
|
|
|
|
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Get the config files (paths). |
72
|
|
|
* |
73
|
|
|
* @return array|false |
74
|
|
|
*/ |
75
|
|
|
protected function configFilesPaths() |
76
|
|
|
{ |
77
|
|
|
return glob($this->getConfigFolder().DIRECTORY_SEPARATOR.'*.php'); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Register configs. |
82
|
|
|
* |
83
|
|
|
* @param string $separator |
84
|
|
|
*/ |
85
|
18 |
|
protected function registerConfig(string $separator = '.'): void |
86
|
|
|
{ |
87
|
18 |
|
$this->multiConfigs |
88
|
|
|
? $this->registerMultipleConfigs($separator) |
89
|
18 |
|
: $this->registerSingleConfig(); |
90
|
18 |
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Register a single config file. |
94
|
|
|
*/ |
95
|
18 |
|
protected function registerSingleConfig(): void |
96
|
|
|
{ |
97
|
18 |
|
$this->mergeConfigFrom($this->getConfigFile(), $this->getConfigKey()); |
|
|
|
|
98
|
18 |
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Register all package configs. |
102
|
|
|
* |
103
|
|
|
* @param string $separator |
104
|
|
|
*/ |
105
|
|
|
protected function registerMultipleConfigs(string $separator = '.'): void |
106
|
|
|
{ |
107
|
|
|
foreach ($this->configFilesPaths() as $path) { |
|
|
|
|
108
|
|
|
$key = $this->getConfigKey(true, $separator).$separator.basename($path, '.php'); |
109
|
|
|
|
110
|
|
|
$this->mergeConfigFrom($path, $key); |
|
|
|
|
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Publish the config file. |
116
|
|
|
* |
117
|
|
|
* @param string|null $path |
118
|
|
|
*/ |
119
|
|
|
protected function publishConfig(?string $path = null): void |
120
|
|
|
{ |
121
|
|
|
$this->multiConfigs |
122
|
|
|
? $this->publishMultipleConfigs() |
123
|
|
|
: $this->publishSingleConfig($path); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Publish a single config file. |
128
|
|
|
* |
129
|
|
|
* @param string|null $path |
130
|
|
|
*/ |
131
|
|
|
protected function publishSingleConfig(?string $path = null): void |
132
|
|
|
{ |
133
|
|
|
$this->publishes([ |
|
|
|
|
134
|
|
|
$this->getConfigFile() => $path ?: config_path("{$this->getPackageName()}.php"), |
|
|
|
|
135
|
|
|
], $this->getPublishedTags('config')); |
|
|
|
|
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Publish multiple config files. |
140
|
|
|
*/ |
141
|
|
|
protected function publishMultipleConfigs(): void |
142
|
|
|
{ |
143
|
|
|
$paths = []; |
144
|
|
|
$package = $this->getConfigKey(true, DIRECTORY_SEPARATOR); |
145
|
|
|
|
146
|
|
|
foreach ($this->configFilesPaths() as $file) { |
|
|
|
|
147
|
|
|
$paths[$file] = config_path($package.DIRECTORY_SEPARATOR.basename($file)); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
$this->publishes($paths, $this->getPublishedTags('config')); |
|
|
|
|
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
|
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idable
provides a methodequalsId
that in turn relies on the methodgetId()
. If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()
as an abstract method to the trait will make sure it is available.