hyperf/bin/composer-json-fixer

56 lines
1.7 KiB
Plaintext
Raw Normal View History

2019-08-02 10:09:33 +08:00
#!/usr/bin/env php
<?php
use Symfony\Component\Finder\Finder;
foreach ([__DIR__ . '/../../autoload.php', __DIR__ . '/../vendor/autoload.php', __DIR__ . '/vendor/autoload.php'] as $file) {
if (file_exists($file)) {
require $file;
break;
}
}
$files = Finder::create()
->in(__DIR__ . '/../src')
->name('composer.json')
->files();
$require = [];
$autoload = [];
2019-08-06 19:05:22 +08:00
$autoloadFiles = [];
2019-08-02 10:09:33 +08:00
$autoloadDev = [];
$configProviders = [];
foreach ($files as $file) {
$component = basename(dirname($file));
$composerJson = json_decode(file_get_contents($file), true);
foreach ($composerJson['autoload']['files'] ?? [] as $file) {
2019-08-06 19:05:22 +08:00
$autoloadFiles[] = "src/{$component}/". preg_replace('#^./#', '', $file);
}
foreach ($composerJson['autoload']['psr-4'] ?? [] as $ns => $dir) {
2019-08-02 10:09:33 +08:00
$autoload[$ns] = "src/{$component}/" . trim($dir, '/') . '/';
}
foreach ($composerJson['autoload-dev']['psr-4'] ?? [] as $ns => $dir) {
2019-08-02 10:09:33 +08:00
$autoloadDev[$ns] = "src/{$component}/" . trim($dir, '/') . '/';
}
if (isset($composerJson['extra']['hyperf']['config'])) {
$configProviders = array_merge($configProviders, (array)$composerJson['extra']['hyperf']['config']);
}
}
2019-08-02 10:18:04 +08:00
2019-08-02 10:09:33 +08:00
ksort($autoload);
2019-08-06 19:05:22 +08:00
sort($autoloadFiles);
2019-08-02 10:09:33 +08:00
ksort($autoloadDev);
sort($configProviders);
2019-08-02 10:18:04 +08:00
2019-08-02 10:09:33 +08:00
$json = json_decode(file_get_contents(__DIR__ . '/../composer.json'));
2019-08-06 19:05:22 +08:00
$json->autoload->files = $autoloadFiles;
2019-08-02 10:09:33 +08:00
$json->autoload->{'psr-4'} = $autoload;
$json->{'autoload-dev'}->{'psr-4'} = $autoloadDev;
$json->extra->hyperf->config = $configProviders;
2019-08-02 10:18:04 +08:00
2019-08-02 10:09:33 +08:00
file_put_contents(
__DIR__ . '/../composer.json',
json_encode($json, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT)
);