[dev] Monorepo: PSR-4 autoloader for local dev-environment (#52331)

In this PR we introduce dev-only SPR-4 autoloader, so newly introduced PHP classes are available without dumping autoload (specific the core autoloader).
This commit is contained in:
Vladimir Reznichenko 2024-11-11 09:02:25 +01:00 committed by GitHub
parent 6005bfc2d1
commit 2d89b8b81c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 68 additions and 2 deletions

View File

@ -0,0 +1,35 @@
<?php declare( strict_types=1 );
( function() {
// Ensure we are hooking up after primary autoloading being available.
if ( class_exists( \Automattic\WooCommerce\Autoloader::class, false ) ) {
// Locate the reference point first - WooCommerce autoloader in our case.
$reference_point = ( new \ReflectionClass( \Automattic\WooCommerce\Autoloader::class ) )->getFileName();
$plugin_path = dirname( $reference_point, 2 );
// Load composer manifest for class mapping (we'll pick PRS4 only, as it's where new classes are introduced).
static $namespaces = [];
$declarations = json_decode( file_get_contents( $plugin_path . DIRECTORY_SEPARATOR . 'composer.json' ), true );
foreach ( $declarations['autoload']['psr-4'] ?? [] as $namespace => $path ) {
$namespaces[$namespace] = $plugin_path . DIRECTORY_SEPARATOR . $path;
}
foreach ( $declarations['autoload-dev']['psr-4'] ?? [] as $namespace => $path ) {
$namespaces[$namespace] = $plugin_path . DIRECTORY_SEPARATOR . $path;
}
// Follow the provided mapping and require the matching file if found it.
spl_autoload_register( function ( string $class ) use( $namespaces ): bool {
foreach ( $namespaces as $namespace => $path ) {
if ( strpos( $class, $namespace ) === 0 ) {
$file = $path . str_replace( "\\", DIRECTORY_SEPARATOR, str_replace( $namespace, '', $class ) . '.php' );
if ( file_exists( $file ) ) {
require_once $file;
return true;
}
}
}
return false;
} );
}
} )();

View File

@ -69,6 +69,7 @@ class Plugin implements PluginInterface, EventSubscriberInterface {
return array( return array(
// Make sure this event will run after the Jetpack Autoloader plugin. // Make sure this event will run after the Jetpack Autoloader plugin.
ScriptEvents::POST_AUTOLOAD_DUMP => array( 'onPostAutoloadDump', -100000 ), ScriptEvents::POST_AUTOLOAD_DUMP => array( 'onPostAutoloadDump', -100000 ),
ScriptEvents::PRE_AUTOLOAD_DUMP => array( 'onPreAutoloadDump', -100000 ),
); );
} }
@ -85,4 +86,22 @@ class Plugin implements PluginInterface, EventSubscriberInterface {
$manifest_editor = new JetpackManifestEditor( $this->composer, $this->io ); $manifest_editor = new JetpackManifestEditor( $this->composer, $this->io );
$manifest_editor->update_autoload_manifests( $root_package ); $manifest_editor->update_autoload_manifests( $root_package );
} }
/**
* An event handler that runs before the autoload dump event.
*
* @param Event $event The event to handle.
*/
public function onPreAutoloadDump( Event $event ) {
$root_package = $this->composer->getPackage();
// Inject out custom autoloader for picking up new classes without re-running autoload dump.
$dev_autoload = $root_package->getDevAutoload();
$dev_autoload['files'] = array_merge(
$dev_autoload['files'] ?? array(),
array( 'vendor/woocommerce/monorepo-plugin/autoload-dev.php' )
);
$root_package->setDevAutoload( $dev_autoload );
}
} }

View File

@ -0,0 +1,4 @@
Significance: patch
Type: dev
Monorepo: tweak monorepo composer packages installation (no symlinking)

View File

@ -14,7 +14,10 @@
}, },
{ {
"type": "path", "type": "path",
"url": "../../packages/php/monorepo-plugin" "url": "../../packages/php/monorepo-plugin",
"options": {
"symlink": false
}
}, },
{ {
"type": "path", "type": "path",

View File

@ -5007,6 +5007,7 @@
} }
}, },
"transport-options": { "transport-options": {
"symlink": false,
"relative": true "relative": true
} }
}, },

View File

@ -38,7 +38,7 @@
"env:test": "pnpm env:dev", "env:test": "pnpm env:dev",
"env:perf": "pnpm env:dev && pnpm env:performance-init", "env:perf": "pnpm env:dev && pnpm env:performance-init",
"preinstall": "npx only-allow pnpm", "preinstall": "npx only-allow pnpm",
"postinstall": "XDEBUG_MODE=off composer install --quiet", "postinstall": "rimraf -g vendor/woocommerce && XDEBUG_MODE=off composer install --quiet",
"lint": "pnpm --if-present '/^lint:lang:.*$/'", "lint": "pnpm --if-present '/^lint:lang:.*$/'",
"lint:changes:branch": "pnpm '/^lint:changes:branch:.*$/'", "lint:changes:branch": "pnpm '/^lint:changes:branch:.*$/'",
"lint:fix": "pnpm --if-present '/^lint:fix:lang:.*$/'", "lint:fix": "pnpm --if-present '/^lint:fix:lang:.*$/'",
@ -699,6 +699,7 @@
"mocha": "7.2.0", "mocha": "7.2.0",
"nodemon": "^3.0.2", "nodemon": "^3.0.2",
"prettier": "npm:wp-prettier@^2.8.5", "prettier": "npm:wp-prettier@^2.8.5",
"rimraf": "5.0.5",
"stylelint": "^14.16.1", "stylelint": "^14.16.1",
"typescript": "5.3.x", "typescript": "5.3.x",
"uuid": "^9.0.1", "uuid": "^9.0.1",

View File

@ -3464,6 +3464,9 @@ importers:
prettier: prettier:
specifier: npm:wp-prettier@^2.8.5 specifier: npm:wp-prettier@^2.8.5
version: wp-prettier@2.8.5 version: wp-prettier@2.8.5
rimraf:
specifier: 5.0.5
version: 5.0.5
stylelint: stylelint:
specifier: ^14.16.1 specifier: ^14.16.1
version: 14.16.1 version: 14.16.1