diff --git a/.travis.yml b/.travis.yml index 18bd35af5..5ae3ce4a5 100644 --- a/.travis.yml +++ b/.travis.yml @@ -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) diff --git a/composer.json b/composer.json index af452c6b8..f619b1d64 100644 --- a/composer.json +++ b/composer.json @@ -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", @@ -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", @@ -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", @@ -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": { diff --git a/src/Bundle/Controller/ResourceController.php b/src/Bundle/Controller/ResourceController.php index ec8f15795..7bb4afad4 100644 --- a/src/Bundle/Controller/ResourceController.php +++ b/src/Bundle/Controller/ResourceController.php @@ -39,7 +39,7 @@ class ResourceController extends AbstractController /** @var RequestConfigurationFactoryInterface */ protected $requestConfigurationFactory; - /** @var ViewHandlerInterface */ + /** @var ViewHandlerInterface|null */ protected $viewHandler; /** @var RepositoryInterface */ @@ -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, @@ -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 @@ -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 @@ -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(); @@ -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); @@ -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()); @@ -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(); @@ -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); @@ -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()); @@ -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(); @@ -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()); @@ -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); @@ -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()); @@ -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(); @@ -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); + } } diff --git a/src/Bundle/DependencyInjection/Driver/AbstractDriver.php b/src/Bundle/DependencyInjection/Driver/AbstractDriver.php index dc679faa1..b6ba561bc 100644 --- a/src/Bundle/DependencyInjection/Driver/AbstractDriver.php +++ b/src/Bundle/DependencyInjection/Driver/AbstractDriver.php @@ -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; @@ -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'), diff --git a/src/Bundle/Resources/config/services/controller.xml b/src/Bundle/Resources/config/services/controller.xml index 031572fad..971840962 100644 --- a/src/Bundle/Resources/config/services/controller.xml +++ b/src/Bundle/Resources/config/services/controller.xml @@ -48,7 +48,7 @@ - + diff --git a/src/Bundle/test/app/AppKernel.php b/src/Bundle/test/app/AppKernel.php index fb4fec68d..914bf60be 100644 --- a/src/Bundle/test/app/AppKernel.php +++ b/src/Bundle/test/app/AppKernel.php @@ -22,21 +22,31 @@ 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; } /** @@ -44,7 +54,7 @@ public function registerBundles() */ public function registerContainerConfiguration(LoaderInterface $loader) { - $loader->load(__DIR__ . '/config/config.yml'); + $loader->load(__DIR__ . '/config/config_' . $this->getEnvironment() . '.yml'); } /** diff --git a/src/Bundle/test/app/config/config.yml b/src/Bundle/test/app/config/config.yml index 630353b8a..bff3a77a9 100644 --- a/src/Bundle/test/app/config/config.yml +++ b/src/Bundle/test/app/config/config.yml @@ -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: diff --git a/src/Bundle/test/app/config/config_dev.yml b/src/Bundle/test/app/config/config_dev.yml new file mode 100644 index 000000000..fda294dc3 --- /dev/null +++ b/src/Bundle/test/app/config/config_dev.yml @@ -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 diff --git a/src/Bundle/test/app/config/config_prod.yml b/src/Bundle/test/app/config/config_prod.yml new file mode 100644 index 000000000..8e0b6ff02 --- /dev/null +++ b/src/Bundle/test/app/config/config_prod.yml @@ -0,0 +1,2 @@ +imports: + - { resource: config.yml } diff --git a/src/Bundle/test/app/config/config_test.yml b/src/Bundle/test/app/config/config_test.yml new file mode 100644 index 000000000..a3b2db43c --- /dev/null +++ b/src/Bundle/test/app/config/config_test.yml @@ -0,0 +1,2 @@ +imports: + - { resource: config_dev.yml } diff --git a/src/Bundle/test/app/console b/src/Bundle/test/app/console index 66798294b..c6b192f78 100755 --- a/src/Bundle/test/app/console +++ b/src/Bundle/test/app/console @@ -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);