Skip to content

Commit

Permalink
add DiscoverSolutionProviders
Browse files Browse the repository at this point in the history
  • Loading branch information
freekmurze committed Jun 12, 2024
1 parent 1370ac0 commit ab0cef4
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 7 deletions.
7 changes: 0 additions & 7 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,6 @@
<directory>tests</directory>
</testsuite>
</testsuites>
<coverage>
<report>
<html outputDirectory="build/coverage"/>
<text outputFile="build/coverage.txt"/>
<clover outputFile="build/logs/clover.xml"/>
</report>
</coverage>
<logging>
<junit outputFile="build/report.junit.xml"/>
</logging>
Expand Down
85 changes: 85 additions & 0 deletions src/DiscoverSolutionProviders.php
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 = [

Check failure on line 9 in src/DiscoverSolutionProviders.php

View workflow job for this annotation

GitHub Actions / phpstan

Property Spatie\ErrorSolutions\DiscoverSolutionProviders::$config type has no value type specified in iterable type array.
'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

View workflow job for this annotation

GitHub Actions / phpstan

Method Spatie\ErrorSolutions\DiscoverSolutionProviders::for() has parameter $types with no value type specified in iterable type array.

Check failure on line 16 in src/DiscoverSolutionProviders.php

View workflow job for this annotation

GitHub Actions / phpstan

Method Spatie\ErrorSolutions\DiscoverSolutionProviders::for() return type has no value type specified in iterable type array.

Check failure on line 16 in src/DiscoverSolutionProviders.php

View workflow job for this annotation

GitHub Actions / phpstan

PHPDoc tag @return has invalid value (array<HasSolutionsForThrowable): Unexpected token "*/", expected '>' at offset 43
{
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

View workflow job for this annotation

GitHub Actions / phpstan

Method Spatie\ErrorSolutions\DiscoverSolutionProviders::get() return type has no value type specified in iterable type array.

Check failure on line 34 in src/DiscoverSolutionProviders.php

View workflow job for this annotation

GitHub Actions / phpstan

PHPDoc tag @return has invalid value (array<HasSolutionsForThrowable): Unexpected token "*/", expected '>' at offset 43
{
$providers = [];

foreach ($this->types as $type) {
$providers = array_merge($providers, $this->getProviderClassesForType($type));
}

return $providers;
}

protected function getProviderClassesForType(string $type): array

Check failure on line 45 in src/DiscoverSolutionProviders.php

View workflow job for this annotation

GitHub Actions / phpstan

Method Spatie\ErrorSolutions\DiscoverSolutionProviders::getProviderClassesForType() return type has no value type specified in iterable 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;

}
}
20 changes: 20 additions & 0 deletions tests/DiscoverSolutionProvidersTest.php
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));
});

0 comments on commit ab0cef4

Please sign in to comment.