Skip to content

Commit

Permalink
✨ Add Livewire Support (#291)
Browse files Browse the repository at this point in the history
✨ Implement the key:generate command
🧑‍💻 Improve the Laravel router implementation
🎨 Polish the Bootloader class
➕ Add `illuminate/queue` to the project
➕ Add `illuminate/encryption` to the project
  • Loading branch information
broskees authored Jan 5, 2024
1 parent 43d93ee commit e89d969
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 28 deletions.
2 changes: 2 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
77 changes: 49 additions & 28 deletions src/Roots/Acorn/Bootloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand All @@ -48,15 +48,15 @@ class Bootloader
protected $absoluteApplicationPathPrefixes = ['/', '\\'];

/**
* Set the Bootloader instance
* Set the Bootloader instance.
*/
public static function setInstance(?self $bootloader)
{
static::$instance = $bootloader;
}

/**
* Get the Bootloader instance
* Get the Bootloader instance.
*
* @return static
*/
Expand Down Expand Up @@ -146,6 +146,7 @@ protected function bootConsole(ApplicationContract $app)
);

$kernel->terminate($input, $status);

exit($status);
}

Expand Down Expand Up @@ -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
{
Expand Down Expand Up @@ -272,7 +292,7 @@ public function getApplication(): ApplicationContract
}

/**
* Get the application basepath
* Get the application's base path.
*/
protected function basePath(): string
{
Expand Down Expand Up @@ -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);
Expand Down
44 changes: 44 additions & 0 deletions src/Roots/Acorn/Console/Commands/KeyGenerateCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

namespace Roots\Acorn\Console\Commands;

use Illuminate\Foundation\Console\KeyGenerateCommand as FoundationKeyGenerateCommand;
use Illuminate\Support\Facades\File;

class KeyGenerateCommand extends FoundationKeyGenerateCommand
{
/**
* Write a new environment file with the given key.
*
* @param string $key
* @return bool
*/
protected function writeNewEnvironmentFileWith($key)
{
$envFile = file_exists($this->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;
}
}
1 change: 1 addition & 0 deletions src/Roots/Acorn/Console/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit e89d969

Please sign in to comment.