Skip to content
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

[PoC] No Kernel events #675

Draft
wants to merge 16 commits into
base: poc-new-resource-metadata
Choose a base branch
from
2 changes: 2 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ parameters:
- %currentWorkingDirectory%/src/Bundle/DependencyInjection/Driver/Doctrine/DoctrinePHPCRDriver.php
- %currentWorkingDirectory%/src/Bundle/Doctrine/ODM/*
- %currentWorkingDirectory%/src/Bundle/EventListener/ODM*
- %currentWorkingDirectory%/src/Bundle/Event/ResourceControllerEvent.php
- %currentWorkingDirectory%/src/Bundle/Form/Extension/HttpFoundation/HttpFoundationRequestHandler.php
- %currentWorkingDirectory%/src/Bundle/Routing/ResourceLoader.php
- %currentWorkingDirectory%/src/Bundle/Routing/Configuration.php
Expand Down Expand Up @@ -73,6 +74,7 @@ parameters:
- '/Method Sylius\\Component\\Resource\\Model\\TimestampableInterface\:\:setCreatedAt\(\) has no return type specified\./'
- '/Method Sylius\\Component\\Resource\\Model\\TimestampableInterface\:\:setUpdatedAt\(\) has no return type specified\./'
- '/Method Sylius\\Component\\Resource\\Repository\\InMemoryRepository\:\:findAll\(\) should return array\<object\> but returns array\./'
- '/Method Sylius\\Component\\Resource\\Symfony\\EventDispatcher\\GenericEvent\:\:stop\(\) has no return type specified./'
- '/Method Sylius\\Bundle\\ResourceBundle\\Form\\Extension\\HttpFoundation\\HttpFoundationRequestHandler::handleRequest\(\) has no return type specified./'
- '/Method Sylius\\Bundle\\ResourceBundle\\Grid\\Controller\\ResourcesResolver::getResources\(\) has no return type specified./'
- '/Method Sylius\\Component\\Resource\\Model\\ResourceInterface::getId\(\) has no return type specified./'
Expand Down
6 changes: 6 additions & 0 deletions psalm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,12 @@
<referencedFunction name="Symfony\Contracts\EventDispatcher\EventDispatcherInterface::dispatch" />
</errorLevel>
</TooManyArguments>

<TypeDoesNotContainType>
<errorLevel type="suppress">
<file name="src/Bundle/Event/ResourceControllerEvent.php" />
</errorLevel>
</TypeDoesNotContainType>

<UndefinedClass>
<errorLevel type="suppress">
Expand Down
1 change: 1 addition & 0 deletions src/Bundle/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
/test/app/logs
/test/config/db.sql
/test/var
/test/vendor/
99 changes: 3 additions & 96 deletions src/Bundle/Event/ResourceControllerEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,103 +13,10 @@

namespace Sylius\Bundle\ResourceBundle\Event;

use Symfony\Component\EventDispatcher\GenericEvent;
use Symfony\Component\HttpFoundation\Response;
\class_exists(\Sylius\Component\Resource\Symfony\EventDispatcher\GenericEvent::class);

class ResourceControllerEvent extends GenericEvent
{
public const TYPE_ERROR = 'error';

public const TYPE_WARNING = 'warning';

public const TYPE_INFO = 'info';

public const TYPE_SUCCESS = 'success';

private string $messageType = '';

private string $message = '';

/** @var array */
private $messageParameters = [];

private int $errorCode = 500;

private ?Response $response = null;

/**
* @psalm-suppress MissingReturnType
*/
public function stop(string $message, string $type = self::TYPE_ERROR, array $parameters = [], int $errorCode = 500)
{
$this->messageType = $type;
$this->message = $message;
$this->messageParameters = $parameters;
$this->errorCode = $errorCode;

$this->stopPropagation();
}

public function isStopped(): bool
{
return $this->isPropagationStopped();
}

public function getMessageType(): string
{
return $this->messageType;
}

