Skip to content

Commit 0514da5

Browse files
author
Matt Janssen
committed
Fixed path config pattern
1 parent c9111af commit 0514da5

File tree

2 files changed

+44
-6
lines changed

2 files changed

+44
-6
lines changed

src/Compiler/ApiConfigCompiler.php

Lines changed: 43 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use MattJanssen\ApiResponseBundle\Annotation\ApiResponse;
66
use MattJanssen\ApiResponseBundle\Model\ApiConfig;
77
use MattJanssen\ApiResponseBundle\Model\ApiConfigInterface;
8+
use MattJanssen\ApiResponseBundle\Model\ApiPathConfig;
89
use 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;
@@ -75,7 +76,7 @@ public function compileApiConfig(Request $request)
7576
// Try to match the request origin to a path in the config.yml.
7677
$originPath = $request->getPathInfo();
7778
foreach ($this->pathConfigs as $pathConfig) {
78-
$pathRegex = $pathConfig['pattern'];
79+
$pathRegex = $pathConfig->getPattern();
7980
if (!preg_match('#' . str_replace('#', '\#', $pathRegex) . '#', $originPath)) {
8081
// No path match.
8182
continue;
@@ -114,8 +115,14 @@ public function compileApiConfig(Request $request)
114115
* @param ApiConfig $compiledConfig
115116
* @param ApiConfigInterface $configToMerge
116117
*/
117-
private function mergeConfig($compiledConfig, $configToMerge)
118+
private function mergeConfig(ApiConfig $compiledConfig, ApiConfigInterface $configToMerge)
118119
{
120+
if (null !== $configToMerge->getSerializer()) {
121+
$compiledConfig->setSerializer($configToMerge->getSerializer());
122+
}
123+
if (null !== $configToMerge->getGroups()) {
124+
$compiledConfig->setGroups($configToMerge->getGroups());
125+
}
119126
if (null !== $configToMerge->getCorsAllowOriginRegex()) {
120127
$compiledConfig->setCorsAllowOriginRegex($configToMerge->getCorsAllowOriginRegex());
121128
}
@@ -133,9 +140,39 @@ private function mergeConfig($compiledConfig, $configToMerge)
133140
* @return ApiConfig $this
134141
*/
135142
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)
136173
{
137174
// @TODO Use PHP 7 null coalescing operator.
138-
return (new ApiConfig())
175+
$apiConfig
139176
->setSerializer(isset($configArray['serializer']) ? $configArray['serializer'] : null)
140177
->setGroups(isset($configArray['serialize_groups']) ? $configArray['serialize_groups'] : null)
141178
->setCorsAllowHeaders(isset($configArray['cors_allow_headers']) ? $configArray['cors_allow_headers'] : null)

src/DependencyInjection/Configuration.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ public function getConfigTreeBuilder()
4242
->useAttributeAsKey('name')
4343
->prototype('array')
4444
->children()
45+
->scalarNode('pattern')->end()
4546
);
4647

4748
return $treeBuilder;

0 commit comments

Comments
 (0)