Skip to content

Commit

Permalink
bug #85 Add support for composer-plugin-api v2 (Ayesh)
Browse files Browse the repository at this point in the history
This PR was merged into the 1.2-dev branch.

Discussion
----------

Add support for composer-plugin-api v2

Composer version 2 has `composer-plugin-api` version 2, and this plugin attemts to support both API versions.

See [What's new in Composer 2](https://php.watch/articles/composer-2) and [UPGRADE-2.0](https://github.com/composer/composer/blob/master/UPGRADE-2.0.md#for-integrators-and-plugin-authors) for more changes in API.

 - Empty methods `\Symfony\Thanks\Thanks::deactivate()` and `\Symfony\Thanks\Thanks::uninstall()` are added to make it compatible both versions.
 - On Composer v2, `\Hirak\Prestissimo\CurlRemoteFilesystem` is not used in favor of Composer's native `\Composer\Util\HttpDownloader` which make multiple Curl connections.

Related: #84 and composer/composer#8726.

`thanks` 🙏

Commits
-------

7502bef Add support for composer-plugin-api v2
  • Loading branch information
nicolas-grekas committed May 30, 2020
2 parents a8a5fbe + 7502bef commit 6e86a8d
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 5 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
],
"require": {
"php": "^5.5.9|^7.0.0",
"composer-plugin-api": "^1.0"
"composer-plugin-api": "^1.0|^2.0"
},
"autoload": {
"psr-4": {
Expand Down
24 changes: 20 additions & 4 deletions src/GitHubClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
use Composer\Json\JsonFile;
use Composer\Plugin\PluginEvents;
use Composer\Plugin\PreFileDownloadEvent;
use Composer\Util\HttpDownloader;
use Composer\Util\RemoteFilesystem;
use Hirak\Prestissimo\CurlRemoteFilesystem;

/**
Expand Down Expand Up @@ -100,7 +102,12 @@ public function __construct(Composer $composer, IOInterface $io)
{
$this->composer = $composer;
$this->io = $io;
$this->rfs = Factory::createRemoteFilesystem($io, $composer->getConfig());

if (class_exists(HttpDownloader::class)) {
$this->rfs = new HttpDownloader($io, $composer->getConfig())
} else {
$this->rfs = Factory::createRemoteFilesystem($io, $composer->getConfig());
}
}

public function getRepositories(array &$failures = null, $withFundingLinks = false)
Expand Down Expand Up @@ -182,19 +189,28 @@ public function call($graphql, array &$failures = [])

if ($eventDispatcher = $this->composer->getEventDispatcher()) {
$preFileDownloadEvent = new PreFileDownloadEvent(PluginEvents::PRE_FILE_DOWNLOAD, $rfs, 'https://api.github.com/graphql');

$eventDispatcher->dispatch($preFileDownloadEvent->getName(), $preFileDownloadEvent);
if (!$preFileDownloadEvent->getRemoteFilesystem() instanceof CurlRemoteFilesystem) {

if ($rfs instanceof RemoteFilesystem && !$preFileDownloadEvent->getRemoteFilesystem() instanceof CurlRemoteFilesystem) {
$rfs = $preFileDownloadEvent->getRemoteFilesystem();
}
}

$result = $rfs->getContents('github.com', 'https://api.github.com/graphql', false, [
$options = [
'http' => [
'method' => 'POST',
'content' => json_encode(['query' => $graphql]),
'header' => ['Content-Type: application/json'],
],
]);
];

if ($rfs instanceof HttpDownloader) {
$result = $rfs->get('https://api.github.com/graphql', $options);
} else {
$result = $rfs->getContents('github.com', 'https://api.github.com/graphql', false, $options);
}

$result = json_decode($result, true);

if (isset($result['errors'][0]['message'])) {
Expand Down
14 changes: 14 additions & 0 deletions src/Thanks.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,4 +115,18 @@ public static function getSubscribedEvents()
ScriptEvents::POST_UPDATE_CMD => 'displayReminder',
];
}

/**
* {@inheritdoc}
*/
public function deactivate(Composer $composer, IOInterface $io)
{
}

/**
* {@inheritdoc}
*/
public function uninstall(Composer $composer, IOInterface $io)
{
}
}

0 comments on commit 6e86a8d

Please sign in to comment.