Skip to content

Commit

Permalink
Fix registering resources
Browse files Browse the repository at this point in the history
  • Loading branch information
loic425 committed Oct 20, 2023
1 parent e237fb0 commit f6c99b7
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 39 deletions.
10 changes: 6 additions & 4 deletions src/Bundle/DependencyInjection/SyliusResourceExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
use Sylius\Bundle\ResourceBundle\SyliusResourceBundle;
use Sylius\Component\Resource\Factory\Factory;
use Sylius\Component\Resource\Factory\FactoryInterface;
use Sylius\Component\Resource\Metadata\AsResource;
use Sylius\Component\Resource\Metadata\Metadata;
use Sylius\Component\Resource\Metadata\ResourceMetadata;
use Sylius\Component\Resource\Reflection\ClassReflection;
Expand Down Expand Up @@ -122,12 +123,13 @@ private function autoRegisterResources(array &$config, ContainerBuilder $contain

/** @var class-string $className */
foreach (ClassReflection::getResourcesByPaths($paths) as $className) {
$resourceAttributes = ClassReflection::getClassAttributes($className, ResourceMetadata::class);
$resourceAttributes = ClassReflection::getClassAttributes($className, AsResource::class);

foreach ($resourceAttributes as $resourceAttribute) {
/** @var ResourceMetadata $resource */
/** @var AsResource $resource */
$resource = $resourceAttribute->newInstance();
$resourceAlias = $this->getResourceAlias($resource, $className);
$resourceMetadata = $resource->toMetadata();
$resourceAlias = $this->getResourceAlias($resourceMetadata, $className);

if ($resources[$resourceAlias] ?? false) {
continue;
Expand All @@ -140,7 +142,7 @@ private function autoRegisterResources(array &$config, ContainerBuilder $contain
'factory' => Factory::class,
'form' => DefaultResourceType::class,
],
'driver' => $resource->getDriver() ?? SyliusResourceBundle::DRIVER_DOCTRINE_ORM,
'driver' => $resourceMetadata->getDriver() ?? SyliusResourceBundle::DRIVER_DOCTRINE_ORM,
];
}
}
Expand Down
68 changes: 36 additions & 32 deletions src/Component/Metadata/AsResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,40 +17,44 @@
final class AsResource
{
public function __construct(
?string $alias = null,
?string $section = null,
?string $formType = null,
?string $templatesDir = null,
?string $routePrefix = null,
?string $name = null,
?string $pluralName = null,
?string $applicationName = null,
?string $identifier = null,
?array $normalizationContext = null,
?array $denormalizationContext = null,
?array $validationContext = null,
?string $class = null,
string|false|null $driver = null,
?array $vars = null,
?array $operations = null,
private ?string $alias = null,
private ?string $section = null,
private ?string $formType = null,
private ?string $templatesDir = null,
private ?string $routePrefix = null,
private ?string $name = null,
private ?string $pluralName = null,
private ?string $applicationName = null,
private ?string $identifier = null,
private ?array $normalizationContext = null,
private ?array $denormalizationContext = null,
private ?array $validationContext = null,
private ?string $class = null,
private string|false|null $driver = null,
private ?array $vars = null,
private ?array $operations = null,
) {
}

public function toMetadata(): ResourceMetadata
{
return new ResourceMetadata(
alias: $alias,
section: $section,
formType: $formType,
templatesDir: $templatesDir,
routePrefix: $routePrefix,
name: $name,
pluralName: $pluralName,
applicationName: $applicationName,
identifier: $identifier,
normalizationContext: $normalizationContext,
denormalizationContext: $denormalizationContext,
validationContext: $validationContext,
class: $class,
driver: $driver,
vars: $vars,
operations: $operations,
alias: $this->alias,
section: $this->section,
formType: $this->formType,
templatesDir: $this->templatesDir,
routePrefix: $this->routePrefix,
name: $this->name,
pluralName: $this->pluralName,
applicationName: $this->applicationName,
identifier: $this->identifier,
normalizationContext: $this->normalizationContext,
denormalizationContext: $this->denormalizationContext,
validationContext: $this->validationContext,
class: $this->class,
driver: $this->driver,
vars: $this->vars,
operations: $this->operations,
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

namespace Sylius\Component\Resource\Metadata\Resource\Factory;

use Sylius\Component\Resource\Metadata\AsResource;
use Sylius\Component\Resource\Metadata\HttpOperation;
use Sylius\Component\Resource\Metadata\MetadataInterface;
use Sylius\Component\Resource\Metadata\Operation;
Expand Down Expand Up @@ -57,9 +58,11 @@ private function buildResourceOperations(array $attributes, string $resourceClas
$index = -1;

foreach ($attributes as $attribute) {
if (is_a($attribute->getName(), ResourceMetadata::class, true)) {
/** @var ResourceMetadata $resource */
$resource = $attribute->newInstance();
if (is_a($attribute->getName(), AsResource::class, true)) {
/** @var AsResource $resourceAttribute */
$resourceAttribute = $attribute->newInstance();
$resource = $this->convertResourceAttributeToMetadata($resourceAttribute);

$resourceAlias = $resource->getAlias();

if (null !== $resourceAlias) {
Expand Down Expand Up @@ -221,4 +224,26 @@ private function buildFormOptions(Operation $operation, MetadataInterface $resou

return $formOptions;
}

private function convertResourceAttributeToMetadata(AsResource $resourceAttribute): ResourceMetadata
{
return new ResourceMetadata(
alias: $resourceAttribute->alias(),
section: $resourceAttribute->section(),
formType: $resourceAttribute->formType(),
templatesDir: $resourceAttribute->templatesDir(),
routePrefix: $resourceAttribute->routePrefix(),
name: $resourceAttribute->name(),
pluralName: $resourceAttribute->pluralName(),
applicationName: $resourceAttribute->applicationName(),
identifier: $resourceAttribute->identifier(),
normalizationContext: $resourceAttribute->normalizationContext(),
denormalizationContext: $resourceAttribute->denormalizationContext(),
validationContext: $resourceAttribute->validationContext(),
class: $resourceAttribute->class(),
driver: $resourceAttribute->driver(),
vars: $resourceAttribute->vars(),
operations: $resourceAttribute->operations(),
);
}
}

0 comments on commit f6c99b7

Please sign in to comment.