Skip to content

Commit

Permalink
Renames variables.
Browse files Browse the repository at this point in the history
  • Loading branch information
a.stecher committed Jan 6, 2025
1 parent 0307f79 commit fd39022
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 27 deletions.
8 changes: 5 additions & 3 deletions src/ApplicationGateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class ApplicationGateway
{
use DispatchesEvents;

public function __construct(protected ApplicationResetter $resetter, protected Application $app, protected Application $sandbox)
public function __construct(protected ApplicationResetter $resetter, protected Application $snapshot, protected Application $sandbox)
{
}

Expand All @@ -26,10 +26,12 @@ public function handle(Request $request): Response
{
$request->enableHttpMethodParameterOverride();

$this->dispatchEvent($this->sandbox, new RequestReceived($this->app, $this->sandbox, $request));
// reset all default services
$this->resetter->prepareApplicationForNextRequest($request);
$this->resetter->prepareApplicationForNextOperation();

$this->dispatchEvent($this->sandbox, new RequestReceived($this->snapshot, $this->sandbox, $request));

if (Octane::hasRouteFor($request->getMethod(), '/'.$request->path())) {
return Octane::invokeRoute($request, $request->getMethod(), '/'.$request->path());
}
Expand All @@ -46,7 +48,7 @@ public function terminate(Request $request, Response $response): void
{
$this->resetter->kernel->terminate($request, $response);

$this->dispatchEvent($this->sandbox, new RequestTerminated($this->app, $this->sandbox, $request, $response));
$this->dispatchEvent($this->sandbox, new RequestTerminated($this->snapshot, $this->sandbox, $request, $response));

$route = $request->route();

Expand Down
13 changes: 8 additions & 5 deletions src/ApplicationResetter.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ class ApplicationResetter

private $octaneHttps;

private $originalAppLocale;
private $initialAppLocale;

private $initialAppFallbackLocale;

private $logDefaultDriver = null;

Expand All @@ -47,7 +49,8 @@ public function __construct(ApplicationSnapshot $snapshot, Application $sandbox)
$this->config = $snapshot['config'];
$this->url = $snapshot['url'];
$this->octaneHttps = $this->config->get('octane.https');
$this->originalAppLocale = $this->snapshot->getLocale();
$this->initialAppLocale = $this->config->get('app.locale');
$this->initialAppFallbackLocale = $this->config->get('app.fallback_locale');
$this->kernel = $this->snapshot->make(Kernel::class);

if ($this->config->get('cache.stores.array')) {
Expand Down Expand Up @@ -99,7 +102,7 @@ private function flushLocaleState(): void
{
// resetting the locale is an expensive operation
// only do it if the locale has changed
if (Carbon::getLocale() !== $this->originalAppLocale) {
if (Carbon::getLocale() !== $this->initialAppLocale) {
(new CarbonServiceProvider($this->sandbox))->updateLocale();
}
}
Expand Down Expand Up @@ -238,8 +241,8 @@ private function flushMonologState(): void
private function flushTranslatorCache(): void
{
$this->snapshot->resetInitialInstance('translator', function ($translator) {
$translator->setLocale($this->config->get('app.locale'));
$translator->setFallback($this->config->get('app.fallback_locale'));
$translator->setLocale($this->initialAppLocale);
$translator->setFallback($this->initialAppFallbackLocale);

if ($translator instanceof NamespacedItemResolver) {
$translator->flushParsedKeys();
Expand Down
38 changes: 19 additions & 19 deletions src/Worker.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class Worker implements WorkerContract
*
* @var Application
*/
protected $app;
protected $sandbox;

/**
* A clone of the warmed up initial application.
Expand All @@ -56,7 +56,7 @@ public function boot(array $initialInstances = []): void
// First we will create an instance of the Laravel application that can serve as
// the base container instance we will clone from on every request. This will
// also perform the initial bootstrapping that's required by the framework.
$this->app = $app = $this->appFactory->createApplication(
$this->sandbox = $app = $this->appFactory->createApplication(
array_merge(
$initialInstances,
[Client::class => $this->client],
Expand All @@ -83,7 +83,7 @@ public function handle(Request $request, RequestContext $context): void
// certain instances that got resolved / mutated during a previous request.
$this->createAppSnapshot();

$gateway = new ApplicationGateway($this->appResetter, $this->appSnapshot, $this->app);
$gateway = new ApplicationGateway($this->appResetter, $this->appSnapshot, $this->sandbox);

try {
$responded = false;
Expand All @@ -108,11 +108,11 @@ public function handle(Request $request, RequestContext $context): void

$responded = true;

$this->invokeRequestHandledCallbacks($request, $response, $this->app);
$this->invokeRequestHandledCallbacks($request, $response, $this->sandbox);

$gateway->terminate($request, $response);
} catch (Throwable $e) {
$this->handleWorkerError($e, $this->app, $request, $context, $responded);
$this->handleWorkerError($e, $this->sandbox, $request, $context, $responded);
} finally {
// After the request handling process has completed we will unset some variables
// plus reset the current application state back to its original state before
Expand All @@ -138,17 +138,17 @@ public function handleTask($data)

try {
$this->appResetter->prepareApplicationForNextOperation();
$this->dispatchEvent($this->app, new TaskReceived($this->appSnapshot, $this->app, $data));
$this->dispatchEvent($this->sandbox, new TaskReceived($this->appSnapshot, $this->sandbox, $data));

$result = $data();

$this->dispatchEvent($this->app, new TaskTerminated($this->appSnapshot, $this->app, $data, $result));
$this->dispatchEvent($this->sandbox, new TaskTerminated($this->appSnapshot, $this->sandbox, $data, $result));
} catch (Throwable $e) {
$this->dispatchEvent($this->app, new WorkerErrorOccurred($e, $this->app));
$this->dispatchEvent($this->sandbox, new WorkerErrorOccurred($e, $this->sandbox));

return TaskExceptionResult::from($e);
} finally {
$this->app->flush();
$this->sandbox->flush();
}

return new TaskResult($result);
Expand All @@ -163,12 +163,12 @@ public function handleTick(): void

try {
$this->appResetter->prepareApplicationForNextOperation();
$this->dispatchEvent($this->app, new TickReceived($this->appSnapshot, $this->app));
$this->dispatchEvent($this->app, new TickTerminated($this->appSnapshot, $this->app));
$this->dispatchEvent($this->sandbox, new TickReceived($this->appSnapshot, $this->sandbox));
$this->dispatchEvent($this->sandbox, new TickTerminated($this->appSnapshot, $this->sandbox));
} catch (Throwable $e) {
$this->dispatchEvent($this->app, new WorkerErrorOccurred($e, $this->app));
$this->dispatchEvent($this->sandbox, new WorkerErrorOccurred($e, $this->sandbox));
} finally {
$this->app->flush();
$this->sandbox->flush();
}
}

Expand Down Expand Up @@ -220,27 +220,27 @@ public function onRequestHandled(Closure $callback)
*/
public function application(): Application
{
if (! $this->app) {
if (! $this->sandbox) {
throw new RuntimeException('Worker has not booted. Unable to access application.');
}

return $this->app;
return $this->sandbox;
}

/**
* Terminate the worker.
*/
public function terminate(): void
{
$this->dispatchEvent($this->app, new WorkerStopping($this->app));
$this->dispatchEvent($this->sandbox, new WorkerStopping($this->sandbox));
}

protected function createAppSnapshot(): void
{
if (! isset($this->appSnapshot)) {
$this->appSnapshot = ApplicationSnapshot::createSnapshotFrom($this->app);
$this->appResetter = new ApplicationResetter($this->appSnapshot, $this->app);
$this->appSnapshot = ApplicationSnapshot::createSnapshotFrom($this->sandbox);
$this->appResetter = new ApplicationResetter($this->appSnapshot, $this->sandbox);
}
$this->appSnapshot->loadSnapshotInto($this->app);
$this->appSnapshot->loadSnapshotInto($this->sandbox);
}
}

0 comments on commit fd39022

Please sign in to comment.