/**
* @param string $messageType Should be one of ResourceEvent's TYPE constants
*/
public function setMessageType($messageType): void
{
$this->messageType = $messageType;
}

public function getMessage(): string
{
return $this->message;
}

public function setMessage(string $message): void
{
$this->message = $message;
}

public function getMessageParameters(): array
{
return $this->messageParameters;
}

public function setMessageParameters(array $messageParameters): void
{
$this->messageParameters = $messageParameters;
}

public function getErrorCode(): int
{
return $this->errorCode;
}

public function setErrorCode(int $errorCode): void
{
$this->errorCode = $errorCode;
}

public function setResponse(Response $response): void
{
$this->response = $response;
}

public function hasResponse(): bool
{
return null !== $this->response;
}

public function getResponse(): ?Response
if (false) {
class ResourceControllerEvent extends \Sylius\Component\Resource\Symfony\EventDispatcher\GenericEvent
{
return $this->response;
}
}
1 change: 1 addition & 0 deletions src/Bundle/Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
<import resource="services/console.xml" />
<import resource="services/context.xml" />
<import resource="services/controller.xml" />
<import resource="services/dispatcher.xml" />
<import resource="services/form.xml" />
<import resource="services/helper.xml" />
<import resource="services/listener.xml" />
Expand Down
6 changes: 6 additions & 0 deletions src/Bundle/Resources/config/services/controller.xml
Original file line number Diff line number Diff line change
Expand Up @@ -83,5 +83,11 @@

<service id="sylius.resource_controller.resource_delete_handler" class="Sylius\Bundle\ResourceBundle\Controller\ResourceDeleteHandler" public="false" />
<service id="Sylius\Bundle\ResourceBundle\Controller\ResourceDeleteHandlerInterface" alias="sylius.resource_controller.resource_delete_handler" public="false" />

<service id="Sylius\Component\Resource\Action\ResourceAction">
<argument type="service" id="sylius.resource_metadata_operation.initiator.http_operation" />
<argument type="service" id="sylius.context.initiator.request_context" />
<argument type="service" id="sylius.state.processor" />
</service>
</services>
</container>
26 changes: 26 additions & 0 deletions src/Bundle/Resources/config/services/dispatcher.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?xml version="1.0" encoding="UTF-8"?>

<!--

This file is part of the Sylius package.

(c) Paweł Jędrzejewski

For the full copyright and license information, please view the LICENSE
file that was distributed with this source code.

-->

<container xmlns="http://symfony.com/schema/dic/services" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
<services>
<service id="sylius.dispatcher.operation" class="Sylius\Component\Resource\Symfony\EventDispatcher\OperationEventDispatcher">
<argument type="service" id="event_dispatcher" />
</service>
<service id="Sylius\Component\Resource\Symfony\EventDispatcher\OperationEventDispatcherInterface" alias="sylius.dispatcher.operation" />

<service id="sylius.event_handler.operation" class="Sylius\Component\Resource\Symfony\EventDispatcher\OperationEventHandler">
<argument type="service" id="sylius.routing.redirect_handler" />
</service>
<service id="Sylius\Component\Resource\Symfony\EventDispatcher\OperationEventHandlerInterface" alias="sylius.event_handler.operation" />
</services>
</container>
28 changes: 14 additions & 14 deletions src/Bundle/Resources/config/services/listener.xml
Original file line number Diff line number Diff line change
Expand Up @@ -38,21 +38,21 @@
<argument type="service" id="sylius.resource_metadata_operation.initiator.http_operation" />
<argument type="service" id="sylius.context.initiator.request_context" />
<argument type="service" id="sylius.form.factory" />
<tag name="kernel.event_listener" event="kernel.view" priority="128" />
<tag name="kernel.event_listener" event="kernel.request" priority="1" />
</service>

<service id="sylius.listener.validate" class="Sylius\Component\Resource\Symfony\EventListener\ValidateListener">
<argument type="service" id="sylius.resource_metadata_operation.initiator.http_operation" />
<argument type="service" id="validator" />
<tag name="kernel.event_listener" event="kernel.view" priority="64" />
<tag name="kernel.event_listener" event="kernel.request" priority="0" />
</service>

