| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Junker\Silex\Provider; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Silex\Application; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Silex\ServiceProviderInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Symfony\Component\Config\ConfigCacheInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Symfony\Component\Config\ConfigCacheFactory; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use Symfony\Component\Config\FileLocator; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use Junker\Silex\YamlFileLoader; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | class YamlConfigurationServiceProvider implements ServiceProviderInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |     protected $cacheDirPath; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |     protected $configFilePath; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |     protected $configCacheFactory; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 17 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 18 |  |  |     public function __construct($configFilePath, $options = null) | 
            
                                                                        
                            
            
                                    
            
            
                | 19 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 20 |  |  |         if (is_array($options)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 21 |  |  |             if (isset($options['cache_dir'])) { | 
            
                                                                        
                            
            
                                    
            
            
                | 22 |  |  |                 $this->cacheDirPath = $options['cache_dir']; | 
            
                                                                        
                            
            
                                    
            
            
                | 23 |  |  |             } | 
            
                                                                        
                            
            
                                    
            
            
                | 24 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 25 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 26 |  |  |         $this->configFilePath = $configFilePath; | 
            
                                                                        
                            
            
                                    
            
            
                | 27 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     public function register(Application $app) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |         $app['config'] = $app->share(function(Application $app) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |             if ($this->cacheDirPath) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |                 $cache = $this->getConfigCacheFactory($app['debug'])->cache($this->cacheDirPath.'/config.cache.php', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |                     function(ConfigCacheInterface $cache) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |                         $config = $this->loadConfig(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |                         $content = sprintf('<?php use Junker\Silex\Config; $c = new Config(%s);', var_export($config->data, true)).PHP_EOL; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |                         $content .= 'return $c;'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |                         $cache->write($content, $config->getResources()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |                     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |                 ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |                 $config = include $cache->getPath(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |             } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |                 $config = $this->loadConfig(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |             return $config->data; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |         }); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |     public function boot(Application $app) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |     private function getConfigCacheFactory($debug = false) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |         if ($this->configCacheFactory === null) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |             $this->configCacheFactory = new ConfigCacheFactory($debug); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |         return $this->configCacheFactory; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |     protected function loadConfig() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |         $loader = new YamlFileLoader(new FileLocator(dirname($this->configFilePath))); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |         $config = $loader->load($this->configFilePath); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |         return $config; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 74 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 75 |  |  |  |