| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace As3\Bundle\ModlrBundle\DependencyInjection\ServiceLoader; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use As3\Bundle\ModlrBundle\DependencyInjection\Utility; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Symfony\Component\DependencyInjection\ContainerBuilder; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Symfony\Component\DependencyInjection\Definition; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Symfony\Component\DependencyInjection\Reference; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * Loads RESTful API services. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  * @author  Jacob Bare <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | class Rest implements ServiceLoaderInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |      * Creates the jsonapi.org Adapter service definition. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |      * @param   string              $configName | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |      * @param   ContainerBuilder    $container | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |      * @return  Definition | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     private function createJsonApiAdapter($configName, ContainerBuilder $container) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |         // Serializer | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |         $serializerName = Utility::getAliasedName('api.serializer'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |         $definition = new Definition( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |             Utility::getLibraryClass('Api\JsonApiOrg\Serializer') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |         $definition->setPublic(false); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |         $container->setDefinition($serializerName, $definition); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |         // Normalizer | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |         $normalizerName = Utility::getAliasedName('api.normalizer'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |         $definition = new Definition( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |             Utility::getLibraryClass('Api\JsonApiOrg\Normalizer') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |         $definition->setPublic(false); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |         $container->setDefinition($normalizerName, $definition); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |         // Adapter | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |         $definition = new Definition( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |             Utility::getLibraryClass('Api\JsonApiOrg\Adapter'), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |             [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |                 new Reference($serializerName), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |                 new Reference($normalizerName), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |                 new Reference(Utility::getAliasedName('store')), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |                 new Reference($configName), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |             ] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |         $definition->setPublic(true); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |         return $definition; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |      * {@inheritdoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |     public function load(array $config, ContainerBuilder $container) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |         $adapterName = Utility::getAliasedName('api.adapter'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |         $configName  = Utility::getAliasedName('rest.configuration'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |         $this->loadConfiguration($configName, $config['rest'], $container); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |         $this->loadAdapter($adapterName, $configName, $config['adapter'], $container); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |         $this->loadKernel($adapterName, $configName, $config['rest']['debug'], $container); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |         return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |      * Loads the Adapter service based on the adapter config. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |      * @param   string              $adapterName | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |      * @param   string              $configName | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |      * @param   array               $adapterConfig | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |      * @param   ContainerBuilder    $container | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |      * @return  self | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 78 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 79 |  |  |     private function loadAdapter($adapterName, $configName, array $adapterConfig, ContainerBuilder $container) | 
            
                                                                        
                            
            
                                    
            
            
                | 80 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 81 |  |  |         if (isset($adapterConfig['service'])) { | 
            
                                                                        
                            
            
                                    
            
            
                | 82 |  |  |             // Custom adapter service. | 
            
                                                                        
                            
            
                                    
            
            
                | 83 |  |  |             $container->setAlias($adapterName, Utility::cleanServiceName($adapterConfig['service'])); | 
            
                                                                        
                            
            
                                    
            
            
                | 84 |  |  |             return $this; | 
            
                                                                        
                            
            
                                    
            
            
                | 85 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 86 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 87 |  |  |         // Built-In Adapter | 
            
                                                                        
                            
            
                                    
            
            
                | 88 |  |  |         switch ($adapterConfig['type']) { | 
            
                                                                        
                            
            
                                    
            
            
                | 89 |  |  |             case 'jsonapiorg': | 
            
                                                                        
                            
            
                                    
            
            
                | 90 |  |  |                 $definition = $this->createJsonApiAdapter($configName, $container); | 
            
                                                                        
                            
            
                                    
            
            
                | 91 |  |  |                 break; | 
            
                                                                        
                            
            
                                    
            
            
                | 92 |  |  |             default: | 
            
                                                                        
                            
            
                                    
            
            
                | 93 |  |  |                 throw new \RuntimeException(sprintf('The adapter type "%s" is currently not supported.', $adapterConfig['type'])); | 
            
                                                                        
                            
            
                                    
            
            
                | 94 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 95 |  |  |         $container->setDefinition($adapterName, $definition); | 
            
                                                                        
                            
            
                                    
            
            
                | 96 |  |  |         return $this; | 
            
                                                                        
                            
            
                                    
            
            
                | 97 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 98 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |      * Loads the Rest config service based on the config. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |      * @param   string              $name | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |      * @param   array               $restConfig | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |      * @param   ContainerBuilder    $container | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |      * @return  self | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |     private function loadConfiguration($name, array $restConfig, ContainerBuilder $container) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |         $definition = new Definition( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |             Utility::getLibraryClass('Rest\RestConfiguration'), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |             [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |                 new Reference(Utility::getAliasedName('util.validator')), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |             ] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |         $definition->setPublic(false); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |         $endpoint = $restConfig['root_endpoint']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |         $definition->addMethodCall('setRootEndpoint', [$endpoint]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |         $container->setDefinition($name, $definition); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |         $container->setParameter(Utility::getAliasedName('rest.root_endpoint'), $endpoint); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |         return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |      * Loads the Rest Kernel service based on the config. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |      * @param   string              $adapterName | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |      * @param   string              $configName | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |      * @param   bool                $debug | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |      * @param   ContainerBuilder    $container | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |      * @return  self | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |     private function loadKernel($adapterName, $configName, $debug, ContainerBuilder $container) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |         $definition = new Definition( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |             Utility::getLibraryClass('Rest\RestKernel'), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |             [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |                 new Reference($adapterName), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |                 new Reference($configName), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |             ] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |         $definition->addMethodCall('enableDebug', [$debug]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |         $container->setDefinition(Utility::getAliasedName('rest.kernel'), $definition); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 147 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 148 |  |  |  |