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
|
|
|
* Private constructor. |
23
|
|
|
*/ |
24
|
|
|
private function __construct(array $configArray) |
25
|
|
|
{ |
26
|
|
|
$this->configArray = $configArray; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Get instance of self with config array set. |
31
|
|
|
* |
32
|
|
|
* @param array $configArray |
33
|
|
|
* |
34
|
|
|
* @return Config |
35
|
|
|
*/ |
36
|
|
|
public static function getInstance(array $configArray = []) |
37
|
|
|
{ |
38
|
|
|
if (empty(self::$instance)) { |
39
|
|
|
self::$instance = new self($configArray); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
return self::$instance; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Get a value from the config array. |
47
|
|
|
* |
48
|
|
|
* @param string|array $value |
49
|
|
|
* |
50
|
|
|
* @return mixed |
51
|
|
|
*/ |
52
|
|
|
public function get($value) |
53
|
|
|
{ |
54
|
|
|
if (!is_array($value)) { |
55
|
|
|
$value = [$value]; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
$current = $this->configArray; |
59
|
|
|
|
60
|
|
|
foreach ($value as $configItem) { |
61
|
|
|
$current = $current[$configItem]; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
return $current; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Return config array. |
69
|
|
|
* |
70
|
|
|
* @return array |
71
|
|
|
*/ |
72
|
|
|
public function getAll() |
73
|
|
|
{ |
74
|
|
|
return $this->configArray; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Return the database directory path. |
79
|
|
|
* |
80
|
|
|
* @return string |
81
|
|
|
*/ |
82
|
|
|
public function getDatabaseDirectory() |
83
|
|
|
{ |
84
|
|
|
return $this->get(['application', 'databaseDir']); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Return the migration directory path. |
89
|
|
|
* |
90
|
|
|
* @param string $path |
91
|
|
|
* |
92
|
|
|
* @return string |
93
|
|
|
*/ |
94
|
|
|
public function getMigrationDirectory($path = '') |
95
|
|
|
{ |
96
|
|
|
return $this->getDatabaseDirectory().'migrations/'.$path; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Return the factory directory path. |
101
|
|
|
* |
102
|
|
|
* @param string $path |
103
|
|
|
* |
104
|
|
|
* @return string |
105
|
|
|
*/ |
106
|
|
|
public function getFactoryDirectory($path = '') |
107
|
|
|
{ |
108
|
|
|
return $this->getDatabaseDirectory().'factories/'.$path; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Return the seeds directory path. |
113
|
|
|
* |
114
|
|
|
* @param string $path |
115
|
|
|
* |
116
|
|
|
* @return string |
117
|
|
|
*/ |
118
|
|
|
public function getSeedDirectory($path = '') |
119
|
|
|
{ |
120
|
|
|
return $this->getDatabaseDirectory().'seeds/'.$path; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Make database directory structure if it doesn't exist. |
125
|
|
|
*/ |
126
|
|
|
public function getAllDatabaseDirectories() |
127
|
|
|
{ |
128
|
|
|
return [ |
129
|
|
|
$this->getDatabaseDirectory(), |
130
|
|
|
$this->getMigrationDirectory(), |
131
|
|
|
$this->getFactoryDirectory(), |
132
|
|
|
$this->getSeedDirectory(), |
133
|
|
|
]; |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|