| 1 |
<?php |
| 2 |
// phpcs:disable |
| 3 |
namespace FluentCommunity; |
| 4 |
|
| 5 |
use Composer\Script\Event; |
| 6 |
use InvalidArgumentException; |
| 7 |
use RecursiveIteratorIterator; |
| 8 |
use RecursiveDirectoryIterator; |
| 9 |
|
| 10 |
class ComposerScript |
| 11 |
{ |
| 12 |
public static function postInstall(Event $event) |
| 13 |
{ |
| 14 |
static::postUpdate($event); |
| 15 |
} |
| 16 |
|
| 17 |
public static function postUpdate(Event $event) |
| 18 |
{ |
| 19 |
$vendorDir = $event->getComposer()->getConfig()->get('vendor-dir'); |
| 20 |
$composerJson = json_decode(file_get_contents($vendorDir . '/../composer.json'), true); |
| 21 |
$namespace = $composerJson['extra']['wpfluent']['namespace']['current']; |
| 22 |
|
| 23 |
if (!$namespace) { |
| 24 |
throw new InvalidArgumentException("Namespace not set in composer.json file."); |
| 25 |
} |
| 26 |
|
| 27 |
$itr = new RecursiveIteratorIterator(new RecursiveDirectoryIterator( |
| 28 |
$vendorDir.'/wpfluent/framework/src/', RecursiveDirectoryIterator::SKIP_DOTS |
| 29 |
), RecursiveIteratorIterator::SELF_FIRST); |
| 30 |
|
| 31 |
foreach ($itr as $file) { |
| 32 |
if ($file->isDir()) { |
| 33 |
continue; |
| 34 |
} |
| 35 |
|
| 36 |
$fileName = $file->getPathname(); |
| 37 |
|
| 38 |
$content = file_get_contents($fileName); |
| 39 |
$content = str_replace( |
| 40 |
['WPFluent\\', 'WPFluentPackage\\'], |
| 41 |
[$namespace . '\\Framework\\', $namespace . '\\'], |
| 42 |
$content |
| 43 |
); |
| 44 |
|
| 45 |
file_put_contents($fileName, $content); |
| 46 |
} |
| 47 |
|
| 48 |
static::updateVendorComposerFiles($vendorDir, $namespace); |
| 49 |
} |
| 50 |
|
| 51 |
protected static function updateVendorComposerFiles($vendorDir, $namespace) |
| 52 |
{ |
| 53 |
$composerInstalledJson = json_decode(file_get_contents( |
| 54 |
$installedJsonFile = $vendorDir . '/composer/installed.json' |
| 55 |
), true); |
| 56 |
|
| 57 |
foreach ($composerInstalledJson['packages'] as &$package) { |
| 58 |
if ($package['name'] == 'wpfluent/framework') { |
| 59 |
$package['autoload']['psr-4'] = [ |
| 60 |
$namespace . "\\Framework\\" => "src/WPFluent" |
| 61 |
]; |
| 62 |
} else { |
| 63 |
$packageDir = $vendorDir . "/{$package['name']}/src/"; |
| 64 |
|
| 65 |
$iterator = new RecursiveIteratorIterator( |
| 66 |
new RecursiveDirectoryIterator( |
| 67 |
$packageDir, RecursiveDirectoryIterator::SKIP_DOTS |
| 68 |
), |
| 69 |
RecursiveIteratorIterator::SELF_FIRST |
| 70 |
); |
| 71 |
|
| 72 |
foreach ($iterator as $item) { |
| 73 |
|
| 74 |
if ($item->isDir()) { |
| 75 |
continue; |
| 76 |
} |
| 77 |
|
| 78 |
$fileName = $item->getPathname(); |
| 79 |
$content = file_get_contents($fileName); |
| 80 |
$content = str_replace( |
| 81 |
['WPFluent\\', 'WPFluentPackage\\'], |
| 82 |
[$namespace . '\\Framework\\', $namespace . '\\'], |
| 83 |
$content |
| 84 |
); |
| 85 |
|
| 86 |
file_put_contents($fileName, $content); |
| 87 |
} |
| 88 |
|
| 89 |
$psr4 = array_keys($package['autoload']['psr-4']); |
| 90 |
|
| 91 |
$replaced = str_replace( |
| 92 |
'WPFluentPackage', $namespace, $psr4[0] |
| 93 |
); |
| 94 |
|
| 95 |
$package['autoload']['psr-4'] = [ |
| 96 |
$replaced => "src/" |
| 97 |
]; |
| 98 |
|
| 99 |
$packageComposerJson = json_decode(file_get_contents( |
| 100 |
$vendorDir .'/' . $package['name'] . '/composer.json' |
| 101 |
), true); |
| 102 |
|
| 103 |
$packageComposerJson['autoload']['psr-4'] = [ |
| 104 |
$replaced => "src/" |
| 105 |
]; |
| 106 |
|
| 107 |
file_put_contents( |
| 108 |
$vendorDir .'/' . $package['name'] . '/composer.json', |
| 109 |
json_encode($packageComposerJson, JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES) |
| 110 |
); |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
file_put_contents( |
| 115 |
$installedJsonFile, |
| 116 |
json_encode($composerInstalledJson, JSON_UNESCAPED_SLASHES|JSON_PRETTY_PRINT) |
| 117 |
); |
| 118 |
|
| 119 |
exec('composer dump-autoload'); |
| 120 |
} |
| 121 |
} |
| 122 |
|