Skip to content

Commit

Permalink
Merge pull request #338 from dotkernel/issue-337
Browse files Browse the repository at this point in the history
Issue #337: Implemented handle delegators
  • Loading branch information
arhimede authored Oct 29, 2024
2 parents 9060c52 + dce1cbd commit a079e0d
Show file tree
Hide file tree
Showing 33 changed files with 312 additions and 405 deletions.
24 changes: 14 additions & 10 deletions src/Admin/src/ConfigProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
use Api\Admin\Service\AdminService;
use Api\Admin\Service\AdminServiceInterface;
use Api\App\ConfigProvider as AppConfigProvider;
use Api\App\Factory\HandlerDelegatorFactory;
use Doctrine\ORM\Mapping\Driver\AttributeDriver;
use Dot\DependencyInjection\Factory\AttributedRepositoryFactory;
use Dot\DependencyInjection\Factory\AttributedServiceFactory;
Expand All @@ -43,25 +44,28 @@ public function getDependencies(): array
{
return [
'delegators' => [
Application::class => [
RoutesDelegator::class,
],
Application::class => [RoutesDelegator::class],
AdminAccountHandler::class => [HandlerDelegatorFactory::class],
AdminCollectionHandler::class => [HandlerDelegatorFactory::class],
AdminHandler::class => [HandlerDelegatorFactory::class],
AdminRoleCollectionHandler::class => [HandlerDelegatorFactory::class],
AdminRoleHandler::class => [HandlerDelegatorFactory::class],
],
'factories' => [
AdminHandler::class => AttributedServiceFactory::class,
AdminCollectionHandler::class => AttributedServiceFactory::class,
AdminAccountHandler::class => AttributedServiceFactory::class,
AdminRoleHandler::class => AttributedServiceFactory::class,
AdminRoleCollectionHandler::class => AttributedServiceFactory::class,
AdminService::class => AttributedServiceFactory::class,
AdminRoleService::class => AttributedServiceFactory::class,
AdminCollectionHandler::class => AttributedServiceFactory::class,
AdminCreateCommand::class => AdminCreateCommandFactory::class,
AdminHandler::class => AttributedServiceFactory::class,
AdminRepository::class => AttributedRepositoryFactory::class,
AdminRoleCollectionHandler::class => AttributedServiceFactory::class,
AdminRoleHandler::class => AttributedServiceFactory::class,
AdminRoleRepository::class => AttributedRepositoryFactory::class,
AdminRoleService::class => AttributedServiceFactory::class,
AdminService::class => AttributedServiceFactory::class,
],
'aliases' => [
AdminServiceInterface::class => AdminService::class,
AdminRoleServiceInterface::class => AdminRoleService::class,
AdminServiceInterface::class => AdminService::class,
],
];
}
Expand Down
13 changes: 2 additions & 11 deletions src/Admin/src/Handler/AdminAccountHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,17 @@
use Api\App\Exception\BadRequestException;
use Api\App\Exception\ConflictException;
use Api\App\Exception\NotFoundException;
use Api\App\Handler\HandlerTrait;
use Api\App\Handler\AbstractHandler;
use Dot\DependencyInjection\Attribute\Inject;
use Mezzio\Hal\HalResponseFactory;
use Mezzio\Hal\ResourceGenerator;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;

class AdminAccountHandler implements RequestHandlerInterface
class AdminAccountHandler extends AbstractHandler
{
use HandlerTrait;

#[Inject(
HalResponseFactory::class,
ResourceGenerator::class,
AdminServiceInterface::class,
)]
public function __construct(
protected HalResponseFactory $responseFactory,
protected ResourceGenerator $resourceGenerator,
protected AdminServiceInterface $adminService,
) {
}
Expand Down
13 changes: 2 additions & 11 deletions src/Admin/src/Handler/AdminCollectionHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,17 @@

use Api\Admin\Service\AdminServiceInterface;
use Api\App\Exception\BadRequestException;
use Api\App\Handler\HandlerTrait;
use Api\App\Handler\AbstractHandler;
use Dot\DependencyInjection\Attribute\Inject;
use Mezzio\Hal\HalResponseFactory;
use Mezzio\Hal\ResourceGenerator;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;

