Skip to content

Commit

Permalink
πŸ§‘β€πŸ’» Add Manifest and SkipProvider solutions to Ignition (Fixes #204)
Browse files Browse the repository at this point in the history
  • Loading branch information
Log1x committed Mar 18, 2024
1 parent 162e6f6 commit 1a11931
Show file tree
Hide file tree
Showing 4 changed files with 143 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/Roots/Acorn/DefaultProviders.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class DefaultProviders extends DefaultProvidersBase
* @var array
*/
protected $acornProviders = [
\Roots\Acorn\Exceptions\ExceptionServiceProvider::class,
\Roots\Acorn\Assets\AssetsServiceProvider::class,
\Roots\Acorn\Filesystem\FilesystemServiceProvider::class,
\Roots\Acorn\Providers\AcornServiceProvider::class,
Expand Down
41 changes: 41 additions & 0 deletions src/Roots/Acorn/Exceptions/ExceptionServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace Roots\Acorn\Exceptions;

use Illuminate\Support\ServiceProvider;
use Roots\Acorn\Exceptions\Solutions\ManifestSolutionProvider;
use Roots\Acorn\Exceptions\Solutions\SkipProviderSolutionProvider;
use Spatie\Ignition\Contracts\SolutionProviderRepository;

class ExceptionServiceProvider extends ServiceProvider
{
/**
* The solution providers that should be registered.
*/
protected array $solutions = [
ManifestSolutionProvider::class,
SkipProviderSolutionProvider::class,
];

/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
if (! class_exists('Spatie\Ignition\Ignition')) {
return;
}

$this->registerSolutions();
}

/**
* Register the exception solutions.
*/
protected function registerSolutions()
{
$this->app->make(SolutionProviderRepository::class)->registerSolutionProviders($this->solutions);
}
}
71 changes: 71 additions & 0 deletions src/Roots/Acorn/Exceptions/Solutions/ManifestSolutionProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php

namespace Roots\Acorn\Exceptions\Solutions;

use Illuminate\Support\Str;
use Spatie\Ignition\Contracts\BaseSolution;
use Spatie\Ignition\Contracts\HasSolutionsForThrowable;
use Spatie\Ignition\Contracts\Solution;
use Throwable;

class ManifestSolutionProvider implements HasSolutionsForThrowable
{
/**
* The documentation links.
*/
protected array $links = [
'Sage Documentation' => 'https://roots.io/sage/docs/installation/',
'Bud Documentation' => 'https://bud.js.org/learn/getting-started',
];

/**
* Determine if the provider can solve the given throwable.
*/
public function canSolve(Throwable $throwable): bool
{
return Str::startsWith($throwable->getMessage(), 'The asset manifest');
}

/**
* Get the solutions for the given throwable.
*/
public function getSolutions(Throwable $throwable): array
{
return [$this->getSolution()];
}

/**
* Get the solutions for the given throwable.
*/
public function getSolution(): Solution
{
$baseCommand = collect([
'pnpm-lock.yaml' => 'pnpm',
'yarn.lock' => 'yarn',
])->first(fn ($_, $lockfile) => file_exists(base_path($lockfile)), 'npm run');

return app()->environment('development', 'testing', 'local')
? $this->getLocalSolution($baseCommand)
: $this->getProductionSolution($baseCommand);
}

/**
* Get the local solution.
*/
protected function getLocalSolution(string $baseCommand): Solution
{
return BaseSolution::create('Start the development server')
->setSolutionDescription("Run `{$baseCommand} dev` in your terminal and refresh the page.")
->setDocumentationLinks($this->links);
}

/**
* Get the production solution.
*/
protected function getProductionSolution(string $baseCommand): Solution
{
return BaseSolution::create('Build the production assets')
->setSolutionDescription("Run `{$baseCommand} build` in your deployment script.")
->setDocumentationLinks($this->links);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace Roots\Acorn\Exceptions\Solutions;

use Roots\Acorn\Exceptions\SkipProviderException;
use Spatie\Ignition\Contracts\BaseSolution;
use Spatie\Ignition\Contracts\HasSolutionsForThrowable;
use Throwable;

class SkipProviderSolutionProvider implements HasSolutionsForThrowable
{
/**
* Determine if the provider can solve the given throwable.
*/
public function canSolve(Throwable $throwable): bool
{
return $throwable instanceof SkipProviderException;
}

/**
* Get the solutions for the given throwable.
*/
public function getSolutions(Throwable $throwable): array
{
return [
BaseSolution::create('Clear the provider cache')
->setSolutionDescription('Run `wp acorn optimize:clear` in your terminal and refresh the page.'),
];
}
}

0 comments on commit 1a11931

Please sign in to comment.