Skip to content
Snippets Groups Projects
Handler.php 2.89 KiB
Newer Older
jurgenhaas's avatar
jurgenhaas committed
<?php

jurgenhaas's avatar
jurgenhaas committed
namespace LakeDrops\DorgFlow;
jurgenhaas's avatar
jurgenhaas committed

use Composer\Package\PackageInterface;
use Composer\Composer;
use Composer\IO\IOInterface;
use Composer\Script\Event as ScriptEvent;
jurgenhaas's avatar
jurgenhaas committed
use GitElephant\Repository;
jurgenhaas's avatar
jurgenhaas committed
use Symfony\Component\Filesystem\Filesystem;
jurgenhaas's avatar
jurgenhaas committed

class Handler {

  /**
   * @var \Composer\Composer
   */
  protected $composer;

  /**
   * @var \Composer\IO\IOInterface
   */
  protected $io;

  /**
   * Handler constructor.
   *
   * @param Composer $composer
   * @param IOInterface $io
   */
  public function __construct(Composer $composer, IOInterface $io) {
    $this->composer = $composer;
    $this->io = $io;
  }

  /**
jurgenhaas's avatar
jurgenhaas committed
   * Retrieve a package from the current composer process.
   *
   * @param string $name
   *   Name of the package to get from the current composer installation.
   *
   * @return PackageInterface
   */
  protected function getPackage($name) {
    return $this->composer->getRepositoryManager()->getLocalRepository()->findPackage($name, '*');
  }

  /**
   * Post install/update event to prepare projects for development.
jurgenhaas's avatar
jurgenhaas committed
   *
   * @param ScriptEvent $event
   */
jurgenhaas's avatar
jurgenhaas committed
  public function prepareDevProjects($event) {

    // We only do the fancy stuff for developers
    if (!$event->isDevMode()) {
      return;
    }

    $options = $this->getOptions();
    $installationManager = $this->composer->getInstallationManager();

    foreach ($options['projects'] as $project => $version) {
      $package = $this->getPackage($project);
      if (empty($package)) {
        continue;
      }

      $path = $installationManager->getInstallPath($package);
      $this->prepareWorkingDirectory($path, $project, $version, $options['username']);
    }
  }

  /**
   * Retrieve excludes from optional "extra" configuration.
   *
   * @return array
   */
  protected function getOptions() {
    $extra = $this->composer->getPackage()->getExtra() + ['dorgflow' => []];
    $options = $extra['dorgflow'] + [
      'projects' => [],
      'username' => '',
    ];
    return $options;
  }

  /**
   * @inheritDoc
   */
  protected function prepareWorkingDirectory($path, $project, $version, $username) {
    list(, $projectname) = explode('/', $project);
jurgenhaas's avatar
jurgenhaas committed
    $uri = $username . '@git.drupal.org:project/' . $projectname . '.git';
jurgenhaas's avatar
jurgenhaas committed

    // Git Clone the repository into the working directory
    $repository = Repository::open($path);
jurgenhaas's avatar
jurgenhaas committed
    $repository->init();

    $originExists = FALSE;
    /** @var \GitElephant\Objects\Remote $remote */
    foreach ($repository->getRemotes() as $remote) {
      if ($remote->getName() == 'origin') {
        if ($remote->getFetchURL() != $uri) {
          $remote->setFetchURL($uri);
        }
        $originExists = TRUE;
        break;
      }
    }

    if (!$originExists) {
      $fs = new Filesystem();
      $fs->remove($path);
      $fs->mkdir($path);
      $repository->init();
      $repository->addRemote('origin', $uri);
    }

    $repository->fetch();
jurgenhaas's avatar
jurgenhaas committed
    $repository->checkout($version);
jurgenhaas's avatar
jurgenhaas committed
  }

}