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

Breaking change in registration of inherited providers #51739

Closed
vnrmc opened this issue Jun 7, 2024 · 4 comments
Closed

Breaking change in registration of inherited providers #51739

vnrmc opened this issue Jun 7, 2024 · 4 comments

Comments

@vnrmc
Copy link

vnrmc commented Jun 7, 2024

Laravel Version

11.9

PHP Version

8.2.13

Database Driver & Version

No response

Description

Before v11.9, the registration of services paid attention to instance of service (using instanceof) and avoided the registration if "similar" class was already registered. With v11.9 it matches exactly using fully qualified name of the class.

php:instanceof - instanceof can also be used to determine whether a variable is an instantiated object of a class that inherits from a parent class.

Example:
let define CustomMigrationServiceProvider extends Illuminate\Database\MigrationServiceProvider.
and register the provider.

The order of registration during booting of an application is exactly the same for all 11 laravel versions:

  • ...
  • CustomMigrationServiceProvider
  • ...
  • Illuminate\Database\MigrationServiceProvider

but

  • before 11.9 the application had detected, that previously registered CustomMigrationServiceProvider is instance of Illuminate\Database\MigrationServiceProvider and the registration of Illuminate\Database\MigrationServiceProvider was skipped
  • ** with 11.9+** the application detect only that the class (Illuminate\Database\MigrationServiceProvider) was still not registered and follows with the registration (it means, the migrator will be overwritten)

Reason for the breaking change:

https://github.com/laravel/framework/compare/ceb892..2fd3a25#diff-0a80606049a107748776bea5151a61fbec2afefe3361c340a5de9c8c1ccdf9c8

  • before v11.9
    ** \Illuminate\Foundation\Application::markAsRegistered pushed the registered service onto the end of list
    ** \Illuminate\Foundation\Application::getProvider used getProviders (iterates over the list using instanceof)
  • with v11.9
    ** \Illuminate\Foundation\Application::markAsRegistered set the registered service in the list using fully qualified name (key => value)
    ** \Illuminate\Foundation\Application::getProvider access the registered service directly using fully qualified name

Steps To Reproduce

<?php

declare(strict_types=1);

namespace Tests\Components;

use Illuminate\Contracts\Console\Kernel;
use Illuminate\Foundation\Application;
use Illuminate\Foundation\Testing\TestCase;
use Illuminate\Support\ServiceProvider;

interface ServiceInterface
{
    public function run(): string;
}

class ServiceA implements ServiceInterface
{
    public function run(): string
    {
        return "A";
    }
}

class ServiceB extends ServiceA
{
    public function run(): string
    {
        return "B";
    }
}

class AServiceProvider extends ServiceProvider
{
    public function register(): void
    {
        $this->app->singleton(ServiceInterface::class, ServiceA::class);
    }
}

class BServiceProvider extends AServiceProvider
{
    public function register(): void
    {
        $this->app->singleton(ServiceInterface::class, ServiceB::class);
    }
}

class ServiceProviderRegistrationTest extends TestCase
{
    /**
     * Creates the application.
     *
     * @return Application
     */
    public function createApplication(): Application
    {
        $app = require __DIR__ . '/../../bootstrap/app.php';

        $app->make(Kernel::class)->bootstrap();

        return $app;
    }

    public function testBreakingChange(): void
    {
        $this->app->register(BServiceProvider::class);
        $this->app->register(AServiceProvider::class);

        $service = $this->app->get(ServiceInterface::class);

        // laravel 11.8: the first service wins
        // laravel 11.9: the last service wins
        $this->assertEquals("B", $service->run()); // works with v11.8
    }
}
@crynobone
Copy link
Member

Hey there, thanks for reporting this issue.

We'll need more info and/or code to debug this further. Can you please create a repository with the command below, commit the code that reproduces the issue as one separate commit on the main/master branch and share the repository here?

Please make sure that you have the latest version of the Laravel installer in order to run this command. Please also make sure you have both Git & the GitHub CLI tool properly set up.

laravel new bug-report --github="--public"

Do not amend and create a separate commit with your custom changes. After you've posted the repository, we'll try to reproduce the issue.

Thanks!

@crynobone
Copy link
Member

I don't believe this is the correct way and is never meant to be supported, service providers should be registered via app.providers.

@vnrmc
Copy link
Author

vnrmc commented Jun 11, 2024

Hi Crynobone,
you are probably right, that it was never meant to be supported, but ... it has worked until 11.9 ;)

Affected application uses separate custom package and registered "CommonServiceProvider" (using app.providers).
The common-provider registers few other logically splitted ServiceProviders. Of course, it's possible to register each provider separately (is probably the best solution) - I reported the issue, because the public method Application::register no longer works exactly as before, and maybe it's relevant for other people/causes other bugs.

In case you want to check it, I've prepared the repository, that helps to reproduce it:
https://github.com/vnrmc/laravel-bug-report-51739

Thx!

@crynobone
Copy link
Member

The correct way to solve this is actually:

  1. Remove App\Providers\CommonServiceProvider class and registration from bootstrap/providers.php:
<?php

return [
    App\Providers\AppServiceProvider::class,
-   App\Providers\CommonServiceProvider::class,
];
  1. Update config/app.php and add the following configuration:
    'providers' => Illuminate\Support\ServiceProvider::defaultProviders()
        ->replace([
            'Illuminate\Database\MigrationServiceProvider' => 'App\Providers\CustomMigrationServiceProvider',
        ])
        ->toArray(),

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

No branches or pull requests

2 participants