Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug] Config caching causes a problem when having closure #24103

Closed
daison12006013 opened this issue May 4, 2018 · 3 comments
Closed

[Bug] Config caching causes a problem when having closure #24103

daison12006013 opened this issue May 4, 2018 · 3 comments

Comments

@daison12006013
Copy link

daison12006013 commented May 4, 2018

  • Laravel Version: 5.6.19
  • PHP Version: PHP 7.2.2-1+ubuntu16.04.1+deb.sury.org+1 (cli) (built: Feb 1 2018 16:01:26) ( NTS )
  • Database Driver & Version: 5.7.21-0ubuntu0.16.04.1 (Ubuntu) MySQL

Description:

When running config:cache this thing caches inside your bootstrap/cache/config.php, but I have some closures inside my config, see. Steps To Produce.

After the caching, if you will try to run the artisan command again. It will throw now an error now Class config does not exist in {path to Illuminate/Container/Container.php}

I tried to manually removing Closure::__set_state(array()) inside the cached config and artisan works, but my closure config does not exists now, actually the data of the closure was not able to capture.

Steps To Reproduce:

  • You need to put some Closure inside your config, specially if you want to call other helpers, as example below image.

screen shot 2018-05-04 at 11 00 06 am

Edit 1

If we're strict to use raw config, why can't we use serialize first to prevent using Closures/Objects, only var_export() directly inside ConfigCacheCommand.php. I would like to suggest, maybe to use jeremeamia/super_closure package to support this kind of scenario.

@tillkruss
Copy link
Collaborator

You can't cache closures.

@daison12006013
Copy link
Author

This is possible @tillkruss using SuperClosure, we will serialize it first and the var_export() will store it like this.

'C:32:"SuperClosure\\SerializableClosure":388:{a:5:{s:4:"code";s:253:"function () {
    return [\'email\' => \'[email protected]\', \'fullName\' => \'John Doe\', \'forgotPasswordToken\' => \'qwertyuiopasdfghjklzxcvbnm\', \'link\' => \\route(\'auth.reset-password.update-get\', [\'forgot_password_token\' => \'qwertyuiopasdfghjklzxcvbnm\'])];
}";s:7:"context";a:0:{}s:7:"binding";N;s:5:"scope";s:34:"Illuminate\\Support\\ServiceProvider";s:8:"isStatic";b:0;}}'

At ConfigCacheCommand.php we could add this kind of method.

    /**
     * Serialize the records.
     *
     * @param  array $records
     * @return array
     */
    protected function transformClosure(array $records)
    {
        $serializer = new Serializer;

        foreach ($records as $key => $val) {
            if (is_array($val)) {
                $records[$key] = $this->transformClosure($val);
            }

            if ($val instanceof \Closure) {
                $records[$key] = $serializer->serialize($val);
            }
        }

        return $records;
    }

At Illuminate\Config\Repository.php we can add new method realValue() and enclose it under the get() or getMany() method when calling Arr::get() in it.

    /**
     * Value analyzer and changer.
     *
     * @param  mixed $value
     * @return mixed
     */
    public function realValue($value)
    {
        if (
            is_string($value) &&
            strpos($value, "SuperClosure\\SerializableClosure") !== false
        ) {
            return (new \SuperClosure\Serializer())->unserialize($value);
        }

        return $value;
    }

Proof

screen shot 2018-05-04 at 2 28 55 pm

If you think this is okay, will apply a PR instead.

@tillkruss
Copy link
Collaborator

Yeah, please feel free to submit a PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants