swooletw / laravel-swoole

High performance HTTP server based on Swoole. Speed up your Laravel or Lumen applications.
MIT License
4.05k stars 390 forks source link

How to clear class static variable #142

Closed mitoop closed 6 years ago

mitoop commented 6 years ago

Hi, albertcht, my env is

PHP Version   7.2.9     
Swoole Version  4.1.2                                                  
Laravel Version  5.7.3

I use laravel nova, and in the nova source code, it use class static variable, how can i reset it? Thanks

albertcht commented 6 years ago

Hi @mitoop ,

You need to reset these static variables before every request by yourself. You can write your reset logics in a service provider and add it to providers in swoole_http.php.

mitoop commented 6 years ago

Hi @albertcht I had tried. But it didn't work. I will make an another try. Thanks.

mitoop commented 6 years ago

Hi @albertcht Excuse me. Is there any good practice to reset the class static varible?

albertcht commented 6 years ago

Hi @mitoop ,

I think resetting static variables in service provider should do the trick. Do you mind providing more details about your implementation in your service provider?

mitoop commented 6 years ago

Hi @albertcht

class Nova
{
    use AuthorizesRequests;

    /**
     * The registered resource names.
     *
     * @var array
     */
    public static $resources = [];

    /**
     * An index of resource names keyed by the model name.
     *
     * @var array
     */
    public static $resourcesByModel = [];

    /**
     * The callback used to create new users via the CLI.
     *
     * @var \Closure
     */
    public static $createUserCallback;

   ....

In the code, it's used directly. like $resources. I tried in service provider and write a resetter to make the variable equel its original value. But not works. Is it clear about what I say?

albertcht commented 6 years ago

Hi @mitoop ,

Can you paste the code of your service provider below?

mitoop commented 6 years ago

Hi, @albertcht In swoole_http.php

  'providers' => [
        Illuminate\Pagination\PaginationServiceProvider::class,
        Bugsnag\BugsnagLaravel\BugsnagServiceProvider::class,
        Clockwork\Support\Laravel\ClockworkServiceProvider::class,
        App\Providers\NovaServiceProvider::class, // The service provider
    ], 

the boot and register:

    public function boot()
    {
        $this->routes();

        Nova::serving(function (ServingNova $event) {
            $this->authorization();

            $this->resources();
            Nova::cards($this->cards());
            Nova::tools($this->tools());
        });
    }

    public function register()
    {
        //
    }

In the service provider, it use Nova directly.

The Nova class contains some static class varible.

In the Nova class

  /**
     * Register the given resources.
     *
     * @param  array  $resources
     * @return static
     */
    public static function resources(array $resources)
    {
        static::$resources = array_merge(static::$resources, $resources);

        return new static;
    }

It like this, so static::$resources array items repeat when another request coming.

albertcht commented 6 years ago

Hi @mitoop ,

What if you reset static $resources property directly like:

public function boot()
{
    Nova::$resources = [];
}
mitoop commented 6 years ago

Hi @albertcht I tried and Now I find some clue, It relates to many class, Tomorrow I will do more debugging. Thanks your time.

zacksleo commented 6 years ago

Hi @mitoop ,

What if you reset static $resources property directly like:

public function boot()
{
    Nova::$resources = [];
}

not work well

ibrunotome commented 5 years ago

Did you solved this @zacksleo ?

zacksleo commented 5 years ago

not yet