<service id="sylius.listener.write" class="Sylius\Component\Resource\Symfony\EventListener\WriteListener">
<argument type="service" id="sylius.resource_metadata_operation.initiator.http_operation" />
<argument type="service" id="sylius.context.initiator.request_context" />
<argument type="service" id="sylius.state.processor" />
<tag name="kernel.event_listener" event="kernel.view" priority="32" />
</service>
<!-- <service id="sylius.listener.write" class="Sylius\Component\Resource\Symfony\EventListener\WriteListener">-->
<!-- <argument type="service" id="sylius.resource_metadata_operation.initiator.http_operation" />-->
<!-- <argument type="service" id="sylius.context.initiator.request_context" />-->
<!-- <argument type="service" id="sylius.state.processor" />-->
<!-- <tag name="kernel.event_listener" event="kernel.view" priority="32" />-->
<!-- </service>-->

<service id="sylius.listener.serialize" class="Sylius\Component\Resource\Symfony\EventListener\SerializeListener">
<argument type="service" id="sylius.resource_metadata_operation.initiator.http_operation" />
Expand All @@ -67,12 +67,12 @@
<tag name="kernel.event_listener" event="kernel.view" priority="16" />
</service>

<service id="sylius.listener.respond" class="Sylius\Component\Resource\Symfony\EventListener\RespondListener">
<argument type="service" id="sylius.resource_metadata_operation.initiator.http_operation" />
<argument type="service" id="sylius.context.initiator.request_context" />
<argument type="service" id="sylius.state.responder" />
<tag name="kernel.event_listener" event="kernel.view" priority="8" />
</service>
<!-- <service id="sylius.listener.respond" class="Sylius\Component\Resource\Symfony\EventListener\RespondListener">-->
<!-- <argument type="service" id="sylius.resource_metadata_operation.initiator.http_operation" />-->
<!-- <argument type="service" id="sylius.context.initiator.request_context" />-->
<!-- <argument type="service" id="sylius.state.responder" />-->
<!-- <tag name="kernel.event_listener" event="kernel.view" priority="8" />-->
<!-- </service>-->

<service id="sylius.listener.exception.validation" class="Sylius\Component\Resource\Symfony\Validator\EventListener\ValidationExceptionListener">

Expand Down
7 changes: 7 additions & 0 deletions src/Bundle/Resources/config/services/metadata.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,13 @@
<argument type="service" id=".inner" />
</service>

<service id="sylius.resource_metadata_collection.factory.event_short_name"
class="Sylius\Component\Resource\Metadata\Resource\Factory\EventShortNameResourceMetadataCollectionFactory"
decorates="sylius.resource_metadata_collection.factory.attributes"
>
<argument type="service" id=".inner" />
</service>

<service id="sylius.resource_metadata_operation.initiator.http_operation" class="Sylius\Component\Resource\Metadata\Operation\HttpOperationInitiator">
<argument type="service" id="sylius.resource_registry" />
<argument type="service" id="sylius.resource_metadata_collection.factory.attributes" />
Expand Down
3 changes: 3 additions & 0 deletions src/Bundle/Resources/config/services/routing.xml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
<service id="Sylius\Bundle\ResourceBundle\Routing\RoutesAttributesLoader" alias="sylius.routing.loader.routes_attributes" public="false" />

<service id="sylius.routing.factory.operation_route_name_factory" class="Sylius\Component\Resource\Symfony\Routing\Factory\OperationRouteNameFactory" public="false" />
<service id="Sylius\Component\Resource\Symfony\Routing\Factory\OperationRouteNameFactoryInterface" alias="sylius.routing.factory.operation_route_name_factory" />

