flarum / issue-archive

0 stars 0 forks source link

Eliminate per-request data in application state #115

Open askvortsov1 opened 3 years ago

askvortsov1 commented 3 years ago

From Franz in https://github.com/flarum/core/issues/2307#issuecomment-698836620:

I've taken great care to ensure that Flarum can easily be used with modern approaches like application servers (ReactPHP, PHP-PM, ...). This requires a mental shift from the good old days when everything was always "shared-nothing": global state was global, but only for the current request. No leaks, no worries.

I want to keep the container and application classes free of per-request objects. They should only deal with truly application-wide state, whether the PHP process only handles one request or multiple. There is one object tree that can be long-living, and then there are object trees for each request. In the traditional world, there will be no difference between them, but once you run with a PHP application server, there is.

Laravel does this, true, and I don't particularly like that. :wink: The downsides can be circumvented, alright, but you have to be aware of them. Just look at some of the dark magic that the Swoole adapter for Laravel has to do. Having this clean separation can sometimes make specific things a bit more complicated (though not impossible, in my experience), but has the benefit of being compatible with the different ways to run PHP applications - by design.

As pointed out above, Flarum is doing a relatively good job so far in terms of keeping per-request state separated from application state. However, there are still several places where we could be doing better. For now, the goal of this issue will be to catalogue these places and discuss how they could be improved.

askvortsov1 commented 3 years ago

It wouldn't be in core, but now that Laravel Octane is in beta, we might want to look into it as a potential extension. WRT to core, one of the changes I've seen in Laravel packages is using the value of $container passed into closures as an argument as opposed to reusing $this->container in service providers.