Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save retlehs/3dfd033e196c25e376acbeb89fa41dbd to your computer and use it in GitHub Desktop.

Select an option

Save retlehs/3dfd033e196c25e376acbeb89fa41dbd to your computer and use it in GitHub Desktop.
Google for WooCommerce patch to avoid `psr/log` and `monolog/monolog` conflicts with Acorn
--- a/src/Autoloader.php
+++ b/src/Autoloader.php
@@ -39,6 +39,8 @@
return false;
}
+ self::detach_host_namespaces( $autoloader_result );
+
return $autoloader_result;
}
@@ -71,4 +73,44 @@
}
);
}
+
+ /**
+ * Strip namespaces this plugin bundles a conflicting copy of (Monolog v2,
+ * Psr\Log v1) from the bundled ClassLoader so the host application's
+ * versions win autoload. Without this, Composer's class map plus the
+ * prepended autoload registration load the bundled copies, which collide
+ * with the host's newer versions on signature checks (LogRecord, Level
+ * enum, isHandling(int), Stringable type hints, etc.).
+ *
+ * @param mixed $autoloader The result of requiring this plugin's vendor/autoload.php.
+ */
+ protected static function detach_host_namespaces( $autoloader ): void {
+ if ( ! $autoloader instanceof \Composer\Autoload\ClassLoader ) {
+ return;
+ }
+
+ $detach = array( 'Monolog\\', 'Psr\\Log\\' );
+
+ foreach ( $detach as $prefix ) {
+ $autoloader->setPsr4( $prefix, array() );
+ }
+
+ $reflection = new \ReflectionObject( $autoloader );
+ if ( ! $reflection->hasProperty( 'classMap' ) ) {
+ return;
+ }
+
+ $property = $reflection->getProperty( 'classMap' );
+ $property->setAccessible( true );
+ $class_map = $property->getValue( $autoloader );
+ foreach ( array_keys( $class_map ) as $class ) {
+ foreach ( $detach as $prefix ) {
+ if ( strpos( (string) $class, $prefix ) === 0 ) {
+ unset( $class_map[ $class ] );
+ break;
+ }
+ }
+ }
+ $property->setValue( $autoloader, $class_map );
+ }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment