Last active
June 6, 2023 09:29
-
-
Save vudaltsov/c7348a8aa54d0ada82c22134c6156001 to your computer and use it in GitHub Desktop.
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
<?php | |
declare(strict_types=1); | |
namespace ExtendedTypeSystem\Reflection\ClassLocator; | |
use Composer\Autoload\ClassLoader; | |
use ExtendedTypeSystem\Reflection\ClassLocator; | |
use ExtendedTypeSystem\Reflection\Source; | |
/** | |
* @api | |
*/ | |
final class ComposerClassLocator implements ClassLocator | |
{ | |
public function locateClass(string $class): ?Source | |
{ | |
foreach (ClassLoader::getRegisteredLoaders() as $classLoader) { | |
$file = $classLoader->findFile($class); | |
if ($file !== false) { | |
return Source::fromFile($file, 'composer autoloader'); | |
} | |
} | |
return null; | |
} | |
} |
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
<?php | |
declare(strict_types=1); | |
namespace ExtendedTypeSystem\Reflection\ClassLocator; | |
use ExtendedTypeSystem\Reflection\Source; | |
use PHPUnit\Framework\Attributes\CoversClass; | |
use PHPUnit\Framework\TestCase; | |
/** | |
* @internal | |
*/ | |
#[CoversClass(ComposerClassLocator::class)] | |
final class ComposerClassLocatorTest extends TestCase | |
{ | |
public function testItLocatesClass(): void | |
{ | |
$expectedSource = Source::fromFile(__FILE__, 'composer autoloader'); | |
$locator = new ComposerClassLocator(); | |
$locatedSource = $locator->locateClass(self::class); | |
self::assertEquals($expectedSource, $locatedSource); | |
} | |
public function testItReturnsNullForInternalClass(): void | |
{ | |
$locator = new ComposerClassLocator(); | |
$locatedSource = $locator->locateClass(\stdClass::class); | |
self::assertNull($locatedSource); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment