-
-
Notifications
You must be signed in to change notification settings - Fork 869
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(laravel): provide a trait in addition to the annotation
- Loading branch information
Showing
8 changed files
with
391 additions
and
215 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
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,27 @@ | ||
<?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\Laravel; | ||
|
||
use ApiPlatform\Metadata\ApiResource; | ||
|
||
/** | ||
* @author Kévin Dunglas <kevin@dunglas.dev> | ||
*/ | ||
trait IsApiResource | ||
{ | ||
public static function apiResource(): ApiResource | ||
{ | ||
return new ApiResource(); | ||
} | ||
} |
54 changes: 54 additions & 0 deletions
54
src/Laravel/Metadata/ConcernsResourceMetadataCollectionFactory.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,54 @@ | ||
<?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\Laravel\Metadata; | ||
|
||
use ApiPlatform\Laravel\IsApiResource; | ||
use ApiPlatform\Metadata\Resource\Factory\MetadataCollectionFactoryTrait; | ||
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface; | ||
use ApiPlatform\Metadata\Resource\ResourceMetadataCollection; | ||
|
||
/** | ||
* Creates a resource metadata from {@see IsApiResource} concerns. | ||
* | ||
* @author Kévin Dunglas <kevin@dunglas.dev> | ||
*/ | ||
final class ConcernsResourceMetadataCollectionFactory implements ResourceMetadataCollectionFactoryInterface | ||
{ | ||
use MetadataCollectionFactoryTrait; | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function create(string $resourceClass): ResourceMetadataCollection | ||
{ | ||
$resourceMetadataCollection = $this->decorated?->create($resourceClass) ?? new ResourceMetadataCollection( | ||
$resourceClass | ||
); | ||
|
||
if (!method_exists($resourceClass, 'apiResource')) { | ||
return $resourceMetadataCollection; | ||
} | ||
|
||
$metadataCollection = $resourceClass::apiResource(); | ||
if (!\is_array($metadataCollection)) { | ||
$metadataCollection = [$metadataCollection]; | ||
} | ||
|
||
foreach ($this->buildResourceOperations($metadataCollection, $resourceClass) as $resource) { | ||
$resourceMetadataCollection[] = $resource; | ||
} | ||
|
||
return $resourceMetadataCollection; | ||
} | ||
} |
63 changes: 63 additions & 0 deletions
63
src/Laravel/Metadata/ConcernsResourceNameCollectionFactory.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,63 @@ | ||
<?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\Laravel\Metadata; | ||
|
||
use ApiPlatform\Laravel\IsApiResource; | ||
use ApiPlatform\Metadata\Resource\Factory\ResourceNameCollectionFactoryInterface; | ||
use ApiPlatform\Metadata\Resource\ResourceNameCollection; | ||
use ApiPlatform\Metadata\Util\ReflectionClassRecursiveIterator; | ||
|
||
/** | ||
* Creates a resource name collection from {@see IsApiResource} concerns. | ||
* | ||
* @author Kévin Dunglas <kevin@dunglas.dev> | ||
*/ | ||
final class ConcernsResourceNameCollectionFactory implements ResourceNameCollectionFactoryInterface | ||
{ | ||
/** | ||
* @param string[] $paths | ||
*/ | ||
public function __construct( | ||
private readonly array $paths, | ||
private readonly ?ResourceNameCollectionFactoryInterface $decorated = null, | ||
) { | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function create(): ResourceNameCollection | ||
{ | ||
$classes = []; | ||
|
||
if ($this->decorated) { | ||
foreach ($this->decorated->create() as $resourceClass) { | ||
$classes[$resourceClass] = true; | ||
} | ||
} | ||
|
||
foreach (ReflectionClassRecursiveIterator::getReflectionClassesFromDirectories($this->paths) as $className => $reflectionClass) { | ||
if ( | ||
$reflectionClass->hasMethod('apiResource') | ||
&& ($m = $reflectionClass->getMethod('apiResource')) | ||
&& $m->isPublic() | ||
&& $m->isStatic() | ||
) { | ||
$classes[$className] = true; | ||
} | ||
} | ||
|
||
return new ResourceNameCollection(array_keys($classes)); | ||
} | ||
} |
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.