Skip to content

Instantly share code, notes, and snippets.

@jeremyb
Created January 19, 2017 08:37
Show Gist options
  • Save jeremyb/aaf2f6508c0624558a74aecbb427993e to your computer and use it in GitHub Desktop.
Save jeremyb/aaf2f6508c0624558a74aecbb427993e to your computer and use it in GitHub Desktop.
<?php
namespace Infrastructure\Composer;
use Composer\Installer\InstallationManager;
use Composer\Package\CompletePackage;
use Composer\Repository\InstalledFilesystemRepository;
use Composer\Script\Event;
use Composer\Util\Filesystem as ComposerFilesystem;
use Symfony\Component\Filesystem\Filesystem;
abstract class PathRepositoryScript
{
public static function resolvePath(Event $event)
{
$composer = $event->getComposer();
/** @var InstalledFilesystemRepository $local */
$local = $composer->getRepositoryManager()->getLocalRepository();
/** @var InstallationManager $installer */
$installer = $composer->getInstallationManager();
$pathPackages = [];
foreach ($local->getPackages() as $package) {
if ('path' === $package->getDistType()) {
$pathPackages[] = $package;
}
}
$fs = new Filesystem();
$fsUtils = new ComposerFilesystem();
/** @var CompletePackage $package */
foreach ($pathPackages as $package) {
$packagePath = realpath($package->getDistUrl());
if (false === $packagePath || !is_dir($packagePath)) {
throw new \RuntimeException(sprintf(
'Path "%s" is not found',
$packagePath
));
}
$installationPath = $installer->getInstallPath($package);
$fsUtils->removeDirectory($installationPath);
if ($event->isDevMode()) {
$shortestPath = $fsUtils->findShortestPath($installationPath, $packagePath);
$fs->symlink($shortestPath, $installationPath);
$event->getIO()->write(
sprintf(' Symlinked package: %s', $package->getName()),
true
);
} else {
$fs->mirror($packagePath, $installationPath);
$event->getIO()->write(
sprintf(' Mirrored package: %s', $package->getName()),
true
);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment