Skip to content

Commit

Permalink
Remove rest dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
loic425 committed Oct 12, 2020
1 parent 595ad9d commit 1f86800
Show file tree
Hide file tree
Showing 11 changed files with 89 additions and 60 deletions.
4 changes: 4 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,7 @@ script:
- (cd src/Component && vendor/bin/phpspec run)

- vendor/bin/phpunit

- composer require doctrine/orm:^2.5 --no-update --no-scripts --prefer-dist
- composer update --no-dev --prefer-dist
- (cd src/Bundle/test && app/console cache:clear --env=prod)
13 changes: 8 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@
"babdev/pagerfanta-bundle": "^2.0",
"doctrine/doctrine-bundle": "^1.12|^2.0",
"doctrine/persistence": "^1.3",
"friendsofsymfony/rest-bundle": "^3.0",
"jms/serializer-bundle": "^3.5",
"stof/doctrine-extensions-bundle": "^1.2",
"sylius/registry": "^1.2",
"symfony/config": "^4.4|^5.0",
Expand All @@ -39,12 +37,13 @@
"symfony/validator": "^4.4|^5.0",
"symfony/yaml": "^4.4|^5.0",
"webmozart/assert": "^1.8",
"willdurand/hateoas-bundle": "^2.0",
"winzou/state-machine-bundle": "^0.3.2|^0.4.3|^0.5"
},
"require-dev": {
"doctrine/orm": "^2.5",
"friendsofsymfony/rest-bundle": "^3.0",
"gedmo/doctrine-extensions": "^2.4",
"jms/serializer-bundle": "^3.5",
"lchrusciel/api-test-case": "^5.0",
"matthiasnoback/symfony-dependency-injection-test": "^4.0",
"pamil/phpspec-skip-example-extension": "^4.1",
Expand All @@ -58,7 +57,8 @@
"sylius/grid-bundle": "^1.2",
"symfony/dependency-injection": "^4.4|^5.0",
"twig/twig": "^2.12|^3.0",
"vimeo/psalm": "3.16"
"vimeo/psalm": "3.16",
"willdurand/hateoas-bundle": "^2.0"
},
"suggest": {
"doctrine/orm": "^2.5",
Expand Down Expand Up @@ -92,7 +92,10 @@
]
},
"conflict": {
"amphp/amp": "^2.4.3"
"amphp/amp": "^2.4.3",
"friendsofsymfony/rest-bundle": "<3.0",
"jms/serializer-bundle": "<3.5",
"willdurand/hateoas-bundle": "<2.0"
},
"extra": {
"branch-alias": {
Expand Down
68 changes: 35 additions & 33 deletions src/Bundle/Controller/ResourceController.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class ResourceController extends AbstractController
/** @var RequestConfigurationFactoryInterface */
protected $requestConfigurationFactory;

/** @var ViewHandlerInterface */
/** @var ViewHandlerInterface|null */
protected $viewHandler;

/** @var RepositoryInterface */
Expand Down Expand Up @@ -87,7 +87,7 @@ class ResourceController extends AbstractController
public function __construct(
MetadataInterface $metadata,
RequestConfigurationFactoryInterface $requestConfigurationFactory,
ViewHandlerInterface $viewHandler,
?ViewHandlerInterface $viewHandler,
RepositoryInterface $repository,
FactoryInterface $factory,
NewResourceFactoryInterface $newResourceFactory,
Expand Down Expand Up @@ -140,9 +140,7 @@ public function showAction(Request $request): Response
]);
}

$view = View::create($resource);

return $this->viewHandler->handle($configuration, $view);
return $this->createRestView($configuration, $resource);
}

public function indexAction(Request $request): Response
Expand All @@ -163,9 +161,7 @@ public function indexAction(Request $request): Response
]);
}

$view = View::create($resources);

return $this->viewHandler->handle($configuration, $view);
return $this->createRestView($configuration, $resources);
}

public function createAction(Request $request): Response
Expand Down Expand Up @@ -210,7 +206,7 @@ public function createAction(Request $request): Response
$postEvent = $this->eventDispatcher->dispatchPostEvent(ResourceActions::CREATE, $configuration, $newResource);

