forked from tienvx/composer-downloads-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
159 additions
and
3 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
<?php | ||
|
||
namespace LastCall\DownloadsPlugin\Composer\Installer; | ||
|
||
use Composer\Composer; | ||
use Composer\IO\IOInterface; | ||
use Composer\Package\PackageInterface; | ||
use Composer\Repository\InstalledRepositoryInterface; | ||
use Composer\Util\Filesystem; | ||
use LastCall\DownloadsPlugin\Composer\Package\ExtraDownloadInterface; | ||
use LastCall\DownloadsPlugin\Enum\PackageType; | ||
use LastCall\DownloadsPlugin\Installer\ExecutableInstallerInterface; | ||
use React\Promise\PromiseInterface; | ||
use Symfony\Component\Finder\Finder; | ||
|
||
class FileInstaller extends AbstractInstaller | ||
{ | ||
public const TMP_PREFIX = '.composer-extra-tmp-'; | ||
|
||
protected Filesystem $filesystem; | ||
|
||
public function __construct( | ||
IOInterface $io, | ||
Composer $composer, | ||
?Filesystem $filesystem = null, | ||
?ExecutableInstallerInterface $executableInstaller = null, | ||
) { | ||
parent::__construct($io, $composer, $executableInstaller); | ||
$this->filesystem = $filesystem ?: new Filesystem(); | ||
} | ||
|
||
public function supports(string $packageType): bool | ||
{ | ||
return $packageType === PackageType::FILE->value; | ||
} | ||
|
||
public function install(InstalledRepositoryInterface $repo, PackageInterface $package): PromiseInterface | ||
{ | ||
if (!$package instanceof ExtraDownloadInterface) { | ||
return \React\Promise\resolve(null); | ||
} | ||
|
||
$expectedPath = $package->getInstallPath(); | ||
$tmpDir = \dirname($expectedPath).\DIRECTORY_SEPARATOR.uniqid(self::TMP_PREFIX, true); | ||
$promise = $this->composer->getDownloadManager()->install($package, $tmpDir); | ||
|
||
return $promise->then(function () use ($repo, $package, $expectedPath, $tmpDir) { | ||
$finder = new Finder(); | ||
foreach ($finder->files()->in($tmpDir) as $file) { | ||
$this->filesystem->rename($file, $expectedPath); | ||
break; | ||
} | ||
$this->filesystem->remove($tmpDir); | ||
|
||
return parent::install($repo, $package); | ||
}); | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
<?php | ||
|
||
namespace LastCall\DownloadsPlugin\Composer\Plugin; | ||
|
||
use Composer\Composer; | ||
use Composer\DependencyResolver\Operation\InstallOperation; | ||
use Composer\DependencyResolver\Operation\UpdateOperation; | ||
use Composer\EventDispatcher\EventSubscriberInterface; | ||
use Composer\Installer\PackageEvent; | ||
use Composer\Installer\PackageEvents; | ||
use Composer\IO\IOInterface; | ||
use Composer\Plugin\PluginInterface; | ||
use Composer\Script\Event; | ||
use Composer\Script\ScriptEvents; | ||
use LastCall\DownloadsPlugin\Composer\Installer\ArchiveInstaller; | ||
use LastCall\DownloadsPlugin\Composer\Installer\FileInstaller; | ||
use LastCall\DownloadsPlugin\Handler\PackageHandler; | ||
use LastCall\DownloadsPlugin\Handler\PackageHandlerInterface; | ||
|
||
class ExtraDownloadsPlugin implements PluginInterface, EventSubscriberInterface | ||
{ | ||
private const EVENT_PRIORITY = 10; | ||
|
||
public function __construct(private ?PackageHandlerInterface $handler = null) | ||
{ | ||
} | ||
|
||
public static function getSubscribedEvents(): array | ||
{ | ||
return [ | ||
PackageEvents::POST_PACKAGE_INSTALL => ['handlePackageEvent', self::EVENT_PRIORITY], | ||
PackageEvents::POST_PACKAGE_UPDATE => ['handlePackageEvent', self::EVENT_PRIORITY], | ||
ScriptEvents::POST_INSTALL_CMD => ['handleScriptEvent', self::EVENT_PRIORITY], | ||
ScriptEvents::POST_UPDATE_CMD => ['handleScriptEvent', self::EVENT_PRIORITY], | ||
]; | ||
} | ||
|
||
public function handleScriptEvent(Event $event): void | ||
{ | ||
$rootPackage = $event->getComposer()->getPackage(); | ||
$this->getHandler($event->getComposer(), $event->getIO())->handle($rootPackage); | ||
|
||
// Ensure that any other packages are properly reconciled. | ||
$localRepo = $event->getComposer()->getRepositoryManager()->getLocalRepository(); | ||
foreach ($localRepo->getCanonicalPackages() as $package) { | ||
$this->getHandler($event->getComposer(), $event->getIO())->handle($package); | ||
} | ||
} | ||
|
||
public function handlePackageEvent(PackageEvent $event): void | ||
{ | ||
$package = match (\get_class($event->getOperation())) { | ||
InstallOperation::class => $event->getOperation()->getPackage(), | ||
UpdateOperation::class => $event->getOperation()->getTargetPackage(), | ||
default => throw new \Exception(sprintf('Operation %s not supported', $event->getOperation()->getOperationType())) | ||
}; | ||
$this->getHandler($event->getComposer(), $event->getIO())->handle($package); | ||
} | ||
|
||
public function activate(Composer $composer, IOInterface $io): void | ||
{ | ||
$this->addInstallers($composer, $io); | ||
} | ||
|
||
public function deactivate(Composer $composer, IOInterface $io): void | ||
{ | ||
} | ||
|
||
public function uninstall(Composer $composer, IOInterface $io): void | ||
{ | ||
} | ||
|
||
private function addInstallers(Composer $composer, IOInterface $io): void | ||
{ | ||
$installers = [ | ||
new ArchiveInstaller($io, $composer), | ||
new FileInstaller($io, $composer), | ||
]; | ||
$installationManager = $composer->getInstallationManager(); | ||
foreach ($installers as $installer) { | ||
$installationManager->addInstaller($installer); | ||
} | ||
} | ||
|
||
private function getHandler(Composer $composer, IOInterface $io): PackageHandlerInterface | ||
{ | ||
$this->handler ??= new PackageHandler($composer, $io); | ||
|
||
return $this->handler; | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<?php | ||
|
||
namespace LastCall\DownloadsPlugin\Exception; | ||
|
||
class InvalidAttributeException extends BaseException | ||
{ | ||
} |
This file contains 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