Skip to content

Support Flex directory structure for YAML and XML configs #1683

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 7, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion features/main/configurable.feature
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ Feature: Configurable resource CRUD
}
"""

@dropSchema
Scenario: Retrieve the ConfigDummy resource
When I send a "GET" request to "/fileconfigdummies/1"
Then the response status code should be 200
Expand All @@ -61,3 +60,20 @@ Feature: Configurable resource CRUD
"foo": "Foo"
}
"""

@dropSchema
Scenario: Entities can be configured using a Flex-like directory structure
When I send a "GET" request to "/flex_configs"
Then the response status code should be 200
And the response should be in JSON
And the header "Content-Type" should be equal to "application/ld+json; charset=utf-8"
And the JSON should be equal to:
"""
{
"@context": "/contexts/FlexConfig",
"@id": "/flex_configs",
"@type": "hydra:Collection",
"hydra:member": [],
"hydra:totalItems": 0
}
"""
Original file line number Diff line number Diff line change
Expand Up @@ -221,9 +221,9 @@ private function getBundlesResourcesPaths(ContainerBuilder $container): array
$paths = [];
$dirname = $bundle['path'];
foreach (['.yaml', '.yml', '.xml', ''] as $extension) {
$paths[] = $dirname.'/Resources/config/api_resources'.$extension;
$paths[] = "$dirname/Resources/config/api_resources$extension";
}
$paths[] = $dirname.'/Entity';
$paths[] = "$dirname/Entity";

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

private function getResourcesToWatch(ContainerBuilder $container, array $resourcesPaths): array
{
// Flex structure
$projectDir = $container->getParameter('kernel.project_dir');
if (is_dir($dir = "$projectDir/config/api_platform")) {
$resourcesPaths[] = $dir;
}

$paths = array_unique(array_merge($resourcesPaths, $this->getBundlesResourcesPaths($container)));
$resources = ['yml' => [], 'xml' => [], 'dir' => []];

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

$resources['dir'][] = $path;
$container->addResource(new DirectoryResource($path, '/\.(xml|ya?ml|php)$/'));
} elseif ($container->fileExists($path, false)) {

continue;
}

if ($container->fileExists($path, false)) {
if (!preg_match('/\.(xml|ya?ml)$/', $path, $matches)) {
throw new RuntimeException(sprintf('Unsupported mapping type in "%s", supported types are XML & Yaml.', $path));
}

$resources['yaml' === $matches[1] ? 'yml' : $matches[1]][] = $path;
} else {
throw new RuntimeException(sprintf('Could not open file or directory "%s".', $path));

continue;
}

throw new RuntimeException(sprintf('Could not open file or directory "%s".', $path));
}

$container->setParameter('api_platform.resource_class_directories', $resources['dir']);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -525,7 +525,9 @@ private function getPartialContainerBuilderProphecy($test = false)
$containerBuilderProphecy->setAlias($alias, $service)->shouldBeCalled();
}

$containerBuilderProphecy->getParameter('kernel.project_dir')->willReturn(__DIR__);
$containerBuilderProphecy->getParameter('kernel.debug')->willReturn(false);

$containerBuilderProphecy->getDefinition('api_platform.http_cache.purger.varnish')->willReturn(new Definition());

return $containerBuilderProphecy;
Expand Down
36 changes: 36 additions & 0 deletions tests/Fixtures/TestBundle/Entity/FlexConfig.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
* This entity is configure in tests/Fixtures/app/config/api_platform/flex.yaml.
*
* @ORM\Entity
*/
class FlexConfig
{
/**
* @ORM\Column(type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;

public function getId()
{
return $this->id;
}
}
1 change: 1 addition & 0 deletions tests/Fixtures/app/AppKernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ protected function configureRoutes(RouteCollectionBuilder $routes)
protected function configureContainer(ContainerBuilder $c, LoaderInterface $loader)
{
$environment = $this->getEnvironment();
$c->setParameter('kernel.project_dir', __DIR__);

// patch for behat not supporting %env(APP_ENV)% in older versions
if (($appEnv = $_SERVER['APP_ENV'] ?? 'test') && $appEnv !== $environment) {
Expand Down
1 change: 1 addition & 0 deletions tests/Fixtures/app/config/api_platform/flex.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\FlexConfig: ~