diff --git a/composer.json b/composer.json index 020ba092..e79b41cc 100644 --- a/composer.json +++ b/composer.json @@ -49,10 +49,12 @@ "illuminate/container": "^10.33", "illuminate/contracts": "^10.33", "illuminate/database": "^10.33", + "illuminate/encryption": "^10.33", "illuminate/events": "^10.33", "illuminate/filesystem": "^10.33", "illuminate/http": "^10.33", "illuminate/log": "^10.33", + "illuminate/queue": "^10.33", "illuminate/routing": "^10.33", "illuminate/support": "^10.33", "illuminate/view": "^10.33", diff --git a/src/Roots/Acorn/Bootloader.php b/src/Roots/Acorn/Bootloader.php index 3009d196..15fa12b2 100644 --- a/src/Roots/Acorn/Bootloader.php +++ b/src/Roots/Acorn/Bootloader.php @@ -13,28 +13,28 @@ class Bootloader { /** - * Bootloader instance + * The Bootloader instance. * * @var static */ protected static $instance; /** - * Application instance + * The Application instance. * * @var \Illuminate\Contracts\Foundation\Application */ protected $app; /** - * Filesystem helper + * The Filesystem instance. * * @var \Roots\Acorn\Filesystem\Filesystem */ protected $files; /** - * Base path for the application + * The application's base path. * * @var string */ @@ -48,7 +48,7 @@ class Bootloader protected $absoluteApplicationPathPrefixes = ['/', '\\']; /** - * Set the Bootloader instance + * Set the Bootloader instance. */ public static function setInstance(?self $bootloader) { @@ -56,7 +56,7 @@ public static function setInstance(?self $bootloader) } /** - * Get the Bootloader instance + * Get the Bootloader instance. * * @return static */ @@ -146,6 +146,7 @@ protected function bootConsole(ApplicationContract $app) ); $kernel->terminate($input, $status); + exit($status); } @@ -200,47 +201,66 @@ protected function bootHttp(ApplicationContract $app) { $kernel = $app->make(\Illuminate\Contracts\Http\Kernel::class); $request = \Illuminate\Http\Request::capture(); + $time = microtime(); $app->instance('request', $request); Facade::clearResolvedInstance('request'); $kernel->bootstrap($request); - try { + add_filter('do_parse_request', function ($doParse, \WP $wp, $extraQueryVars) use ($app, $request) { if (! $app->make('router')->getRoutes()->match($request)) { - throw new \Symfony\Component\HttpKernel\Exception\NotFoundHttpException(); + return $doParse; } - } catch (\Exception $e) { - return; - } - add_filter( - 'do_parse_request', - fn ($doParse, \WP $wp, $extraQueryVars) => apply_filters('acorn/router/do_parse_request', $doParse, $wp, $extraQueryVars), - 100, - 3 - ); + return apply_filters('acorn/router/do_parse_request', $doParse, $wp, $extraQueryVars); + }, 100, 3); - add_action('parse_request', function () use ($kernel, $request) { - /** @var \Illuminate\Http\Response */ - $response = $kernel->handle($request); + $app->make('router') + ->any('{any?}', fn () => response()->json(['message' => "wordpress_request_{$time}"])) + ->where('any', '.*'); - if (! $response->isServerError() && $response->status() >= 400) { - return; - } + add_action('parse_request', fn () => $this->handleRequest($time, $kernel, $request)); + } + /** + * Handle the request. + * + * @return void + */ + protected function handleRequest( + string $time, + \Illuminate\Contracts\Http\Kernel $kernel, + \Illuminate\Http\Request $request + ) { + $response = $kernel->handle($request); + + if ( + $response instanceof \Symfony\Component\HttpFoundation\Response + && ! $response->isServerError() + && $response->getStatusCode() >= 400 + ) { + return; + } + + if ( + in_array(false, [ + $response instanceof \Illuminate\Http\JsonResponse, + is_string($response->getContent()), + $data = json_decode($response->getContent()), + isset($data->message) && $data->message == "wordpress_request_{$time}", + ]) + ) { $body = $response->send(); $kernel->terminate($request, $body); exit; - }); + } } /** - * Get Application instance. - * - * @param ApplicationContract $app + * Get the Application instance. */ public function getApplication(): ApplicationContract { @@ -272,7 +292,7 @@ public function getApplication(): ApplicationContract } /** - * Get the application basepath + * Get the application's base path. */ protected function basePath(): string { @@ -388,6 +408,7 @@ protected function fallbackPath(string $path): string protected function fallbackStoragePath() { $path = WP_CONTENT_DIR.DIRECTORY_SEPARATOR.'cache'.DIRECTORY_SEPARATOR.'acorn'; + $this->files->ensureDirectoryExists($path.DIRECTORY_SEPARATOR.'framework'.DIRECTORY_SEPARATOR.'cache'.DIRECTORY_SEPARATOR.'data', 0755, true); $this->files->ensureDirectoryExists($path.DIRECTORY_SEPARATOR.'framework'.DIRECTORY_SEPARATOR.'views', 0755, true); $this->files->ensureDirectoryExists($path.DIRECTORY_SEPARATOR.'framework'.DIRECTORY_SEPARATOR.'sessions', 0755, true); diff --git a/src/Roots/Acorn/Console/Commands/KeyGenerateCommand.php b/src/Roots/Acorn/Console/Commands/KeyGenerateCommand.php new file mode 100644 index 00000000..f6f52a81 --- /dev/null +++ b/src/Roots/Acorn/Console/Commands/KeyGenerateCommand.php @@ -0,0 +1,44 @@ +laravel->environmentFilePath()) + ? $this->laravel->environmentFilePath() + : File::closest($this->laravel->basePath(), '.env'); + + if (! $envFile) { + $this->error('Unable to set application key. Create a .env file.'); + + return false; + } + + $replaced = preg_replace( + $this->keyReplacementPattern(), + 'APP_KEY='.$key, + $input = file_get_contents($envFile) + ); + + if ($replaced === $input || $replaced === null) { + $this->error('Unable to set application key. No APP_KEY variable was found in the .env file.'); + + return false; + } + + file_put_contents($envFile, $replaced); + + return true; + } +} diff --git a/src/Roots/Acorn/Console/Kernel.php b/src/Roots/Acorn/Console/Kernel.php index 6f06e668..3bf0a093 100644 --- a/src/Roots/Acorn/Console/Kernel.php +++ b/src/Roots/Acorn/Console/Kernel.php @@ -36,6 +36,7 @@ class Kernel extends FoundationConsoleKernel \Roots\Acorn\Console\Commands\AcornInitCommand::class, \Roots\Acorn\Console\Commands\ComposerMakeCommand::class, \Roots\Acorn\Console\Commands\ConfigCacheCommand::class, + \Roots\Acorn\Console\Commands\KeyGenerateCommand::class, \Roots\Acorn\Console\Commands\OptimizeClearCommand::class, \Roots\Acorn\Console\Commands\OptimizeCommand::class, \Roots\Acorn\Console\Commands\RouteCacheCommand::class,