|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file is part of the m1\vars library |
|
5
|
|
|
* |
|
6
|
|
|
* (c) m1 <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
* |
|
11
|
|
|
* @package m1/vars |
|
12
|
|
|
* @version 0.2.0 |
|
13
|
|
|
* @author Miles Croxford <[email protected]> |
|
14
|
|
|
* @copyright Copyright (c) Miles Croxford <[email protected]> |
|
15
|
|
|
* @license http://github.com/m1/vars/blob/master/LICENSE |
|
16
|
|
|
* @link http://github.com/m1/vars/blob/master/README.MD Documentation |
|
17
|
|
|
*/ |
|
18
|
|
|
|
|
19
|
|
|
namespace M1\Vars\Traits; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Vars transformer class for to*() functions |
|
23
|
|
|
* |
|
24
|
|
|
* @since 0.2.0 |
|
25
|
|
|
*/ |
|
26
|
|
|
trait TransformerTrait |
|
27
|
|
|
{ |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Makes it so the content is available in getenv() |
|
31
|
|
|
*/ |
|
32
|
1 |
|
public function toEnv(){ |
|
33
|
1 |
|
$dots = $this->toDots($this->content); |
|
|
|
|
|
|
34
|
|
|
|
|
35
|
1 |
|
foreach ($dots as $dot_k => $dot_v) { |
|
36
|
1 |
|
putenv(sprintf('%s=%s', $dot_k, $dot_v)); |
|
37
|
|
|
|
|
38
|
1 |
|
} |
|
39
|
1 |
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Converts the array into a flat dot notation array |
|
43
|
|
|
* |
|
44
|
|
|
* @return array The dot notation array |
|
45
|
|
|
*/ |
|
46
|
1 |
|
public function toDots() |
|
47
|
|
|
{ |
|
48
|
1 |
|
return $this->dotTransformer($this->content); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Converts the array into a flat dot notation array |
|
53
|
|
|
* |
|
54
|
|
|
* @param array $content The content array |
|
55
|
|
|
* @param string $prefix The prefix for the key |
|
56
|
|
|
* |
|
57
|
|
|
* @return array The dot notation array |
|
58
|
|
|
*/ |
|
59
|
1 |
|
private function dotTransformer($content, $prefix = '') |
|
60
|
|
|
{ |
|
61
|
1 |
|
$parsed = array(); |
|
62
|
|
|
|
|
63
|
1 |
|
foreach ($content as $arr_k => $arr_v) { |
|
64
|
1 |
|
if (is_array($arr_v)) { |
|
65
|
1 |
|
$parsed = array_merge($parsed, $this->dotTransformer($arr_v, $prefix.$arr_k.".")); |
|
66
|
1 |
|
} else { |
|
67
|
1 |
|
$parsed[$prefix.$arr_k] = $arr_v; |
|
68
|
|
|
} |
|
69
|
1 |
|
} |
|
70
|
|
|
|
|
71
|
1 |
|
return $parsed; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
} |
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: