Skip to content

Instantly share code, notes, and snippets.

@jackbentley
Last active March 16, 2023 16:53
Show Gist options
  • Save jackbentley/3da7d39ee842b851b36efcb30cbad027 to your computer and use it in GitHub Desktop.
Save jackbentley/3da7d39ee842b851b36efcb30cbad027 to your computer and use it in GitHub Desktop.
Custom task in grumphp-shim
{
"scripts": {
"post-install-cmd": [
"php -d phar.readonly=Off ./InstallTask.php"
]
}
}
#!/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