Skip to content

Instantly share code, notes, and snippets.

@polevaultweb
Last active October 31, 2024 19:10
Show Gist options
  • Save polevaultweb/de44d747b57191a851947d5f5175e922 to your computer and use it in GitHub Desktop.
Save polevaultweb/de44d747b57191a851947d5f5175e922 to your computer and use it in GitHub Desktop.
Patch file to run after PHP Scoper to ensure the Composer Autoload namespace is prefixed
<?php
/**
* Make sure the Composer\Autoload namespace is prefixed.
* Needs to be run after php-scoper.
*
* `php ./patch-scoper-autoloader-namespace.php MY_PREFIX`
*/
if ( empty( $argv[1] ) ) {
return;
}
$prefix = $argv[1];
$scoper_path = './build/vendor/composer';
prefix_namespace_in_autoloader_file( $scoper_path . '/autoload_static.php', $prefix );
prefix_namespace_in_autoloader_file( $scoper_path . '/autoload_real.php', $prefix );
prefix_namespace_in_autoloader_file( $scoper_path . '/ClassLoader.php', $prefix );
function prefix_namespace_in_autoloader_file( $file, $prefix ) {
$path = $file;
$contents = file_get_contents( $path );
$contents = str_replace( 'Composer\Autoload', $prefix . '\Composer\Autoload', $contents );
file_put_contents( $path, $contents );
}
@hirasso
Copy link

hirasso commented Oct 31, 2024

Hi there, this looks like I'll need it! Came here from the php-scoper issue: humbug/php-scoper#355 (comment)

Do you have a public plugin somewhere where I could see everything you do to scope it in one package? This would be much appreciated.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment