diff --git a/src/Symfony/Bundle/DataCollector/RequestDataCollector.php b/src/Symfony/Bundle/DataCollector/RequestDataCollector.php index ddde1871a0e..82e289144a3 100644 --- a/src/Symfony/Bundle/DataCollector/RequestDataCollector.php +++ b/src/Symfony/Bundle/DataCollector/RequestDataCollector.php @@ -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; @@ -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; } /** diff --git a/tests/Symfony/Bundle/DataCollector/RequestDataCollectorTest.php b/tests/Symfony/Bundle/DataCollector/RequestDataCollectorTest.php index c9af7f056eb..9a5a35a00f8 100644 --- a/tests/Symfony/Bundle/DataCollector/RequestDataCollectorTest.php +++ b/tests/Symfony/Bundle/DataCollector/RequestDataCollectorTest.php @@ -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; @@ -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