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

[RFC] PHP based configuration #3558

Closed
Grafikart opened this issue May 12, 2020 · 6 comments
Closed

[RFC] PHP based configuration #3558

Grafikart opened this issue May 12, 2020 · 6 comments
Labels

Comments

@Grafikart
Copy link
Contributor

Grafikart commented May 12, 2020

Description
I like using ApiPlatform but I find the configuration painfull to set up (lack of autocompletion, no reusability, no automation, break easily without usefull warning...).
Being able to configure API platform using simple PHP class would solve these problems.

Example

function __invoke(ApiConfigurator $configurator) {
   // itemOperations
   $operations = array_map(function ($method) {
            return (new ItemOperation(Comment::class, $method))
                ->setNormalizationContext(['read:comment'], 'ReadComment')
                ->setSecurity($method === 'GET' ? CommentVoter::READ : CommentVoter::EDIT, 'object');
        ;
    }, ['GET','DELETE','PUT']);
    $operations[] = new ItemOperation(Comment::class, 'patch');

    // collection operation
    $operations[] = new CollectionOperation(Comment::class, 'get')
    $configurator->addOperations($operations);
}

This approach solve many problems I have with the configuration right now :

  • It's self documenting. It's easier to discover features reading the source code
  • It's easier to automate some features using functions or custom classes.
  • It's easier to detect problems when the configuration API changes using static analysis.
@Grafikart Grafikart changed the title PHP based configuration [RFC] PHP based configuration May 12, 2020
@alanpoulain
Copy link
Member

Somehow related: symfony/symfony#36778

@dunglas
Copy link
Member

dunglas commented May 12, 2020

+1 on my side to have a PHP configuration. The metadata subsystem already allows to plug any source of configuration, so it should not be too hard. I agree with @alanpoulain, it must be consistent with what Symfony provides.

@Grafikart are you using PHPStorm? Is yes, the PHP Annotations plugin will enable auto completion for almost everything. As we provide XSDs, autocompletion should also work with XML config file (but TBH I’ve not tested).

@Grafikart
Copy link
Contributor Author

Grafikart commented May 12, 2020

@dunglas I'm using PHPStorm and it offers autocompletion for top level property but doesn't work for parameters for operations for instance. Also I couldn't figure out how to use constant for the security is_granted.

/**
*  itemOperations={
*.     "get"={???},
*      "post"={"normalization_context"=???}   
* }
**/

It's easy to mix up normalizationContext and normalization_context and with annotation you have no feedback for this kind of typo :(

I'm not a fan of the symfony suggestion since it's not easy to know what options are available. But It could work since it could be decorated easily.

@weaverryan
Copy link
Contributor

I would love this feature also. API Platform metadata is too powerful (and thus, complex) to fit nicely into annotations (and I normally LOVE annotations). I also just replied to a user on SymfonyCasts that loves API Platform, but dislikes the annotations.

Getting this BEST "syntax" of a config system is the problem. If someone has some time to work on it, I would be very happy with an "first try" imperfect system. Once we have that, I have no doubt that the community will start iterating on it to improve it. But we need that first version!

Cheers!

@stale
Copy link

stale bot commented Nov 5, 2022

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the wontfix label Nov 5, 2022
@soyuka
Copy link
Member

soyuka commented Nov 5, 2022

Its easier in 3.0 closing this for now

@soyuka soyuka closed this as completed Nov 5, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants