diff --git a/composer.json b/composer.json index 525bb21aa60..57f472c7697 100644 --- a/composer.json +++ b/composer.json @@ -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.", diff --git a/features/configurable.feature b/features/configurable.feature index a6dda0666bb..0a86c68a91f 100644 --- a/features/configurable.feature +++ b/features/configurable.feature @@ -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" diff --git a/src/Bridge/Symfony/Bundle/DependencyInjection/ApiPlatformExtension.php b/src/Bridge/Symfony/Bundle/DependencyInjection/ApiPlatformExtension.php index df7fb4cd07d..b74598fbc53 100644 --- a/src/Bridge/Symfony/Bundle/DependencyInjection/ApiPlatformExtension.php +++ b/src/Bridge/Symfony/Bundle/DependencyInjection/ApiPlatformExtension.php @@ -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; /** @@ -148,28 +149,31 @@ private function registerAnnotationLoaders(ContainerBuilder $container) */ private function registerFileLoaders(ContainerBuilder $container) { - $paths = []; + $prefix = DIRECTORY_SEPARATOR.'Resources'.DIRECTORY_SEPARATOR.'config'.DIRECTORY_SEPARATOR; + $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); } /** diff --git a/tests/Fixtures/resourcenotfound.xml b/tests/Fixtures/FileConfigurations/resourcenotfound.xml similarity index 100% rename from tests/Fixtures/resourcenotfound.xml rename to tests/Fixtures/FileConfigurations/resourcenotfound.xml diff --git a/tests/Fixtures/resourcenotfound.yml b/tests/Fixtures/FileConfigurations/resourcenotfound.yml similarity index 100% rename from tests/Fixtures/resourcenotfound.yml rename to tests/Fixtures/FileConfigurations/resourcenotfound.yml diff --git a/tests/Fixtures/resources.xml b/tests/Fixtures/FileConfigurations/resources.xml similarity index 100% rename from tests/Fixtures/resources.xml rename to tests/Fixtures/FileConfigurations/resources.xml diff --git a/tests/Fixtures/resources.yml b/tests/Fixtures/FileConfigurations/resources.yml similarity index 100% rename from tests/Fixtures/resources.yml rename to tests/Fixtures/FileConfigurations/resources.yml diff --git a/tests/Fixtures/resourcesinvalid.xml b/tests/Fixtures/FileConfigurations/resourcesinvalid.xml similarity index 100% rename from tests/Fixtures/resourcesinvalid.xml rename to tests/Fixtures/FileConfigurations/resourcesinvalid.xml diff --git a/tests/Fixtures/resourcesoptional.xml b/tests/Fixtures/FileConfigurations/resourcesoptional.xml similarity index 100% rename from tests/Fixtures/resourcesoptional.xml rename to tests/Fixtures/FileConfigurations/resourcesoptional.xml diff --git a/tests/Fixtures/resourcesoptional.yml b/tests/Fixtures/FileConfigurations/resourcesoptional.yml similarity index 100% rename from tests/Fixtures/resourcesoptional.yml rename to tests/Fixtures/FileConfigurations/resourcesoptional.yml diff --git a/tests/Fixtures/TestBundle/Entity/SingleFileConfigDummy.php b/tests/Fixtures/TestBundle/Entity/SingleFileConfigDummy.php new file mode 100644 index 00000000000..72ee7b9b336 --- /dev/null +++ b/tests/Fixtures/TestBundle/Entity/SingleFileConfigDummy.php @@ -0,0 +1,53 @@ + + * + * 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; + } +} diff --git a/tests/Fixtures/TestBundle/Resources/config/resources.yml b/tests/Fixtures/TestBundle/Resources/config/api_resources.yml similarity index 100% rename from tests/Fixtures/TestBundle/Resources/config/resources.yml rename to tests/Fixtures/TestBundle/Resources/config/api_resources.yml diff --git a/tests/Fixtures/TestBundle/Resources/config/api_resources/my_resource.yml b/tests/Fixtures/TestBundle/Resources/config/api_resources/my_resource.yml new file mode 100644 index 00000000000..7b773d7faa7 --- /dev/null +++ b/tests/Fixtures/TestBundle/Resources/config/api_resources/my_resource.yml @@ -0,0 +1,5 @@ +resources: + single_file_config: + shortName: 'single_file_config' + description: 'File configured resource' + class: 'ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\SingleFileConfigDummy' diff --git a/tests/Metadata/Resource/Factory/FileConfigurationMetadataTest.php b/tests/Metadata/Resource/Factory/FileConfigurationMetadataTest.php index b0fce1b6169..a7ab2a1ef24 100644 --- a/tests/Metadata/Resource/Factory/FileConfigurationMetadataTest.php +++ b/tests/Metadata/Resource/Factory/FileConfigurationMetadataTest.php @@ -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([ @@ -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([ @@ -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); @@ -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); @@ -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]); @@ -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]); @@ -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]); @@ -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); @@ -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);