if (!$configuration->isHtmlRequest()) {
return $this->viewHandler->handle($configuration, View::create($newResource, Response::HTTP_CREATED));
return $this->createRestView($configuration, $newResource, Response::HTTP_CREATED);
}

$postEventResponse = $postEvent->getResponse();
Expand All @@ -222,7 +218,7 @@ public function createAction(Request $request): Response
}

if (!$configuration->isHtmlRequest()) {
return $this->viewHandler->handle($configuration, View::create($form, Response::HTTP_BAD_REQUEST));
return $this->createRestView($configuration, $form, Response::HTTP_BAD_REQUEST);
}

$initializeEvent = $this->eventDispatcher->dispatchInitializeEvent(ResourceActions::CREATE, $configuration, $newResource);
Expand Down Expand Up @@ -278,10 +274,7 @@ public function updateAction(Request $request): Response
$this->resourceUpdateHandler->handle($resource, $configuration, $this->manager);
} catch (UpdateHandlingException $exception) {
if (!$configuration->isHtmlRequest()) {
return $this->viewHandler->handle(
$configuration,
View::create($form, $exception->getApiResponseCode())
);
return $this->createRestView($configuration, $form, $exception->getApiResponseCode());
}

$this->flashHelper->addErrorFlash($configuration, $exception->getFlash());
Expand All @@ -296,9 +289,11 @@ public function updateAction(Request $request): Response
$postEvent = $this->eventDispatcher->dispatchPostEvent(ResourceActions::UPDATE, $configuration, $resource);

if (!$configuration->isHtmlRequest()) {
$view = $configuration->getParameters()->get('return_content', false) ? View::create($resource, Response::HTTP_OK) : View::create(null, Response::HTTP_NO_CONTENT);
if ($configuration->getParameters()->get('return_content', false)) {
return $this->createRestView($configuration, $resource, Response::HTTP_OK);
}

return $this->viewHandler->handle($configuration, $view);
return $this->createRestView($configuration, null, Response::HTTP_NO_CONTENT);
}

$postEventResponse = $postEvent->getResponse();
Expand All @@ -310,7 +305,7 @@ public function updateAction(Request $request): Response
}

if (!$configuration->isHtmlRequest()) {
return $this->viewHandler->handle($configuration, View::create($form, Response::HTTP_BAD_REQUEST));
return $this->createRestView($configuration, $form, Response::HTTP_BAD_REQUEST);
}

$initializeEvent = $this->eventDispatcher->dispatchInitializeEvent(ResourceActions::UPDATE, $configuration, $resource);
Expand Down Expand Up @@ -359,10 +354,7 @@ public function deleteAction(Request $request): Response
$this->resourceDeleteHandler->handle($resource, $this->repository);
} catch (DeleteHandlingException $exception) {
if (!$configuration->isHtmlRequest()) {
return $this->viewHandler->handle(
$configuration,
View::create(null, $exception->getApiResponseCode())
);
return $this->createRestView($configuration, null, $exception->getApiResponseCode());
}

$this->flashHelper->addErrorFlash($configuration, $exception->getFlash());
Expand All @@ -377,7 +369,7 @@ public function deleteAction(Request $request): Response
$postEvent = $this->eventDispatcher->dispatchPostEvent(ResourceActions::DELETE, $configuration, $resource);

if (!$configuration->isHtmlRequest()) {
return $this->viewHandler->handle($configuration, View::create(null, Response::HTTP_NO_CONTENT));
return $this->createRestView($configuration, null, Response::HTTP_NO_CONTENT);
}

$postEventResponse = $postEvent->getResponse();
Expand Down Expand Up @@ -425,10 +417,7 @@ public function bulkDeleteAction(Request $request): Response
$this->resourceDeleteHandler->handle($resource, $this->repository);
} catch (DeleteHandlingException $exception) {
if (!$configuration->isHtmlRequest()) {
return $this->viewHandler->handle(
$configuration,
View::create(null, $exception->getApiResponseCode())
);
return $this->createRestView($configuration, null, $exception->getApiResponseCode());
}

$this->flashHelper->addErrorFlash($configuration, $exception->getFlash());
Expand All @@ -440,7 +429,7 @@ public function bulkDeleteAction(Request $request): Response
}

