Skip to content

Commit 3b66e65

Browse files
authored
Merge pull request #1683 from dunglas/flex-yaml-xml
Support Flex directory structure for YAML and XML configs
2 parents 9fd528a + 728bf0d commit 3b66e65

File tree

6 files changed

+74
-6
lines changed

6 files changed

+74
-6
lines changed

features/main/configurable.feature

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ Feature: Configurable resource CRUD
4444
}
4545
"""
4646

47-
@dropSchema
4847
Scenario: Retrieve the ConfigDummy resource
4948
When I send a "GET" request to "/fileconfigdummies/1"
5049
Then the response status code should be 200
@@ -61,3 +60,20 @@ Feature: Configurable resource CRUD
6160
"foo": "Foo"
6261
}
6362
"""
63+
64+
@dropSchema
65+
Scenario: Entities can be configured using a Flex-like directory structure
66+
When I send a "GET" request to "/flex_configs"
67+
Then the response status code should be 200
68+
And the response should be in JSON
69+
And the header "Content-Type" should be equal to "application/ld+json; charset=utf-8"
70+
And the JSON should be equal to:
71+
"""
72+
{
73+
"@context": "/contexts/FlexConfig",
74+
"@id": "/flex_configs",
75+
"@type": "hydra:Collection",
76+
"hydra:member": [],
77+
"hydra:totalItems": 0
78+
}
79+
"""

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

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -221,9 +221,9 @@ private function getBundlesResourcesPaths(ContainerBuilder $container): array
221221
$paths = [];
222222
$dirname = $bundle['path'];
223223
foreach (['.yaml', '.yml', '.xml', ''] as $extension) {
224-
$paths[] = $dirname.'/Resources/config/api_resources'.$extension;
224+
$paths[] = "$dirname/Resources/config/api_resources$extension";
225225
}
226-
$paths[] = $dirname.'/Entity';
226+
$paths[] = "$dirname/Entity";
227227

228228
foreach ($paths as $path) {
229229
if ($container->fileExists($path, false)) {
@@ -237,6 +237,12 @@ private function getBundlesResourcesPaths(ContainerBuilder $container): array
237237

238238
private function getResourcesToWatch(ContainerBuilder $container, array $resourcesPaths): array
239239
{
240+
// Flex structure
241+
$projectDir = $container->getParameter('kernel.project_dir');
242+
if (is_dir($dir = "$projectDir/config/api_platform")) {
243+
$resourcesPaths[] = $dir;
244+
}
245+
240246
$paths = array_unique(array_merge($resourcesPaths, $this->getBundlesResourcesPaths($container)));
241247
$resources = ['yml' => [], 'xml' => [], 'dir' => []];
242248

@@ -248,15 +254,21 @@ private function getResourcesToWatch(ContainerBuilder $container, array $resourc
248254

249255
$resources['dir'][] = $path;
250256
$container->addResource(new DirectoryResource($path, '/\.(xml|ya?ml|php)$/'));
251-
} elseif ($container->fileExists($path, false)) {
257+
258+
continue;
259+
}
260+
261+
if ($container->fileExists($path, false)) {
252262
if (!preg_match('/\.(xml|ya?ml)$/', $path, $matches)) {
253263
throw new RuntimeException(sprintf('Unsupported mapping type in "%s", supported types are XML & Yaml.', $path));
254264
}
255265

256266
$resources['yaml' === $matches[1] ? 'yml' : $matches[1]][] = $path;
257-
} else {
258-
throw new RuntimeException(sprintf('Could not open file or directory "%s".', $path));
267+
268+
continue;
259269
}
270+
271+
throw new RuntimeException(sprintf('Could not open file or directory "%s".', $path));
260272
}
261273

262274
$container->setParameter('api_platform.resource_class_directories', $resources['dir']);

tests/Bridge/Symfony/Bundle/DependencyInjection/ApiPlatformExtensionTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -525,7 +525,9 @@ private function getPartialContainerBuilderProphecy($test = false)
525525
$containerBuilderProphecy->setAlias($alias, $service)->shouldBeCalled();
526526
}
527527

528+
$containerBuilderProphecy->getParameter('kernel.project_dir')->willReturn(__DIR__);
528529
$containerBuilderProphecy->getParameter('kernel.debug')->willReturn(false);
530+
529531
$containerBuilderProphecy->getDefinition('api_platform.http_cache.purger.varnish')->willReturn(new Definition());
530532

531533
return $containerBuilderProphecy;
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the API Platform project.
5+
*
6+
* (c) Kévin Dunglas <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity;
15+
16+
use Doctrine\ORM\Mapping as ORM;
17+
18+
/**
19+
* This entity is configure in tests/Fixtures/app/config/api_platform/flex.yaml.
20+
*
21+
* @ORM\Entity
22+
*/
23+
class FlexConfig
24+
{
25+
/**
26+
* @ORM\Column(type="integer")
27+
* @ORM\Id
28+
* @ORM\GeneratedValue(strategy="AUTO")
29+
*/
30+
private $id;
31+
32+
public function getId()
33+
{
34+
return $this->id;
35+
}
36+
}

tests/Fixtures/app/AppKernel.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ protected function configureRoutes(RouteCollectionBuilder $routes)
7070
protected function configureContainer(ContainerBuilder $c, LoaderInterface $loader)
7171
{
7272
$environment = $this->getEnvironment();
73+
$c->setParameter('kernel.project_dir', __DIR__);
7374

7475
// patch for behat not supporting %env(APP_ENV)% in older versions
7576
if (($appEnv = $_SERVER['APP_ENV'] ?? 'test') && $appEnv !== $environment) {
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\FlexConfig: ~

0 commit comments

Comments
 (0)