1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Arcanedev\Support\Providers\Concerns; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Str; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Trait HasConfig |
9
|
|
|
* |
10
|
|
|
* @package Arcanedev\Support\Providers\Concerns |
11
|
|
|
* @author ARCANEDEV <[email protected]> |
12
|
|
|
*/ |
13
|
|
|
trait HasConfig |
14
|
|
|
{ |
15
|
|
|
/* ----------------------------------------------------------------- |
16
|
|
|
| Properties |
17
|
|
|
| ----------------------------------------------------------------- |
18
|
|
|
*/ |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Merge multiple config files into one instance (package name as root key) |
22
|
|
|
* |
23
|
|
|
* @var bool |
24
|
|
|
*/ |
25
|
|
|
protected $multiConfigs = false; |
26
|
|
|
|
27
|
|
|
/* ----------------------------------------------------------------- |
28
|
|
|
| Main Methods |
29
|
|
|
| ----------------------------------------------------------------- |
30
|
|
|
*/ |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Get config folder. |
34
|
|
|
* |
35
|
|
|
* @return string |
36
|
|
|
*/ |
37
|
|
|
protected function getConfigFolder() |
38
|
|
|
{ |
39
|
|
|
return realpath($this->getBasePath().DS.'config'); |
|
|
|
|
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Get config key. |
44
|
|
|
* |
45
|
|
|
* @return string |
46
|
|
|
*/ |
47
|
6 |
|
protected function getConfigKey() |
48
|
|
|
{ |
49
|
6 |
|
return Str::slug($this->package); |
|
|
|
|
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Get config file path. |
54
|
|
|
* |
55
|
|
|
* @return string |
56
|
|
|
*/ |
57
|
6 |
|
protected function getConfigFile() |
58
|
|
|
{ |
59
|
6 |
|
return $this->getConfigFolder().DS."{$this->package}.php"; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Get config file destination path. |
64
|
|
|
* |
65
|
|
|
* @return string |
66
|
|
|
*/ |
67
|
|
|
protected function getConfigFileDestination() |
68
|
|
|
{ |
69
|
|
|
return config_path("{$this->package}.php"); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Register configs. |
74
|
|
|
* |
75
|
|
|
* @param string $separator |
76
|
|
|
*/ |
77
|
6 |
|
protected function registerConfig($separator = '.') |
78
|
|
|
{ |
79
|
6 |
|
$this->multiConfigs |
80
|
|
|
? $this->registerMultipleConfigs($separator) |
81
|
6 |
|
: $this->mergeConfigFrom($this->getConfigFile(), $this->getConfigKey()); |
|
|
|
|
82
|
6 |
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Register all package configs. |
86
|
|
|
* |
87
|
|
|
* @param string $separator |
88
|
|
|
*/ |
89
|
|
|
private function registerMultipleConfigs($separator = '.') |
90
|
|
|
{ |
91
|
|
|
foreach (glob($this->getConfigFolder().'/*.php') as $configPath) { |
92
|
|
|
$this->mergeConfigFrom( |
|
|
|
|
93
|
|
|
$configPath, $this->getConfigKey().$separator.basename($configPath, '.php') |
94
|
|
|
); |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Publish the config file. |
100
|
|
|
*/ |
101
|
|
|
protected function publishConfig() |
102
|
|
|
{ |
103
|
|
|
$this->publishes([ |
|
|
|
|
104
|
|
|
$this->getConfigFile() => $this->getConfigFileDestination() |
105
|
|
|
], 'config'); |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|
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.