Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions src/Phaseolies/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -256,9 +256,7 @@ public function langPath($path = ''): string
*/
public function configure(Application $app): ApplicationBuilder
{
return (new ApplicationBuilder($app))
->withTimezone()
->withMiddlewareStack();
return (new ApplicationBuilder($app))->withTimezone();
}

/**
Expand Down Expand Up @@ -804,7 +802,8 @@ public function bindApplicationNecessaryPath(): void
protected function bindSingletonClasses(): void
{
$this->bindApplicationNecessaryPath();
$this->singleton('request', fn() => Request::createFromGlobals());

$this->singleton('request', fn() => Request::capture());

$this->bindHttpGateway();

Expand Down Expand Up @@ -1149,6 +1148,13 @@ protected function cleanupRequestScopedServices(): void
foreach (['session', 'request', 'response', 'redirect'] as $abstract) {
$this->forgetResolved($abstract);
}

// re-resolves fresh instance instead of reusing it.
$this->forgetRequestScopedInstances();

// Undo any config() mutation made while handling
// this request, so it doesn't leak into the next one.
Config::resetRuntimeOverrides();
}

/**
Expand Down Expand Up @@ -1276,6 +1282,8 @@ public function handle(Request $request): Response
*/
public function dispatch($request): DispatchResult
{
$this->snapshotBootBindings();

try {
$this->instance('request', $request);

Expand Down
76 changes: 2 additions & 74 deletions src/Phaseolies/ApplicationBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,13 @@
namespace Phaseolies;

use Phaseolies\Support\TimezoneHandler;
use Phaseolies\Middleware\Contracts\Middleware as ContractsMiddleware;

class ApplicationBuilder
{
/**
* Holds the current HTTP request instance
*
* @var \Phaseolies\Http\Request<string>
*/
protected $request;

/**
* @param Application $app
*/
public function __construct(protected Application $app)
{
$this->request = $this->app->make('request');
}
public function __construct(protected Application $app) {}

/**
* Set the application timezone
Expand All @@ -39,68 +28,7 @@ public function withTimezone(): self
}

/**
* Configures the application with middleware stack handling
*
* @return self
* @throws \Exception
*/
public function withMiddlewareStack(): self
{
$middlewareStack = $this->buildMiddlewareStack();

$handler = $this->processMiddlewareStack($middlewareStack);

$this->app->router->getGateway()->handle($this->request, $handler);

return $this;
}

/**
* Constructs the middleware stack based on request type
*
* @return array
*/
protected function buildMiddlewareStack(): array
{
$gateway = $this->app->router->getGateway();

$middlewareStack = $gateway->getGlobalMiddleware();

$groupKey = $this->request->isApiRequest() ? 'api' : 'web';
$groupMiddleware = $gateway->getMiddlewareGroups()[$groupKey] ?? [];

return array_merge($middlewareStack, $groupMiddleware);
}

/**
* Processes the middleware stack into a handler pipeline.
*
* @param array $middlewareStack
* @return callable
* @throws \Exception
*/
protected function processMiddlewareStack(array $middlewareStack): callable
{
$response = fn() => $this->app->make('response');

foreach ($middlewareStack as $middlewareClass) {
$middlewareInstance = $this->app->make($middlewareClass);
if (!$middlewareInstance instanceof ContractsMiddleware) {
throw new \Exception(
"Failed to register middleware {$middlewareClass}: it must implement " . ContractsMiddleware::class . "."
);
}

$response = function ($request) use ($middlewareInstance, $response) {
return $middlewareInstance($request, $response);
};
}

return $response;
}

/**
* Finalizes the builder process and returns the configured application.
* Finalizes the builder process and returns the configured application
*
* @return Application
*/
Expand Down
25 changes: 21 additions & 4 deletions src/Phaseolies/Config/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,13 @@ final class Config
*/
protected static ?array $configFiles = null;

/**
* A snapshot of `$config` exactly as it stood right after boot
*
* @var array<string, mixed>
*/
protected static array $bootSnapshot = [];

/**
* Initialize the configuration system.
*
Expand All @@ -58,9 +65,20 @@ public static function initialize(): void
if (self::$cacheFile === null) {
self::$cacheFile = storage_path('framework/cache/config.php');
self::loadFromCache();
self::$bootSnapshot = self::$config;
}
}

/**
* Undo any runtime mutation made by `set()` since boot
*
* @return void
*/
public static function resetRuntimeOverrides(): void
{
self::$config = self::$bootSnapshot;
}

/**
* Get all config file paths, cached for the request lifetime.
*
Expand Down Expand Up @@ -243,9 +261,6 @@ public static function set(string $key, mixed $value): void
} else {
$current = $value;
}

self::$configModified = true;
self::cacheConfig();
}

/**
Expand Down Expand Up @@ -278,7 +293,9 @@ public static function has(string $key): bool
*/
public static function clearCache(): void
{
if (file_exists(self::$cacheFile)) @unlink(self::$cacheFile);
if (self::$cacheFile !== null && file_exists(self::$cacheFile)) {
@unlink(self::$cacheFile);
}

self::$config = [];
self::$fileHashes = [];
Expand Down
Loading
Loading