Skip to content

Commit

Permalink
fix(symfony): fetch api-platform/symfony version debug bar
Browse files Browse the repository at this point in the history
fixes #6709
  • Loading branch information
soyuka committed Oct 16, 2024
1 parent 99262dc commit 639e898
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 4 deletions.
28 changes: 25 additions & 3 deletions src/Symfony/Bundle/DataCollector/RequestDataCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
use ApiPlatform\State\Util\RequestAttributesExtractor;
use Composer\InstalledVersions;
use PackageVersions\Versions;
use Psr\Container\ContainerInterface;
use Symfony\Component\HttpFoundation\Request;
Expand Down Expand Up @@ -67,16 +68,37 @@ private function setFilters(ApiResource $resourceMetadata, int $index, array &$f
}
}

// TODO: 4.1 remove Versions as its deprecated
public function getVersion(): ?string
{
if (class_exists(InstalledVersions::class)) {
return InstalledVersions::getPrettyVersion('api-platform/symfony') ?? InstalledVersions::getPrettyVersion('api-platform/core');
}

if (!class_exists(Versions::class)) {
return null;
}

$version = Versions::getVersion('api-platform/core');
preg_match('/^v(.*?)@/', (string) $version, $output);
try {
$version = strtok(Versions::getVersion('api-platform/symfony'), '@');
} catch (\OutOfBoundsException) {
$version = false;
}

if ($version === false) {
try {
$version = strtok(Versions::getVersion('api-platform/core'), '@');
} catch (\OutOfBoundsException) {
$version = false;
}
}

if ($version === false) {
return null;
}

return $output[1] ?? strtok($version, '@');
preg_match('/^v(.*?)$/', $version, $output);
return $output[1] ?? $version;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use ApiPlatform\Metadata\Resource\ResourceMetadataCollection;
use ApiPlatform\Symfony\Bundle\DataCollector\RequestDataCollector;
use ApiPlatform\Tests\Fixtures\DummyEntity;
use Composer\InstalledVersions;
use PackageVersions\Versions;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
Expand Down Expand Up @@ -163,7 +164,11 @@ public function testVersionCollection(): void
$this->response
);

$this->assertSame(null !== $dataCollector->getVersion(), class_exists(Versions::class));
if (class_exists(InstalledVersions::class)) {
$this->assertTrue(null !== $dataCollector->getVersion());
} else {
$this->assertSame(null !== $dataCollector->getVersion(), class_exists(Versions::class));
}
}

public function testWithPreviousData(): void
Expand Down

0 comments on commit 639e898

Please sign in to comment.