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 1.1.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\Provider\Silex; |
20
|
|
|
|
21
|
|
|
use Silex\Application; |
22
|
|
|
use Silex\ServiceProviderInterface; |
23
|
|
|
use M1\Vars\Vars; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* The Silex service provider for Vars |
27
|
|
|
* |
28
|
|
|
* @since 0.1.0 |
29
|
|
|
*/ |
30
|
|
|
class VarsServiceProvider implements ServiceProviderInterface |
31
|
|
|
{ |
32
|
|
|
/** |
33
|
|
|
* The entity to use Vars with |
34
|
|
|
* |
35
|
|
|
* @var null |
36
|
|
|
*/ |
37
|
|
|
private $entity; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* The available option keys |
41
|
|
|
* |
42
|
|
|
* @var array |
43
|
|
|
*/ |
44
|
|
|
private $option_keys = array( |
45
|
|
|
'cache', |
46
|
|
|
'cache_path', |
47
|
|
|
'cache_expire', |
48
|
|
|
'loaders', |
49
|
|
|
'replacements', |
50
|
|
|
'merge_globals', |
51
|
|
|
); |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* The service provider constructor sets the entity to use with vars |
55
|
|
|
* |
56
|
|
|
* @param mixed $entity The entity |
57
|
|
|
*/ |
58
|
4 |
|
public function __construct($entity = null) |
59
|
|
|
{ |
60
|
4 |
|
$this->entity = $entity; |
61
|
4 |
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Registers the service provider, sets the user defined options and returns the vars instance |
65
|
|
|
* |
66
|
|
|
* @param \Silex\Application $app The silex app |
67
|
|
|
*/ |
68
|
|
|
public function register(Application $app) |
69
|
|
|
{ |
70
|
4 |
|
$app['vars'] = function ($app) { |
71
|
4 |
|
return new Vars($this->entity, $this->createOptions($app)); |
72
|
|
|
}; |
73
|
|
|
|
74
|
4 |
|
$app['vars.merge'] = $app->protect(function () use ($app) { |
75
|
1 |
|
static $initialized = false; |
76
|
1 |
|
if ($initialized) { |
77
|
|
|
return; |
78
|
|
|
} |
79
|
1 |
|
$initialized = true; |
80
|
|
|
|
81
|
1 |
|
foreach ($app['vars']->getGlobals() as $key => $value) { |
82
|
1 |
|
$app[$key] = $value; |
83
|
|
|
} |
84
|
|
|
|
85
|
1 |
|
foreach ($app['vars']->toDots(false) as $key => $value) { |
86
|
1 |
|
$app['vars.'.$key] = $value; |
87
|
|
|
} |
88
|
4 |
|
}); |
89
|
4 |
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Creates the defined options into a way that Vars can use |
93
|
|
|
* |
94
|
|
|
* @param \Silex\Application $app The silex app |
95
|
|
|
* |
96
|
|
|
* @return array The created options |
97
|
|
|
*/ |
98
|
4 |
|
private function createOptions($app) |
99
|
|
|
{ |
100
|
4 |
|
$options = array(); |
101
|
|
|
|
102
|
4 |
|
if (isset($app['vars.path'])) { |
103
|
1 |
|
$options['path'] = $app['vars.path']; |
104
|
|
|
} |
105
|
|
|
|
106
|
4 |
|
if (isset($app['vars.options'])) { |
107
|
3 |
|
$options = $this->createKeyedOptions($options, $app['vars.options']); |
108
|
|
|
} |
109
|
|
|
|
110
|
4 |
|
if (!isset($options['merge_globals']) || is_null($options['merge_globals'])) { |
111
|
4 |
|
$options['merge_globals'] = false; |
112
|
|
|
} |
113
|
|
|
|
114
|
4 |
|
if (isset($app['debug']) && $app['debug']) { |
115
|
1 |
|
$options['cache'] = false; |
116
|
|
|
} |
117
|
|
|
|
118
|
4 |
|
return $options; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Registers the service provider, sets the user defined options and returns the vars instance |
123
|
|
|
* |
124
|
|
|
* @param array $options The already parsed options |
125
|
|
|
* @param array $vars_options The options defined in the Silex app |
126
|
|
|
* |
127
|
|
|
* @return array The keyed options |
128
|
|
|
*/ |
129
|
3 |
|
private function createKeyedOptions($options, $vars_options) |
130
|
|
|
{ |
131
|
3 |
|
foreach ($this->option_keys as $option) { |
132
|
3 |
|
$options[$option] = (isset($vars_options[$option])) ? $vars_options[$option] : null; |
133
|
|
|
} |
134
|
|
|
|
135
|
3 |
|
return $options; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* The silex service provider boot function |
140
|
|
|
* |
141
|
|
|
* @param \Silex\Application $app The silex app |
142
|
|
|
* |
143
|
|
|
* @codeCoverageIgnore |
144
|
|
|
*/ |
145
|
|
|
public function boot(Application $app) |
146
|
|
|
{ |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
|