Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 17 additions & 5 deletions packages/discovery/src/BootDiscovery.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@

namespace Tempest\Discovery;

use ArgumentCountError;
use AssertionError;
use Closure;
use Pest\Exceptions\InvalidPestCommand;
use Psr\Container\ContainerInterface;
use Tempest\Container\GenericContainer;
use Psr\Container\NotFoundExceptionInterface;
use Tempest\Discovery\Exceptions\DiscoveryClassCouldNotBeResolved;
use Tempest\Reflection\ClassReflector;
use Tempest\Support\Filesystem;
use Throwable;
Expand Down Expand Up @@ -260,18 +262,28 @@ private function discoverPath(string $input, DiscoveryLocation $location, array
/**
* Create a discovery instance from a class name.
* Optionally set the cached discovery items whenever caching is enabled.
*
* @template T of Discovery
* @param class-string<T> $discoveryClass
* @return T
*/
private function resolveDiscovery(string $discoveryClass): Discovery
{
if ($this->container instanceof GenericContainer || $this->container->has($discoveryClass)) {
$discovery = null;

try {
/** @var Discovery $discovery */
$discovery = $this->container->get($discoveryClass);
} else {
/** @var Discovery $discovery */
$discovery = new $discoveryClass();
} catch (NotFoundExceptionInterface) {
// @mago-expect lint:no-empty-catch-clause
}

if ($discovery === null) {
try {
$discovery = new $discoveryClass();
} catch (ArgumentCountError) { // @phpstan-ignore catch.neverThrown
throw DiscoveryClassCouldNotBeResolved::forDiscoveryClass($discoveryClass);
}
}

$discovery->setItems(new DiscoveryItems());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Tempest\Discovery\Exceptions;

use Exception;

final class DiscoveryClassCouldNotBeResolved extends Exception implements DiscoveryException
{
public static function forDiscoveryClass(string $discoveryClass): self
{
return new self("Failed to resolve discovery class [{$discoveryClass}]: it is not bound in the container and cannot be instantiated without constructor arguments.");
}
}
5 changes: 5 additions & 0 deletions packages/discovery/src/Exceptions/DiscoveryException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

namespace Tempest\Discovery\Exceptions;

interface DiscoveryException {}
Loading