Skip to content

[RFC] PHP based configuration #3558

@Grafikart

Description

@Grafikart
Contributor

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.

Activity

changed the title [-]PHP based configuration[/-] [+][RFC] PHP based configuration[/+] on May 12, 2020
alanpoulain

alanpoulain commented on May 12, 2020

@alanpoulain
Member

Somehow related: symfony/symfony#36778

dunglas

dunglas commented on May 12, 2020

@dunglas
Member

+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

Grafikart commented on May 12, 2020

@Grafikart
ContributorAuthor

@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

weaverryan commented on Aug 13, 2020

@weaverryan
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

stale commented on Nov 5, 2022

@stale

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.

soyuka

soyuka commented on Nov 5, 2022

@soyuka
Member

Its easier in 3.0 closing this for now

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @dunglas@weaverryan@Grafikart@soyuka@alanpoulain

        Issue actions

          [RFC] PHP based configuration · Issue #3558 · api-platform/core