| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentCart\App; |
| 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 |
//phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents |
| 21 |
$composerJson = json_decode(file_get_contents($vendorDir . '/../composer.json'), true); |
| 22 |
$namespace = $composerJson['extra']['wpfluent']['namespace']['current']; |
| 23 |
|
| 24 |
if (!$namespace) { |
| 25 |
throw new InvalidArgumentException("Namespace not set in composer.json file."); |
| 26 |
} |
| 27 |
|
| 28 |
// Folders or packages to ignore |
| 29 |
$ignoreFolders = [ |
| 30 |
'woocommerce', |
| 31 |
'fakerphp', |
| 32 |
'carbonphp', |
| 33 |
'brick' |
| 34 |
]; |
| 35 |
|
| 36 |
$itr = new RecursiveIteratorIterator( |
| 37 |
new RecursiveDirectoryIterator( |
| 38 |
$vendorDir . '/wpfluent/framework/src/', |
| 39 |
RecursiveDirectoryIterator::SKIP_DOTS |
| 40 |
), |
| 41 |
RecursiveIteratorIterator::SELF_FIRST |
| 42 |
); |
| 43 |
|
| 44 |
foreach ($itr as $file) { |
| 45 |
if ($file->isDir()) { |
| 46 |
continue; |
| 47 |
} |
| 48 |
|
| 49 |
$filePath = $file->getPathname(); |
| 50 |
|
| 51 |
// Skip ignored folders/packages |
| 52 |
foreach ($ignoreFolders as $ignore) { |
| 53 |
if (strpos($filePath, DIRECTORY_SEPARATOR . $ignore . DIRECTORY_SEPARATOR) !== false) { |
| 54 |
continue 2; // skip this file |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
//phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents |
| 59 |
$content = file_get_contents($filePath); |
| 60 |
|
| 61 |
// Skip if no namespace match to replace |
| 62 |
if (strpos($content, 'WPFluent\\') === false) { |
| 63 |
continue; |
| 64 |
} |
| 65 |
|
| 66 |
$content = str_replace( |
| 67 |
'WPFluent\\', |
| 68 |
$namespace . '\\Framework\\', |
| 69 |
$content |
| 70 |
); |
| 71 |
|
| 72 |
//phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_file_put_contents |
| 73 |
file_put_contents($filePath, $content); |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
} |
| 78 |
|