1 | <?php |
||
29 | class StashServiceProvider implements ServiceProviderInterface |
||
30 | { |
||
31 | /** |
||
32 | * The session handler before this service provider checks to see if it should set a session handler |
||
33 | * |
||
34 | * @var null |
||
35 | */ |
||
36 | private $session_handler; |
||
37 | |||
38 | /** |
||
39 | * Registers the service provider, sets the user defined options and sets the session handler |
||
40 | * |
||
41 | * @param \Silex\Application $app The silex app |
||
42 | */ |
||
43 | 57 | public function register(Application $app) |
|
44 | { |
||
45 | 57 | $app['pool.default_options'] = array( |
|
46 | 57 | 'driver' => 'Ephemeral', |
|
47 | 57 | 'session' => false, |
|
48 | 57 | 'logger' => false, |
|
49 | ); |
||
50 | |||
51 | $app['pools.options.init'] = $app->protect(function () use ($app) { |
||
52 | 54 | static $init = false; |
|
53 | |||
54 | 54 | if ($init) { |
|
55 | 9 | return; |
|
56 | } |
||
57 | |||
58 | 54 | $init = true; |
|
59 | |||
60 | 54 | $app['pools.options'] = self::createOptions($app); |
|
61 | 57 | }); |
|
62 | |||
63 | $app['pools'] = $app->share(function () use ($app) { |
||
64 | 51 | $app['pools.options.init'](); |
|
65 | |||
66 | 51 | return self::createPools($app); |
|
67 | |||
68 | 57 | }); |
|
69 | |||
70 | 57 | $this->session_handler = isset($app['session.storage.handler']) ? $app['session.storage.handler'] : array(); |
|
71 | |||
72 | $app['session.storage.handler'] = $app->share(function () use ($app) { |
||
73 | 12 | $app['pools.options.init'](); |
|
74 | |||
75 | 12 | foreach ($app['pools.options'] as $name => $options) { |
|
76 | 12 | if (isset($options['session']) && $options['session']) { |
|
77 | 9 | $session_options = (is_array($options['session'])) ? $options['session'] : array(); |
|
78 | |||
79 | 9 | return new StashSessionHandler($app['pools'][$name], $session_options); |
|
80 | } |
||
81 | 3 | } |
|
82 | |||
83 | 3 | return $this->session_handler; |
|
84 | 57 | }); |
|
85 | |||
86 | $app['pool'] = $app->share(function ($app) { |
||
87 | 24 | $pools = $app['pools']; |
|
88 | |||
89 | 21 | return $pools[$app['pools.default']]; |
|
90 | 57 | }); |
|
91 | 57 | } |
|
92 | |||
93 | /** |
||
94 | * The silex service provider boot function |
||
95 | * |
||
96 | * @param \Silex\Application $app The silex app |
||
97 | * |
||
98 | * @codeCoverageIgnore |
||
99 | */ |
||
100 | public function boot(Application $app) |
||
103 | |||
104 | /** |
||
105 | * Creates and parses the options from the user options |
||
106 | * |
||
107 | * @param \Silex\Application $app The silex app |
||
108 | * |
||
109 | * @return array $options The parsed options |
||
110 | */ |
||
111 | 54 | private static function createOptions($app) |
|
132 | |||
133 | /** |
||
134 | * Creates the pools with user defined options |
||
135 | * |
||
136 | * @param \Silex\Application $app The silex app |
||
137 | * |
||
138 | * @return \Pimple $pools The pools in the pimple container |
||
139 | */ |
||
140 | 51 | private static function createPools($app) |
|
141 | { |
||
142 | 51 | $pools = new \Pimple(); |
|
143 | |||
144 | 51 | foreach ($app['pools.options'] as $name => $options) { |
|
145 | 51 | $pool = new \Stash\Pool(self::fetchDriver($options)); |
|
146 | 48 | $pool->setLogger(self::fetchLogger($app, $options)); |
|
147 | |||
148 | 45 | $pools[$name] = $pools->share(function () use ($pool) { |
|
149 | 42 | return $pool; |
|
150 | 45 | }); |
|
151 | |||
152 | 45 | } |
|
153 | |||
154 | 45 | return $pools; |
|
155 | } |
||
156 | |||
157 | /** |
||
158 | * Fetches the driver if exists, else throws exception |
||
159 | * |
||
160 | * @param array $options The options for Stash |
||
161 | * |
||
162 | * @throws \InvalidArgumentException If the driver class can not be found |
||
163 | * |
||
164 | * @return mixed Instance of the driver class |
||
165 | */ |
||
166 | 51 | private static function fetchDriver($options) |
|
181 | |||
182 | /** |
||
183 | * Fetches the driver if exists, else throws exception |
||
184 | * |
||
185 | * @param \Silex\Application $app The silex app |
||
186 | * @param array $options The options for Stash |
||
187 | * |
||
188 | * @throws \InvalidArgumentException If the logger service does not exist |
||
189 | * |
||
190 | * @return mixed The logger service |
||
191 | */ |
||
192 | 48 | private static function fetchLogger($app, $options) |
|
208 | } |
||
209 |