1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Vinelab\UrlShortener\Base; |
4
|
|
|
|
5
|
|
|
use Illuminate\Config\Repository; |
6
|
|
|
use Symfony\Component\Finder\Finder; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Extends the Laravel `Illuminate\Config\Repository` thus it can be used as in Laravel. |
10
|
|
|
* it also contains some helper functions to facilitate reading the configuration from this package. |
11
|
|
|
* Once an object of this class is initialized it will automatically read the package config file. |
12
|
|
|
* |
13
|
|
|
* Yes this class is doing multiple things it could be easily splited into multiple classes, however |
14
|
|
|
* I preferred to keep it as portable as possible. |
15
|
|
|
* |
16
|
|
|
* @category Manager Class (of the configuration) |
17
|
|
|
* |
18
|
|
|
* @author Mahmoud Zalt <[email protected]> |
19
|
|
|
*/ |
20
|
|
|
class ConfigManager extends Repository |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* the config file name of this package. |
24
|
|
|
*/ |
25
|
|
|
const CONFIG_FILE_NAME = 'url-shortener'; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* load the configuration files. |
29
|
|
|
*/ |
30
|
|
|
public function __construct() |
31
|
|
|
{ |
32
|
|
|
$path = $this->configurationPath(); |
33
|
|
|
|
34
|
|
|
$this->loadConfigurationFiles($path); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* extend the functionality of the default get() of the Repository |
39
|
|
|
* but always prepend the keys with the config file name. |
40
|
|
|
* |
41
|
|
|
* @param $key |
42
|
|
|
* |
43
|
|
|
* @return mixed |
44
|
|
|
*/ |
45
|
|
|
public function read($key) |
46
|
|
|
{ |
47
|
|
|
return $this->get(self::CONFIG_FILE_NAME.'.'.$key); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* helper function to return the driver default name. |
52
|
|
|
* |
53
|
|
|
* @return mixed |
54
|
|
|
*/ |
55
|
|
|
public function driverName() |
56
|
|
|
{ |
57
|
|
|
return $this->read('default'); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* helper function to return the parameters of the driver $name. |
62
|
|
|
* |
63
|
|
|
* @param $name |
64
|
|
|
* |
65
|
|
|
* @return mixed |
66
|
|
|
*/ |
67
|
|
|
public function driverParameters($name) |
68
|
|
|
{ |
69
|
|
|
return $this->read('drivers'.'.'.$name); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Initialize the paths. |
74
|
|
|
* check if this package is used inside of laravel project, |
75
|
|
|
* if it is laravel project then try to load the config file |
76
|
|
|
* from the laravel config directory. |
77
|
|
|
* if the file was not found (not published) ten load the config |
78
|
|
|
* file form the package directory. |
79
|
|
|
* |
80
|
|
|
* @return string |
81
|
|
|
*/ |
82
|
|
|
private function configurationPath() |
83
|
|
|
{ |
84
|
|
|
// the config file of the package directory |
85
|
|
|
$config_path = str_replace('/Base', '/Config', __DIR__); |
86
|
|
|
|
87
|
|
|
// check if this laravel specific function `config_path()` exist (means this package is used inside |
88
|
|
|
// a laravel framework). If so then load then try to load the laravel config file if it exist. |
89
|
|
|
if (function_exists('config_path')) { |
90
|
|
|
$config_file = config_path().'/'.self::CONFIG_FILE_NAME.'.php'; |
91
|
|
|
|
92
|
|
|
if (file_exists($config_file)) { |
93
|
|
|
// override the path by the laravel specific config directory |
94
|
|
|
$config_path = config_path(); |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
return $config_path; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Load the configuration items from all of the files. |
103
|
|
|
* |
104
|
|
|
* @param $path |
105
|
|
|
*/ |
106
|
|
|
private function loadConfigurationFiles($path) |
107
|
|
|
{ |
108
|
|
|
$this->configPath = $path; |
|
|
|
|
109
|
|
|
|
110
|
|
|
foreach ($this->getConfigurationFiles() as $fileKey => $path) { |
111
|
|
|
$this->set($fileKey, require $path); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
foreach ($this->getConfigurationFiles() as $fileKey => $path) { |
115
|
|
|
$envConfig = require $path; |
116
|
|
|
|
117
|
|
|
foreach ($envConfig as $envKey => $value) { |
118
|
|
|
$this->set($fileKey.'.'.$envKey, $value); |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Get the configuration files for the selected environment. |
125
|
|
|
* |
126
|
|
|
* @return array |
127
|
|
|
*/ |
128
|
|
|
private function getConfigurationFiles() |
129
|
|
|
{ |
130
|
|
|
$path = $this->configPath; |
131
|
|
|
|
132
|
|
|
if (!is_dir($path)) { |
133
|
|
|
return []; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
$files = []; |
137
|
|
|
$phpFiles = Finder::create()->files()->name('*.php')->in($path)->depth(0); |
138
|
|
|
|
139
|
|
|
foreach ($phpFiles as $file) { |
140
|
|
|
$files[basename($file->getRealPath(), '.php')] = $file->getRealPath(); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
return $files; |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: