function Plugin::preAutoloadDump
Same name in this branch
- 11.1.x vendor/tbachert/spi/src/Composer/Plugin.php \Nevay\SPI\Composer\Plugin::preAutoloadDump()
File
-
vendor/
php-http/ discovery/ src/ Composer/ Plugin.php, line 369
Class
- Plugin
- Auto-installs missing implementations.
Namespace
Http\Discovery\ComposerCode
public function preAutoloadDump(Event $event) {
$filesystem = new Filesystem();
// Double realpath() on purpose, see https://bugs.php.net/72738
$vendorDir = $filesystem->normalizePath(realpath(realpath($event->getComposer()
->getConfig()
->get('vendor-dir'))));
$filesystem->ensureDirectoryExists($vendorDir . '/composer');
$pinned = $event->getComposer()
->getPackage()
->getExtra()['discovery'] ?? [];
$candidates = [];
$allInterfaces = array_merge(...array_values(self::INTERFACE_MAP));
foreach ($pinned as $abstraction => $class) {
if (isset(self::INTERFACE_MAP[$abstraction])) {
$interfaces = self::INTERFACE_MAP[$abstraction];
}
elseif (false !== ($k = array_search($abstraction, $allInterfaces, true))) {
$interfaces = [
$allInterfaces[$k],
];
}
else {
throw new \UnexpectedValueException(sprintf('Invalid "extra.discovery" pinned in composer.json: "%s" is not one of ["%s"].', $abstraction, implode('", "', array_keys(self::INTERFACE_MAP))));
}
foreach ($interfaces as $interface) {
$candidates[] = sprintf("case %s: return [['class' => %s]];\n", var_export($interface, true), var_export($class, true));
}
}
$file = $vendorDir . '/composer/GeneratedDiscoveryStrategy.php';
if (!$candidates) {
if (file_exists($file)) {
unlink($file);
}
return;
}
$candidates = implode(' ', $candidates);
$code = <<<EOPHP
<?php
namespace Http\\Discovery\\Strategy;
class GeneratedDiscoveryStrategy implements DiscoveryStrategy
{
public static function getCandidates(\$type)
{
switch (\$type) {
{<span class="php-variable">$candidates</span>}
default: return [];
}
}
}
EOPHP;
if (!file_exists($file) || $code !== file_get_contents($file)) {
file_put_contents($file, $code);
}
$rootPackage = $event->getComposer()
->getPackage();
$autoload = $rootPackage->getAutoload();
$autoload['classmap'][] = $vendorDir . '/composer/GeneratedDiscoveryStrategy.php';
$rootPackage->setAutoload($autoload);
}