Skip to content

Commit 1c7cc9c

Browse files
authored
refactor #872 Move fos rest, jms serializer and hateoas on optional requirements (loic425)
This PR was merged into the 1.12 branch. Discussion ---------- | Q | A | --------------- | ----- | Bug fix? | no | New feature? | no | BC breaks? | mentioned in upgrade | Deprecations? | no | Related tickets | | License | MIT It's time to get rid of these packages. Let's start by moving them on optional requirements. - [x] move on dev dependencies - [x] fix DI - [x] FosRestPassTest - [x] HateoasPassTest Commits ------- 7f1762f Move fos rest, jms serializer and hateoas on dev requirements 496c369 Add tests for compiler passes f5d84ad Fix step name 472bfea Apply suggestions from code review 10e80cf Rename compiler passes
2 parents 46a2b13 + 10e80cf commit 1c7cc9c

File tree

11 files changed

+207
-21
lines changed

11 files changed

+207
-21
lines changed

.github/workflows/build.yml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,10 +155,11 @@ jobs:
155155
sed -i -e 's/state_machine_component: symfony/state_machine_component: winzou/g' tests/Application/config/packages/test/sylius_resource.yaml
156156
157157
-
158-
name: Run smoke tests without friendsofsymfony/rest-bundle willdurand/hateoas-bundle jms/serializer-bundle packages
158+
name: Run lint container without friendsofsymfony/rest-bundle willdurand/hateoas-bundle jms/serializer-bundle packages
159159
run: |
160-
composer remove friendsofsymfony/rest-bundle willdurand/hateoas-bundle jms/serializer-bundle --no-scripts
160+
composer remove --dev friendsofsymfony/rest-bundle willdurand/hateoas-bundle jms/serializer-bundle --no-scripts
161161
(cd tests/Application && bin/console cache:clear --env=test_without_fosrest)
162+
(cd tests/Application && bin/console lint:container --env=test_without_fosrest)
162163
composer require friendsofsymfony/rest-bundle willdurand/hateoas-bundle jms/serializer-bundle --no-scripts
163164
164165
-

UPGRADE.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1+
## UPGRADE FOR `1.12.x`
2+
3+
### FROM `1.11.x` to `1.12.x`
4+
5+
In preparation of removal, following dependencies were moved to optional requirements. If still in use by your app, require them explicitly in your `composer.json`.
6+
7+
* friendsofsymfony/rest-bundle
8+
* jms/serializer-bundle
9+
* willdurand/hateoas-bundle
10+
111
## UPGRADE FOR `1.11.x`
212

313
### FROM `1.10.x` to `1.11.x`

