chris_j's avatar

Adding a method to vendor class

I'm using the laravel-elfinder package (https://github.com/barryvdh/laravel-elfinder).

My aim is to automatically resize and copy any uploaded images so I can so they can be served responsively.

Given the standard Elfinder package, you can bind methods to actions e.g.: https://github.com/Studio-42/elFinder/wiki/Logging#using-class-instance-callback

However, in this example the elFinderSimpleLogger class is initialised in the Connector file. Using laravel-elfinder, this file resides in the vendor directory so I don't want to change it.

The $opts array is available in the packages config file (config/elfinder.php) and in there I've tried to reference the new class:

'bind' => array(
            'upload' => array(new App\Classes\elFinderSimpleLogger(public_path('log.txt')), 'log')
        ),

But this doesn't work. So I then created a ServiceProvider to try and make the method available:

namespace App\Providers;

use Illuminate\Support\ServiceProvider;

class CustomElfinderServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap services.
     *
     * @return void
     */
    public function boot()
    {
        //
    }

    /**
     * Register services.
     *
     * @return void
     */
    public function register()
    {
        $this->app->singleton('App\Classes\elFinderSimpleLogger', function () {
            return new \App\Classes\elFinderSimpleLogger(public_path('log.txt'));
        });
    }
}

but I have no luck here.

Should I be trying to override the whole controller or can I just extend the controller to provide a new method?

Thanks.

0 likes
5 replies
bobbybouwmann's avatar

You're on the good way. The service provider is the right away. You need to figure out what class the package is binding. You should replace that class in your service provider

$this->app->singleton(\Namespace\Of\Package\Class::class, function () {
    return new \App\Classes\elFinderSimpleLogger(public_path('log.txt'));
});

However in your case this is not going work because the package is not registering that class. It's just using a vendor package as well. Because of this you're not able to simple override the implementation in the container of Laravel. It seems that vendor is not using namespaces, which makes it a lot harder to replace it in Laravel.

At least I can't think of a way right now.

1 like
dan1234827's avatar

In the past I’ve used cron jobs to monitor an upload directory and then add any new files to a queue for processing whenever the check ran (typically once per minute).

This was quite a while ago, so it might not be best practise today (at the very least, you might use supervisor to run a looping watch script so files are picked up faster), but it did the job.

chris_j's avatar

Thank you for your answers. I'll see what I can come up with!

chris_j's avatar

@BOBBYBOUWMANN - Hi Bobby,

Thanks for your insight. When you say that vendor is not using namespaces are you talking about Elfinder (the package being referenced by the intermediate Laravel package?

Please or to participate in this conversation.