class AdminCollectionHandler implements RequestHandlerInterface
class AdminCollectionHandler extends AbstractHandler
{
use HandlerTrait;

#[Inject(
HalResponseFactory::class,
ResourceGenerator::class,
AdminServiceInterface::class,
)]
public function __construct(
protected HalResponseFactory $responseFactory,
protected ResourceGenerator $resourceGenerator,
protected AdminServiceInterface $adminService,
) {
}
Expand Down
13 changes: 2 additions & 11 deletions src/Admin/src/Handler/AdminHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,17 @@
use Api\App\Exception\BadRequestException;
use Api\App\Exception\ConflictException;
use Api\App\Exception\NotFoundException;
use Api\App\Handler\HandlerTrait;
use Api\App\Handler\AbstractHandler;
use Dot\DependencyInjection\Attribute\Inject;
use Mezzio\Hal\HalResponseFactory;
use Mezzio\Hal\ResourceGenerator;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;

class AdminHandler implements RequestHandlerInterface
class AdminHandler extends AbstractHandler
{
use HandlerTrait;

#[Inject(
HalResponseFactory::class,
ResourceGenerator::class,
AdminServiceInterface::class,
)]
public function __construct(
protected HalResponseFactory $responseFactory,
protected ResourceGenerator $resourceGenerator,
protected AdminServiceInterface $adminService,
) {
}
Expand Down
13 changes: 2 additions & 11 deletions src/Admin/src/Handler/AdminRoleCollectionHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,17 @@

use Api\Admin\Service\AdminRoleServiceInterface;
use Api\App\Exception\BadRequestException;
use Api\App\Handler\HandlerTrait;
use Api\App\Handler\AbstractHandler;
use Dot\DependencyInjection\Attribute\Inject;
use Mezzio\Hal\HalResponseFactory;
use Mezzio\Hal\ResourceGenerator;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;

class AdminRoleCollectionHandler implements RequestHandlerInterface
class AdminRoleCollectionHandler extends AbstractHandler
{
use HandlerTrait;

#[Inject(
HalResponseFactory::class,
ResourceGenerator::class,
AdminRoleServiceInterface::class,
)]
public function __construct(
protected HalResponseFactory $responseFactory,
protected ResourceGenerator $resourceGenerator,
protected AdminRoleServiceInterface $roleService,
) {
}
Expand Down
13 changes: 2 additions & 11 deletions src/Admin/src/Handler/AdminRoleHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,17 @@

use Api\Admin\Service\AdminRoleServiceInterface;
use Api\App\Exception\NotFoundException;
use Api\App\Handler\HandlerTrait;
use Api\App\Handler\AbstractHandler;
use Dot\DependencyInjection\Attribute\Inject;
use Mezzio\Hal\HalResponseFactory;
use Mezzio\Hal\ResourceGenerator;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;

