-
-
Notifications
You must be signed in to change notification settings - Fork 93
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
143 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
71
src/Roots/Acorn/Exceptions/Solutions/ManifestSolutionProvider.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
src/Roots/Acorn/Exceptions/Solutions/SkipProviderSolutionProvider.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.'), | ||
]; | ||
} | ||
} |