Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Plugin helpers for v2 #9278

Merged
merged 4 commits into from
Oct 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 17 additions & 4 deletions doc/articles/plugins.md
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,14 @@ class AwsPlugin implements PluginInterface, EventSubscriberInterface
$this->io = $io;
}

public function deactivate(Composer $composer, IOInterface $io)
{
}

public function uninstall(Composer $composer, IOInterface $io)
{
}

public static function getSubscribedEvents()
{
return array(
Expand All @@ -175,9 +183,7 @@ class AwsPlugin implements PluginInterface, EventSubscriberInterface
$protocol = parse_url($event->getProcessedUrl(), PHP_URL_SCHEME);

if ($protocol === 's3') {
$awsClient = new AwsClient($this->io, $this->composer->getConfig());
$s3Downloader = new S3Downloader($this->io, $event->getHttpDownloader()->getOptions(), $awsClient);
$event->setHttpdownloader($s3Downloader);
// ...
}
}
}
Expand Down Expand Up @@ -263,7 +269,7 @@ Now the `custom-plugin-command` is available alongside Composer commands.

## Running plugins manually

Plugins for an event can be run manually by the `run-script` command. This works the same way as
Plugins for an event can be run manually by the `run-script` command. This works the same way as
[running scripts manually](scripts.md#running-scripts-manually).

## Using Plugins
Expand All @@ -278,6 +284,12 @@ local project plugins are loaded.
> installed plugins. This may be particularly helpful if any of the plugins
> causes errors and you wish to update or uninstall it.

## Plugin Helpers

As of Composer 2, due to the fact that DownloaderInterface can sometimes return Promises
and have been split up in more steps than they used to, we provide a [SyncHelper][11]
to make downloading and installing packages easier.

[1]: ../04-schema.md#type
[2]: ../04-schema.md#extra
[3]: https://github.com/composer/composer/blob/master/src/Composer/Plugin/PluginInterface.php
Expand All @@ -288,3 +300,4 @@ local project plugins are loaded.
[8]: https://github.com/composer/composer/blob/master/src/Composer/Plugin/Capable.php
[9]: https://github.com/composer/composer/blob/master/src/Composer/Plugin/Capability/CommandProvider.php
[10]: https://symfony.com/doc/current/components/console.html
[11]: https://github.com/composer/composer/blob/master/src/Composer/Util/SyncHelper.php
2 changes: 2 additions & 0 deletions src/Composer/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -591,6 +591,8 @@ public static function create(IOInterface $io, $config = null, $disablePlugins =
}

/**
* If you are calling this in a plugin, you probably should instead use $composer->getLoop()->getHttpDownloader()
*
* @param IOInterface $io IO instance
* @param Config $config Config instance
* @param array $options Array of options passed directly to HttpDownloader constructor
Expand Down
26 changes: 22 additions & 4 deletions src/Composer/Util/Loop.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,40 @@
*/
class Loop
{
/** @var HttpDownloader */
private $httpDownloader;
/** @var ProcessExecutor|null */
private $processExecutor;
/** @var Promise[]|null */
private $currentPromises;

public function __construct(HttpDownloader $httpDownloader = null, ProcessExecutor $processExecutor = null)
public function __construct(HttpDownloader $httpDownloader, ProcessExecutor $processExecutor = null)
{
$this->httpDownloader = $httpDownloader;
if ($this->httpDownloader) {
$this->httpDownloader->enableAsync();
}
$this->httpDownloader->enableAsync();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know this is not an issue with this PR, but it seems odd that we mutate the client here.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah it's not ideal, but it lets people create and use a sync client without shooting themselves in the foot.


$this->processExecutor = $processExecutor;
if ($this->processExecutor) {
$this->processExecutor->enableAsync();
}
}

/**
* @return HttpDownloader
*/
public function getHttpDownloader()
{
return $this->httpDownloader;
}

/**
* @return ProcessExecutor|null
*/
public function getProcessExecutor()
{
return $this->processExecutor;
}

public function wait(array $promises, ProgressBar $progress = null)
{
/** @var \Exception|null */
Expand Down
56 changes: 56 additions & 0 deletions src/Composer/Util/SyncHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

namespace Composer\Util;

use Composer\Downloader\DownloaderInterface;
use Composer\Package\PackageInterface;
use React\Promise\PromiseInterface;

class SyncHelper
{
/**
* Helps you download + install a single package in a synchronous way
*
* This executes all the required steps and waits for promises to complete
*
* @param Loop $loop Loop instance which you can get from $composer->getLoop()
* @param DownloaderInterface $downloader Downloader instance you can get from $composer->getDownloadManager()->getDownloader('zip') for example
* @param string $path the installation path for the package
* @param PackageInterface $package the package to install
* @param PackageInterface|null $prevPackage the previous package if this is an update and not an initial installation
*/
public static function downloadAndInstallPackageSync(Loop $loop, DownloaderInterface $downloader, $path, PackageInterface $package, PackageInterface $prevPackage = null)
{
$type = $prevPackage ? 'update' : 'install';

try {
self::await($loop, $downloader->download($package, $path, $prevPackage));

self::await($loop, $downloader->prepare($type, $package, $path, $prevPackage));

if ($type === 'update') {
self::await($loop, $downloader->update($package, $path, $prevPackage));
} else {
self::await($loop, $downloader->install($package, $path, $prevPackage));
}
} catch (\Exception $e) {
self::await($loop, $downloader->cleanup($type, $package, $path, $prevPackage));
throw $e;
}

self::await($loop, $downloader->cleanup($type, $package, $path, $prevPackage));
}

/**
* Waits for a promise to resolve
*
* @param Loop $loop Loop instance which you can get from $composer->getLoop()
* @param PromiseInterface|null $promise
*/
public static function await(Loop $loop, PromiseInterface $promise = null)
{
if ($promise) {
$loop->wait(array($promise));
}
}
}