muzafferdede
6 months ago
942
9
Laravel

How to re-construct a Class in single process?

Posted 6 months ago by muzafferdede

Hi, Is there any way i can reconstruct my controller classes after some method calls?

Because i am doing some logic in "while" loop and i need to re instance some classes to update its properties during same process.

for now i am doing it with


    public function status()
    {
        $this->__construct();
        return $this->successful($this->device);
    }
public function register()
    {
        $this->__construct();
        $device = Device::updateOrCreate(
            [
                'serial_number' => $this->request->serial_number,
            ],
            [
                'name' => $this->request->serial_number,
                'type' => $this->request->type,
                'properties' => $this->request->properties,
            ]
        );

        return $device->save() ? $this->successful($device) : $this->failed(15);
    }

    public function activate()
    {
        $this->__construct();
        $device = Device::whereActivationCode($this->request->activation_code)
            ->whereSerialNumber($this->request->serial_number)
            ->first();

        if (!$device) {
            return $this->failed(9);
        }

        if ($device->activated) {
            return $this->failed(10);
        }

        $store = Store::whereUid($this->request->uid)->first() ?? $device->store;
        if (!$store) {
            return $this->failed(0);
        }

        $device->store()->associate($store);
        $device->activated = now();
        $device->api_token = Str::uuid();
        $device->save();

        return $this->successful($device);
    }

as you can see, all my methods has to re-construct to renew its properties. I can't kill the process because i am listening a socket for new events. Because of this, application only can boot 1 time. I am not sure how can i renew the existing instances in the application container. I would like to use service provider to achieve it. And update the singleton anytime new events causes.

any advice? Thanks.

Please sign in or create an account to participate in this conversation.