55use MattJanssen \ApiResponseBundle \Annotation \ApiResponse ;
66use MattJanssen \ApiResponseBundle \Model \ApiConfig ;
77use MattJanssen \ApiResponseBundle \Model \ApiConfigInterface ;
8+ use MattJanssen \ApiResponseBundle \Model \ApiPathConfig ;
89use Symfony \Component \HttpFoundation \Request ;
910
1011/**
@@ -26,7 +27,7 @@ class ApiConfigCompiler
2627 *
2728 * Each relative URL path has its own CORS configuration settings in this array.
2829 *
29- * @var ApiConfig []
30+ * @var ApiPathConfig []
3031 */
3132 private $ pathConfigs ;
3233
@@ -43,8 +44,8 @@ public function __construct(
4344 $ defaultConfig = $ this ->generateApiConfig ($ defaultConfigArray );
4445
4546 $ pathConfigs = [];
46- foreach ($ pathConfigArrays as $ path => $ configArray ) {
47- $ pathConfigs [$ path ] = $ this ->generateApiConfig ($ configArray );
47+ foreach ($ pathConfigArrays as $ configArray ) {
48+ $ pathConfigs [] = $ this ->generateApiPathConfig ($ configArray );
4849 }
4950
5051 $ this ->defaultConfig = $ defaultConfig ;
@@ -74,7 +75,8 @@ public function compileApiConfig(Request $request)
7475
7576 // Try to match the request origin to a path in the config.yml.
7677 $ originPath = $ request ->getPathInfo ();
77- foreach ($ this ->pathConfigs as $ pathRegex => $ pathConfig ) {
78+ foreach ($ this ->pathConfigs as $ pathConfig ) {
79+ $ pathRegex = $ pathConfig ->getPattern ();
7880 if (!preg_match ('# ' . str_replace ('# ' , '\# ' , $ pathRegex ) . '# ' , $ originPath )) {
7981 // No path match.
8082 continue ;
@@ -113,8 +115,14 @@ public function compileApiConfig(Request $request)
113115 * @param ApiConfig $compiledConfig
114116 * @param ApiConfigInterface $configToMerge
115117 */
116- private function mergeConfig ($ compiledConfig , $ configToMerge )
118+ private function mergeConfig (ApiConfig $ compiledConfig , ApiConfigInterface $ configToMerge )
117119 {
120+ if (null !== $ configToMerge ->getSerializer ()) {
121+ $ compiledConfig ->setSerializer ($ configToMerge ->getSerializer ());
122+ }
123+ if (null !== $ configToMerge ->getGroups ()) {
124+ $ compiledConfig ->setGroups ($ configToMerge ->getGroups ());
125+ }
118126 if (null !== $ configToMerge ->getCorsAllowOriginRegex ()) {
119127 $ compiledConfig ->setCorsAllowOriginRegex ($ configToMerge ->getCorsAllowOriginRegex ());
120128 }
@@ -132,9 +140,39 @@ private function mergeConfig($compiledConfig, $configToMerge)
132140 * @return ApiConfig $this
133141 */
134142 private function generateApiConfig (array $ configArray )
143+ {
144+ $ apiConfig = new ApiConfig ();
145+
146+ $ this ->applyApiConfig ($ configArray , $ apiConfig );
147+
148+ return $ apiConfig ;
149+ }
150+
151+ /**
152+ * @param array $configArray
153+ *
154+ * @return ApiConfig $this
155+ */
156+ private function generateApiPathConfig (array $ configArray )
157+ {
158+ $ apiPathConfig = new ApiPathConfig ();
159+
160+ $ this ->applyApiConfig ($ configArray , $ apiPathConfig );
161+
162+ // @TODO Use PHP 7 null coalescing operator.
163+ $ apiPathConfig ->setPattern (isset ($ configArray ['pattern ' ]) ? $ configArray ['pattern ' ] : null );
164+
165+ return $ apiPathConfig ;
166+ }
167+
168+ /**
169+ * @param array $configArray
170+ * @param ApiConfig $apiConfig
171+ */
172+ private function applyApiConfig (array $ configArray , ApiConfig $ apiConfig )
135173 {
136174 // @TODO Use PHP 7 null coalescing operator.
137- return ( new ApiConfig ())
175+ $ apiConfig
138176 ->setSerializer (isset ($ configArray ['serializer ' ]) ? $ configArray ['serializer ' ] : null )
139177 ->setGroups (isset ($ configArray ['serialize_groups ' ]) ? $ configArray ['serialize_groups ' ] : null )
140178 ->setCorsAllowHeaders (isset ($ configArray ['cors_allow_headers ' ]) ? $ configArray ['cors_allow_headers ' ] : null )
0 commit comments