Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update to suport plugin usage in vendor package #9

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 50 additions & 7 deletions src/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
use Composer\Plugin\PluginEvents;
use Composer\Plugin\PluginInterface;
use ComposerIncludeFiles\Composer\AutoloadGenerator;
use Composer\Package\CompletePackage;
use Composer\Script\Event;
use Composer\Util\Filesystem;

class Plugin implements PluginInterface, EventSubscriberInterface
{
Expand Down Expand Up @@ -38,19 +41,59 @@ public function activate(Composer $composer, IOInterface $io)
*/
public static function getSubscribedEvents()
{
return array(
return array(
'post-autoload-dump' => 'dumpFiles',
);
}

public function dumpFiles()
public function dumpFiles(Event $event)
{
$extraConfig = $this->composer->getPackage()->getExtra();
// var to hold the include files
$extraIncludeFiles = [];

if (!array_key_exists('include_files', $extraConfig) || !is_array($extraConfig['include_files'])) {
return;
}
// setup filesystem object
$filesystem = new Filesystem();

$this->generator->dumpFiles($this->composer, $extraConfig['include_files']);
// set up installation manage
$installationManager = $event->getComposer()->getInstallationManager();

// get all packages except the root package
$packages = $this->composer->getRepositoryManager()->getLocalRepository()->getCanonicalPackages();

// process packages
foreach ($packages as $package) {

// only include CompletePackages
if ($package instanceof CompletePackage) {

// get this package extra config values
$packageConfig = $package->getExtra();

// if has include files
if (isset($packageConfig['include_files'])) {

// get this package base dir
$packageBaseDir = $filesystem->normalizePath($installationManager->getInstallPath($package));

// process each include file for the package
foreach ($packageConfig['include_files'] as $file) {

// update the path of the package file to be realitive of the vendor dir
$extraIncludeFiles[] = $filesystem->normalizePath($packageBaseDir . '/' . $file);
}
}
}
}

// check if the root package has include files
$package = $this->composer->getPackage()->getExtra();
if (isset($package['include_files'])) {
$extraIncludeFiles = array_merge($extraIncludeFiles, $package['include_files']);
}

// if we have files, then process them
if (!empty($extraIncludeFiles)) {
$this->generator->dumpFiles($this->composer, $extraIncludeFiles);
}
}
}