1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Yarak\Config; |
4
|
|
|
|
5
|
|
|
class Config |
6
|
|
|
{ |
7
|
|
|
/** |
8
|
|
|
* Instance of self. |
9
|
|
|
* |
10
|
|
|
* @var Config |
11
|
|
|
*/ |
12
|
|
|
private static $instance; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Yarak config array. |
16
|
|
|
* |
17
|
|
|
* @var array |
18
|
|
|
*/ |
19
|
|
|
protected $configArray; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Default setting values. |
23
|
|
|
* |
24
|
|
|
* @var array |
25
|
|
|
*/ |
26
|
|
|
const DEFAULTS = [ |
27
|
|
|
'migratorType' => 'fileDate', |
28
|
|
|
'migrationRepository' => 'database', |
29
|
|
|
]; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Private constructor. |
33
|
|
|
*/ |
34
|
|
|
private function __construct(array $configArray) |
35
|
|
|
{ |
36
|
|
|
$this->configArray = $configArray; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Get instance of self with config array set. |
41
|
|
|
* |
42
|
|
|
* @param array $configArray |
43
|
|
|
* |
44
|
|
|
* @return Config |
45
|
|
|
*/ |
46
|
|
|
public static function getInstance(array $configArray = []) |
47
|
|
|
{ |
48
|
|
|
if (empty(self::$instance)) { |
49
|
|
|
if (empty($configArray)) { |
50
|
|
|
$di = \Phalcon\DI::getDefault(); |
51
|
|
|
|
52
|
|
|
$configArray = $di->getShared('yarak')->getConfig(); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
self::$instance = new self($configArray); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
return self::$instance; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Get a value from the config array. |
63
|
|
|
* |
64
|
|
|
* @param string|array $value |
65
|
|
|
* |
66
|
|
|
* @return mixed |
67
|
|
|
*/ |
68
|
|
|
public function get($value) |
69
|
|
|
{ |
70
|
|
|
if (!is_array($value)) { |
71
|
|
|
$value = [$value]; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
$current = $this->configArray; |
75
|
|
|
|
76
|
|
|
foreach ($value as $configItem) { |
77
|
|
|
if (!isset($current[$configItem])) { |
78
|
|
|
return $this->getDefault($configItem); |
79
|
|
|
} else { |
80
|
|
|
$current = $current[$configItem]; |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
return $current; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Get a setting's default value. |
89
|
|
|
* |
90
|
|
|
* @param string $value |
91
|
|
|
* |
92
|
|
|
* @return mixed|null |
93
|
|
|
*/ |
94
|
|
|
public function getDefault($value) |
95
|
|
|
{ |
96
|
|
|
if (array_key_exists($value, self::DEFAULTS)) { |
97
|
|
|
return self::DEFAULTS[$value]; |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Return config array. |
103
|
|
|
* |
104
|
|
|
* @return array |
105
|
|
|
*/ |
106
|
|
|
public function getAll() |
107
|
|
|
{ |
108
|
|
|
return $this->configArray; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Return the database directory path. |
113
|
|
|
* |
114
|
|
|
* @return string |
115
|
|
|
*/ |
116
|
|
|
public function getDatabaseDirectory() |
117
|
|
|
{ |
118
|
|
|
return $this->get(['application', 'databaseDir']); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Return the migration directory path. |
123
|
|
|
* |
124
|
|
|
* @param string $path |
125
|
|
|
* |
126
|
|
|
* @return string |
127
|
|
|
*/ |
128
|
|
|
public function getMigrationDirectory($path = '') |
129
|
|
|
{ |
130
|
|
|
return $this->getDatabaseDirectory().'migrations/'.$path; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Return the factory directory path. |
135
|
|
|
* |
136
|
|
|
* @param string $path |
137
|
|
|
* |
138
|
|
|
* @return string |
139
|
|
|
*/ |
140
|
|
|
public function getFactoryDirectory($path = '') |
141
|
|
|
{ |
142
|
|
|
return $this->getDatabaseDirectory().'factories/'.$path; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Return the seeds directory path. |
147
|
|
|
* |
148
|
|
|
* @param string $path |
149
|
|
|
* |
150
|
|
|
* @return string |
151
|
|
|
*/ |
152
|
|
|
public function getSeedDirectory($path = '') |
153
|
|
|
{ |
154
|
|
|
return $this->getDatabaseDirectory().'seeds/'.$path; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* Make database directory structure if it doesn't exist. |
159
|
|
|
*/ |
160
|
|
|
public function getAllDatabaseDirectories() |
161
|
|
|
{ |
162
|
|
|
return [ |
163
|
|
|
$this->getDatabaseDirectory(), |
164
|
|
|
$this->getMigrationDirectory(), |
165
|
|
|
$this->getFactoryDirectory(), |
166
|
|
|
$this->getSeedDirectory(), |
167
|
|
|
]; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* Return the config array. |
172
|
|
|
* |
173
|
|
|
* @return array |
174
|
|
|
*/ |
175
|
|
|
public function toArray() |
176
|
|
|
{ |
177
|
|
|
return $this->getAll(); |
178
|
|
|
} |
179
|
|
|
} |
180
|
|
|
|