if (!$configuration->isHtmlRequest()) {
return $this->viewHandler->handle($configuration, View::create(null, Response::HTTP_NO_CONTENT));
return $this->createRestView($configuration, null, Response::HTTP_NO_CONTENT);
}

$this->flashHelper->addSuccessFlash($configuration, ResourceActions::BULK_DELETE);
Expand Down Expand Up @@ -490,10 +479,7 @@ public function applyStateMachineTransitionAction(Request $request): Response
$this->resourceUpdateHandler->handle($resource, $configuration, $this->manager);
} catch (UpdateHandlingException $exception) {
if (!$configuration->isHtmlRequest()) {
return $this->viewHandler->handle(
$configuration,
View::create($resource, $exception->getApiResponseCode())
);
return $this->createRestView($configuration, $resource, $exception->getApiResponseCode());
}

$this->flashHelper->addErrorFlash($configuration, $exception->getFlash());
Expand All @@ -508,9 +494,11 @@ public function applyStateMachineTransitionAction(Request $request): Response
$postEvent = $this->eventDispatcher->dispatchPostEvent(ResourceActions::UPDATE, $configuration, $resource);

if (!$configuration->isHtmlRequest()) {
$view = $configuration->getParameters()->get('return_content', true) ? View::create($resource, Response::HTTP_OK) : View::create(null, Response::HTTP_NO_CONTENT);
if ($configuration->getParameters()->get('return_content', true)) {
return $this->createRestView($configuration, $resource, Response::HTTP_OK);
}

return $this->viewHandler->handle($configuration, $view);
return $this->createRestView($configuration, null, Response::HTTP_NO_CONTENT);
}

$postEventResponse = $postEvent->getResponse();
Expand Down Expand Up @@ -548,4 +536,18 @@ protected function findOr404(RequestConfiguration $configuration): ResourceInter

return $resource;
}

/**
* @param mixed $data
*/
protected function createRestView(RequestConfiguration $configuration, $data, int $statusCode = null): Response
{
if (null === $this->viewHandler) {
throw new \LogicException('You can not use the "non-html" request if FriendsOfSymfony Rest Bundle is not available. Try running "composer require friendsofsymfony/rest-bundle".');
}

$view = View::create($data, $statusCode);

return $this->viewHandler->handle($configuration, $view);
}
}
5 changes: 4 additions & 1 deletion src/Bundle/DependencyInjection/Driver/AbstractDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

namespace Sylius\Bundle\ResourceBundle\DependencyInjection\Driver;

