Last active
March 16, 2023 16:53
-
-
Save jackbentley/3da7d39ee842b851b36efcb30cbad027 to your computer and use it in GitHub Desktop.
Custom task in grumphp-shim
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
{ | |
"scripts": { | |
"post-install-cmd": [ | |
"php -d phar.readonly=Off ./InstallTask.php" | |
] | |
} | |
} |
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
#!/usr/bin/env php | |
<?php | |
$phar = new Phar('vendor/phpro/grumphp-shim/grumphp.phar'); | |
$phar->addFile('MyCustom.php', 'src/Task/MyCustom.php'); | |
$newAutoloaderContent = <<<FILE | |
'GrumPHP\\Task\\MyCustom' => __DIR__ . '/../..' . '/src/Task/MyCustom.php', | |
FILE; | |
$autoloader = $phar->offsetGet('vendor/composer/autoload_static.php'); | |
$file = $autoloader->openFile('rb+'); | |
$temp = fopen('php://temp', 'rwb+'); | |
while (!$file->eof()) { | |
fwrite($temp, $file->fread(1024)); | |
} | |
$file->seek(392); // Somewhere in the $classMap | |
$currentLine = $file->fgets(); | |
if (stripos($currentLine, $newAutoloaderContent) !== false) { | |
echo 'Already added.'; | |
return; | |
} | |
$file->seek(392); // Reset pointer after read | |
fseek($temp, $file->ftell()); | |
fwrite($temp, $newAutoloaderContent); | |
while (!$file->eof()) { | |
fwrite($temp, $file->fread(1024)); | |
} | |
rewind($temp); | |
$file = null; // Close the file to allow overwriting | |
$contents = stream_get_contents($temp); | |
// If you just write to the $file stream it will corrupt the phar | |
$phar->addFromString('vendor/composer/autoload_static.php', $contents); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment