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
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,18 @@
namespace Sylius\Resource\Metadata\Resource\Factory;

use Sylius\Resource\Metadata\AsResource;
use Sylius\Resource\Metadata\HttpOperation;
use Sylius\Resource\Metadata\MetadataInterface;
use Sylius\Resource\Metadata\Operation;
use Sylius\Resource\Metadata\Operations;
use Sylius\Resource\Metadata\RegistryInterface;
use Sylius\Resource\Metadata\Resource\ResourceMetadataCollection;
use Sylius\Resource\Metadata\ResourceMetadata;
use Sylius\Resource\Reflection\ClassReflection;
use Sylius\Resource\Symfony\Request\State\Responder;
use Sylius\Resource\Symfony\Routing\Factory\RouteName\OperationRouteNameFactory;

final class AttributesResourceMetadataCollectionFactory implements ResourceMetadataCollectionFactoryInterface
{
use OperationDefaultsTrait;

public function __construct(
private RegistryInterface $resourceRegistry,
private OperationRouteNameFactory $operationRouteNameFactory,
Expand Down Expand Up @@ -77,7 +76,7 @@ private function buildResourceOperations(array $attributes, string $resourceClas

/** @var Operation $operation */
foreach ($resource->getOperations() ?? new Operations() as $operation) {
[$key, $operation] = $this->getOperationWithDefaults($resources[$index], $operation);
[$key, $operation] = $this->getOperationWithDefaults($operation, $resources[$index], $this->operationRouteNameFactory, $this->resourceRegistry);
$operations[$key] = $operation;
}

Expand Down Expand Up @@ -107,7 +106,7 @@ private function buildResourceOperations(array $attributes, string $resourceClas
/** @var Operation $operationAttribute */
$operationAttribute = $attribute->newInstance();

[$key, $operation] = $this->getOperationWithDefaults($resources[$index], $operationAttribute);
[$key, $operation] = $this->getOperationWithDefaults($operationAttribute, $resources[$index], $this->operationRouteNameFactory, $this->resourceRegistry);

$operations = $resources[$index]->getOperations() ?? new Operations();

Expand All @@ -117,111 +116,4 @@ private function buildResourceOperations(array $attributes, string $resourceClas

return $resources;
}

private function getResourceWithDefaults(string $resourceClass, ResourceMetadata $resource, MetadataInterface $resourceConfiguration): ResourceMetadata
{
$resource = $resource->withClass($resourceClass);

if (null === $resource->getAlias()) {
$resource = $resource->withAlias($resourceConfiguration->getAlias());
}

if (null === $resource->getApplicationName()) {
$resource = $resource->withApplicationName($resourceConfiguration->getApplicationName());
}

if (null === $resource->getName()) {
$resource = $resource->withName($resourceConfiguration->getName());
}

return $resource;
}

private function getOperationWithDefaults(ResourceMetadata $resource, Operation $operation): array
{
$resourceConfiguration = $this->resourceRegistry->get($resource->getAlias() ?? '');

$operation = $operation->withResource($resource);

if (null === $resource->getName()) {
$resourceName = $resourceConfiguration->getName();

$resource = $resource->withName($resourceName);
$operation = $operation->withResource($resource);
}

if (null === $resource->getPluralName()) {
$resourcePluralName = $resourceConfiguration->getPluralName();

$resource = $resource->withPluralName($resourcePluralName);
$operation = $operation->withResource($resource);
}

if (null === $operation->getNormalizationContext()) {
$operation = $operation->withNormalizationContext($resource->getNormalizationContext());
}

if (null === $operation->getDenormalizationContext()) {
$operation = $operation->withDenormalizationContext($resource->getDenormalizationContext());
}

if (null === $operation->getValidationContext()) {
$operation = $operation->withValidationContext($resource->getValidationContext());
}

$operation = $operation->withResource($resource);

if (null === $operation->getRepository()) {
$operation = $operation->withRepository($resourceConfiguration->getServiceId('repository'));
}

if (null === $operation->getFormType()) {
$formType = $resource->getFormType() ?? $resourceConfiguration->getClass('form');
$operation = $operation->withFormType($formType);
}

$formOptions = $this->buildFormOptions($operation, $resourceConfiguration);
$operation = $operation->withFormOptions($formOptions);

if ($operation instanceof HttpOperation) {
if (null === $operation->getRoutePrefix()) {
$operation = $operation->withRoutePrefix($resource->getRoutePrefix() ?? null);
}

if (null === $operation->getTwigContextFactory()) {
$operation = $operation->withTwigContextFactory('sylius.twig.context.factory.default');
}

if (null === $routeName = $operation->getRouteName()) {
$routeName = $this->operationRouteNameFactory->createRouteName($operation);
$operation = $operation->withRouteName($routeName);
}

if (null === $operation->getResponder()) {
$operation = $operation->withResponder(Responder::class);
}

$operation = $operation->withName($routeName);
}

$operationName = $operation->getName();

return [$operationName, $operation];
}

private function buildFormOptions(Operation $operation, MetadataInterface $resourceConfiguration): array
{
$formOptions = array_merge(
['data_class' => $resourceConfiguration->getClass('model')],
$operation->getFormOptions() ?? [],
);

$validationGroups = $operation->getValidationContext()['groups'] ?? null;

if (null !== $validationGroups) {
$formOptions = array_merge(['validation_groups' => $validationGroups], $formOptions);
}

return $formOptions;
}
}
142 changes: 142 additions & 0 deletions src/Component/src/Metadata/Resource/Factory/OperationDefaultsTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
<?php

/*
* This file is part of the Sylius package.
*
* (c) Sylius Sp. z o.o.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Sylius\Resource\Metadata\Resource\Factory;

use Sylius\Resource\Metadata\HttpOperation;
use Sylius\Resource\Metadata\MetadataInterface;
use Sylius\Resource\Metadata\Operation;
use Sylius\Resource\Metadata\RegistryInterface;
use Sylius\Resource\Metadata\ResourceMetadata;
use Sylius\Resource\Symfony\Request\State\Responder;
use Sylius\Resource\Symfony\Routing\Factory\RouteName\OperationRouteNameFactory;

/**
* @internal
*/
Comment on lines +24 to +26
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/**
* @internal
*/
/** @internal */

trait OperationDefaultsTrait
{
private function getResourceWithDefaults(
string $resourceClass,
ResourceMetadata $resource,
MetadataInterface $resourceConfiguration,
): ResourceMetadata {
$resource = $resource->withClass($resourceClass);

if (null === $resource->getAlias()) {
$resource = $resource->withAlias($resourceConfiguration->getAlias());
}

if (null === $resource->getApplicationName()) {
$resource = $resource->withApplicationName($resourceConfiguration->getApplicationName());
}

if (null === $resource->getName()) {
$resource = $resource->withName($resourceConfiguration->getName());
}

return $resource;
}

private function getOperationWithDefaults(
Operation $operation,
ResourceMetadata $resource,
OperationRouteNameFactory $operationRouteNameFactory,
RegistryInterface $resourceRegistry,
): array {
$resourceConfiguration = $resourceRegistry->get($resource->getAlias() ?? '');

$operation = $operation->withResource($resource);

if (null === $resource->getName()) {
$resourceName = $resourceConfiguration->getName();

$resource = $resource->withName($resourceName);
$operation = $operation->withResource($resource);
}

if (null === $resource->getPluralName()) {
$resourcePluralName = $resourceConfiguration->getPluralName();

$resource = $resource->withPluralName($resourcePluralName);
$operation = $operation->withResource($resource);
}

if (null === $operation->getNormalizationContext()) {
$operation = $operation->withNormalizationContext($resource->getNormalizationContext());
}

if (null === $operation->getDenormalizationContext()) {
$operation = $operation->withDenormalizationContext($resource->getDenormalizationContext());
}

if (null === $operation->getValidationContext()) {
$operation = $operation->withValidationContext($resource->getValidationContext());
}

$operation = $operation->withResource($resource);

if (null === $operation->getRepository()) {
$operation = $operation->withRepository($resourceConfiguration->getServiceId('repository'));
}

if (null === $operation->getFormType()) {
$formType = $resource->getFormType() ?? $resourceConfiguration->getClass('form');
$operation = $operation->withFormType($formType);
}

$formOptions = $this->buildFormOptions($operation, $resourceConfiguration);
$operation = $operation->withFormOptions($formOptions);

if ($operation instanceof HttpOperation) {
if (null === $operation->getRoutePrefix()) {
$operation = $operation->withRoutePrefix($resource->getRoutePrefix() ?? null);
}

if (null === $operation->getTwigContextFactory()) {
$operation = $operation->withTwigContextFactory('sylius.twig.context.factory.default');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unrelated to current PR, but why metadata factory is aware of the service ID? Should be handled via tagging/configuration/interfaces instead.

}

if (null === $routeName = $operation->getRouteName()) {
$routeName = $operationRouteNameFactory->createRouteName($operation);
$operation = $operation->withRouteName($routeName);
}

if (null === $operation->getResponder()) {
$operation = $operation->withResponder(Responder::class);
}

$operation = $operation->withName($routeName);
}

$operationName = $operation->getName();

return [$operationName, $operation];
}

private function buildFormOptions(Operation $operation, MetadataInterface $resourceConfiguration): array
{
$formOptions = array_merge(
['data_class' => $resourceConfiguration->getClass('model')],
$operation->getFormOptions() ?? [],
);

$validationGroups = $operation->getValidationContext()['groups'] ?? null;

if (null !== $validationGroups) {
$formOptions = array_merge(['validation_groups' => $validationGroups], $formOptions);
}

return $formOptions;
}
}