-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
An advanced usage case where composer.json is not available at runtime, so using classloader object to fetch classes and data. Created tests and documentation for new feature. Also updated phpunit version to latest.
- Loading branch information
1 parent
beedf72
commit 33a3a50
Showing
5 changed files
with
131 additions
and
9 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 |
---|---|---|
@@ -1,14 +1,8 @@ | ||
<?xml version="1.0"?> | ||
<phpunit | ||
backupGlobals="false" | ||
colors="true" | ||
convertErrorsToExceptions="true" | ||
convertNoticesToExceptions="true" | ||
convertWarningsToExceptions="true" | ||
> | ||
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" backupGlobals="false" colors="true" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.5/phpunit.xsd" cacheDirectory=".phpunit.cache"> | ||
<testsuites> | ||
<testsuite name="unit"> | ||
<directory suffix="Test.php">./tests/</directory> | ||
</testsuite> | ||
</testsuites> | ||
</phpunit> | ||
</phpunit> |
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,68 @@ | ||
<?php | ||
|
||
namespace SH\AutoHook; | ||
|
||
use \Composer\Autoload\ClassLoader; | ||
|
||
class ComposerClassLoaderParser | ||
{ | ||
/** | ||
* Returns an array of classes in namespaces defined in the composer.json file. | ||
* | ||
* @param ClassLoader $classLoader | ||
* @param array<string> $namespaces Since the classloader does not know which are project vs vendor namespaces, you can pass in an array of namespaces to filter by. | ||
* | ||
* @return array<string> | ||
*/ | ||
public static function getClasses(ClassLoader $classLoader, array $namespaces = []): array | ||
{ | ||
['namespaces' => $allNamespaces, 'classes' => $classes] = self::getNamespacesAndClasses($classLoader); | ||
|
||
$namespaces = $namespaces ?: array_keys($allNamespaces); | ||
$classes = array_keys($classes); | ||
|
||
$matches = []; | ||
foreach ($namespaces as $namespace) { | ||
foreach ($classes as $key => $class) { | ||
if (str_starts_with($class, $namespace)) { | ||
$matches[] = $class; | ||
|
||
unset($classes[$key]); | ||
} | ||
} | ||
} | ||
|
||
return $matches; | ||
} | ||
|
||
/** | ||
* Returns an array with detailed information about namespaces and classes. | ||
* | ||
* Namespaces(k:namespace, v:paths array): map of namespaces and their absolute paths. | ||
* Classes(k:class, v:path): map of classes and their absolute path. | ||
* | ||
* @return array{ | ||
* namespaces: array<string, array<string>>, | ||
* classes: array<string, string> | ||
* } | ||
* | ||
* Example: | ||
* [ | ||
* 'namespaces' => [ | ||
* 'MyApp\\Controllers' => ['/Users/jphndpe/Project/src/Controllers/'], | ||
* 'MyApp\\Models' => ['/Users/jphndpe/Project/src/Models/'], | ||
* ], | ||
* 'classes' => [ | ||
* 'MyApp\\Controllers\\HomeController' => '/Users/jphndpe/Project/src/Controllers/HomeController.php', | ||
* 'MyApp\\Models\\UserModel' => '/Users/jphndpe/Project/src/Models/UserModel.php', | ||
* ] | ||
* ] | ||
*/ | ||
public static function getNamespacesAndClasses(ClassLoader $classLoader): array | ||
{ | ||
return [ | ||
'namespaces' => $classLoader->getPrefixesPsr4(), | ||
'classes' => $classLoader->getClassMap() | ||
]; | ||
} | ||
} |
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,40 @@ | ||
<?php | ||
|
||
namespace SH\AutoHook\Tests; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use \Composer\Autoload\ClassLoader; | ||
use SH\AutoHook\ComposerClassLoaderParser; | ||
|
||
class ComposerClassLoaderParserTest extends TestCase | ||
{ | ||
public function testGetClasses(): void | ||
{ | ||
$classes = ComposerClassLoaderParser::getClasses(self::getClassLoader()); | ||
|
||
self::assertContains(ComposerClassLoaderParser::class, $classes); | ||
|
||
$classes = ComposerClassLoaderParser::getClasses(self::getClassLoader(), ['SH\\AutoHook\\']); | ||
|
||
self::assertContains(ComposerClassLoaderParser::class, $classes); | ||
} | ||
|
||
public function testGetNamespacesAndClasses(): void | ||
{ | ||
['namespaces' => $namespaces, 'classes' => $classes] = ComposerClassLoaderParser::getNamespacesAndClasses(self::getClassLoader()); | ||
|
||
self::assertArrayHasKey('SH\\AutoHook\\', $namespaces); | ||
self::assertArrayHasKey(ComposerClassLoaderParser::class, $classes); | ||
} | ||
|
||
private static function getClassLoader(): ClassLoader | ||
{ | ||
static $classLoader; | ||
|
||
if (null === $classLoader) { | ||
$classLoader = require __DIR__.'/../vendor/autoload.php'; | ||
} | ||
|
||
return $classLoader; | ||
} | ||
} |