Skip to content

Commit f002cf5

Browse files
soyukadunglas
authored andcommitted
Configuration filename to api_resources and allow single file declaration
1 parent 0acadce commit f002cf5

File tree

14 files changed

+104
-26
lines changed

14 files changed

+104
-26
lines changed

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,11 @@
3838
"phpdocumentor/reflection-docblock": "^3.0",
3939
"symfony/cache": "^3.1@dev",
4040
"symfony/dependency-injection": "^2.8 || ^3.0",
41-
"symfony/finder": "^2.3",
4241
"symfony/framework-bundle": "^3.1",
4342
"symfony/phpunit-bridge": "^3.1",
4443
"symfony/security": "^2.7 || ^3.0",
45-
"symfony/validator": "^2.5 || ^3.0"
44+
"symfony/validator": "^2.5 || ^3.0",
45+
"symfony/finder": "^2.8 || ^3.1"
4646
},
4747
"suggest": {
4848
"friendsofsymfony/user-bundle": "To use the FOSUserBundle bridge.",

features/configurable.feature

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,22 @@ Feature: Configurable resource CRUD
2626
}
2727
"""
2828

29+
Scenario: Get a single file configured resource
30+
When I send a "GET" request to "/single_file_configs"
31+
Then the response status code should be 200
32+
And the response should be in JSON
33+
And the header "Content-Type" should be equal to "application/ld+json"
34+
And the JSON should be equal to:
35+
"""
36+
{
37+
"@context": "/contexts/single_file_config",
38+
"@id": "/single_file_configs",
39+
"@type": "hydra:Collection",
40+
"hydra:member": [],
41+
"hydra:totalItems": 0
42+
}
43+
"""
44+
2945
@dropSchema
3046
Scenario: Retrieve the ConfigDummy resource
3147
When I send a "GET" request to "/fileconfigdummies/1"

src/Bridge/Symfony/Bundle/DependencyInjection/ApiPlatformExtension.php

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Symfony\Component\DependencyInjection\ContainerBuilder;
1717
use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface;
1818
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
19+
use Symfony\Component\Finder\Finder;
1920
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
2021

2122
/**
@@ -148,28 +149,31 @@ private function registerAnnotationLoaders(ContainerBuilder $container)
148149
*/
149150
private function registerFileLoaders(ContainerBuilder $container)
150151
{
151-
$paths = [];
152+
$prefix = DIRECTORY_SEPARATOR.'Resources'.DIRECTORY_SEPARATOR.'config'.DIRECTORY_SEPARATOR;
153+
$yamlResources = [];
154+
$xmlResources = [];
155+
152156
foreach ($container->getParameter('kernel.bundles') as $bundle) {
153157
$reflectionClass = new \ReflectionClass($bundle);
154-
$bundleDirectory = dirname($reflectionClass->getFileName());
155-
$glob = $bundleDirectory.DIRECTORY_SEPARATOR.'Resources'.DIRECTORY_SEPARATOR.'config'.DIRECTORY_SEPARATOR.'resources.{xml,yml}';
158+
$configDirectory = dirname($reflectionClass->getFileName()).$prefix;
159+
$yamlResources = array_merge($yamlResources, glob($configDirectory.'api_resources.{yml,yaml}'));
156160

157-
$paths = array_merge($paths, glob($glob, GLOB_BRACE | GLOB_NOSORT));
158-
}
161+
foreach (Finder::create()->files()->in($configDirectory)->path('api_resources')->name('*.{yml,yaml}') as $file) {
162+
$yamlResources[] = $file->getRealPath();
163+
}
159164

160-
$yamlPaths = array_filter($paths, function ($v) {
161-
return preg_match('/\.yml$/', $v);
162-
});
165+
$xmlResources = array_merge($xmlResources, glob($configDirectory.'api_resources.xml'));
163166

164-
$xmlPaths = array_filter($paths, function ($v) {
165-
return preg_match('/\.xml$/', $v);
166-
});
167+
foreach (Finder::create()->files()->in($configDirectory)->path('api_resources')->name('*.xml') as $file) {
168+
$xmlResources[] = $file->getRealPath();
169+
}
170+
}
167171

168-
$container->getDefinition('api_platform.metadata.resource.name_collection_factory.yaml')->replaceArgument(0, $yamlPaths);
169-
$container->getDefinition('api_platform.metadata.resource.metadata_factory.yaml')->replaceArgument(0, $yamlPaths);
172+
$container->getDefinition('api_platform.metadata.resource.name_collection_factory.yaml')->replaceArgument(0, $yamlResources);
173+
$container->getDefinition('api_platform.metadata.resource.metadata_factory.yaml')->replaceArgument(0, $yamlResources);
170174

171-
$container->getDefinition('api_platform.metadata.resource.name_collection_factory.xml')->replaceArgument(0, $xmlPaths);
172-
$container->getDefinition('api_platform.metadata.resource.metadata_factory.xml')->replaceArgument(0, $xmlPaths);
175+
$container->getDefinition('api_platform.metadata.resource.name_collection_factory.xml')->replaceArgument(0, $xmlResources);
176+
$container->getDefinition('api_platform.metadata.resource.metadata_factory.xml')->replaceArgument(0, $xmlResources);
173177
}
174178

175179
/**
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)