Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1206 from soyuka/improve-subresources
Implement subresource metadata and annotation
- Loading branch information
Showing
24 changed files
with
347 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -67,9 +67,4 @@ final class ApiProperty | |
* @var array | ||
*/ | ||
public $attributes = []; | ||
|
||
/** | ||
* @var bool | ||
*/ | ||
public $subresource; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?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. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace ApiPlatform\Core\Annotation; | ||
|
||
/** | ||
* Property annotation. | ||
* | ||
* @author Antoine Bluchet <soyuka@gmail.com> | ||
* | ||
* @Annotation | ||
* @Target({"METHOD", "PROPERTY"}) | ||
*/ | ||
final class ApiSubresource | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
88 changes: 88 additions & 0 deletions
88
src/Metadata/Property/Factory/AnnotationSubresourceMetadataFactory.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
<?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. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace ApiPlatform\Core\Metadata\Property\Factory; | ||
|
||
use ApiPlatform\Core\Annotation\ApiSubresource; | ||
use ApiPlatform\Core\Metadata\Property\PropertyMetadata; | ||
use ApiPlatform\Core\Metadata\Property\SubresourceMetadata; | ||
use ApiPlatform\Core\Util\Reflection; | ||
use Doctrine\Common\Annotations\Reader; | ||
|
||
/** | ||
* Adds subresources to the properties metadata from {@see ApiResource} annotations. | ||
* | ||
* @author Antoine Bluchet <soyuka@gmail.com> | ||
*/ | ||
final class AnnotationSubresourceMetadataFactory implements PropertyMetadataFactoryInterface | ||
{ | ||
private $reader; | ||
private $decorated; | ||
|
||
public function __construct(Reader $reader, PropertyMetadataFactoryInterface $decorated) | ||
{ | ||
$this->reader = $reader; | ||
$this->decorated = $decorated; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function create(string $resourceClass, string $property, array $options = []): PropertyMetadata | ||
{ | ||
$propertyMetadata = $this->decorated->create($resourceClass, $property, $options); | ||
|
||
try { | ||
$reflectionClass = new \ReflectionClass($resourceClass); | ||
} catch (\ReflectionException $reflectionException) { | ||
return $propertyMetadata; | ||
} | ||
|
||
if ($reflectionClass->hasProperty($property)) { | ||
$annotation = $this->reader->getPropertyAnnotation($reflectionClass->getProperty($property), ApiSubresource::class); | ||
|
||
if (null !== $annotation) { | ||
return $this->updateMetadata($annotation, $propertyMetadata); | ||
} | ||
} | ||
|
||
foreach (array_merge(Reflection::ACCESSOR_PREFIXES, Reflection::MUTATOR_PREFIXES) as $prefix) { | ||
$methodName = $prefix.ucfirst($property); | ||
if (!$reflectionClass->hasMethod($methodName)) { | ||
continue; | ||
} | ||
|
||
$reflectionMethod = $reflectionClass->getMethod($methodName); | ||
if (!$reflectionMethod->isPublic()) { | ||
continue; | ||
} | ||
|
||
$annotation = $this->reader->getMethodAnnotation($reflectionMethod, ApiSubresource::class); | ||
|
||
if (null !== $annotation) { | ||
return $this->updateMetadata($annotation, $propertyMetadata); | ||
} | ||
} | ||
|
||
return $propertyMetadata; | ||
} | ||
|
||
private function updateMetadata(ApiSubresource $annotation, PropertyMetadata $propertyMetadata): PropertyMetadata | ||
{ | ||
$type = $propertyMetadata->getType(); | ||
$isCollection = $type->isCollection(); | ||
$resourceClass = $isCollection ? $type->getCollectionValueType()->getClassName() : $type->getClassName(); | ||
|
||
return $propertyMetadata->withSubresource(new SubresourceMetadata($resourceClass, $isCollection)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.