Skip to content

PoC PHP file resource metadata factory#983

Closed
loic425 wants to merge 12 commits intoSylius:1.13from
loic425:poc/php_file_resource_metadata
Closed

PoC PHP file resource metadata factory#983
loic425 wants to merge 12 commits intoSylius:1.13from
loic425:poc/php_file_resource_metadata

Conversation

@loic425
Copy link
Copy Markdown
Member

@loic425 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
->arrayNode('mapping')
->addDefaultsIfNotSet()
->children()
->arrayNode('imports')
Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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 863f09b to d90f4ee Compare February 24, 2025 10:53
@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
@loic425
Copy link
Copy Markdown
Member Author

loic425 commented Oct 28, 2025

Feature added in #1028

@loic425 loic425 closed this Oct 28, 2025
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