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
37 changes: 30 additions & 7 deletions src/Metadata/Resource/Factory/XmlResourceMetadataFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,22 +61,18 @@ public function create(string $resourceClass) : ResourceMetadata
$metadata = null;

foreach ($this->paths as $path) {
$this->xmlParser->loadXML(file_get_contents($path));
$resources = $this->getResourcesDom($path);

$internalErrors = libxml_use_internal_errors(true);

if (false === @$this->xmlParser->schemaValidate(self::RESOURCE_SCHEMA)) {
if (false === @$resources->schemaValidate(self::RESOURCE_SCHEMA)) {
throw new \InvalidArgumentException(sprintf('XML Schema loaded from path %s is not valid! Errors: %s', realpath($path), implode("\n", $this->getXmlErrors($internalErrors))));
}

libxml_clear_errors();
libxml_use_internal_errors($internalErrors);

$xpath = new \DOMXpath($this->xmlParser);

$resources = $xpath->query('/resources/resource');

foreach ($resources as $resource) {
foreach ($resources->getElementsByTagName('resource') as $resource) {
$class = $resource->getAttribute('class');

if ($resourceClass !== $class) {
Expand All @@ -93,6 +89,8 @@ public function create(string $resourceClass) : ResourceMetadata
return $this->handleNotFound($parentResourceMetadata, $resourceClass);
}

$xpath = new \DOMXpath($resources);

$metadata = [
'shortName' => $metadata->getAttribute('shortName') ?: null,
'description' => $metadata->getAttribute('description') ?: null,
Expand Down Expand Up @@ -126,6 +124,31 @@ public function create(string $resourceClass) : ResourceMetadata
return $resourceMetadata;
}

/**
* Creates a DOMDocument based on `resource` tags of a file-loaded xml document.
*
* @param string $path the xml file path
*
* @return \DOMDocument
*/
private function getResourcesDom(string $path) : \DOMDocument
{
$doc = new \DOMDocument('1.0', 'utf-8');
$root = $doc->createElement('resources');
$doc->appendChild($root);

$this->xmlParser->loadXML(file_get_contents($path));

$xpath = new \DOMXpath($this->xmlParser);
$resources = $xpath->query('//resource');

foreach ($resources as $resource) {
$root->appendChild($doc->importNode($resource, true));
}

return $doc;
}

/**
* Get operations from xml.
*
Expand Down
35 changes: 28 additions & 7 deletions src/Metadata/Resource/Factory/XmlResourceNameCollectionFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,29 +51,50 @@ public function create() : ResourceNameCollection
}

foreach ($this->paths as $path) {
$this->xmlParser->loadXML(file_get_contents($path));
$resources = $this->getResourcesDom($path);

$internalErrors = libxml_use_internal_errors(true);

if (false === @$this->xmlParser->schemaValidate(self::RESOURCE_SCHEMA)) {
if (false === @$resources->schemaValidate(self::RESOURCE_SCHEMA)) {
throw new \InvalidArgumentException(sprintf('XML Schema loaded from path %s is not valid! Errors: %s', realpath($path), implode("\n", $this->getXmlErrors($internalErrors))));
}

libxml_clear_errors();
libxml_use_internal_errors($internalErrors);

$xpath = new \DOMXpath($this->xmlParser);

$resources = $xpath->query('/resources/resource');

foreach ($resources as $resource) {
foreach ($resources->getElementsByTagName('resource') as $resource) {
$classes[$resource->getAttribute('class')] = true;
}
}

return new ResourceNameCollection(array_keys($classes));
}

/**
* Creates a DOMDocument based on `resource` tags of a file-loaded xml document.
*
* @param string $path the xml file path
*
* @return \DOMDocument
*/
private function getResourcesDom(string $path) : \DOMDocument
{
$doc = new \DOMDocument('1.0', 'utf-8');
$root = $doc->createElement('resources');
$doc->appendChild($root);

$this->xmlParser->loadXML(file_get_contents($path));

$xpath = new \DOMXpath($this->xmlParser);
$resources = $xpath->query('//resource');

foreach ($resources as $resource) {
$root->appendChild($doc->importNode($resource, true));
}

return $doc;
}

/**
* Returns the XML errors of the internal XML parser.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,9 @@ public function create(string $resourceClass) : ResourceMetadata
$metadata = null;

foreach ($this->paths as $path) {
$resources = $this->yamlParser->parse(file_get_contents($path))['resources'];
$resources = $this->yamlParser->parse(file_get_contents($path));

$resources = $resources['resources'] ?? $resources;

foreach ($resources as $resource) {
if (!isset($resource['class'])) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@ public function create() : ResourceNameCollection
}

foreach ($this->paths as $path) {
$resources = $this->yamlParser->parse(file_get_contents($path))['resources'];
$resources = $this->yamlParser->parse(file_get_contents($path));

$resources = $resources['resources'] ?? $resources;

foreach ($resources as $resource) {
if (!isset($resource['class'])) {
Expand Down
26 changes: 26 additions & 0 deletions tests/Fixtures/FileConfigurations/single_resource.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?xml version="1.0" encoding="UTF-8" ?>
<resource class="ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\FileConfigDummy" shortName="thedummyshortname" description="Dummy resource" iri="someirischema">
<itemOperations>
<operation key="my_op_name" method="GET" />
<operation key="my_other_op_name" method="POST" />
</itemOperations>
<collectionOperations>
<operation key="my_collection_op" method="POST" path="the/collection/path"/>
</collectionOperations>
<attributes>
<attribute key="normalization_context">
<attribute key="groups">
<attribute>default</attribute>
</attribute>
</attribute>
<attribute key="denormalization_context">
<attribute key="groups">
<attribute>default</attribute>
</attribute>
</attribute>
<attribute key="hydra_context">
<attribute key="@type">hydra:Operation</attribute>
<attribute key="@hydra:title">File config Dummy</attribute>
</attribute>
</attributes>
</resource>
22 changes: 22 additions & 0 deletions tests/Fixtures/FileConfigurations/single_resource.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
configdummy:
shortName: 'thedummyshortname'
description: 'Dummy resource'
class: 'ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\FileConfigDummy'
itemOperations:
my_op_name:
method: 'GET'
my_other_op_name:
method: 'POST'
collectionOperations:
my_collection_op:
method: 'POST'
path: 'the/collection/path'
attributes:
normalization_context:
groups: ['default']
denormalization_context:
groups: ['default']
hydra_context:
'@type': 'hydra:Operation'
'@hydra:title': 'File config Dummy'
iri: 'someirischema'
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
resources:
single_file_config:
shortName: 'single_file_config'
description: 'File configured resource'
class: 'ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\SingleFileConfigDummy'
single_file_config:
shortName: 'single_file_config'
description: 'File configured resource'
class: 'ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\SingleFileConfigDummy'
48 changes: 48 additions & 0 deletions tests/Metadata/Resource/Factory/FileConfigurationMetadataTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -192,4 +192,52 @@ public function testYamlOptionalResourceMetadata($expectedResourceMetadata)
$this->assertInstanceOf(ResourceMetadata::class, $resourceMetadata);
$this->assertEquals($expectedResourceMetadata, $resourceMetadata);
}

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

$this->assertEquals($xmlResourceNameCollectionFactory->create(), new ResourceNameCollection([
FileConfigDummy::class,
]));
}

/**
* @dataProvider resourceMetadataProvider
*/
public function testXmlSingleResourceMetadata($expectedResourceMetadata)
{
$configPath = __DIR__.'/../../../Fixtures/FileConfigurations/single_resource.xml';

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

$this->assertInstanceOf(ResourceMetadata::class, $resourceMetadata);
$this->assertEquals($expectedResourceMetadata, $resourceMetadata);
}

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

$this->assertEquals($yamlResourceNameCollectionFactory->create(), new ResourceNameCollection([
FileConfigDummy::class,
]));
}

/**
* @dataProvider resourceMetadataProvider
*/
public function testYamlSingleResourceMetadata(ResourceMetadata $expectedResourceMetadata)
{
$configPath = __DIR__.'/../../../Fixtures/FileConfigurations/single_resource.yml';

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

$this->assertInstanceOf(ResourceMetadata::class, $resourceMetadata);
$this->assertEquals($expectedResourceMetadata, $resourceMetadata);
}
}