-
-
Notifications
You must be signed in to change notification settings - Fork 540
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Throw a helpful exception if a referenced blueprint does not exist (#…
- Loading branch information
1 parent
646793c
commit 080d3e7
Showing
2 changed files
with
55 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
<?php | ||
|
||
namespace Statamic\Exceptions; | ||
|
||
use Exception; | ||
use Facade\Ignition\Support\StringComparator; | ||
use Facade\IgnitionContracts\BaseSolution; | ||
use Facade\IgnitionContracts\ProvidesSolution; | ||
use Facade\IgnitionContracts\Solution; | ||
use Statamic\Facades\Blueprint; | ||
use Statamic\Statamic; | ||
|
||
class BlueprintNotFoundException extends Exception implements ProvidesSolution | ||
{ | ||
protected $blueprintHandle; | ||
protected $namespace; | ||
|
||
public function __construct($blueprintHandle, $namespace = null) | ||
{ | ||
parent::__construct("Blueprint [{$blueprintHandle}] not found"); | ||
|
||
$this->blueprintHandle = $blueprintHandle; | ||
$this->namespace = $namespace; | ||
} | ||
|
||
public function getSolution(): Solution | ||
{ | ||
$description = ($suggestedBlueprint = $this->getSuggestedBlueprint()) | ||
? "Did you mean `$suggestedBlueprint`?" | ||
: 'Are you sure the blueprint exists?'; | ||
|
||
return BaseSolution::create("The {$this->blueprintHandle} blueprint was not found.") | ||
->setSolutionDescription($description) | ||
->setDocumentationLinks([ | ||
'Read the blueprints guide' => Statamic::docsUrl('/blueprints'), | ||
]); | ||
} | ||
|
||
protected function getSuggestedBlueprint() | ||
{ | ||
if (! $this->namespace) { | ||
return null; | ||
} | ||
|
||
return StringComparator::findClosestMatch( | ||
Blueprint::in($this->namespace)->map->handle()->values()->all(), | ||
$this->blueprintHandle | ||
); | ||
} | ||
} |
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