Skip to content
This repository has been archived by the owner on Nov 24, 2020. It is now read-only.

Update to suport plugin usage in vendor package #1

Merged
merged 1 commit into from
Nov 29, 2019
Merged
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);
}
}
}