laravel-doctrine / extensions

Extensions integration for Doctrine2 and Laravel
http://laraveldoctrine.org/
MIT License
48 stars 24 forks source link

BeberleiExtensions doesn't register properly in laravel 5.7 #43

Open hshahdoost opened 5 years ago

hshahdoost commented 5 years ago

I'm using the following packages and have just upgraded into Laravel 5.7 (from 5.4)

        "laravel-doctrine/orm": "1.4.*",
        "laravel-doctrine/extensions": "1.0.*",
        "beberlei/DoctrineExtensions": "1.1.5",
        "gedmo/doctrine-extensions": "^2.4",

I don't know why this is happening, but after upgrading into Laravel 5.7, I can no longer use BeberleiExtensions methods such as Month. I have removed the updated package service providers from my app config and kept beberlei and gedmo there. So far everything. Here is how I have registered the service providers


        /*
         * Application Service Providers...
         */
        App\Providers\AppServiceProvider::class,
        App\Providers\AuthServiceProvider::class,
        // App\Providers\BroadcastServiceProvider::class,
        App\Providers\EventServiceProvider::class,
        App\Providers\RouteServiceProvider::class,

        LaravelDoctrine\Extensions\BeberleiExtensionsServiceProvider::class,
        LaravelDoctrine\Extensions\GedmoExtensionsServiceProvider::class,

The Gedmo library is working fine, but Berberlei doesn't get registered properly, Both of them are added to Providers in bootstrap and are registered after LaravelDoctrine. But there is a difference in the way Beberlei is registered which makes this happen. Beberlei is using the following code to register it's datetime functions in BeberleiExtensionsServiceProvider

        $manager->extendAll(function (Configuration $configuration) {
            $configuration->setCustomDatetimeFunctions($this->datetime);
            $configuration->setCustomNumericFunctions($this->numeric);
            $configuration->setCustomStringFunctions($this->string);
        });

While Gedmo is using a more sophisticated way of registering it's annotations (You can see it here)

The callback of $manager->extendAll is never called for some reason, while the callback of Gedmo is called properly.

hshahdoost commented 5 years ago

The problem was solved by creating a new ServiceProvider for Beberlei but registering it's methods the way Gedmo was doing it

class BeberleiExtensionsServiceProvider extends BaseServiceProvider
{
    public function boot(DoctrineManager $manager){

    }

    /**
     * Register the service provider.
     * @return void
     */
    public function register()
    {
        $this->app['events']->listen('doctrine.extensions.booting', function () {
            /**
             * @var Registry $registry
             */
            $registry = $this->app->make('registry');

            foreach ($registry->getManagers() as $manager) {
                $configuration = $manager->getConfiguration();
                $configuration->setCustomDatetimeFunctions($this->datetime);
                $configuration->setCustomNumericFunctions($this->numeric);
                $configuration->setCustomStringFunctions($this->string);
            }
        });
    }
}
whizsid commented 2 years ago

Can reproduce this issue in laravel 8.1 too. @hshahdoost 's fix is still working.

dpslwk commented 2 years ago

This is working fine for me. We would need a more detailed error and example code of what is not working to help more.