| 1 |
<?php |
| 2 |
|
| 3 |
namespace Http\Discovery\Composer; |
| 4 |
|
| 5 |
use Composer\Composer; |
| 6 |
use Composer\DependencyResolver\Pool; |
| 7 |
use Composer\EventDispatcher\EventSubscriberInterface; |
| 8 |
use Composer\Factory; |
| 9 |
use Composer\Installer; |
| 10 |
use Composer\IO\IOInterface; |
| 11 |
use Composer\Json\JsonFile; |
| 12 |
use Composer\Json\JsonManipulator; |
| 13 |
use Composer\Package\Locker; |
| 14 |
use Composer\Package\Version\VersionParser; |
| 15 |
use Composer\Package\Version\VersionSelector; |
| 16 |
use Composer\Plugin\PluginInterface; |
| 17 |
use Composer\Repository\InstalledRepositoryInterface; |
| 18 |
use Composer\Repository\RepositorySet; |
| 19 |
use Composer\Script\Event; |
| 20 |
use Composer\Script\ScriptEvents; |
| 21 |
use Composer\Util\Filesystem; |
| 22 |
use Http\Discovery\ClassDiscovery; |
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
class Plugin implements PluginInterface, EventSubscriberInterface |
| 40 |
{ |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
private const PROVIDE_RULES = [ |
| 47 |
'php-http/async-client-implementation' => [ |
| 48 |
'symfony/http-client:>=6.3' => ['guzzlehttp/promises', 'psr/http-factory-implementation', 'php-http/httplug'], |
| 49 |
'symfony/http-client' => ['guzzlehttp/promises', 'php-http/message-factory', 'psr/http-factory-implementation', 'php-http/httplug'], |
| 50 |
'php-http/guzzle7-adapter' => [], |
| 51 |
'php-http/guzzle6-adapter' => [], |
| 52 |
'php-http/curl-client' => [], |
| 53 |
'php-http/react-adapter' => [], |
| 54 |
], |
| 55 |
'php-http/client-implementation' => [ |
| 56 |
'symfony/http-client:>=6.3' => ['psr/http-factory-implementation', 'php-http/httplug'], |
| 57 |
'symfony/http-client' => ['php-http/message-factory', 'psr/http-factory-implementation', 'php-http/httplug'], |
| 58 |
'php-http/guzzle7-adapter' => [], |
| 59 |
'php-http/guzzle6-adapter' => [], |
| 60 |
'php-http/cakephp-adapter' => [], |
| 61 |
'php-http/curl-client' => [], |
| 62 |
'php-http/react-adapter' => [], |
| 63 |
'php-http/buzz-adapter' => [], |
| 64 |
'php-http/artax-adapter' => [], |
| 65 |
'kriswallsmith/buzz:^1' => [], |
| 66 |
], |
| 67 |
'psr/http-client-implementation' => [ |
| 68 |
'symfony/http-client' => ['psr/http-factory-implementation', 'psr/http-client'], |
| 69 |
'guzzlehttp/guzzle' => [], |
| 70 |
'kriswallsmith/buzz:^1' => [], |
| 71 |
], |
| 72 |
'psr/http-message-implementation' => [ |
| 73 |
'php-http/discovery' => ['psr/http-factory-implementation'], |
| 74 |
], |
| 75 |
'psr/http-factory-implementation' => [ |
| 76 |
'nyholm/psr7' => [], |
| 77 |
'guzzlehttp/psr7:>=2' => [], |
| 78 |
'slim/psr7' => [], |
| 79 |
'laminas/laminas-diactoros' => [], |
| 80 |
'phalcon/cphalcon:^4' => [], |
| 81 |
'http-interop/http-factory-guzzle' => [], |
| 82 |
'http-interop/http-factory-diactoros' => [], |
| 83 |
'http-interop/http-factory-slim' => [], |
| 84 |
'httpsoft/http-message' => [], |
| 85 |
], |
| 86 |
]; |
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
private const STICKYNESS_RULES = [ |
| 93 |
'symfony/http-client' => 'symfony/framework-bundle', |
| 94 |
'php-http/guzzle7-adapter' => 'guzzlehttp/guzzle:^7', |
| 95 |
'php-http/guzzle6-adapter' => 'guzzlehttp/guzzle:^6', |
| 96 |
'php-http/guzzle5-adapter' => 'guzzlehttp/guzzle:^5', |
| 97 |
'php-http/cakephp-adapter' => 'cakephp/cakephp', |
| 98 |
'php-http/react-adapter' => 'react/event-loop', |
| 99 |
'php-http/buzz-adapter' => 'kriswallsmith/buzz:^0.15.1', |
| 100 |
'php-http/artax-adapter' => 'amphp/artax:^3', |
| 101 |
'http-interop/http-factory-guzzle' => 'guzzlehttp/psr7:^1', |
| 102 |
'http-interop/http-factory-slim' => 'slim/slim:^3', |
| 103 |
]; |
| 104 |
|
| 105 |
private const INTERFACE_MAP = [ |
| 106 |
'php-http/async-client-implementation' => [ |
| 107 |
'Http\Client\HttpAsyncClient', |
| 108 |
], |
| 109 |
'php-http/client-implementation' => [ |
| 110 |
'Http\Client\HttpClient', |
| 111 |
], |
| 112 |
'psr/http-client-implementation' => [ |
| 113 |
'Psr\Http\Client\ClientInterface', |
| 114 |
], |
| 115 |
'psr/http-factory-implementation' => [ |
| 116 |
'Psr\Http\Message\RequestFactoryInterface', |
| 117 |
'Psr\Http\Message\ResponseFactoryInterface', |
| 118 |
'Psr\Http\Message\ServerRequestFactoryInterface', |
| 119 |
'Psr\Http\Message\StreamFactoryInterface', |
| 120 |
'Psr\Http\Message\UploadedFileFactoryInterface', |
| 121 |
'Psr\Http\Message\UriFactoryInterface', |
| 122 |
], |
| 123 |
]; |
| 124 |
|
| 125 |
public static function getSubscribedEvents(): array |
| 126 |
{ |
| 127 |
return [ |
| 128 |
ScriptEvents::PRE_AUTOLOAD_DUMP => 'preAutoloadDump', |
| 129 |
ScriptEvents::POST_UPDATE_CMD => 'postUpdate', |
| 130 |
]; |
| 131 |
} |
| 132 |
|
| 133 |
public function activate(Composer $composer, IOInterface $io): void |
| 134 |
{ |
| 135 |
} |
| 136 |
|
| 137 |
public function deactivate(Composer $composer, IOInterface $io) |
| 138 |
{ |
| 139 |
} |
| 140 |
|
| 141 |
public function uninstall(Composer $composer, IOInterface $io) |
| 142 |
{ |
| 143 |
} |
| 144 |
|
| 145 |
public function postUpdate(Event $event) |
| 146 |
{ |
| 147 |
$composer = $event->getComposer(); |
| 148 |
$repo = $composer->getRepositoryManager()->getLocalRepository(); |
| 149 |
$requires = [ |
| 150 |
$composer->getPackage()->getRequires(), |
| 151 |
$composer->getPackage()->getDevRequires(), |
| 152 |
]; |
| 153 |
$pinnedAbstractions = []; |
| 154 |
$pinned = $composer->getPackage()->getExtra()['discovery'] ?? []; |
| 155 |
foreach (self::INTERFACE_MAP as $abstraction => $interfaces) { |
| 156 |
foreach (isset($pinned[$abstraction]) ? [] : $interfaces as $interface) { |
| 157 |
if (!isset($pinned[$interface])) { |
| 158 |
continue 2; |
| 159 |
} |
| 160 |
} |
| 161 |
$pinnedAbstractions[$abstraction] = true; |
| 162 |
} |
| 163 |
|
| 164 |
$missingRequires = $this->getMissingRequires($repo, $requires, 'project' === $composer->getPackage()->getType(), $pinnedAbstractions); |
| 165 |
$missingRequires = [ |
| 166 |
'require' => array_fill_keys(array_merge([], ...array_values($missingRequires[0])), '*'), |
| 167 |
'require-dev' => array_fill_keys(array_merge([], ...array_values($missingRequires[1])), '*'), |
| 168 |
'remove' => array_fill_keys(array_merge([], ...array_values($missingRequires[2])), '*'), |
| 169 |
]; |
| 170 |
|
| 171 |
if (!$missingRequires = array_filter($missingRequires)) { |
| 172 |
return; |
| 173 |
} |
| 174 |
|
| 175 |
$composerJsonContents = file_get_contents(Factory::getComposerFile()); |
| 176 |
$this->updateComposerJson($missingRequires, $composer->getConfig()->get('sort-packages')); |
| 177 |
|
| 178 |
$installer = null; |
| 179 |
|
| 180 |
foreach (debug_backtrace(\DEBUG_BACKTRACE_PROVIDE_OBJECT) as $trace) { |
| 181 |
if (isset($trace['object']) && $trace['object'] instanceof Installer) { |
| 182 |
$installer = $trace['object']; |
| 183 |
break; |
| 184 |
} |
| 185 |
} |
| 186 |
|
| 187 |
if (!$installer) { |
| 188 |
return; |
| 189 |
} |
| 190 |
|
| 191 |
$event->stopPropagation(); |
| 192 |
|
| 193 |
$dispatcher = $composer->getEventDispatcher(); |
| 194 |
$disableScripts = !method_exists($dispatcher, 'setRunScripts') || !((array) $dispatcher)["\0*\0runScripts"]; |
| 195 |
$composer = Factory::create($event->getIO(), null, false, $disableScripts); |
| 196 |
|
| 197 |
|
| 198 |
$installer = clone $installer; |
| 199 |
if (method_exists($installer, 'setAudit')) { |
| 200 |
$trace['object']->setAudit(false); |
| 201 |
} |
| 202 |
|
| 203 |
$installer->__construct( |
| 204 |
$event->getIO(), |
| 205 |
$composer->getConfig(), |
| 206 |
$composer->getPackage(), |
| 207 |
$composer->getDownloadManager(), |
| 208 |
$composer->getRepositoryManager(), |
| 209 |
$composer->getLocker(), |
| 210 |
$composer->getInstallationManager(), |
| 211 |
$composer->getEventDispatcher(), |
| 212 |
$composer->getAutoloadGenerator() |
| 213 |
); |
| 214 |
if (method_exists($installer, 'setPlatformRequirementFilter')) { |
| 215 |
$installer->setPlatformRequirementFilter(((array) $trace['object'])["\0*\0platformRequirementFilter"]); |
| 216 |
} |
| 217 |
|
| 218 |
if (0 !== $installer->run()) { |
| 219 |
file_put_contents(Factory::getComposerFile(), $composerJsonContents); |
| 220 |
|
| 221 |
return; |
| 222 |
} |
| 223 |
|
| 224 |
$versionSelector = new VersionSelector(ClassDiscovery::safeClassExists(RepositorySet::class) ? new RepositorySet() : new Pool()); |
| 225 |
$updateComposerJson = false; |
| 226 |
|
| 227 |
foreach ($composer->getRepositoryManager()->getLocalRepository()->getPackages() as $package) { |
| 228 |
foreach (['require', 'require-dev'] as $key) { |
| 229 |
if (!isset($missingRequires[$key][$package->getName()])) { |
| 230 |
continue; |
| 231 |
} |
| 232 |
$updateComposerJson = true; |
| 233 |
$missingRequires[$key][$package->getName()] = $versionSelector->findRecommendedRequireVersion($package); |
| 234 |
} |
| 235 |
} |
| 236 |
|
| 237 |
if ($updateComposerJson) { |
| 238 |
$this->updateComposerJson($missingRequires, $composer->getConfig()->get('sort-packages')); |
| 239 |
$this->updateComposerLock($composer, $event->getIO()); |
| 240 |
} |
| 241 |
} |
| 242 |
|
| 243 |
public function getMissingRequires(InstalledRepositoryInterface $repo, array $requires, bool $isProject, array $pinnedAbstractions): array |
| 244 |
{ |
| 245 |
$allPackages = []; |
| 246 |
$devPackages = method_exists($repo, 'getDevPackageNames') ? array_fill_keys($repo->getDevPackageNames(), true) : []; |
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
if (!isset($requires[0]['php-http/discovery'])) { |
| 251 |
$requires = [[], []]; |
| 252 |
} |
| 253 |
|
| 254 |
foreach ($repo->getPackages() as $package) { |
| 255 |
$allPackages[$package->getName()] = true; |
| 256 |
|
| 257 |
if (1 < \count($names = $package->getNames(false))) { |
| 258 |
$allPackages += array_fill_keys($names, false); |
| 259 |
|
| 260 |
if (isset($devPackages[$package->getName()])) { |
| 261 |
$devPackages += $names; |
| 262 |
} |
| 263 |
} |
| 264 |
|
| 265 |
if (isset($package->getRequires()['php-http/discovery'])) { |
| 266 |
$requires[(int) isset($devPackages[$package->getName()])] += $package->getRequires(); |
| 267 |
} |
| 268 |
} |
| 269 |
|
| 270 |
$missingRequires = [[], [], []]; |
| 271 |
$versionParser = new VersionParser(); |
| 272 |
|
| 273 |
if (ClassDiscovery::safeClassExists(\Phalcon\Http\Message\RequestFactory::class, false)) { |
| 274 |
$missingRequires[0]['psr/http-factory-implementation'] = []; |
| 275 |
$missingRequires[1]['psr/http-factory-implementation'] = []; |
| 276 |
} |
| 277 |
|
| 278 |
foreach ($requires as $dev => $rules) { |
| 279 |
$abstractions = []; |
| 280 |
$rules = array_intersect_key(self::PROVIDE_RULES, $rules); |
| 281 |
|
| 282 |
while ($rules) { |
| 283 |
$abstraction = key($rules); |
| 284 |
|
| 285 |
if (isset($pinnedAbstractions[$abstraction])) { |
| 286 |
unset($rules[$abstraction]); |
| 287 |
continue; |
| 288 |
} |
| 289 |
|
| 290 |
$abstractions[] = $abstraction; |
| 291 |
|
| 292 |
foreach (array_shift($rules) as $candidate => $deps) { |
| 293 |
[$candidate, $version] = explode(':', $candidate, 2) + [1 => null]; |
| 294 |
|
| 295 |
if (!isset($allPackages[$candidate])) { |
| 296 |
continue; |
| 297 |
} |
| 298 |
if (null !== $version && !$repo->findPackage($candidate, $versionParser->parseConstraints($version))) { |
| 299 |
continue; |
| 300 |
} |
| 301 |
if ($isProject && !$dev && isset($devPackages[$candidate])) { |
| 302 |
$missingRequires[0][$abstraction] = [$candidate]; |
| 303 |
$missingRequires[2][$abstraction] = [$candidate]; |
| 304 |
} else { |
| 305 |
$missingRequires[$dev][$abstraction] = []; |
| 306 |
} |
| 307 |
|
| 308 |
foreach ($deps as $dep) { |
| 309 |
if (isset(self::PROVIDE_RULES[$dep])) { |
| 310 |
$rules[$dep] = self::PROVIDE_RULES[$dep]; |
| 311 |
} elseif (!isset($allPackages[$dep])) { |
| 312 |
$missingRequires[$dev][$abstraction][] = $dep; |
| 313 |
} elseif ($isProject && !$dev && isset($devPackages[$dep])) { |
| 314 |
$missingRequires[0][$abstraction][] = $dep; |
| 315 |
$missingRequires[2][$abstraction][] = $dep; |
| 316 |
} |
| 317 |
} |
| 318 |
break; |
| 319 |
} |
| 320 |
} |
| 321 |
|
| 322 |
while ($abstractions) { |
| 323 |
$abstraction = array_shift($abstractions); |
| 324 |
|
| 325 |
if (isset($missingRequires[$dev][$abstraction])) { |
| 326 |
continue; |
| 327 |
} |
| 328 |
$candidates = self::PROVIDE_RULES[$abstraction]; |
| 329 |
|
| 330 |
foreach ($candidates as $candidate => $deps) { |
| 331 |
[$candidate, $version] = explode(':', $candidate, 2) + [1 => null]; |
| 332 |
|
| 333 |
if (null !== $version && !$repo->findPackage($candidate, $versionParser->parseConstraints($version))) { |
| 334 |
continue; |
| 335 |
} |
| 336 |
if (isset($allPackages[$candidate]) && (!$isProject || $dev || !isset($devPackages[$candidate]))) { |
| 337 |
continue 2; |
| 338 |
} |
| 339 |
} |
| 340 |
|
| 341 |
foreach (array_intersect_key(self::STICKYNESS_RULES, $candidates) as $candidate => $stickyRule) { |
| 342 |
[$stickyName, $stickyVersion] = explode(':', $stickyRule, 2) + [1 => null]; |
| 343 |
if (!isset($allPackages[$stickyName]) || ($isProject && !$dev && isset($devPackages[$stickyName]))) { |
| 344 |
continue; |
| 345 |
} |
| 346 |
if (null !== $stickyVersion && !$repo->findPackage($stickyName, $versionParser->parseConstraints($stickyVersion))) { |
| 347 |
continue; |
| 348 |
} |
| 349 |
|
| 350 |
$candidates = [$candidate => $candidates[$candidate]]; |
| 351 |
break; |
| 352 |
} |
| 353 |
|
| 354 |
$dep = key($candidates); |
| 355 |
[$dep] = explode(':', $dep, 2); |
| 356 |
$missingRequires[$dev][$abstraction] = [$dep]; |
| 357 |
|
| 358 |
if ($isProject && !$dev && isset($devPackages[$dep])) { |
| 359 |
$missingRequires[2][$abstraction][] = $dep; |
| 360 |
} |
| 361 |
} |
| 362 |
} |
| 363 |
|
| 364 |
$missingRequires[1] = array_diff_key($missingRequires[1], $missingRequires[0]); |
| 365 |
|
| 366 |
return $missingRequires; |
| 367 |
} |
| 368 |
|
| 369 |
public function preAutoloadDump(Event $event) |
| 370 |
{ |
| 371 |
$filesystem = new Filesystem(); |
| 372 |
|
| 373 |
$vendorDir = $filesystem->normalizePath(realpath(realpath($event->getComposer()->getConfig()->get('vendor-dir')))); |
| 374 |
$filesystem->ensureDirectoryExists($vendorDir.'/composer'); |
| 375 |
$pinned = $event->getComposer()->getPackage()->getExtra()['discovery'] ?? []; |
| 376 |
$candidates = []; |
| 377 |
|
| 378 |
$allInterfaces = array_merge(...array_values(self::INTERFACE_MAP)); |
| 379 |
foreach ($pinned as $abstraction => $class) { |
| 380 |
if (isset(self::INTERFACE_MAP[$abstraction])) { |
| 381 |
$interfaces = self::INTERFACE_MAP[$abstraction]; |
| 382 |
} elseif (false !== $k = array_search($abstraction, $allInterfaces, true)) { |
| 383 |
$interfaces = [$allInterfaces[$k]]; |
| 384 |
} else { |
| 385 |
throw new \UnexpectedValueException(sprintf('Invalid "extra.discovery" pinned in composer.json: "%s" is not one of ["%s"].', $abstraction, implode('", "', array_keys(self::INTERFACE_MAP)))); |
| 386 |
} |
| 387 |
|
| 388 |
foreach ($interfaces as $interface) { |
| 389 |
$candidates[] = sprintf("case %s: return [['class' => %s]];\n", var_export($interface, true), var_export($class, true)); |
| 390 |
} |
| 391 |
} |
| 392 |
|
| 393 |
$file = $vendorDir.'/composer/GeneratedDiscoveryStrategy.php'; |
| 394 |
|
| 395 |
if (!$candidates) { |
| 396 |
if (file_exists($file)) { |
| 397 |
unlink($file); |
| 398 |
} |
| 399 |
|
| 400 |
return; |
| 401 |
} |
| 402 |
|
| 403 |
$candidates = implode(' ', $candidates); |
| 404 |
$code = <<<EOPHP |
| 405 |
<?php |
| 406 |
|
| 407 |
namespace Http\Discovery\Strategy; |
| 408 |
|
| 409 |
class GeneratedDiscoveryStrategy implements DiscoveryStrategy |
| 410 |
{ |
| 411 |
public static function getCandidates(\$type) |
| 412 |
{ |
| 413 |
switch (\$type) { |
| 414 |
$candidates |
| 415 |
default: return []; |
| 416 |
} |
| 417 |
} |
| 418 |
} |
| 419 |
|
| 420 |
EOPHP |
| 421 |
; |
| 422 |
|
| 423 |
if (!file_exists($file) || $code !== file_get_contents($file)) { |
| 424 |
file_put_contents($file, $code); |
| 425 |
} |
| 426 |
|
| 427 |
$rootPackage = $event->getComposer()->getPackage(); |
| 428 |
$autoload = $rootPackage->getAutoload(); |
| 429 |
$autoload['classmap'][] = $vendorDir.'/composer/GeneratedDiscoveryStrategy.php'; |
| 430 |
$rootPackage->setAutoload($autoload); |
| 431 |
} |
| 432 |
|
| 433 |
private function updateComposerJson(array $missingRequires, bool $sortPackages) |
| 434 |
{ |
| 435 |
$file = Factory::getComposerFile(); |
| 436 |
$contents = file_get_contents($file); |
| 437 |
|
| 438 |
$manipulator = new JsonManipulator($contents); |
| 439 |
|
| 440 |
foreach ($missingRequires as $key => $packages) { |
| 441 |
foreach ($packages as $package => $constraint) { |
| 442 |
if ('remove' === $key) { |
| 443 |
$manipulator->removeSubNode('require-dev', $package); |
| 444 |
} else { |
| 445 |
$manipulator->addLink($key, $package, $constraint, $sortPackages); |
| 446 |
} |
| 447 |
} |
| 448 |
} |
| 449 |
|
| 450 |
file_put_contents($file, $manipulator->getContents()); |
| 451 |
} |
| 452 |
|
| 453 |
private function updateComposerLock(Composer $composer, IOInterface $io) |
| 454 |
{ |
| 455 |
if (false === $composer->getConfig()->get('lock')) { |
| 456 |
return; |
| 457 |
} |
| 458 |
|
| 459 |
$lock = substr(Factory::getComposerFile(), 0, -4).'lock'; |
| 460 |
$composerJson = file_get_contents(Factory::getComposerFile()); |
| 461 |
$lockFile = new JsonFile($lock, null, $io); |
| 462 |
$locker = ClassDiscovery::safeClassExists(RepositorySet::class) |
| 463 |
? new Locker($io, $lockFile, $composer->getInstallationManager(), $composerJson) |
| 464 |
: new Locker($io, $lockFile, $composer->getRepositoryManager(), $composer->getInstallationManager(), $composerJson); |
| 465 |
|
| 466 |
if (!$locker->isLocked()) { |
| 467 |
return; |
| 468 |
} |
| 469 |
|
| 470 |
$lockData = $locker->getLockData(); |
| 471 |
$lockData['content-hash'] = Locker::getContentHash($composerJson); |
| 472 |
$lockFile->write($lockData); |
| 473 |
} |
| 474 |
} |