class AdminRoleHandler implements RequestHandlerInterface
class AdminRoleHandler extends AbstractHandler
{
use HandlerTrait;

#[Inject(
HalResponseFactory::class,
ResourceGenerator::class,
AdminRoleServiceInterface::class,
)]
public function __construct(
protected HalResponseFactory $responseFactory,
protected ResourceGenerator $resourceGenerator,
protected AdminRoleServiceInterface $roleService,
) {
}
Expand Down
20 changes: 9 additions & 11 deletions src/App/src/ConfigProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
use Api\App\Entity\EntityListenerResolver;
use Api\App\Factory\AuthenticationMiddlewareFactory;
use Api\App\Factory\EntityListenerResolverFactory;
use Api\App\Factory\HandlerDelegatorFactory;
use Api\App\Factory\RouteListCommandFactory;
use Api\App\Factory\TokenGenerateCommandFactory;
use Api\App\Handler\ErrorReportHandler;
use Api\App\Handler\HomeHandler;
use Api\App\Middleware\AuthenticationMiddleware;
use Api\App\Middleware\AuthorizationMiddleware;
use Api\App\Middleware\ContentNegotiationMiddleware;
Expand Down Expand Up @@ -57,9 +57,8 @@ public function getDependencies(): array
{
return [
'delegators' => [
Application::class => [
RoutesDelegator::class,
],
Application::class => [RoutesDelegator::class],
ErrorReportHandler::class => [HandlerDelegatorFactory::class],
],
'factories' => [
'doctrine.entity_manager.orm_default' => EntityManagerFactory::class,
Expand All @@ -69,24 +68,23 @@ public function getDependencies(): array
AuthorizationMiddleware::class => AttributedServiceFactory::class,
ContentNegotiationMiddleware::class => AttributedServiceFactory::class,
DeprecationMiddleware::class => AttributedServiceFactory::class,
EntityListenerResolver::class => EntityListenerResolverFactory::class,
Environment::class => TwigEnvironmentFactory::class,
TwigExtension::class => TwigExtensionFactory::class,
TwigRenderer::class => TwigRendererFactory::class,
HomeHandler::class => AttributedServiceFactory::class,
ErrorReportHandler::class => AttributedServiceFactory::class,
ErrorReportService::class => AttributedServiceFactory::class,
ErrorResponseMiddleware::class => AttributedServiceFactory::class,
RouteListCommand::class => RouteListCommandFactory::class,
TokenGenerateCommand::class => TokenGenerateCommandFactory::class,
ErrorReportService::class => AttributedServiceFactory::class,
EntityListenerResolver::class => EntityListenerResolverFactory::class,
TwigExtension::class => TwigExtensionFactory::class,
TwigRenderer::class => TwigRendererFactory::class,
],
'aliases' => [
Authentication\AuthenticationInterface::class => Authentication\OAuth2\OAuth2Adapter::class,
MailService::class => 'dot-mail.service.default',
EntityManager::class => 'doctrine.entity_manager.orm_default',
EntityManagerInterface::class => 'doctrine.entity_manager.orm_default',
TemplateRendererInterface::class => TwigRenderer::class,
ErrorReportServiceInterface::class => ErrorReportService::class,
MailService::class => 'dot-mail.service.default',
TemplateRendererInterface::class => TwigRenderer::class,
],
];
}
Expand Down
2 changes: 1 addition & 1 deletion src/App/src/Entity/AbstractEntity.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
use function ucfirst;

#[ORM\MappedSuperclass]
abstract class AbstractEntity implements ArraySerializableInterface
abstract class AbstractEntity implements ArraySerializableInterface, EntityInterface
{
#[ORM\Id]
#[ORM\Column(name: 'uuid', type: "uuid_binary", unique: true)]
Expand Down
9 changes: 9 additions & 0 deletions src/App/src/Entity/EntityInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

declare(strict_types=1);

namespace Api\App\Entity;

interface EntityInterface
{
}
9 changes: 9 additions & 0 deletions src/App/src/Exception/RuntimeException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

declare(strict_types=1);

namespace Api\App\Exception;

class RuntimeException extends \RuntimeException
{
}
47 changes: 47 additions & 0 deletions src/App/src/Factory/HandlerDelegatorFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

declare(strict_types=1);

namespace Api\App\Factory;

use Api\App\Exception\RuntimeException;
use Api\App\Handler\AbstractHandler;
use Api\App\Message;
use Mezzio\Hal\HalResponseFactory;
use Mezzio\Hal\ResourceGenerator;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\ContainerInterface;
use Psr\Container\NotFoundExceptionInterface;
use Psr\Http\Server\RequestHandlerInterface;

use function assert;
use function sprintf;

class HandlerDelegatorFactory
{
/**
* @param class-string $name
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
* @throws RuntimeException
*/
public function __invoke(
ContainerInterface $container,
string $name,
callable $callback
): RequestHandlerInterface {
if (! $container->has(HalResponseFactory::class)) {
throw new RuntimeException(sprintf(Message::SERVICE_NOT_FOUND, HalResponseFactory::class));
}
if (! $container->has(ResourceGenerator::class)) {
throw new RuntimeException(sprintf(Message::SERVICE_NOT_FOUND, ResourceGenerator::class));
}

$handler = $callback();
assert($handler instanceof AbstractHandler);

return $handler
->setResponseFactory($container->get(HalResponseFactory::class))
->setResourceGenerator($container->get(ResourceGenerator::class));
}
}
Loading

0 comments on commit a079e0d

Please sign in to comment.