1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of the Zemit Framework. |
5
|
|
|
* |
6
|
|
|
* (c) Zemit Team <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE.txt |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Zemit\Config; |
13
|
|
|
|
14
|
|
|
use Phalcon\Config\ConfigInterface as PhalconConfigInterface; |
15
|
|
|
use Zemit\Mvc\Model; |
16
|
|
|
|
17
|
|
|
class Config extends \Phalcon\Config\Config implements ConfigInterface |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* Return the element as an array |
21
|
|
|
*/ |
22
|
24 |
|
public function pathToArray(string $path, ?array $defaultValue = null, ?string $delimiter = null): ?array |
23
|
|
|
{ |
24
|
24 |
|
$ret = $this->path($path, $defaultValue, $delimiter); |
25
|
|
|
|
26
|
24 |
|
if (is_null($ret)) { |
27
|
2 |
|
return null; |
28
|
|
|
} |
29
|
|
|
|
30
|
24 |
|
if ($ret instanceof PhalconConfigInterface) { |
31
|
24 |
|
return $ret->toArray(); |
32
|
|
|
} |
33
|
|
|
|
34
|
1 |
|
return (array)$ret; |
35
|
|
|
} |
36
|
|
|
|
37
|
2 |
|
public function merge($toMerge, bool $append = false): PhalconConfigInterface |
38
|
|
|
{ |
39
|
2 |
|
if (!$append) { |
40
|
2 |
|
return parent::merge($toMerge); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
$source = $this->toArray(); |
44
|
|
|
$this->clear(); |
45
|
|
|
$toMerge = $toMerge instanceof PhalconConfigInterface ? $toMerge->toArray() : $toMerge; |
46
|
|
|
|
47
|
|
|
if (!is_array($toMerge)) { |
48
|
|
|
throw new \Exception('Invalid data type for merge.'); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
$result = $this->internalMergeAppend($source, $toMerge); |
52
|
|
|
$this->init($result); |
53
|
|
|
return $this; |
54
|
|
|
} |
55
|
|
|
|
56
|
24 |
|
final protected function internalMergeAppend(array $source, array $target): array |
57
|
|
|
{ |
58
|
24 |
|
foreach ($target as $key => $value) { |
59
|
24 |
|
if (is_array($value) && isset($source[$key]) && is_array($source[$key])) { |
60
|
24 |
|
$source[$key] = $this->internalMergeAppend($source[$key], $value); |
61
|
|
|
} |
62
|
24 |
|
elseif (is_int($key)) { |
63
|
24 |
|
$source[] = $value; |
64
|
|
|
} |
65
|
|
|
else { |
66
|
24 |
|
$source[$key] = $value; |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|
70
|
24 |
|
return $source; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Return a new model instance from class name |
75
|
|
|
* @todo use DI instead |
76
|
|
|
*/ |
77
|
|
|
public function getModelInstance(string $class): Model |
78
|
|
|
{ |
79
|
|
|
$modelClass = $this->getModelClass($class); |
80
|
|
|
return new $modelClass(); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Return the mapped model class name from $this->models->$class |
85
|
|
|
* @todo use DI instead |
86
|
|
|
*/ |
87
|
3 |
|
public function getModelClass(string $class): string |
88
|
|
|
{ |
89
|
3 |
|
return $this->get('models')->get($class) ?: $class; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Map a new model class |
94
|
|
|
* @todo use DI instead |
95
|
|
|
*/ |
96
|
1 |
|
public function setModelClass(string $class, string $expected): void |
97
|
|
|
{ |
98
|
1 |
|
$this->get('models')->set($class, $expected); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Map a new model class |
103
|
|
|
* @todo use DI instead |
104
|
|
|
*/ |
105
|
1 |
|
public function resetModelClass(string $class): void |
106
|
|
|
{ |
107
|
1 |
|
$this->get('models')->set($class, $class); |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|