composer.json

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,7 @@
3333
"doctrine/event-manager": "^1.1",
3434
"doctrine/inflector": "^1.4 || ^2.0",
3535
"doctrine/persistence": "^2.0 || ^3.0",
36-
"friendsofsymfony/rest-bundle": "^3.0",
3736
"gedmo/doctrine-extensions": "^2.4.12 || ^3.0",
38-
"jms/serializer-bundle": "^3.5 || ^4.0 || ^5.0",
3937
"sylius/registry": "^1.2",
4038
"symfony/config": "^5.4 || ^6.4",
4139
"symfony/deprecation-contracts": "^2.1 || ^3.0",
@@ -52,7 +50,6 @@
5250
"symfony/validator": "^5.4 || ^6.4",
5351
"symfony/yaml": "^5.4 || ^6.4",
5452
"webmozart/assert": "^1.8",
55-
"willdurand/hateoas-bundle": "^2.0",
5653
"winzou/state-machine-bundle": "^0.6",
5754
"willdurand/negotiation": "^3.1"
5855
},
@@ -61,6 +58,8 @@
6158
},
6259
"require-dev": {
6360
"doctrine/orm": "^2.5",
61+
"friendsofsymfony/rest-bundle": "^3.0",
62+
"jms/serializer-bundle": "^3.5 || ^4.0 || ^5.0",
6463
"lchrusciel/api-test-case": "^5.0",
6564
"matthiasnoback/symfony-dependency-injection-test": "^4.2.1",
6665
"pagerfanta/pagerfanta": "^3.7 || ^4.0",
@@ -84,7 +83,13 @@
8483
"rector/rector": "^0.18.2",
8584
"symfony/messenger": "^5.4 || ^6.4",
8685
"symfony/serializer": "^5.4 || ^6.4",
87-
"symfony/security-bundle": "^5.4 || ^6.4"
86+
"symfony/security-bundle": "^5.4 || ^6.4",
87+
"willdurand/hateoas-bundle": "^2.0"
88+
},
89+
"conflict": {
90+
"friendsofsymfony/rest-bundle": "<3.0",
91+
"jms/serializer-bundle": "<3.5",
92+
"willdurand/hateoas-bundle": "<2.0"
8893
},
8994
"suggest": {
9095
"doctrine/orm": "^2.5",

src/Bundle/Controller/ResourcesCollectionProvider.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,10 @@
2121

2222
final class ResourcesCollectionProvider implements ResourcesCollectionProviderInterface
2323
{
24-
private ResourcesResolverInterface $resourcesResolver;
25-
26-
private PagerfantaFactory $pagerfantaRepresentationFactory;
27-
28-
public function __construct(ResourcesResolverInterface $resourcesResolver, PagerfantaFactory $pagerfantaRepresentationFactory)
29-
{
30-
$this->resourcesResolver = $resourcesResolver;
31-
$this->pagerfantaRepresentationFactory = $pagerfantaRepresentationFactory;
24+
public function __construct(
25+
private ResourcesResolverInterface $resourcesResolver,
26+
private ?PagerfantaFactory $pagerfantaRepresentationFactory = null,
27+
) {
3228
}
3329

3430
/**
@@ -61,6 +57,10 @@ public function get(RequestConfiguration $requestConfiguration, RepositoryInterf
6157
$paginator->getCurrentPageResults();
6258

6359
if (!$requestConfiguration->isHtmlRequest()) {
60+
if (null === $this->pagerfantaRepresentationFactory) {
61+
throw new \LogicException('The "willdurand/hateoas-bundle" must be installed and configured to render a resource collection on non-HTML request. Try running "composer require willdurand/hateoas-bundle"');
62+
}
63+
6464
$route = new Route($request->attributes->get('_route'), array_merge($request->attributes->get('_route_params'), $request->query->all()));
6565

6666
return $this->pagerfantaRepresentationFactory->createRepresentation($paginator, $route);

src/Bundle/Controller/ViewHandler.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,17 @@
1919

2020
final class ViewHandler implements ViewHandlerInterface
2121
{
22-
private ConfigurableViewHandlerInterface $restViewHandler;
23-
24-
public function __construct(ConfigurableViewHandlerInterface $restViewHandler)
22+
public function __construct(private ConfigurableViewHandlerInterface $restViewHandler)
2523
{
26-
$this->restViewHandler = $restViewHandler;
2724
}
2825

2926
public function handle(RequestConfiguration $requestConfiguration, View $view): Response
3027
{
3128
if (!$requestConfiguration->isHtmlRequest()) {
3229
$this->restViewHandler->setExclusionStrategyGroups($requestConfiguration->getSerializationGroups() ?? []);
3330

34-
if ($version = $requestConfiguration->getSerializationVersion()) {
31+
$version = $requestConfiguration->getSerializationVersion();
32+
if (null !== $version) {
3533
$this->restViewHandler->setExclusionStrategyVersion($version);
3634
}
3735

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Sylius package.
5+
*
6+
* (c) Sylius Sp. z o.o.
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace Sylius\Bundle\ResourceBundle\DependencyInjection\Compiler;
15+
16+
use FOS\RestBundle\FOSRestBundle;
17+
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
18+
use Symfony\Component\DependencyInjection\ContainerBuilder;
19+
20+
final class UnregisterFosRestDefinitionsPass implements CompilerPassInterface
21+
{
22+
public function process(ContainerBuilder $container): void
23+
{
24+
/** @var array $bundles */
25+
$bundles = $container->getParameter('kernel.bundles');
26+
27+
if (in_array(FOSRestBundle::class, $bundles, true)) {
28+
return;
29+
}
30+
31+
$container->removeDefinition('sylius.resource_controller.view_handler');
32+
}
33+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Sylius package.
5+
*
6+
* (c) Sylius Sp. z o.o.
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace Sylius\Bundle\ResourceBundle\DependencyInjection\Compiler;
15+
16+
use Bazinga\Bundle\HateoasBundle\BazingaHateoasBundle;
17+
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
18+
use Symfony\Component\DependencyInjection\ContainerBuilder;
19+
20+
final class UnregisterHateoasDefinitionsPass implements CompilerPassInterface
21+
{
22+
public function process(ContainerBuilder $container): void
23+
{
24+
/** @var array $bundles */
25+
$bundles = $container->getParameter('kernel.bundles');
26+
27+
if (in_array(BazingaHateoasBundle::class, $bundles, true)) {
28+
return;
29+
}
30+
31+
$container->removeDefinition('sylius.resource_controller.pagerfanta_representation_factory');
32+
}
33+
}

src/Bundle/Resources/config/services/controller.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050

5151
<service id="sylius.resource_controller.resources_collection_provider" class="Sylius\Bundle\ResourceBundle\Controller\ResourcesCollectionProvider" public="false">
5252
<argument type="service" id="sylius.resource_controller.resources_resolver" />
53-
<argument type="service" id="sylius.resource_controller.pagerfanta_representation_factory" />
53+
<argument type="service" id="sylius.resource_controller.pagerfanta_representation_factory" on-invalid="null" />
5454
</service>
5555
<service id="Sylius\Bundle\ResourceBundle\Controller\ResourcesCollectionProviderInterface" alias="sylius.resource_controller.resources_collection_provider" public="false" />
5656

@@ -80,7 +80,7 @@
8080
<service id="Sylius\Bundle\ResourceBundle\Controller\EventDispatcherInterface" alias="sylius.resource_controller.event_dispatcher" public="false" />
8181

8282
<service id="sylius.resource_controller.view_handler" class="Sylius\Bundle\ResourceBundle\Controller\ViewHandler" public="false">
83-
<argument type="service" id="fos_rest.view_handler" on-invalid="null" />
83+
<argument type="service" id="fos_rest.view_handler" />
8484
</service>
8585
<service id="Sylius\Bundle\ResourceBundle\Controller\ViewHandlerInterface" alias="sylius.resource_controller.view_handler" public="false" />
8686

src/Bundle/SyliusResourceBundle.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
use Sylius\Bundle\ResourceBundle\DependencyInjection\Compiler\RegisterResourceStateMachinePass;
2626
use Sylius\Bundle\ResourceBundle\DependencyInjection\Compiler\RegisterStateMachinePass;
2727
use Sylius\Bundle\ResourceBundle\DependencyInjection\Compiler\TwigPass;
28+
use Sylius\Bundle\ResourceBundle\DependencyInjection\Compiler\UnregisterFosRestDefinitionsPass;
29+
use Sylius\Bundle\ResourceBundle\DependencyInjection\Compiler\UnregisterHateoasDefinitionsPass;
2830
use Sylius\Bundle\ResourceBundle\DependencyInjection\Compiler\WinzouStateMachinePass;
2931
use Sylius\Bundle\ResourceBundle\DependencyInjection\PagerfantaExtension;
3032
use Sylius\Resource\Symfony\DependencyIjection\Compiler\DisableMetadataCachePass;
@@ -56,6 +58,8 @@ public function build(ContainerBuilder $container): void
5658
$container->addCompilerPass(new RegisterResourcesPass());
5759
$container->addCompilerPass(new RegisterStateMachinePass());
5860
$container->addCompilerPass(new RegisterResourceStateMachinePass());
61+
$container->addCompilerPass(new UnregisterFosRestDefinitionsPass());
62+
$container->addCompilerPass(new UnregisterHateoasDefinitionsPass());
5963
$container->addCompilerPass(new TwigPass());
6064
$container->addCompilerPass(new WinzouStateMachinePass());
6165

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Sylius package.
5+
*
6+
* (c) Sylius Sp. z o.o.
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace Bundle\DependencyInjection\Compiler;
15+
16+
use FOS\RestBundle\FOSRestBundle;
17+
use Matthias\SymfonyDependencyInjectionTest\PhpUnit\AbstractCompilerPassTestCase;
18+
use Sylius\Bundle\ResourceBundle\Controller\ViewHandler;
19+
use Sylius\Bundle\ResourceBundle\DependencyInjection\Compiler\UnregisterFosRestDefinitionsPass;
20+
use Symfony\Component\DependencyInjection\ContainerBuilder;
21+
22+
final class UnregisterFosRestDefinitionsPassTest extends AbstractCompilerPassTestCase
23+
{
24+
/** @test */
25+
public function it_remove_view_handler_if_fos_rest_is_not_available(): void
26+
{
27+
$this->setParameter('kernel.bundles', []);
28+
29+
$this->compile();
30+
31+
$this->assertContainerBuilderNotHasService('sylius.resource_controller.view_handler');
32+
}
33+
34+
/** @test */
35+
public function it_keeps_the_view_handler_if_fos_rest_is_available(): void
36+
{
37+
$this->setParameter('kernel.bundles', [FOSRestBundle::class]);
38+
39+
$this->compile();
40+
41+
$this->assertContainerBuilderHasService('sylius.resource_controller.view_handler');
42+
}
43+
44+
protected function registerCompilerPass(ContainerBuilder $container): void
45+
{
46+
$this->registerService('sylius.resource_controller.view_handler', ViewHandler::class);
47+
$this->setParameter('kernel.bundles', []);
48+
49+
$container->addCompilerPass(new UnregisterFosRestDefinitionsPass());
50+
}
51+
}

0 commit comments

Comments
 (0)