-
-
Notifications
You must be signed in to change notification settings - Fork 349
Add command to dump required PHP extensions based on vendor/composer/… #599
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
crazywhalecc
merged 5 commits into
crazywhalecc:main
from
CyberLine:feature/dump-extensions
Mar 7, 2025
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
9b809a0
Add command to dump required PHP extensions based on vendor/composer/…
CyberLine 4d51ece
remove unused use
CyberLine dfadcce
missing translation
CyberLine 0bfc65a
Adjust dump-extensions
crazywhalecc b362c7d
Add docs for dump-extension command
crazywhalecc File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,160 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace SPC\command; | ||
|
|
||
| use SPC\store\FileSystem; | ||
| use Symfony\Component\Console\Attribute\AsCommand; | ||
| use Symfony\Component\Console\Input\InputArgument; | ||
| use Symfony\Component\Console\Input\InputOption; | ||
|
|
||
| #[AsCommand(name: 'dump-extensions', description: 'Determines the required php extensions')] | ||
| class DumpExtensionsCommand extends BaseCommand | ||
| { | ||
| protected bool $no_motd = true; | ||
|
|
||
| public function configure(): void | ||
| { | ||
| // path to project files or specific composer file | ||
| $this->addArgument('path', InputArgument::OPTIONAL, 'Path to project root', '.'); | ||
| $this->addOption('format', 'F', InputOption::VALUE_REQUIRED, 'Parsed output format', 'default'); | ||
| // output zero extension replacement rather than exit as failure | ||
| $this->addOption('no-ext-output', 'N', InputOption::VALUE_REQUIRED, 'When no extensions found, output default combination (comma separated)'); | ||
| // no dev | ||
| $this->addOption('no-dev', null, null, 'Do not include dev dependencies'); | ||
| // no spc filter | ||
| $this->addOption('no-spc-filter', 'S', null, 'Do not use SPC filter to determine the required extensions'); | ||
| } | ||
|
|
||
| public function handle(): int | ||
| { | ||
| $path = FileSystem::convertPath($this->getArgument('path')); | ||
|
|
||
| $path_installed = FileSystem::convertPath(rtrim($path, '/\\') . '/vendor/composer/installed.json'); | ||
| $path_lock = FileSystem::convertPath(rtrim($path, '/\\') . '/composer.lock'); | ||
|
|
||
| $ext_installed = $this->extractFromInstalledJson($path_installed, !$this->getOption('no-dev')); | ||
| if ($ext_installed === null) { | ||
| if ($this->getOption('format') === 'default') { | ||
| $this->output->writeln('<comment>vendor/composer/installed.json load failed, skipped</comment>'); | ||
| } | ||
| $ext_installed = []; | ||
| } | ||
|
|
||
| $ext_lock = $this->extractFromComposerLock($path_lock, !$this->getOption('no-dev')); | ||
| if ($ext_lock === null) { | ||
| $this->output->writeln('<error>composer.lock load failed</error>'); | ||
| return static::FAILURE; | ||
| } | ||
|
|
||
| $extensions = array_unique(array_merge($ext_installed, $ext_lock)); | ||
| sort($extensions); | ||
|
|
||
| if (empty($extensions)) { | ||
| if ($this->getOption('no-ext-output')) { | ||
| $this->outputExtensions(explode(',', $this->getOption('no-ext-output'))); | ||
| return static::SUCCESS; | ||
| } | ||
| $this->output->writeln('<error>No extensions found</error>'); | ||
| return static::FAILURE; | ||
| } | ||
|
|
||
| $this->outputExtensions($extensions); | ||
| return static::SUCCESS; | ||
| } | ||
|
|
||
| private function filterExtensions(array $requirements): array | ||
| { | ||
| return array_map( | ||
| fn ($key) => substr($key, 4), | ||
| array_keys( | ||
| array_filter($requirements, function ($key) { | ||
| return str_starts_with($key, 'ext-'); | ||
| }, ARRAY_FILTER_USE_KEY) | ||
| ) | ||
| ); | ||
| } | ||
|
|
||
| private function loadJson(string $file): array|bool | ||
| { | ||
| if (!file_exists($file)) { | ||
| return false; | ||
| } | ||
|
|
||
| $data = json_decode(file_get_contents($file), true); | ||
| if (!$data) { | ||
| return false; | ||
| } | ||
| return $data; | ||
| } | ||
|
|
||
| private function extractFromInstalledJson(string $file, bool $include_dev = true): ?array | ||
| { | ||
| if (!($data = $this->loadJson($file))) { | ||
| return null; | ||
| } | ||
|
|
||
| $packages = $data['packages'] ?? []; | ||
|
|
||
| if (!$include_dev) { | ||
| $packages = array_filter($packages, fn ($package) => !in_array($package['name'], $data['dev-package-names'] ?? [])); | ||
| } | ||
|
|
||
| return array_merge( | ||
| ...array_map(fn ($x) => isset($x['require']) ? $this->filterExtensions($x['require']) : [], $packages) | ||
| ); | ||
| } | ||
|
|
||
| private function extractFromComposerLock(string $file, bool $include_dev = true): ?array | ||
| { | ||
| if (!($data = $this->loadJson($file))) { | ||
| return null; | ||
| } | ||
|
|
||
| // get packages ext | ||
| $packages = $data['packages'] ?? []; | ||
| $exts = array_merge( | ||
| ...array_map(fn ($package) => $this->filterExtensions($package['require'] ?? []), $packages) | ||
| ); | ||
|
|
||
| // get dev packages ext | ||
| if ($include_dev) { | ||
| $packages = $data['packages-dev'] ?? []; | ||
| $exts = array_merge( | ||
| $exts, | ||
| ...array_map(fn ($package) => $this->filterExtensions($package['require'] ?? []), $packages) | ||
| ); | ||
| } | ||
|
|
||
| // get require ext | ||
| $platform = $data['platform'] ?? []; | ||
| $exts = array_merge($exts, $this->filterExtensions($platform)); | ||
|
|
||
| // get require-dev ext | ||
| if ($include_dev) { | ||
| $platform = $data['platform-dev'] ?? []; | ||
| $exts = array_merge($exts, $this->filterExtensions($platform)); | ||
| } | ||
|
|
||
| return $exts; | ||
| } | ||
|
|
||
| private function outputExtensions(array $extensions): void | ||
| { | ||
| if (!$this->getOption('no-spc-filter')) { | ||
| $extensions = $this->parseExtensionList($extensions); | ||
| } | ||
| switch ($this->getOption('format')) { | ||
| case 'json': | ||
| $this->output->writeln(json_encode($extensions, JSON_PRETTY_PRINT)); | ||
| break; | ||
| case 'text': | ||
| $this->output->writeln(implode(',', $extensions)); | ||
| break; | ||
| default: | ||
| $this->output->writeln('<info>Required PHP extensions' . ($this->getOption('no-dev') ? ' (without dev)' : '') . ':</info>'); | ||
| $this->output->writeln(implode(',', $extensions)); | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.