use FOS\RestBundle\FOSRestBundle;
use Sylius\Component\Resource\Factory\Factory;
use Sylius\Component\Resource\Factory\TranslatableFactoryInterface;
use Sylius\Component\Resource\Metadata\Metadata;
Expand Down Expand Up @@ -60,13 +61,15 @@ protected function setClassesParameters(ContainerBuilder $container, MetadataInt

protected function addController(ContainerBuilder $container, MetadataInterface $metadata): void
{
$viewHandler = new Reference('sylius.resource_controller.view_handler', ContainerInterface::NULL_ON_INVALID_REFERENCE);

$definition = new Definition($metadata->getClass('controller'));
$definition
->setPublic(true)
->setArguments([
$this->getMetadataDefinition($metadata),
new Reference('sylius.resource_controller.request_configuration_factory'),
new Reference('sylius.resource_controller.view_handler'),
$viewHandler,
new Reference($metadata->getServiceId('repository')),
new Reference($metadata->getServiceId('factory')),
new Reference('sylius.resource_controller.new_resource_factory'),
Expand Down
2 changes: 1 addition & 1 deletion src/Bundle/Resources/config/services/controller.xml
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
<argument type="service" id="event_dispatcher" />
</service>
<service id="sylius.resource_controller.view_handler" class="Sylius\Bundle\ResourceBundle\Controller\ViewHandler" public="false">
<argument type="service" id="fos_rest.view_handler" />
<argument type="service" id="fos_rest.view_handler" on-invalid="null" />
</service>
<service id="sylius.resource_controller.state_machine" class="Sylius\Bundle\ResourceBundle\Controller\StateMachine" public="false">
<argument type="service" id="sm.factory" />
Expand Down
26 changes: 18 additions & 8 deletions src/Bundle/test/app/AppKernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,29 +22,39 @@ class AppKernel extends Kernel
*/
public function registerBundles()
{
return [
if ('prod' === $this->getEnvironment()) {
$loader = require __DIR__ . '/../../../../vendor/autoload.php';
$loader->addPsr4('AppBundle\\', __DIR__ . '/../src/AppBundle/');
}

$bundles = [
new Symfony\Bundle\FrameworkBundle\FrameworkBundle(),
new FOS\RestBundle\FOSRestBundle(),
new JMS\SerializerBundle\JMSSerializerBundle(),
new Doctrine\Bundle\DoctrineBundle\DoctrineBundle(),
new Sylius\Bundle\ResourceBundle\SyliusResourceBundle(),
new BabDev\PagerfantaBundle\BabDevPagerfantaBundle(),
new Bazinga\Bundle\HateoasBundle\BazingaHateoasBundle(),
new Symfony\Bundle\TwigBundle\TwigBundle(),
new \winzou\Bundle\StateMachineBundle\winzouStateMachineBundle(),
new winzou\Bundle\StateMachineBundle\winzouStateMachineBundle(),
new Stof\DoctrineExtensionsBundle\StofDoctrineExtensionsBundle(),
new Fidry\AliceDataFixtures\Bridge\Symfony\FidryAliceDataFixturesBundle(),
new Nelmio\Alice\Bridge\Symfony\NelmioAliceBundle(),
new AppBundle\AppBundle(),
];

if (in_array($this->getEnvironment(), ['dev', 'test'])) {
$bundles[] = new FOS\RestBundle\FOSRestBundle();
$bundles[] = new JMS\SerializerBundle\JMSSerializerBundle();
$bundles[] = new Bazinga\Bundle\HateoasBundle\BazingaHateoasBundle();
$bundles[] = new Fidry\AliceDataFixtures\Bridge\Symfony\FidryAliceDataFixturesBundle();
$bundles[] = new Nelmio\Alice\Bridge\Symfony\NelmioAliceBundle();
}

return $bundles;
}

/**
* {@inheritdoc}
*/
public function registerContainerConfiguration(LoaderInterface $loader)
{
$loader->load(__DIR__ . '/config/config.yml');
$loader->load(__DIR__ . '/config/config_' . $this->getEnvironment() . '.yml');
}

/**
Expand Down
11 changes: 0 additions & 11 deletions src/Bundle/test/app/config/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,6 @@ doctrine:
default:
auto_mapping: true

fos_rest:
view:
formats:
json: true
empty_content: 204
format_listener:
rules:
- { path: '^/', priorities: ['json'], fallback_format: json, prefer_extension: true }
exception:
enabled: false

stof_doctrine_extensions:
default_locale: "%locale%"
orm:
Expand Down
13 changes: 13 additions & 0 deletions src/Bundle/test/app/config/config_dev.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
imports:
- { resource: config.yml }

fos_rest:
view:
formats:
json: true
empty_content: 204
format_listener:
rules:
- { path: '^/', priorities: ['json'], fallback_format: json, prefer_extension: true }
exception:
enabled: false
2 changes: 2 additions & 0 deletions src/Bundle/test/app/config/config_prod.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
imports:
- { resource: config.yml }
2 changes: 2 additions & 0 deletions src/Bundle/test/app/config/config_test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
imports:
- { resource: config_dev.yml }
3 changes: 2 additions & 1 deletion src/Bundle/test/app/console
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Component\Console\Input\ArgvInput;

$input = new ArgvInput();
$env = $input->getParameterOption(array('--env', '-e'), getenv('SYMFONY_ENV') ?: 'test');

$application = new Application(new AppKernel('test', true));
$application = new Application(new AppKernel($env, true));
$application->run($input);

0 comments on commit 1f86800

Please sign in to comment.