<service id="sylius.routing.factory.route_attributes" class="Sylius\Bundle\ResourceBundle\Routing\RouteAttributesFactory" public="false" />
<service id="Sylius\Bundle\ResourceBundle\Routing\RouteAttributesFactoryInterface" alias="sylius.routing.factory.route_attributes" />
Expand All @@ -63,6 +64,8 @@
<service id="sylius.routing.redirect_handler" class="Sylius\Component\Resource\Symfony\Routing\RedirectHandler">
<argument type="service" id="router" />
<argument type="service" id="sylius.routing.argument_parser" />
<argument type="service" id="sylius.routing.factory.operation_route_name_factory" />
</service>
<service id="Sylius\Component\Resource\Symfony\Routing\RedirectHandlerInterface" alias="sylius.routing.redirect_handler" />
</services>
</container>
53 changes: 53 additions & 0 deletions src/Bundle/Resources/config/services/state.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,64 @@
</service>
<service id="Sylius\Component\Resource\State\ProviderInterface" alias="sylius.state.provider" />

<service id="sylius.state.provider.event_dispatcher"
class="Sylius\Component\Resource\State\EventDispatcherProvider"
decorates="sylius.state.provider"
decoration-priority="-32"
>
<argument type="service" id=".inner" />
<argument type="service" id="sylius.dispatcher.operation" />
</service>

<service id="sylius.state.processor" class="Sylius\Component\Resource\State\Processor">
<argument type="tagged_locator" tag="sylius.state_processor" />
</service>
<service id="Sylius\Component\Resource\State\ProcessorInterface" alias="sylius.state.processor" />

<service id="sylius.state.processor.event_dispatcher"
class="Sylius\Component\Resource\State\EventDispatcherProcessor"
decorates="sylius.state.processor"
decoration-priority="-32"
>
<argument type="service" id=".inner" />
<argument type="service" id="sylius.dispatcher.operation" />
<argument type="service" id="sylius.event_handler.operation" />
</service>

<service id="sylius.state.processor.bulk_aware"
class="Sylius\Component\Resource\State\BulkAwareProcessor"
decorates="sylius.state.processor"
decoration-priority="-64"
>
<argument type="service" id=".inner" />
</service>

<service id="sylius.state.processor.event_dispatcher_bulk_aware"
class="Sylius\Component\Resource\State\EventDispatcherBulkAwareProcessor"
decorates="sylius.state.processor.bulk_aware"
decoration-priority="-32"
>
<argument type="service" id=".inner" />
<argument type="service" id="sylius.dispatcher.operation" />
</service>

<service id="sylius.state.processor.write"
class="Sylius\Component\Resource\State\WriteProcessor"
decorates="sylius.state.processor.event_dispatcher_bulk_aware"
decoration-priority="-32"
>
<argument type="service" id=".inner" />
</service>

<service id="sylius.state.processor.respond"
class="Sylius\Component\Resource\State\RespondProcessor"
decorates="sylius.state.processor.event_dispatcher_bulk_aware"
decoration-priority="-64"
>
<argument type="service" id=".inner" />
<argument type="service" id="sylius.state.responder" />
</service>

<service id="sylius.state.responder" class="Sylius\Component\Resource\State\Responder">
<argument type="tagged_locator" tag="sylius.state_responder" />
</service>
Expand Down
4 changes: 2 additions & 2 deletions src/Bundle/Routing/OperationRouteFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
namespace Sylius\Bundle\ResourceBundle\Routing;

use Gedmo\Sluggable\Util\Urlizer;
use Sylius\Component\Resource\Action\PlaceHolderAction;
use Sylius\Component\Resource\Action\ResourceAction;
use Sylius\Component\Resource\Metadata\BulkOperationInterface;
use Sylius\Component\Resource\Metadata\CollectionOperationInterface;
use Sylius\Component\Resource\Metadata\CreateOperationInterface;
Expand All @@ -39,7 +39,7 @@ public function create(MetadataInterface $metadata, Resource $resource, HttpOper
return new Route(
path: $routePath,
defaults: [
'_controller' => PlaceHolderAction::class,
'_controller' => ResourceAction::class,
'_sylius' => $this->getSyliusOptions($resource, $operation),
],
methods: $operation->getMethods() ?? [],
Expand Down
Loading