scramble icon indicating copy to clipboard operation
scramble copied to clipboard

[Bug]: Extension ignored even though it is registered

Open FrankVisuals opened this issue 1 year ago • 3 comments

What happened?

Registration of extensions does not seem to work - my simple extensions looks like

<?php

namespace Extensions;

use Dedoc\Scramble\Support\Generator\Operation;
use Dedoc\Scramble\Support\RouteInfo;

class LaravelActionsExtension extends \Dedoc\Scramble\Extensions\OperationExtension
{
    public function handle(Operation $operation, RouteInfo $routeInfo)
    {
        $operation->tags = ['MY TAG'];
        dd("Test Errror Output");
    }
}

With my scramble config being

    // ...
    'extensions' => [
        LaravelActionsExtension::class
    ],

Looking into it the problems seems to be in ScrambleServiceProvider.php

Specifically these lines:

    public function bootingPackage()
    {
        Scramble::configure()
            ->useConfig(config('scramble'))
            ->withOperationTransformers(function (OperationTransformers $transformers) {
                $extensions = array_merge(config('scramble.extensions', []), Scramble::$extensions);

                $operationExtensions = array_values(array_filter(
                    $extensions,
                    fn ($e) => is_a($e, OperationExtension::class, true),
                ));
                $transformers->append($operationExtensions);
            });
            // ...

The is_a($e, OperationExtension::class, true)returns false for my class. $e resolves to "Extensions\LaravelActionsExtension"

How to reproduce the bug

  1. install "dedoc/scramble": "^0.12.18"
  2. create the extension as shown above
  3. add extension to extensions

Package Version

0.12.18

PHP Version

8.4.5

Laravel Version

^12.0

Which operating systems does with happen with?

macOS

Notes

No response

FrankVisuals avatar Apr 23 '25 09:04 FrankVisuals

@FrankVisuals any chance you can create a repository with a fresh Laravel application with reproduction of this error?

romalytvynenko avatar Apr 23 '25 09:04 romalytvynenko

https://github.com/FrankVisuals/scrambleissue I've created it here.

However, I've noticed it does work when I put my "Extensions" folder into my app folder and adjust the namespace to App\Extensions.

Didn't know this can make a difference though.

FrankVisuals avatar Apr 23 '25 10:04 FrankVisuals

@FrankVisuals This is not Scramble's problem. This is an autoloading problem. Your class is not "seen" by PHP due to it is not being loaded. You should either put it into app/Extensions and use App\Extensions namespace, or you should explicitly register your Extensions folder and the corresponding namespace in composer.json.

You can read more in Composer's documentation.

romalytvynenko avatar Apr 23 '25 13:04 romalytvynenko