Skip to content

Commit

Permalink
Merge branch '1.x' into 2.x
Browse files Browse the repository at this point in the history
* 1.x:
  Re-run composer when flex is installed after deps are resolved
  Fix compat with old composer versions
  Improve declinating composer.lock into symfony.lock
  Add some native return types
  Update Downloader.php
  • Loading branch information
nicolas-grekas committed May 2, 2022
2 parents 3eb57ba + e74319d commit dcbe58c
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 14 deletions.
4 changes: 2 additions & 2 deletions src/Downloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -218,8 +218,8 @@ public function getRecipes(array $operations): array
break;
}

if (isset($links['recipes_template_relative'])) {
$links['recipes_template'] = preg_replace('{[^/\?]*+(?=\?|$)}', $links['recipes_template_relative'], $endpoint, 1);
if (isset($links['recipe_template_relative'])) {
$links['recipe_template'] = preg_replace('{[^/\?]*+(?=\?|$)}', $links['recipe_template_relative'], $endpoint, 1);
}

$urls[] = strtr($links['recipe_template'], [
Expand Down
39 changes: 27 additions & 12 deletions src/Flex.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ class Flex implements PluginInterface, EventSubscriberInterface
private $lock;
private $displayThanksReminder = 0;
private $dryRun = false;
private $reinstall;
private static $activated = true;
private static $aliasResolveCommands = [
'require' => true,
Expand Down Expand Up @@ -117,8 +118,12 @@ class_exists(__NAMESPACE__.str_replace('/', '\\', substr($file, \strlen(__DIR__)
$this->filter = new PackageFilter($io, $symfonyRequire, $this->downloader);
}

$composerFile = Factory::getComposerFile();
$composerLock = 'json' === pathinfo($composerFile, \PATHINFO_EXTENSION) ? substr($composerFile, 0, -4).'lock' : $composerFile.'.lock';
$symfonyLock = str_replace('composer', 'symfony', basename($composerLock));

$this->configurator = new Configurator($composer, $io, $this->options);
$this->lock = new Lock(getenv('SYMFONY_LOCKFILE') ?: str_replace('composer.json', 'symfony.lock', Factory::getComposerFile()));
$this->lock = new Lock(getenv('SYMFONY_LOCKFILE') ?: \dirname($composerLock).'/'.(basename($composerLock) !== $symfonyLock ? $symfonyLock : 'symfony.lock'));

$disable = true;
foreach (array_merge($composer->getPackage()->getRequires() ?? [], $composer->getPackage()->getDevRequires() ?? []) as $link) {
Expand Down Expand Up @@ -244,6 +249,13 @@ public function configureProject(Event $event)
$this->updateComposerLock();
}

public function recordFlexInstall(PackageEvent $event)
{
if (null === $this->reinstall && 'symfony/flex' === $event->getOperation()->getPackage()->getName()) {
$this->reinstall = true;
}
}

public function record(PackageEvent $event)
{
if ($this->shouldRecordOperation($event->getOperation(), $event->isDevMode(), $event->getComposer())) {
Expand Down Expand Up @@ -286,7 +298,7 @@ public function update(Event $event, $operations = [])
$contents = file_get_contents($file);
$json = JsonFile::parseJson($contents);

if (!isset($json['flex-require']) && !isset($json['flex-require-dev'])) {
if (!$this->reinstall && !isset($json['flex-require']) && !isset($json['flex-require-dev'])) {
$this->unpack($event);

return;
Expand All @@ -298,17 +310,18 @@ public function update(Event $event, $operations = [])
$symfonyVersion = $json['extra']['symfony']['require'] ?? null;
$versions = $symfonyVersion ? $this->downloader->getVersions() : null;
foreach (['require', 'require-dev'] as $type) {
if (isset($json['flex-'.$type])) {
foreach ($json['flex-'.$type] as $package => $constraint) {
if ($symfonyVersion && '*' === $constraint && isset($versions['splits'][$package])) {
// replace unbounded constraints for symfony/* packages by extra.symfony.require
$constraint = $symfonyVersion;
}
$manipulator->addLink($type, $package, $constraint, $sortPackages);
if (!isset($json['flex-'.$type])) {
continue;
}
foreach ($json['flex-'.$type] as $package => $constraint) {
if ($symfonyVersion && '*' === $constraint && isset($versions['splits'][$package])) {
// replace unbounded constraints for symfony/* packages by extra.symfony.require
$constraint = $symfonyVersion;
}

$manipulator->removeMainKey('flex-'.$type);
$manipulator->addLink($type, $package, $constraint, $sortPackages);
}

$manipulator->removeMainKey('flex-'.$type);
}

file_put_contents($file, $manipulator->getContents());
Expand Down Expand Up @@ -671,7 +684,7 @@ private function formatOrigin(Recipe $recipe): string

private function shouldRecordOperation(OperationInterface $operation, bool $isDevMode, Composer $composer = null): bool
{
if ($this->dryRun) {
if ($this->dryRun || $this->reinstall) {
return false;
}

Expand Down Expand Up @@ -746,6 +759,7 @@ private function unpack(Event $event)

private function reinstall(Event $event, bool $update)
{
$this->reinstall = false;
$event->stopPropagation();

$ed = $this->composer->getEventDispatcher();
Expand Down Expand Up @@ -783,6 +797,7 @@ public static function getSubscribedEvents(): array

$events = [
PackageEvents::POST_PACKAGE_UPDATE => 'enableThanksReminder',
PackageEvents::POST_PACKAGE_INSTALL => 'recordFlexInstall',
InstallerEvents::PRE_OPERATIONS_EXEC => 'recordOperations',
PluginEvents::PRE_POOL_CREATE => 'truncatePackages',
ScriptEvents::POST_CREATE_PROJECT_CMD => 'configureProject',
Expand Down

0 comments on commit dcbe58c

Please sign in to comment.