generated from spatie/package-skeleton-php
-
-
Notifications
You must be signed in to change notification settings - Fork 6
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
1 parent
1370ac0
commit ab0cef4
Showing
3 changed files
with
105 additions
and
7 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,85 @@ | ||
<?php | ||
|
||
namespace Spatie\ErrorSolutions; | ||
|
||
use Spatie\ErrorSolutions\Contracts\HasSolutionsForThrowable; | ||
|
||
class DiscoverSolutionProviders | ||
{ | ||
protected array $config = [ | ||
'ai' => 'SolutionProviders/OpenAi', | ||
'php' => 'SolutionProviders', | ||
'laravel' => 'SolutionProviders/Laravel' | ||
]; | ||
|
||
/** @return array<HasSolutionsForThrowable */ | ||
public static function for(array $types): array | ||
Check failure on line 16 in src/DiscoverSolutionProviders.php GitHub Actions / phpstan
Check failure on line 16 in src/DiscoverSolutionProviders.php GitHub Actions / phpstan
Check failure on line 16 in src/DiscoverSolutionProviders.php GitHub Actions / phpstan
|
||
{ | ||
if (in_array('php', $types)) { | ||
$types[] = 'ai'; | ||
} | ||
|
||
return (new self($types))->get(); | ||
} | ||
|
||
/** | ||
* @param array<string> $types | ||
*/ | ||
public function __construct(protected array $types) | ||
{ | ||
|
||
} | ||
|
||
/** @return array<HasSolutionsForThrowable */ | ||
public function get(): array | ||
Check failure on line 34 in src/DiscoverSolutionProviders.php GitHub Actions / phpstan
Check failure on line 34 in src/DiscoverSolutionProviders.php GitHub Actions / phpstan
|
||
{ | ||
$providers = []; | ||
|
||
foreach ($this->types as $type) { | ||
$providers = array_merge($providers, $this->getProviderClassesForType($type)); | ||
} | ||
|
||
return $providers; | ||
} | ||
|
||
protected function getProviderClassesForType(string $type): array | ||
{ | ||
$relativePath = $this->config[$type] ?? null; | ||
|
||
if (! $relativePath) { | ||
return []; | ||
} | ||
|
||
$namespace = $this->getNamespaceForPath($relativePath); | ||
|
||
$globPattern = __DIR__ . '/' . $relativePath . '/*.php'; | ||
|
||
$files = glob($globPattern); | ||
|
||
if (!$files) { | ||
return []; | ||
} | ||
|
||
$solutionProviders = array_map(function (string $solutionProviderFilePath) use ($namespace) { | ||
$fileName = pathinfo($solutionProviderFilePath, PATHINFO_FILENAME); | ||
|
||
$fqcn = $namespace . '\\' . $fileName; | ||
|
||
$validClass = in_array(HasSolutionsForThrowable::class, class_implements($fqcn) ?: []); | ||
|
||
return $validClass ? $fqcn : null; | ||
}, $files); | ||
|
||
return array_values(array_filter($solutionProviders)); | ||
} | ||
|
||
protected function getNamespaceForPath(string $relativePath): string | ||
{ | ||
$namespacePath = str_replace('/', '\\', $relativePath); | ||
|
||
$namespace = 'Spatie\\ErrorSolutions\\' . $namespacePath; | ||
|
||
return $namespace; | ||
|
||
} | ||
} |
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,20 @@ | ||
<?php | ||
|
||
use Spatie\ErrorSolutions\Contracts\HasSolutionsForThrowable; | ||
use Spatie\ErrorSolutions\DiscoverSolutionProviders; | ||
|
||
it('can get all solution providers', function() { | ||
$providers = DiscoverSolutionProviders::for(['php', 'laravel']); | ||
|
||
expect($providers) | ||
->not()->toBeEmpty() | ||
->toImplement(HasSolutionsForThrowable::class); | ||
}); | ||
|
||
it('will discover more solution providers for more types', function() { | ||
$phpProviders = DiscoverSolutionProviders::for(['php']); | ||
|
||
$laravelProviders = DiscoverSolutionProviders::for(['php', 'laravel']); | ||
|
||
expect(count($laravelProviders))->toBeGreaterThan(count($phpProviders)); | ||
}); |