10000 PoC PHP file resource metadata factory by loic425 · Pull Request #983 · Sylius/SyliusResourceBundle · GitHub
[go: up one dir, main page]

Skip to content

PoC PHP file resource metadata factory #983

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

Draft
wants to merge 12 commits into
base: 1.13
Choose a base branch
from

Conversation

loic425
Copy link
Member
@loic425 8000 loic425 commented Feb 20, 2025
Q A
Bug fix? no
New feature? no
BC breaks? no
Deprecations? no
Related tickets
License MIT
// config/app/resources/country.php
declare(strict_types=1);

use Sylius\Bundle\AdminBundle\Form\Type\CountryType;
use Sylius\Resource\Metadata\Create;
use Sylius\Resource\Metadata\Index;
use Sylius\Resource\Metadata\Operations;
use Sylius\Resource\Metadata\ResourceMetadata;
use Sylius\Resource\Metadata\Update;

return (new ResourceMetadata())
    ->withRoutePrefix('/admin')
    ->withClass('%sylius.model.country.class%')
    ->withSection('admin')
    ->withTemplatesDir("@SyliusAdmin\\shared\\crud")
    ->withFormType(CountryType::class)
    ->withOperations(new Operations(operations: [
        new Create(redirectToRoute: 'sylius_admin_country_update'),
        new Update(redirectToRoute: 'sylius_admin_country_update'),
        new Index(grid: 'sylius_admin_country'),
    ]))
;
// config/app/resources/product.php
declare(strict_types=1);

use Sylius\Bundle\AdminBundle\Form\Type\ProductType;
use Sylius\Resource\Metadata\BulkDelete;
use Sylius\Resource\Metadata\Create;
use Sylius\Resource\Metadata\Delete;
use Sylius\Resource\Metadata\Index;
use Sylius\Resource\Metadata\Operations;
use Sylius\Resource\Metadata\ResourceMetadata;
use Sylius\Resource\Metadata\Show;
use Sylius\Resource\Metadata\Update;

return (new ResourceMetadata())
    ->withRoutePrefix('/admin')
    ->withClass('%sylius.model.product.class%')
    ->withSection('admin')
    ->withTemplatesDir("@SyliusAdmin\\shared\\crud")
    ->withFormType(ProductType::class)
    ->withOperations(new Operations([
        new Create(redirectToRoute: 'sylius_admin_product_update'),
        new Create(
            path: 'products/new/simple',
            template: '@SyliusAdmin/shared/crud/create.html.twig',
            shortName: 'create_simple',
            factoryMethod: 'createWithVariant',
            redirectToRoute: 'sylius_admin_product_update',
        ),
        new Update(redirectToRoute: 'sylius_admin_product_update'),
        new Delete(),
        new BulkDelete(),
        new Index(grid: 'sylius_admin_product'),
        new Show(),
    ]))
;
#sylius_admin_country:
#    resource: |
#        alias: sylius.country
#        section: admin
#        templates: "@SyliusAdmin\\shared\\crud"
#        except: ['show', 'delete']
#        redirect: update
#        grid: sylius_admin_country
#        form:
#            type: Sylius\Bundle\AdminBundle\Form\Type\CountryType
#        permission: true
#    type: sylius.resource
#sylius_admin_product:
#    resource: |
#        alias: sylius.product
#        section: admin
#        templates: "@SyliusAdmin\\shared\\crud"
#        redirect: update
#        grid: sylius_admin_product
#        form:
#            type: Sylius\Bundle\AdminBundle\Form\Type\ProductType
#        permission: true
#    type: sylius.resource

#sylius_admin_product_create_simple:
#    path: /products/new/simple
#    methods: [GET, POST]
#    defaults:
#        _controller: sylius.controller.product::createAction
#        _sylius:
#            section: admin
#            permission: true
#            factory:
#                method: createWithVariant
#            form:
#                type: Sylius\Bundle\AdminBundle\Form\Type\ProductType
#            template: "@SyliusAdmin/shared/crud/create.html.twig"
#            redirect: sylius_admin_product_update
#            vars:
#                route:
#                    name: sylius_admin_product_create_simple

Capture d’écran du 2025-02-24 10-55-10

@loic425 loic425 marked this pull request as draft February 20, 2025 07:02
@loic425 loic425 force-pushed the poc/php_file_resource_metadata branch from 8ae6c72 to da5094c Compare February 21, 2025 13:42
@@ -39,6 +39,9 @@ public function getConfigTreeBuilder(): TreeBuilder
->arrayNode('mapping')
->addDefaultsIfNotSet()
->children()
->arrayNode('imports')
Copy link
Member Author
@loic425 loic425 Feb 21, 2025

Choose a reason for hiding this comment

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

This new path ensures we do not load any files that are on Autoloader which cannot be include serveral times.
So here, we'll import config files (PHP ones).

Copy link
Member Author

Choose a reason for hiding this comment

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

Interesting discussions there
api-platform/core#6943

private function getPHPFileClosure(string $filePath): \Closure
{
return \Closure::bind(function () use ($filePath): mixed {
return require $filePath;
Copy link
Member Author

Choose a reason for hiding this comment

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

that's why we cannot import the mapping paths here.
require Classes, Trait, Interface and things like that is not possible. It needs to be basic php file.

$finder->in($path);
}

return $finder->files();
Copy link
Member Author

Choose a reason for hiding this comment

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

it misses sth to filter on php file only.


$resourceAlias = $resource->getAlias();

if (null !== $resourceAlias) {
Copy link
Member Author
@loic425 loic425 Feb 21, 2025

Choose a reason for hiding this comment

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

I want to remove that RegistryInterface at some point. I need to work on this.
We should use the MetadataNameCollection instead.

@loic425 loic425 force-pushed the poc/php_file_resource_metadata branch 9 times, most recently from bdeb733 to 09d90cc Compare February 21, 2025 14:45
@loic425 loic425 self-assigned this Feb 21, 2025
@loic425 loic425 force-pushed the poc/php_file_resource_metadata branch 4 times, most recently from c3a5b3a to 863f09b Compare February 24, 2025 10:39
@loic425 loic425 force-pushed the poc/php_file_resource_metadata branch from 6a8beb2 to 3992255 Compare February 27, 2025 13:39
@loic425 loic425 force-pushed the poc/php_file_resource_metadata branch from 2d69656 to a0dd5eb Compare February 28, 2025 07:51
@loic425 loic425 force-pushed the poc/php_file_resource_metadata branch from 1842d63 to 4530671 Compare February 28, 2025 08:22
@loic425 loic425 force-pushed the poc/php_file_resource_metadata branch 6 times, most recently from 19d2b8f to 3d74f73 Compare March 10, 2025 14:07
@loic425 loic425 force-pushed the poc/php_file_resource_metadata branch from 3d74f73 to e979ac1 Compare March 10, 2025 14:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant
0