diff --git a/inc/composer/class-plugin.php b/inc/composer/class-plugin.php index cd4042b..b041b91 100644 --- a/inc/composer/class-plugin.php +++ b/inc/composer/class-plugin.php @@ -5,6 +5,7 @@ use Composer\Composer; use Composer\EventDispatcher\EventSubscriberInterface; use Composer\IO\IOInterface; +use Composer\Package\PackageInterface; use Composer\Plugin\PluginInterface; class Plugin implements PluginInterface, EventSubscriberInterface { @@ -16,6 +17,7 @@ class Plugin implements PluginInterface, EventSubscriberInterface { * @param IOInterface $io */ public function activate( Composer $composer, IOInterface $io ) { + $this->composer = $composer; } /** @@ -27,6 +29,7 @@ public static function getSubscribedEvents() : array { return [ 'post-update-cmd' => [ 'install_files' ], 'post-install-cmd' => [ 'install_files' ], + 'post-autoload-dump' => [ 'generate_module_manifest' ], ]; } @@ -35,7 +38,7 @@ public static function getSubscribedEvents() : array { */ public function install_files() { $source = dirname( __DIR__, 2 ); - $dest = dirname( $source, 3 ); + $dest = dirname( $this->composer->getConfig()->get( 'vendor-dir' ) ); copy( $source . '/index.php', $dest . '/index.php' ); copy( $source . '/wp-config.php', $dest . '/wp-config.php' ); @@ -70,4 +73,41 @@ public function install_files() { mkdir( $dest . '/content/themes' ); } } + + /** + * Generate the manifest of module entrypoints to be included automatically. + */ + public function generate_module_manifest() { + $repository = $this->composer->getRepositoryManager()->getLocalRepository(); + $packages = $repository->getCanonicalPackages(); + $vendor_dir = $this->composer->getConfig()->get( 'vendor-dir' ); + $module_loader = "getExtra(); + + // Only process Altis packages. + if ( ! isset( $extra['altis'] ) ) { + continue; + } + + // Determine absolute file path. + $default_base = $vendor_dir . DIRECTORY_SEPARATOR . str_replace( '/', DIRECTORY_SEPARATOR, $package->getName() ); + $base = $package->getTargetDir() ?? $default_base; + $file = $base . DIRECTORY_SEPARATOR . 'load.php'; + + if ( ! file_exists( $file ) ) { + continue; + } + + // Make the path relative to work across environments. + $file = str_replace( $vendor_dir, '', $file ); + + // Add the require line to the file. + $module_loader .= "\n// Load {$package->getName()}.\nrequire_once __DIR__ . '{$file}';"; + } + + // Write the loader file. + file_put_contents( $vendor_dir . DIRECTORY_SEPARATOR . 'modules.php', $package_list ); + } } diff --git a/wp-config.php b/wp-config.php index 6d73299..d26d094 100644 --- a/wp-config.php +++ b/wp-config.php @@ -1,6 +1,5 @@