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.3.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
|
|
|
'variables' |
50
|
|
|
); |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* The service provider constructor sets the entity to use with vars |
54
|
|
|
* |
55
|
|
|
* @param mixed $entity The entity |
56
|
|
|
*/ |
57
|
3 |
|
public function __construct($entity = null) |
58
|
|
|
{ |
59
|
3 |
|
$this->entity = $entity; |
60
|
3 |
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Registers the service provider, sets the user defined options and returns the vars instance |
64
|
|
|
* |
65
|
|
|
* @param \Silex\Application $app The silex app |
66
|
|
|
*/ |
67
|
|
|
public function register(Application $app) |
68
|
|
|
{ |
69
|
3 |
|
$app['vars'] = function ($app) { |
70
|
3 |
|
return new Vars($this->entity, $this->createOptions($app)); |
71
|
|
|
}; |
72
|
3 |
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Creates the defined options into a way that Vars can use |
76
|
|
|
* |
77
|
|
|
* @param \Silex\Application $app The silex app |
78
|
|
|
* |
79
|
|
|
* @return array The created options |
80
|
|
|
*/ |
81
|
3 |
|
private function createOptions($app) |
82
|
|
|
{ |
83
|
3 |
|
$options = array(); |
84
|
|
|
|
85
|
3 |
|
if (isset($app['vars.path'])) { |
86
|
1 |
|
$options['base_path'] = $app['vars.path']; |
87
|
1 |
|
} |
88
|
|
|
|
89
|
3 |
|
if (isset($app['vars.options'])) { |
90
|
2 |
|
$options = $this->createKeyedOptions($options, $app['vars.options']); |
91
|
|
|
|
92
|
2 |
|
} |
93
|
|
|
|
94
|
3 |
|
if (isset($app['debug']) && $app['debug']) { |
95
|
1 |
|
$options['cache'] = false; |
96
|
1 |
|
} |
97
|
|
|
|
98
|
3 |
|
return $options; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Registers the service provider, sets the user defined options and returns the vars instance |
103
|
|
|
* |
104
|
|
|
* @param array $options The already parsed options |
105
|
|
|
* @param array $vars_options The options defined in the Silex app |
106
|
|
|
* |
107
|
|
|
* @return array The keyed options |
108
|
|
|
*/ |
109
|
2 |
|
private function createKeyedOptions($options, $vars_options) |
110
|
|
|
{ |
111
|
2 |
|
foreach ($this->option_keys as $option) { |
112
|
2 |
|
$options[$option] = (isset($vars_options[$option])) ? $vars_options[$option] : null; |
113
|
2 |
|
} |
114
|
|
|
|
115
|
2 |
|
return $options; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* The silex service provider boot function |
120
|
|
|
* |
121
|
|
|
* @param \Silex\Application $app The silex app |
122
|
|
|
* |
123
|
|
|
* @codeCoverageIgnore |
124
|
|
|
*/ |
125
|
|
|
public function boot(Application $app) |
126
|
|
|
{ |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|