-
Notifications
You must be signed in to change notification settings - Fork 73
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
first draft to guess dependencies from composer's autoloader
- Loading branch information
Showing
5 changed files
with
76 additions
and
0 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
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
35 changes: 35 additions & 0 deletions
35
src/ComposerRequireChecker/DependencyGuesser/GuessFromComposerAutoloader.php
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,35 @@ | ||
<?php | ||
|
||
namespace ComposerRequireChecker\DependencyGuesser; | ||
|
||
|
||
use Composer\Autoload\ClassLoader; | ||
|
||
class GuessFromComposerAutoloader implements GuesserInterface | ||
{ | ||
|
||
/** | ||
* @var ClassLoader | ||
*/ | ||
private $composerAutoloader; | ||
|
||
private $configVendorDir; | ||
|
||
public function __construct(string $composerJsonPath) | ||
{ | ||
$composerJson = json_decode(file_get_contents($composerJsonPath), true); | ||
$this->configVendorDir = dirname($composerJsonPath) . '/' . ($composerJson['config']['vendor-dir'] ?? 'vendor'); | ||
$this->composerAutoloader = include $this->configVendorDir . '/autoload.php'; | ||
} | ||
|
||
public function __invoke(string $symbolName): \Generator | ||
{ | ||
$fullFileName = $this->composerAutoloader->findFile(ltrim($symbolName, '\\/ ')); | ||
if ($fullFileName) { | ||
$fileName = ltrim(substr(realpath($fullFileName), strlen($this->configVendorDir)), '\\/'); | ||
$packageName = preg_replace('/^([^\/]+\/[^\/]+).*/', '$1', $fileName); | ||
yield $packageName; | ||
} | ||
} | ||
|
||
} |
33 changes: 33 additions & 0 deletions
33
test/ComposerRequireCheckerTest/DependencyGuesser/GuessFromComposerAutoloaderTest.php
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,33 @@ | ||
<?php | ||
|
||
namespace ComposerRequireCheckerTest\DependencyGuesser; | ||
|
||
|
||
use ComposerRequireChecker\DependencyGuesser\GuessFromComposerAutoloader; | ||
use PhpParser\ParserFactory; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class GuessFromComposerAutoloaderTest extends TestCase | ||
{ | ||
|
||
/** | ||
* @var GuessFromComposerAutoloader | ||
*/ | ||
private $guesser; | ||
|
||
public function setUp() | ||
{ | ||
parent::setUp(); | ||
$dir = dirname(__DIR__, 3); | ||
$this->guesser = new GuessFromComposerAutoloader($dir . '/composer.json'); | ||
} | ||
|
||
public function testClassWillBeFound() | ||
{ | ||
$quessedDependencies = $this->guesser->__invoke(ParserFactory::class); | ||
$guessedDependencies = iterator_to_array($quessedDependencies); | ||
|
||
$this->assertCount(1, $guessedDependencies); | ||
$this->assertContains('nikic/php-parser', $guessedDependencies); | ||
} | ||
} |