openapi-php-attributes icon indicating copy to clipboard operation
openapi-php-attributes copied to clipboard

Extending functionality

Open dam1r89 opened this issue 3 years ago • 7 comments

Hello, I have a suggestion for a new feature - the ability to extend functionality externally.

Specifically, I would like to be able to resolve route paths based on the settings of the framework I am using, such as Laravel. I think it would be helpful to have a new RouteDynamic attribute that takes a ResolverClass as an argument. This ResolverClass could have a method that accepts a $pathBuilder object, which would allow users to customize the route path resolution based on their specific needs. Thank you for considering this suggestion.

I can submit a simple PR as a proof of concept and continue the discussion there.

dam1r89 avatar Dec 12 '22 23:12 dam1r89

Hi !

This is an interesting one ! Well I guess this would be quite easy to implement. PathMethodBuilder would need a little cleaning up to easily extend it. Would you want to also set the builder as a general setting or is it for a specific route in your case ?

Do you have anything "core" that could be added in for everyone ? Could you share what is missing for you ?

Thanks,

uderline avatar Dec 13 '22 10:12 uderline

I'm mocking up something in a PR (not a solution) to give you an idea.

dam1r89 avatar Dec 13 '22 11:12 dam1r89

One more note, it was difficult to make it run with a Laravel, so I had to bootstrap app differently:

Artisan::command('openapi-generate', function () {
    $files = Symfony\Component\Finder\Finder::create()
        ->files()
        ->name('*.php')
        ->in([app_path('PublicApi/v2')]);

    foreach ($files as $autoload) {
        include_once $autoload->getPathName();
    }

    $generate = \OpenApiGenerator\Generator::create()->generate();

    echo json_encode($generate, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
});

It would be great if class names could be passed as an argument of Generator::create() (or generate method) that way I could decide how to fetch those classes instead of relying on get_declared_classes() function.

dam1r89 avatar Dec 13 '22 11:12 dam1r89

Ok I get the idea, thanks for the PR. I'll try some stuff and will come back to you with a proposition.

For the class list as a command argument, I'm not totally sure because you can already specify the path (or paths) to your controller(s).

uderline avatar Dec 20 '22 10:12 uderline

I understand that specifying paths is only an option if you are using the tool as a CLI rather than as a library. That's okay, I can find another solution to that problem.

dam1r89 avatar Dec 26 '22 10:12 dam1r89

Ok so after several months, I managed to find some time to work on a solution -- hopefully you are still using this lib.

So, I refactored the HttpGenerator (it needed to be done anyway) and added a dynamic builder as you suggested on this commit https://github.com/uderline/openapi-php-attributes/commit/8df02e099337c4eb885675c3d1db347973f73f65

I'd like your input on wether it was what you were expecting

uderline avatar Apr 08 '23 13:04 uderline

Hah, thanks for considering it. Yes, I still want to use this library. I will try to play around with this version and make it work.

dam1r89 avatar Apr 18 '23 18:04 dam1r89