| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  * @package: chapi | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * @author:  msiebeneicher | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * @since:   2017-03-20 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | namespace Chapi\Component\DependencyInjection\Loader; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use Doctrine\Instantiator\Exception\InvalidArgumentException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use Symfony\Component\Config\Resource\FileResource; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use Symfony\Component\Yaml\Exception\ParseException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | use Symfony\Component\Yaml\Exception\RuntimeException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | use Symfony\Component\Yaml\Parser as YamlParser; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | use Symfony\Component\Yaml\Yaml; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | class YamChapiConfigLoader extends \Symfony\Component\DependencyInjection\Loader\YamlFileLoader | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |      * @var YamlParser | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     private $yamlParser; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |      * @inheritdoc | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     public function loadProfileParameters($sResource, $sProfile) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |         $path = $this->locator->locate($sResource); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |         $content = $this->loadFile($path); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |         $this->container->addResource(new FileResource($path)); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |         // empty file | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |         if (null === $content) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |             return; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |         // parameters | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |         if (isset($content['profiles']) && isset($content['profiles'][$sProfile]) && isset($content['profiles'][$sProfile]['parameters'])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |             if (!is_array($content['profiles'][$sProfile]['parameters'])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |                 throw new InvalidArgumentException(sprintf('The "parameters" key should contain an array in %s. Check your YAML syntax.', $sResource)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |             foreach ($content['profiles'][$sProfile]['parameters'] as $key => $value) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |                 $this->container->setParameter($key, $value); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |      * @inheritdoc | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |     protected function loadFile($file) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |         if (!class_exists('Symfony\Component\Yaml\Parser')) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |             throw new RuntimeException('Unable to load YAML config files as the Symfony Yaml Component is not installed.'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |         if (!stream_is_local($file)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |             throw new InvalidArgumentException(sprintf('This is not a local file "%s".', $file)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |         if (!file_exists($file)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |             throw new InvalidArgumentException(sprintf('The service file "%s" is not valid.', $file)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |         if (null === $this->yamlParser) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |             $this->yamlParser = new YamlParser(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |         try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |             $configuration = $this->yamlParser->parse(file_get_contents($file), Yaml::PARSE_CONSTANT); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |         } catch (ParseException $e) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |             throw new InvalidArgumentException(sprintf('The file "%s" does not contain valid YAML.', $file), 0, $e); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |         return $this->validate($configuration, $file); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |      * @inheritdoc | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 87 |  |  |      */ | 
            
                                                        
            
                                    
            
            
                | 88 |  |  |     private function validate($content, $file) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                     | 
            
                                                        
            
                                    
            
            
                | 89 |  |  |     { | 
            
                                                        
            
                                    
            
            
                | 90 |  |  |         if (null === $content) { | 
            
                                                        
            
                                    
            
            
                | 91 |  |  |             return $content; | 
            
                                                        
            
                                    
            
            
                | 92 |  |  |         } | 
            
                                                        
            
                                    
            
            
                | 93 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 94 |  |  |         if (!is_array($content)) { | 
            
                                                        
            
                                    
            
            
                | 95 |  |  |             throw new InvalidArgumentException(sprintf('The service file "%s" is not valid. It should contain an array. Check your YAML syntax.', $file)); | 
            
                                                        
            
                                    
            
            
                | 96 |  |  |         } | 
            
                                                        
            
                                    
            
            
                | 97 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 98 |  |  |         foreach ($content as $namespace => $data) { | 
            
                                                        
            
                                    
            
            
                | 99 |  |  |             if (in_array($namespace, array('profiles'))) { | 
            
                                                        
            
                                    
            
            
                | 100 |  |  |                 continue; | 
            
                                                        
            
                                    
            
            
                | 101 |  |  |             } | 
            
                                                        
            
                                    
            
            
                | 102 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 103 |  |  |             if (!$this->container->hasExtension($namespace)) { | 
            
                                                        
            
                                    
            
            
                | 104 |  |  |                 $extensionNamespaces = array_filter(array_map(function ($ext) { return $ext->getAlias(); }, $this->container->getExtensions())); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                        
            
                                    
            
            
                | 105 |  |  |                 throw new InvalidArgumentException(sprintf( | 
            
                                                        
            
                                    
            
            
                | 106 |  |  |                     'There is no extension able to load the configuration for "%s" (in %s). Looked for namespace "%s", found %s', | 
            
                                                        
            
                                    
            
            
                | 107 |  |  |                     $namespace, | 
            
                                                        
            
                                    
            
            
                | 108 |  |  |                     $file, | 
            
                                                        
            
                                    
            
            
                | 109 |  |  |                     $namespace, | 
            
                                                        
            
                                    
            
            
                | 110 |  |  |                     $extensionNamespaces ? sprintf('"%s"', implode('", "', $extensionNamespaces)) : 'none' | 
            
                                                        
            
                                    
            
            
                | 111 |  |  |                 )); | 
            
                                                        
            
                                    
            
            
                | 112 |  |  |             } | 
            
                                                        
            
                                    
            
            
                | 113 |  |  |         } | 
            
                                                        
            
                                    
            
            
                | 114 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 115 |  |  |         return $content; | 
            
                                                        
            
                                    
            
            
                | 116 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 117 |  |  | } |