laravel/framework

config:cache command crashes on closures

Closed this issue ยท 6 comments

I have a third party package that uses a closure in its configuration (dingo/api), unfortunately that prevents me from using the config:cache command as this crashes the whole application with no friendly error message nor way to recover from it besides manually deleting the config cache:

PHP Fatal error:  Call to undefined method Closure::__set_state() in /Users/anahkiasen/config-closures/bootstrap/cache/config.php on line 22
PHP Stack trace:
PHP   1. {main}() /Users/anahkiasen/config-closures/artisan:0
PHP   2. Illuminate\Foundation\Console\Kernel->handle() /Users/anahkiasen/config-closures/artisan:36
PHP   3. Illuminate\Foundation\Console\Kernel->bootstrap() /Users/anahkiasen/config-closures/vendor/laravel/framework/src/Illuminate/Foundation/Console/Kernel.php:96
PHP   4. Illuminate\Foundation\Application->bootstrapWith() /Users/anahkiasen/config-closures/vendor/laravel/framework/src/Illuminate/Foundation/Console/Kernel.php:196
PHP   5. Illuminate\Foundation\Bootstrap\LoadConfiguration->bootstrap() /Users/anahkiasen/config-closures/vendor/laravel/framework/src/Illuminate/Foundation/Application.php:203
PHP   6. require() /Users/anahkiasen/config-closures/vendor/laravel/framework/src/Illuminate/Foundation/Bootstrap/LoadConfiguration.php:27

Fatal error: Call to undefined method Closure::__set_state() in /Users/anahkiasen/config-closures/bootstrap/cache/config.php on line 22

And the faulty part in the cached configuration file:

    array (
      'oauth' => 
      Closure::__set_state(array(
      )),
    ),

Shouldn't Laravel leverage serializable closures here?

This has come up before. We don't support closures on purpose because the overhead of unserializing is greater than anything gained otherwise.

Any config system that contain closures is badly designed.

Any solution to this ? config:cache is unusable right now

is it solved??

Any config system that contain closures is badly designed.

@GrahamCampbell I have to confess that my laravel and php skills are very rudimentary, so could you please advise me how I could solve the following task without closures?

I want to dynamically declare CRUD routes

if (!function_exists('registerResourceRoute')) {
  function registerResourceRoute($endpoint, $tablename, $claim)
  {
    // Route::get($endpoint . '/{id}', $controller . '@getOne')->prefix('adminpanel')->middleware('jwtclaim:VIEW_' . strtoupper($endpoint));

    //Get a list of records
    //getList	GET http://my.api.url/posts?sort=["title","ASC"]&range=[0, 24]&filter={"title":"bar"}
    Route::get($endpoint, [
      function (Request $request) use ($tablename, $endpoint) {
        return App::make('App\Http\Controllers\CRUDController', ['tablename' => $tablename])->getList($request, $endpoint);
      }
    ])->prefix('adminpanel')->middleware('jwtclaim:VIEW_' . $claim);

    //Get a single record

    // adminpanel/bookings/1
    Route::get($endpoint . '/{id}', [
      function (Request $request, string $id) use ($tablename) {
        return App::make('App\Http\Controllers\CRUDController', ['tablename' => $tablename])->getOne($request, $id);
      }
    ])->prefix('adminpanel')->middleware('jwtclaim:VIEW_' . $claim);

    Route::post()
    Route::delete() 
   ....
registerResourceRoute('locations', 'public.location', 'DB');
registerResourceRoute('locationTranslations', 'public.locationTranslation', 'DB');
registerResourceRoute('locationMappings', 'public.location_mapping', 'DB');
registerResourceRoute('locationRegions', 'public.location_region', 'DB');
.... and 40 more

The controller code is 100% identical between all controllers, how would the syntax look like to solve this task without the need to write 40 - 50 controllers and without closures?

In laravel, when running the config:cache command, the __set_state method (for me TelegramBotHandler) will be used to create an object from the cached configs. So just write a similar function for TelegramBotHandler and the problem will be solved.

class TelegramBotHandler extends MonologTelegramBotHandler
{
    public static function __set_state(array $array): TelegramBotHandler
    {
        return new self(
            $array['apiKey'],
            $array['channel'],
            $array['level'],
            $array['bubble'],
            $array['parseMode'],
            $array['disableWebPagePreview'],
            $array['disableNotification'],
            $array['splitLongMessages'],
            $array['delayBetweenMessages'],
            $array['topic']
        );
    }
}