Created
January 19, 2017 08:37
-
-
Save jeremyb/aaf2f6508c0624558a74aecbb427993e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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