-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #221 from auraphp/class_scanner_improve
Improve class scanner by using a binary to execute scans
- Loading branch information
Showing
29 changed files
with
1,097 additions
and
271 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 |
---|---|---|
@@ -0,0 +1,110 @@ | ||
#!/usr/bin/env php | ||
<?php | ||
|
||
use Aura\Di\ClassScanner\CachedFileGenerator; | ||
use Aura\Di\ClassScanner\ComposerMapGenerator; | ||
|
||
if (PHP_SAPI !== 'cli') { | ||
echo 'Warning: auradi should be invoked via the CLI version of PHP, not the ' . PHP_SAPI . ' SAPI' . PHP_EOL; | ||
} | ||
|
||
$currentCwd = getcwd(); | ||
|
||
if (!file_exists($currentCwd . '/vendor/autoload.php')) { | ||
throw new RuntimeException('Could not find autoload.php in path ' . $currentCwd . '/vendor/autoload.php'); | ||
} | ||
|
||
require $currentCwd . '/vendor/autoload.php'; | ||
|
||
if (\count($argv) < 2) { | ||
echo 'Use one of the following commands:' . PHP_EOL; | ||
echo PHP_EOL; | ||
echo 'scan Scan for classes and annotations, uses cache file' . PHP_EOL; | ||
echo ' --force Forced full rescan, no cache used' . PHP_EOL; | ||
echo ' -u {filename} Scan only passed updated file' . PHP_EOL; | ||
} | ||
|
||
$args = $argv; | ||
$scriptDir = array_shift($args); | ||
$command = array_shift($args); | ||
|
||
if ($command === 'scan') { | ||
if (!file_exists($currentCwd . '/vendor/composer/installed.json')) { | ||
throw new RuntimeException('Could not include ./vendor/composer/installed.json in path ' . $currentCwd . '/vendor/composer/installed.json'); | ||
} | ||
|
||
echo 'Using ./vendor/composer/installed.json to detect which paths to scan:' . PHP_EOL; | ||
|
||
$installedJsonFile = $currentCwd . '/vendor/composer/installed.json'; | ||
$installedJsonContents = file_get_contents($installedJsonFile); | ||
if (!$installedJsonContents) { | ||
throw new RuntimeException('Could not read json from ./vendor/composer/installed.json'); | ||
} | ||
|
||
$installedJson = json_decode($installedJsonContents, true); | ||
if (!$installedJson) { | ||
throw new RuntimeException('Could not parse json from ./vendor/composer/installed.json'); | ||
} | ||
|
||
$classMapPaths = []; | ||
foreach ($installedJson['packages'] as $package) { | ||
foreach (($package['extra']['aura/di']['classmap-paths'] ?? []) as $classMapPath) { | ||
$fullPath = realpath(dirname($installedJsonFile) . '/' . $package['install-path'] . '/' . $classMapPath); | ||
$classMapPaths[] = $fullPath; | ||
echo '- ' . $fullPath . PHP_EOL; | ||
} | ||
} | ||
|
||
echo 'Found ' . count($classMapPaths) . ' classmap paths' . PHP_EOL; | ||
echo PHP_EOL; | ||
|
||
if (in_array('--force', $args, true)) { | ||
unlink($currentCwd . '/vendor/aura.di.scan.json'); | ||
} | ||
|
||
$generator = new CachedFileGenerator( | ||
new ComposerMapGenerator($classMapPaths, $currentCwd), | ||
$currentCwd . '/vendor/aura.di.scan.json', | ||
); | ||
|
||
echo 'Scanning paths for classes and annotations.' . PHP_EOL; | ||
|
||
$classMap = $generator->generate(); | ||
echo '- ' . count($classMap->getFiles()) . ' files' . PHP_EOL; | ||
echo '- ' . count($classMap->getClasses()) . ' classes' . PHP_EOL; | ||
echo '- ' . count($classMap->getAttributeSpecifications()) . ' attributes' . PHP_EOL; | ||
echo PHP_EOL; | ||
|
||
$updates = []; | ||
$position = 0; | ||
while (array_key_exists($position, $args)) { | ||
if ($args[$position] === '-u') { | ||
$position++; | ||
|
||
if (!array_key_exists($position, $args)) { | ||
throw new \UnexpectedValueException('-u must be followed by a file name'); | ||
} | ||
|
||
$updates[] = $args[$position]; | ||
} | ||
|
||
$position++; | ||
} | ||
|
||
if ($updates) { | ||
echo 'Updating scan for' . PHP_EOL; | ||
foreach ($updates as $update) { | ||
echo '- ' . $update . PHP_EOL; | ||
} | ||
|
||
echo PHP_EOL; | ||
|
||
$generator->update($classMap, $updates); | ||
|
||
echo 'After update we have ' . PHP_EOL; | ||
echo '- ' . count($classMap->getFiles()) . ' files' . PHP_EOL; | ||
echo '- ' . count($classMap->getClasses()) . ' classes' . PHP_EOL; | ||
echo '- ' . count($classMap->getAttributeSpecifications()) . ' attributes' . PHP_EOL; | ||
echo PHP_EOL; | ||
} | ||
} |
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
Oops, something went wrong.