DirectoryTree / LdapRecord-Laravel

Multi-domain LDAP Authentication & Management for Laravel.
https://ldaprecord.com/docs/laravel/v3
MIT License
492 stars 51 forks source link

[Bug] Call to undefined method LdapRecord\ConnectionManager::getEventDispatcher() #583

Closed vatsake closed 10 months ago

vatsake commented 10 months ago

Environment:

Describe the bug: Ehm.. Am I doing something wrong here?

<?php

namespace App\Providers;

use LdapRecord\Container;
use Illuminate\Support\ServiceProvider;

class LdapEventServiceProvider extends ServiceProvider
{
    /**
     * The LDAP event listener mappings for the application.
     *
     * @return array
     */
    protected $listen = [
        \LdapRecord\Models\Events\Updating::class => [
            \App\Ldap\Listeners\ObjectSaving::class
        ],
        \LdapRecord\Models\Events\Updated::class => [
            \App\Ldap\Listeners\ObjectSaved::class
        ],
        \LdapRecord\Models\Events\Creating::class => [
            \App\Ldap\Listeners\ObjectCreated::class
        ],
        \LdapRecord\Models\Events\Deleted::class => [
            \App\Ldap\Listeners\ObjectDeleted::class
        ],
    ];

    /**
     * Bootstrap services.
     *
     * @return void
     */
    public function boot()
    {
        $dispatcher = Container::getEventDispatcher();

        foreach ($this->listen as $event => $listeners) {
            foreach (array_unique($listeners) as $listener) {
                $dispatcher->listen($event, $listener);
            }
        }
    }
}

php artisan optimize throws error

Call to undefined method LdapRecord\ConnectionManager::getEventDispatcher()

  at vendor\directorytree\ldaprecord\src\Container.php:63
     59▕      * Forward missing method calls onto the connection manager.
     60▕      */
     61▕     public function __call(string $method, array $parameters): mixed
     62▕     {
  ➜  63▕         return $this->manager->{$method}(...$parameters);
     64▕     }
     65▕
     66▕     /**
     67▕      * Set the current container instance available globally.

  1   vendor\directorytree\ldaprecord\src\Container.php:47
      LdapRecord\Container::__call("getEventDispatcher", []
stevebauman commented 10 months ago

Hi @vatsake!

This is an error in the documentation, the correct method name is Container::getDispatcher():

https://ldaprecord.com/docs/core/v3/models/#events

use LdapRecord\Container;
use LdapRecord\Models\Events\Creating;

$dispatcher = Container::getDispatcher();

$dispatcher->listen(Creating::class, function ($event) {
    $model = $event->getModel();
});

I've just pushed a fix in the docs for this! Let me know if you encounter further issues and I can re-open. 👍