Skip to content
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
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@
"phpdocumentor/reflection-docblock": "^3.0",
"symfony/cache": "^3.1@dev",
"symfony/dependency-injection": "^2.8 || ^3.0",
"symfony/finder": "^2.3",
"symfony/framework-bundle": "^3.1",
"symfony/phpunit-bridge": "^3.1",
"symfony/security": "^2.7 || ^3.0",
"symfony/validator": "^2.5 || ^3.0"
"symfony/validator": "^2.5 || ^3.0",
"symfony/finder": "^2.8 || ^3.1"
},
"suggest": {
"friendsofsymfony/user-bundle": "To use the FOSUserBundle bridge.",
Expand Down
16 changes: 16 additions & 0 deletions features/configurable.feature
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,22 @@ Feature: Configurable resource CRUD
}
"""

Scenario: Get a single file configured resource
When I send a "GET" request to "/single_file_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"
And the JSON should be equal to:
"""
{
"@context": "/contexts/single_file_config",
"@id": "/single_file_configs",
"@type": "hydra:Collection",
"hydra:member": [],
"hydra:totalItems": 0
}
"""

@dropSchema
Scenario: Retrieve the ConfigDummy resource
When I send a "GET" request to "/fileconfigdummies/1"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface;
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
use Symfony\Component\Finder\Finder;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;

/**
Expand Down Expand Up @@ -148,28 +149,31 @@ private function registerAnnotationLoaders(ContainerBuilder $container)
*/
private function registerFileLoaders(ContainerBuilder $container)
{
$paths = [];
$prefix = DIRECTORY_SEPARATOR.'Resources'.DIRECTORY_SEPARATOR.'config'.DIRECTORY_SEPARATOR;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you think about using the Symfony Finder Component instead of glob directly?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can but I wanted to avoid adding a dependency only for this.
Le mar. 7 juin 2016 à 12:07, Kévin Dunglas notifications@github.com a
écrit :

In src/Bridge/Symfony/Bundle/DependencyInjection/ApiPlatformExtension.php
#568 (comment):

@@ -149,12 +149,16 @@ private function registerAnnotationLoaders(ContainerBuilder $container)
private function registerFileLoaders(ContainerBuilder $container)
{
$paths = [];

  •    $globOptions = GLOB_BRACE | GLOB_NOSORT;
    
  •    $prefix = DIRECTORY_SEPARATOR.'Resources'.DIRECTORY_SEPARATOR.'config'.DIRECTORY_SEPARATOR;
    

What do you think about using the Symfony Finder Component instead of glob
directly?


You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
https://github.com/api-platform/core/pull/568/files/fd99357da41448ed77aceac668050c0b1b7d3ad8#r66043028,
or mute the thread
https://github.com/notifications/unsubscribe/ABQr81YXxO09RMmxY-MAwF1LJ_-k7Z6Dks5qJULFgaJpZM4Ivt1f
.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair enough.

$yamlResources = [];
$xmlResources = [];

foreach ($container->getParameter('kernel.bundles') as $bundle) {
$reflectionClass = new \ReflectionClass($bundle);
$bundleDirectory = dirname($reflectionClass->getFileName());
$glob = $bundleDirectory.DIRECTORY_SEPARATOR.'Resources'.DIRECTORY_SEPARATOR.'config'.DIRECTORY_SEPARATOR.'resources.{xml,yml}';
$configDirectory = dirname($reflectionClass->getFileName()).$prefix;
$yamlResources = array_merge($yamlResources, glob($configDirectory.'api_resources.{yml,yaml}'));

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

$yamlPaths = array_filter($paths, function ($v) {
return preg_match('/\.yml$/', $v);
});
$xmlResources = array_merge($xmlResources, glob($configDirectory.'api_resources.xml'));

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

$container->getDefinition('api_platform.metadata.resource.name_collection_factory.yaml')->replaceArgument(0, $yamlPaths);
$container->getDefinition('api_platform.metadata.resource.metadata_factory.yaml')->replaceArgument(0, $yamlPaths);
$container->getDefinition('api_platform.metadata.resource.name_collection_factory.yaml')->replaceArgument(0, $yamlResources);
$container->getDefinition('api_platform.metadata.resource.metadata_factory.yaml')->replaceArgument(0, $yamlResources);

$container->getDefinition('api_platform.metadata.resource.name_collection_factory.xml')->replaceArgument(0, $xmlPaths);
$container->getDefinition('api_platform.metadata.resource.metadata_factory.xml')->replaceArgument(0, $xmlPaths);
$container->getDefinition('api_platform.metadata.resource.name_collection_factory.xml')->replaceArgument(0, $xmlResources);
$container->getDefinition('api_platform.metadata.resource.metadata_factory.xml')->replaceArgument(0, $xmlResources);
}

/**
Expand Down
53 changes: 53 additions & 0 deletions tests/Fixtures/TestBundle/Entity/SingleFileConfigDummy.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

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

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

use Doctrine\ORM\Mapping as ORM;

/**
* FileConfigDummy.
*
* @ORM\Entity
*/
class SingleFileConfigDummy
{
/**
* @var int The id.
*
* @ORM\Column(type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;

/**
* @var string The dummy name.
*
* @ORM\Column
*/
private $name;

public function getId()
{
return $this->id;
}

public function setName($name)
{
$this->name = $name;
}

public function getName()
{
return $this->name;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
resources:
single_file_config:
shortName: 'single_file_config'
description: 'File configured resource'
class: 'ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\SingleFileConfigDummy'
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public function optionalResourceMetadataProvider()

public function testYamlResourceName()
{
$configPath = __DIR__.'/../../../Fixtures/resources.yml';
$configPath = __DIR__.'/../../../Fixtures/FileConfigurations/resources.yml';
$yamlResourceNameCollectionFactory = new YamlResourceNameCollectionFactory([$configPath]);

$this->assertEquals($yamlResourceNameCollectionFactory->create(), new ResourceNameCollection([
Expand All @@ -86,7 +86,7 @@ public function testYamlResourceName()

public function testXmlResourceName()
{
$configPath = __DIR__.'/../../../Fixtures/resources.xml';
$configPath = __DIR__.'/../../../Fixtures/FileConfigurations/resources.xml';
$xmlResourceNameCollectionFactory = new XmlResourceNameCollectionFactory([$configPath]);

$this->assertEquals($xmlResourceNameCollectionFactory->create(), new ResourceNameCollection([
Expand All @@ -99,7 +99,7 @@ public function testXmlResourceName()
*/
public function testYamlCreateResourceMetadata(ResourceMetadata $expectedResourceMetadata)
{
$configPath = __DIR__.'/../../../Fixtures/resources.yml';
$configPath = __DIR__.'/../../../Fixtures/FileConfigurations/resources.yml';

$resourceMetadataFactory = new YamlResourceMetadataFactory([$configPath]);
$resourceMetadata = $resourceMetadataFactory->create(FileConfigDummy::class);
Expand All @@ -113,7 +113,7 @@ public function testYamlCreateResourceMetadata(ResourceMetadata $expectedResourc
*/
public function testXmlCreateResourceMetadata($expectedResourceMetadata)
{
$configPath = __DIR__.'/../../../Fixtures/resources.xml';
$configPath = __DIR__.'/../../../Fixtures/FileConfigurations/resources.xml';

$resourceMetadataFactory = new XmlResourceMetadataFactory([$configPath]);
$resourceMetadata = $resourceMetadataFactory->create(FileConfigDummy::class);
Expand All @@ -127,7 +127,7 @@ public function testXmlCreateResourceMetadata($expectedResourceMetadata)
*/
public function testYamlDoesNotExistMetadataFactory()
{
$configPath = __DIR__.'/../../../Fixtures/resourcenotfound.yml';
$configPath = __DIR__.'/../../../Fixtures/FileConfigurations/resourcenotfound.yml';
$yamlResourceNameCollectionFactory = new YamlResourceNameCollectionFactory([$configPath]);
$resourceMetadataFactory = new YamlResourceMetadataFactory([$configPath]);

Expand All @@ -141,7 +141,7 @@ public function testYamlDoesNotExistMetadataFactory()
*/
public function testXmlDoesNotExistMetadataFactory()
{
$configPath = __DIR__.'/../../../Fixtures/resourcenotfound.xml';
$configPath = __DIR__.'/../../../Fixtures/FileConfigurations/resourcenotfound.xml';
$xmlResourceNameCollectionFactory = new XmlResourceNameCollectionFactory([$configPath]);
$resourceMetadataFactory = new XmlResourceMetadataFactory([$configPath]);

Expand All @@ -155,7 +155,7 @@ public function testXmlDoesNotExistMetadataFactory()
*/
public function testNotValidXml()
{
$configPath = __DIR__.'/../../../Fixtures/resourcesinvalid.xml';
$configPath = __DIR__.'/../../../Fixtures/FileConfigurations/resourcesinvalid.xml';
$xmlResourceNameCollectionFactory = new XmlResourceNameCollectionFactory([$configPath]);
$resourceMetadataFactory = new XmlResourceMetadataFactory([$configPath]);

Expand All @@ -169,7 +169,7 @@ public function testNotValidXml()
*/
public function testXmlOptionalResourceMetadata($expectedResourceMetadata)
{
$configPath = __DIR__.'/../../../Fixtures/resourcesoptional.xml';
$configPath = __DIR__.'/../../../Fixtures/FileConfigurations/resourcesoptional.xml';

$resourceMetadataFactory = new XmlResourceMetadataFactory([$configPath]);
$resourceMetadata = $resourceMetadataFactory->create(FileConfigDummy::class);
Expand All @@ -184,7 +184,7 @@ public function testXmlOptionalResourceMetadata($expectedResourceMetadata)
*/
public function testYamlOptionalResourceMetadata($expectedResourceMetadata)
{
$configPath = __DIR__.'/../../../Fixtures/resourcesoptional.yml';
$configPath = __DIR__.'/../../../Fixtures/FileConfigurations/resourcesoptional.yml';

$resourceMetadataFactory = new YamlResourceMetadataFactory([$configPath]);
$resourceMetadata = $resourceMetadataFactory->create(FileConfigDummy::class);
Expand Down