Skip to content

Commit

Permalink
Use composer API instead of checking explicit paths (#3)
Browse files Browse the repository at this point in the history
* Use composer to find installed packages, and determine the directory to write config and service files to

* Add an explicit require_once for ExtenderManager

* Move require for ExtenderManager

* Fix variable name

* Fix CS

* Fix duplicate call to addConfigFile
  • Loading branch information
rbayliss authored and jmolivas committed May 2, 2017
1 parent df23967 commit aa3a2a8
Showing 1 changed file with 28 additions and 10 deletions.
38 changes: 28 additions & 10 deletions src/Extender.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@
use Composer\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Yaml\Yaml;

// Explicitly require ExtenderManager here.
// When this package is uninstalled, ExtenderManager needs to be available any
// time this class is available.
require_once 'ExtenderManager.php';

class Extender implements PluginInterface, EventSubscriberInterface
{
/**
Expand Down Expand Up @@ -53,16 +58,29 @@ public static function getSubscribedEvents()
public function processPackages(PackageEvent $event)
{
$extenderManager = new ExtenderManager();
$directory = realpath(__DIR__.'/../../../../');
$extenderManager->processProjectPackages($directory);

if (is_dir($directory.'/vendor/drupal/console')) {
$directory = $directory.'/vendor/drupal/console';
} else {
$configFile = $directory.'/console.config.yml';
$servicesFile = $directory.'/console.services.yml';
$extenderManager->addConfigFile($configFile);
$extenderManager->addServicesFile($servicesFile);

$composer = $event->getComposer();
$installationManager = $composer->getInstallationManager();
$repositoryManager = $composer->getRepositoryManager();
$localRepository = $repositoryManager->getLocalRepository();

foreach ($localRepository->getPackages() as $package) {
if ($installationManager->isPackageInstalled($localRepository, $package)) {
if ($package->getType() === 'drupal-console-library') {
$extenderManager->addServicesFile($installationManager->getInstallPath($package) . '/console.services.yml');
$extenderManager->addConfigFile($installationManager->getInstallPath($package) . '/console.config.yml');
}
}
}

if ($consolePackage = $localRepository->findPackage('drupal/console', '*')) {
if ($localRepository->hasPackage($consolePackage)) {
$directory = $installationManager->getInstallPath($consolePackage);
}
}
if (empty($directory)) {
// cwd should be the project root. This is the same logic Symfony uses.
$directory = getcwd();
}

$configFile = $directory . '/extend.console.config.yml';
Expand Down

0 comments on commit aa3a2a8

Please sign in to comment.