-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
IBX-8335: Initial integration with API Platform #114
base: main
Are you sure you want to change the base?
Changes from all commits
d1e031d
071286d
010800c
52a2cfc
adf4b2b
d8ad694
e9d9fde
d8473d1
6b56a98
cdc8f37
dfda25b
723516b
600717f
998a687
ec3b0d5
dd6c9d0
ebff100
f45837e
40cff86
dab22d3
d860b11
50f6a84
70c7f0c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php | ||
|
||
/** | ||
* @copyright Copyright (C) Ibexa AS. All rights reserved. | ||
* @license For full copyright and license information view LICENSE file distributed with this source code. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Ibexa\Bundle\Rest\ApiPlatform; | ||
|
||
use ApiPlatform\Metadata\Resource\Factory\ResourceNameCollectionFactoryInterface; | ||
use ApiPlatform\Metadata\Resource\ResourceNameCollection; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
final class ClassNameResourceNameCollectionFactory implements ResourceNameCollectionFactoryInterface | ||
{ | ||
/** | ||
* @var array<string> | ||
*/ | ||
Comment on lines
+19
to
+21
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We tend to use one-liner here. |
||
private array $resources = []; | ||
|
||
public function create(): ResourceNameCollection | ||
{ | ||
return new ResourceNameCollection($this->resources); | ||
} | ||
|
||
/** | ||
* @param array<string> $newResources | ||
*/ | ||
public function addResources(array $newResources): void | ||
{ | ||
$this->resources = array_merge($this->resources, $newResources); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,147 @@ | ||||||
<?php | ||||||
|
||||||
/** | ||||||
* @copyright Copyright (C) Ibexa AS. All rights reserved. | ||||||
* @license For full copyright and license information view LICENSE file distributed with this source code. | ||||||
*/ | ||||||
declare(strict_types=1); | ||||||
|
||||||
namespace Ibexa\Bundle\Rest\ApiPlatform; | ||||||
|
||||||
use ApiPlatform\OpenApi\Factory\OpenApiFactoryInterface; | ||||||
use ApiPlatform\OpenApi\Model\Response; | ||||||
use ApiPlatform\OpenApi\OpenApi; | ||||||
use Symfony\Component\HttpKernel\KernelInterface; | ||||||
|
||||||
final class OpenApiFactory implements OpenApiFactoryInterface | ||||||
{ | ||||||
public function __construct( | ||||||
private readonly OpenApiFactoryInterface $decorated, | ||||||
private readonly SchemasCollectionFactory $schemaCollectionFactory, | ||||||
private readonly KernelInterface $kernel, | ||||||
Comment on lines
+19
to
+21
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Cannot the whole class be |
||||||
) { | ||||||
} | ||||||
|
||||||
/** | ||||||
* @param array<mixed> $context | ||||||
*/ | ||||||
public function __invoke(array $context = []): OpenApi | ||||||
{ | ||||||
$openApi = $this->decorated->__invoke($context); | ||||||
$openApi = $this->addSchemas($openApi); | ||||||
|
||||||
$this->insertExampleFilesContent($openApi); | ||||||
|
||||||
return $openApi; | ||||||
} | ||||||
|
||||||
private function addSchemas(OpenApi $openApi): OpenApi | ||||||
{ | ||||||
$schemasCollection = $this->schemaCollectionFactory->create(); | ||||||
$schemas = iterator_to_array($schemasCollection); | ||||||
|
||||||
$components = $openApi->getComponents(); | ||||||
$components = $components->withSchemas(new \ArrayObject($schemas)); | ||||||
|
||||||
$openApi = $openApi->withComponents($components); | ||||||
|
||||||
return $openApi; | ||||||
} | ||||||
|
||||||
private function insertExampleFilesContent(OpenApi $openApi): void | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Readability is not great, I will split this method into smaller ones. |
||||||
{ | ||||||
$paths = $openApi->getPaths(); | ||||||
|
||||||
/** @var \ApiPlatform\OpenApi\Model\PathItem $pathItem */ | ||||||
foreach ($paths->getPaths() as $path => $pathItem) { | ||||||
$newPathItem = $pathItem; | ||||||
|
||||||
/** @var array<string, \ApiPlatform\OpenApi\Model\Operation|null> $methods */ | ||||||
$methods = [ | ||||||
'GET' => $pathItem->getGet(), | ||||||
'PUT' => $pathItem->getPut(), | ||||||
'POST' => $pathItem->getPost(), | ||||||
'DELETE' => $pathItem->getDelete(), | ||||||
'OPTIONS' => $pathItem->getOptions(), | ||||||
'HEAD' => $pathItem->getHead(), | ||||||
'PATCH' => $pathItem->getPatch(), | ||||||
'TRACE' => $pathItem->getTrace(), | ||||||
]; | ||||||
foreach ($methods as $method => $operation) { | ||||||
if (empty($operation)) { | ||||||
continue; | ||||||
} | ||||||
|
||||||
$responses = $operation->getResponses(); | ||||||
|
||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
if ($responses === null) { | ||||||
continue; | ||||||
} | ||||||
|
||||||
$newOperation = $operation; | ||||||
|
||||||
/** | ||||||
* @var int $responseCode | ||||||
* @var \ApiPlatform\OpenApi\Model\Response|array<string, array<mixed>> $response | ||||||
*/ | ||||||
foreach ($operation->getResponses() as $responseCode => $response) { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
if (!is_array($response) || !array_key_exists('content', $response)) { | ||||||
continue; | ||||||
} | ||||||
|
||||||
$content = $response['content']; | ||||||
$newContent = $content; | ||||||
|
||||||
foreach ($newContent as $mediaType => $responseContent) { | ||||||
if (array_key_exists('x-ibexa-example-file', $responseContent)) { | ||||||
$exampleFilePath = $this->kernel->locateResource($responseContent['x-ibexa-example-file']); | ||||||
$exampleFileContent = file_get_contents($exampleFilePath); | ||||||
$newContent[$mediaType]['example'] = $exampleFileContent; | ||||||
unset($newContent[$mediaType]['x-ibexa-example-file']); | ||||||
} | ||||||
} | ||||||
|
||||||
if ($newContent !== $content) { | ||||||
$newOperation = $newOperation->withResponse( | ||||||
$responseCode, | ||||||
new Response((string)$responseCode, new \ArrayObject($newContent)), | ||||||
); | ||||||
} | ||||||
} | ||||||
|
||||||
if ($newOperation !== $operation) { | ||||||
switch ($method) { | ||||||
case 'GET': | ||||||
$newPathItem = $newPathItem->withGet($newOperation); | ||||||
break; | ||||||
case 'PUT': | ||||||
$newPathItem = $newPathItem->withPut($newOperation); | ||||||
break; | ||||||
case 'POST': | ||||||
$newPathItem = $newPathItem->withPost($newOperation); | ||||||
break; | ||||||
case 'DELETE': | ||||||
$newPathItem = $newPathItem->withDelete($newOperation); | ||||||
break; | ||||||
case 'OPTIONS': | ||||||
$newPathItem = $newPathItem->withOptions($newOperation); | ||||||
break; | ||||||
case 'HEAD': | ||||||
$newPathItem = $newPathItem->withHead($newOperation); | ||||||
break; | ||||||
case 'PATCH': | ||||||
$newPathItem = $newPathItem->withPatch($newOperation); | ||||||
break; | ||||||
case 'TRACE': | ||||||
$newPathItem = $newPathItem->withTrace($newOperation); | ||||||
break; | ||||||
} | ||||||
} | ||||||
} | ||||||
|
||||||
if ($newPathItem !== $pathItem) { | ||||||
$paths->addPath($path, $newPathItem); | ||||||
} | ||||||
} | ||||||
} | ||||||
} |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,40 @@ | ||||||
<?php | ||||||
|
||||||
/** | ||||||
* @copyright Copyright (C) Ibexa AS. All rights reserved. | ||||||
* @license For full copyright and license information view LICENSE file distributed with this source code. | ||||||
*/ | ||||||
declare(strict_types=1); | ||||||
|
||||||
namespace Ibexa\Bundle\Rest\ApiPlatform; | ||||||
|
||||||
use Ibexa\Rest\ApiPlatform\SchemasCollection; | ||||||
use Ibexa\Rest\ApiPlatform\SchemasCollectionFactoryInterface; | ||||||
use Ibexa\Rest\ApiPlatform\SchemasProviderInterface; | ||||||
|
||||||
/** | ||||||
* @internal | ||||||
*/ | ||||||
final class SchemasCollectionFactory implements SchemasCollectionFactoryInterface | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Perhaps it can be:
Suggested change
|
||||||
{ | ||||||
/** | ||||||
* @var array<SchemasProviderInterface> | ||||||
*/ | ||||||
Comment on lines
+20
to
+22
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. One line comment here. |
||||||
private array $providers = []; | ||||||
|
||||||
public function create(): SchemasCollection | ||||||
{ | ||||||
$schemas = []; | ||||||
|
||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
foreach ($this->providers as $provider) { | ||||||
$schemas = array_merge($schemas, $provider->getSchemas()); | ||||||
} | ||||||
|
||||||
return new SchemasCollection($schemas); | ||||||
} | ||||||
|
||||||
public function addProvider(SchemasProviderInterface $provider): void | ||||||
{ | ||||||
$this->providers[] = $provider; | ||||||
} | ||||||
} |
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,35 @@ | ||||||||
<?php | ||||||||
|
||||||||
/** | ||||||||
* @copyright Copyright (C) Ibexa AS. All rights reserved. | ||||||||
* @license For full copyright and license information view LICENSE file distributed with this source code. | ||||||||
*/ | ||||||||
|
||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
namespace Ibexa\Bundle\Rest\DependencyInjection\Compiler; | ||||||||
|
||||||||
use Ibexa\Bundle\Rest\ApiPlatform\ClassNameResourceNameCollectionFactory; | ||||||||
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; | ||||||||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||||||||
|
||||||||
class ClassNameResourceNamePass implements CompilerPassInterface | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
{ | ||||||||
public const API_PLATFORM_RESOURCE_SERVICE_TAG = 'ibexa.api_platform.resource'; | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
|
||||||||
public function process(ContainerBuilder $container): void | ||||||||
{ | ||||||||
if (!$container->hasDefinition(ClassNameResourceNameCollectionFactory::class)) { | ||||||||
return; | ||||||||
} | ||||||||
|
||||||||
$definition = $container->getDefinition(ClassNameResourceNameCollectionFactory::class); | ||||||||
|
||||||||
$taggedServiceIds = $container->findTaggedServiceIds(self::API_PLATFORM_RESOURCE_SERVICE_TAG); | ||||||||
foreach ($taggedServiceIds as $id => $attributes) { | ||||||||
$taggedServiceDefinition = $container->getDefinition($id); | ||||||||
$definition->addMethodCall( | ||||||||
'addResources', | ||||||||
[[$taggedServiceDefinition->getClass()]] | ||||||||
); | ||||||||
} | ||||||||
} | ||||||||
} |
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,35 @@ | ||||||||
<?php | ||||||||
|
||||||||
/** | ||||||||
* @copyright Copyright (C) Ibexa AS. All rights reserved. | ||||||||
* @license For full copyright and license information view LICENSE file distributed with this source code. | ||||||||
*/ | ||||||||
|
||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
namespace Ibexa\Bundle\Rest\DependencyInjection\Compiler; | ||||||||
|
||||||||
use Ibexa\Bundle\Rest\ApiPlatform\SchemasCollectionFactory; | ||||||||
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; | ||||||||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||||||||
use Symfony\Component\DependencyInjection\Reference; | ||||||||
|
||||||||
class SchemaProviderPass implements CompilerPassInterface | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
{ | ||||||||
public const API_PLATFORM_SCHEMA_PROVIDER_SERVICE_TAG = 'ibexa.api_platform.schemas_provider'; | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
|
||||||||
public function process(ContainerBuilder $container): void | ||||||||
{ | ||||||||
if (!$container->hasDefinition(SchemasCollectionFactory::class)) { | ||||||||
return; | ||||||||
} | ||||||||
|
||||||||
$definition = $container->getDefinition(SchemasCollectionFactory::class); | ||||||||
|
||||||||
$taggedServiceIds = $container->findTaggedServiceIds(self::API_PLATFORM_SCHEMA_PROVIDER_SERVICE_TAG); | ||||||||
foreach ($taggedServiceIds as $serviceId => $attributes) { | ||||||||
$definition->addMethodCall( | ||||||||
'addProvider', | ||||||||
[new Reference($serviceId)] | ||||||||
); | ||||||||
} | ||||||||
} | ||||||||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can it be readonly?