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

Sylius route with attributes #334

Merged
merged 17 commits into from
Jan 11, 2022

Conversation

loic425
Copy link
Member

@loic425 loic425 commented Nov 10, 2021

Q A
Bug fix? no
New feature? yes
BC breaks? no
Deprecations? no
Related tickets
License MIT
<?php

declare(strict_types=1);

namespace App\Entity;

use App\Annotation\SyliusCrudRoutes;
use App\Annotation\SyliusRoute;
use Sylius\Component\Resource\Model\ResourceInterface;

#[SyliusCrudRoutes(
    alias: 'app.book',
    path: 'library',
    section: 'backend',
    redirect: 'index',
    grid: 'sylius_backend_admin_user',
    except: ['show'],
    vars: [
        'all' => [
            'subheader' => 'sylius.ui.manage_users_able_to_access_administration_panel',
        ],
        'index' => [
            'icon' => 'lock',
        ]
    ],
)]

#[SyliusRoute(
    name: 'app_backend_book_show',
    path: '/library/{id}',
    methods: ['GET'],
    controller: 'app.controller.book::indexAction',
    template: 'backend/book/show.html.twig',
    vars: [
        'subheader' => 'sylius.ui.manage_users_able_to_access_administration_panel',
    ]
)]
class Book implements ResourceInterface
{
    private ?int $id = null;

    public function getId(): ?int
    {
        return $this->id;
    }
}

Crud routes

  • CRUD routes with alias
  • CRUD routes with section
  • CRUD routes with criteria
  • CRUD routes with template
  • CRUD routes with grid
  • CRUD routes with vars
  • CRUD routes with redirect
  • CRUD routes with persmission
  • CRUD routes with except
  • CRUD routes with only

Single routes

  • Single route with name, path and controller
  • Single route with methods
  • Single route with criteria
  • Single route with template
  • Single route with repository
  • Single route with serializationGroups
  • Single route with serializationVersion
  • Single route with requirements
  • Single route with options
  • Single route with host
  • Single route with schemes
  • Single route with priority
  • Single route with vars

@loic425 loic425 requested a review from a team as a code owner November 10, 2021 06:58
@loic425 loic425 force-pushed the draft/sylius-routes-with-attributes branch 3 times, most recently from e2ba131 to c373b7f Compare November 10, 2021 14:24
@loic425 loic425 changed the title [WIP] Sylius route with attributes Sylius route with attributes Nov 10, 2021
@loic425 loic425 force-pushed the draft/sylius-routes-with-attributes branch 2 times, most recently from fba67b2 to cc1ae18 Compare November 16, 2021 09:49
@@ -13,14 +13,21 @@

<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>
<defaults public="true" />
<defaults public="false" />
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Keeping it public would make it more coherent with Sylius itself

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

<argument type="service" id="sylius.resource_registry" />
<argument type="service">
<service class="Sylius\Bundle\ResourceBundle\Routing\RouteFactory" />
</argument>
<tag name="routing.loader" />
</service>

<service id="sylius.routing.loader.attributes" class="Sylius\Bundle\ResourceBundle\Routing\AttributesLoader">
<!-- TODO make it configurable -->
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you mean?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I forgot to remove the TODO :)
That's done.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just have to remove the TODO.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done for this one.

src/Bundle/Routing/AttributesLoader.php Outdated Show resolved Hide resolved
$syliusOptions['serialization_version'] = $arguments['serializationVersion'];
}

$route = new Route(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably \Sylius\Bundle\ResourceBundle\Routing\RouteFactory should be used. But I'm even thinking about createRoute method extraction from \Sylius\Bundle\ResourceBundle\Routing\ResourceLoader or making it public, so it could be here. Won't it make sense?

src/Bundle/Routing/AttributesLoader.php Outdated Show resolved Hide resolved
Comment on lines 1 to 7
sylius_resource:
resource: 'sylius.routing.loader.attributes'
type: service
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do I understand i correctly, that without this configuration, attributes won't work?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes without this configuration, the routes would not be generated.
But It should be added on a Symfony flex recipe.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't it be part of sylius_resource configuration tree?

Copy link
Member Author

@loic425 loic425 Jan 3, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's not possible.
On ApiPlatform it's done on a recipe.
https://github.com/symfony/recipes/blob/master/api-platform/core/2.5/config/routes/api_platform.yaml

But we have to mention it on documentation.

src/Bundle/spec/Routing/AttributesLoaderSpec.php Outdated Show resolved Hide resolved
src/Bundle/Routing/AttributesLoader.php Outdated Show resolved Hide resolved

}

private function getResourcesByPath(string $path): array
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This class or its parent seems to be a great candidate for extraction as well. This should be probably tested with spec/phpunit to check its finding behaviour. Right now, a lot is happening in this class alone. Is it fetched from some other project? Could it make sense, to reuse this logic somewhere else?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes it's extracted from here
https://github.com/symfony/symfony/blob/6.1/src/Symfony/Component/Validator/Command/DebugCommand.php#L172

It's my only one contribution on Symfony 👍

@lchrusciel
Copy link
Member

really nice work!

@loic425 loic425 force-pushed the draft/sylius-routes-with-attributes branch from b8ea7f6 to 43e42a5 Compare December 20, 2021 11:22
return $reflectionClass->getAttributes($attributeName);
}

private function getReflectionClasses(): iterable
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would extract this one to separate service/method as well, as it is 100% duplicated. But it is good to go for me as well

src/Component/Reflection/ClassReflection.php Show resolved Hide resolved
@@ -0,0 +1,7 @@
sylius_crud_routes:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As this configuration is required to make route attribute work, we should probably mention it in some docs or UPGRADE file (or both 💃)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd like to add it on a documentation PR. Cause it's not documented yet.

src/Bundle/spec/Routing/RoutesAttributesLoaderSpec.php Outdated Show resolved Hide resolved
src/Bundle/Routing/RoutesAttributesLoader.php Show resolved Hide resolved
src/Bundle/Routing/RoutesAttributesLoader.php Outdated Show resolved Hide resolved
src/Bundle/Tests/Routing/RoutesAttributesLoaderTest.php Outdated Show resolved Hide resolved
@loic425 loic425 force-pushed the draft/sylius-routes-with-attributes branch from 04f5653 to 732f512 Compare January 10, 2022 23:57
@loic425 loic425 force-pushed the draft/sylius-routes-with-attributes branch from 58994e1 to 6c8092b Compare January 11, 2022 00:18
@Zales0123 Zales0123 added the Feature New feature proposals. label Jan 11, 2022
@Zales0123 Zales0123 merged commit 7ddf718 into Sylius:master Jan 11, 2022
@Zales0123
Copy link
Member

Thanks Loïc! Spectacular work 🎉 🚀

@loic425 loic425 deleted the draft/sylius-routes-with-attributes branch January 11, 2022 08:11
Zales0123 added a commit that referenced this pull request Jan 17, 2022
This PR was merged into the 1.9-dev branch
Zales0123 added a commit to Zales0123/SyliusResourceBundle that referenced this pull request Jan 18, 2022
This PR was merged into the 1.9-dev branch
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Feature New feature proposals.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants