Skip to content

Commit

Permalink
Merge pull request #23 from humanmade/module-manifest
Browse files Browse the repository at this point in the history
Generate a manifest of modules to load via wp-config.php
  • Loading branch information
roborourke authored Oct 1, 2019
2 parents e31031f + 7710d76 commit 8dd9627
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 3 deletions.
42 changes: 41 additions & 1 deletion inc/composer/class-plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -16,6 +17,7 @@ class Plugin implements PluginInterface, EventSubscriberInterface {
* @param IOInterface $io
*/
public function activate( Composer $composer, IOInterface $io ) {
$this->composer = $composer;
}

/**
Expand All @@ -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' ],
];
}

Expand All @@ -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' );
Expand Down Expand Up @@ -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 = "<?php\n/**\n * Altis Module Loader.\n *\n * DO NOT EDIT THIS FILE.\n */\n";

foreach ( $packages as $package ) {
$extra = $package->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 );
}
}
6 changes: 4 additions & 2 deletions wp-config.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<?php

/*
/**
* Main config file for loading Altis.
*
* DO NOT EDIT THIS FILE.
Expand All @@ -20,6 +19,9 @@
// all `autoload.files` from all modules.
require_once __DIR__ . '/vendor/autoload.php';

// Load all modules.
require_once __DIR__ . '/vendor/modules.php';

do_action( 'altis.loaded_autoloader' );

if ( ! defined( 'ABSPATH' ) ) {
Expand Down

0 comments on commit 8dd9627

Please sign in to comment.