Laravel-permission: After installing application breaks becuase of custom cache driver

Created on 23 Jan 2018  Â·  7Comments  Â·  Source: spatie/laravel-permission

Hi,

I have an application that is using this custom cache driver: https://github.com/ethanhann/Laravel-RedisStore

immediately after i have installed this package my application breaks with the message "Driver [ehann-redis] is not supported."

it seems that installing this page breaks the possiblity to use custom cache drivers.

AppServiceProvider.php

Cache::extend('ehann-redis', function ($app) {
            return Cache::repository(new \Ehann\Cache\RedisStore(
                $app['redis'],
                $app['config']['cache.prefix'],
                $app['config']['cache.stores.redis.connection']
            ));
        });

config/cache.php

'redis' => [
            'driver' => 'ehann-redis',
            'connection' => 'default',
        ],

Most helpful comment

To me it sound like this package is using some cache functionality before the custom driver is setup

On that point you are correct.

I'm still not sure it's the package's fault.

It's a chicken/egg issue related to Laravel's Auto-Discovery.

Laravel's Auto-Discovery is inserting this package's ServiceProvider into the boot order above your AppServiceProvider, so when the package's boot method fires and resolves the Cache Repository, it hasn't seen your custom extend, and so it legitimately doesn't know about your custom driver yet.
You can verify this by looking at your /bootstrap/cache/services.php.

One solution is very simple: don't use auto-discovery for this package.

  1. Add to the composer.json dont-discover: "spatie/laravel-permission"
  2. Edit your /config/app.php and add this package after your AppServiceProvider:
        App\Providers\AppServiceProvider::class,
        ...
        Spatie\Permission\PermissionServiceProvider::class,

You will probably run into a similar issue with other packages that leverage caching services in their service providers.

Related:
https://github.com/laravel/framework/issues/22198
https://github.com/ethanhann/Laravel-RedisStore/issues/3
https://laracasts.com/discuss/channels/laravel/custom-cache-driver-in-55

All 7 comments

I don't think this issue is specific to the laravel-permission package. This package simply makes calls to the caching layer. Nothing fancy about its implementation.

Your error message appears to be coming from the Laravel Framework, related to your specific driver:
https://github.com/laravel/framework/blob/5.5/src/Illuminate/Cache/CacheManager.php#L107

Yeah well I don’t agree. It’s indeed thrown over there and it’s caused by this package. Without this package it’s working like a rocket. It cannot even complete the auto discover when this package is included in composer.json because it fails.

To me it sound like this package is using some cache functionality before the custom driver is setup

On 23 Jan 2018, 17:18 +0100, Chris Brown notifications@github.com, wrote:

I don't think this issue is specific to the laravel-permission package. This package simply makes calls to the caching layer. Nothing fancy about its implementation.
Your error message appears to be coming from the Laravel Framework, related to your specific driver:
https://github.com/laravel/framework/blob/5.5/src/Illuminate/Cache/CacheManager.php#L107
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub, or mute the thread.

To me it sound like this package is using some cache functionality before the custom driver is setup

On that point you are correct.

I'm still not sure it's the package's fault.

It's a chicken/egg issue related to Laravel's Auto-Discovery.

Laravel's Auto-Discovery is inserting this package's ServiceProvider into the boot order above your AppServiceProvider, so when the package's boot method fires and resolves the Cache Repository, it hasn't seen your custom extend, and so it legitimately doesn't know about your custom driver yet.
You can verify this by looking at your /bootstrap/cache/services.php.

One solution is very simple: don't use auto-discovery for this package.

  1. Add to the composer.json dont-discover: "spatie/laravel-permission"
  2. Edit your /config/app.php and add this package after your AppServiceProvider:
        App\Providers\AppServiceProvider::class,
        ...
        Spatie\Permission\PermissionServiceProvider::class,

You will probably run into a similar issue with other packages that leverage caching services in their service providers.

Related:
https://github.com/laravel/framework/issues/22198
https://github.com/ethanhann/Laravel-RedisStore/issues/3
https://laracasts.com/discuss/channels/laravel/custom-cache-driver-in-55

Ok cool! I will try that approach!

Thanks for your time on this!

On 23 Jan 2018, 22:28 +0100, Chris Brown notifications@github.com, wrote:

Travis-CI demonstrations:
Failing build: https://travis-ci.org/drbyte/spatie-permissions-demo/builds/332500310
Fixed build after using dont-discover: https://travis-ci.org/drbyte/spatie-permissions-demo/builds/332506018
Demo example branch: https://github.com/drbyte/spatie-permissions-demo/tree/ehann-redis-test
Fixing commit: drbyte/spatie-permissions-demo@d22c4f6
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub, or mute the thread.

Did reworking the load-order solve this issue for your project's use of custom cache driver?

Hi,

Yes, using don’t discover and the correct order solved it!

On 25 Jan 2018, 16:31 +0100, Chris Brown notifications@github.com, wrote:

Did reworking the load-order solve this issue for your project's use of custom cache driver?
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub, or mute the thread.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

bhulsman picture bhulsman  Â·  3Comments

feliperoan picture feliperoan  Â·  3Comments

NattananWs picture NattananWs  Â·  3Comments

Dreambox13 picture Dreambox13  Â·  3Comments

holymp2006 picture holymp2006  Â·  4Comments