Skip to content

Commit 6269871

Browse files
committed
fix(symfony): fetch api-platform/symfony version debug bar
fixes #6709
1 parent 99262dc commit 6269871

File tree

2 files changed

+32
-4
lines changed

2 files changed

+32
-4
lines changed

src/Symfony/Bundle/DataCollector/RequestDataCollector.php

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use ApiPlatform\Metadata\ApiResource;
1717
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
1818
use ApiPlatform\State\Util\RequestAttributesExtractor;
19+
use Composer\InstalledVersions;
1920
use PackageVersions\Versions;
2021
use Psr\Container\ContainerInterface;
2122
use Symfony\Component\HttpFoundation\Request;
@@ -67,16 +68,38 @@ private function setFilters(ApiResource $resourceMetadata, int $index, array &$f
6768
}
6869
}
6970

71+
// TODO: 4.1 remove Versions as its deprecated
7072
public function getVersion(): ?string
7173
{
74+
if (class_exists(InstalledVersions::class)) {
75+
return InstalledVersions::getPrettyVersion('api-platform/symfony') ?? InstalledVersions::getPrettyVersion('api-platform/core');
76+
}
77+
7278
if (!class_exists(Versions::class)) {
7379
return null;
7480
}
7581

76-
$version = Versions::getVersion('api-platform/core');
77-
preg_match('/^v(.*?)@/', (string) $version, $output);
82+
try {
83+
$version = strtok(Versions::getVersion('api-platform/symfony'), '@');
84+
} catch (\OutOfBoundsException) {
85+
$version = false;
86+
}
87+
88+
if (false === $version) {
89+
try {
90+
$version = strtok(Versions::getVersion('api-platform/core'), '@');
91+
} catch (\OutOfBoundsException) {
92+
$version = false;
93+
}
94+
}
95+
96+
if (false === $version) {
97+
return null;
98+
}
99+
100+
preg_match('/^v(.*?)$/', $version, $output);
78101

79-
return $output[1] ?? strtok($version, '@');
102+
return $output[1] ?? $version;
80103
}
81104

82105
/**

tests/Symfony/Bundle/DataCollector/RequestDataCollectorTest.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use ApiPlatform\Metadata\Resource\ResourceMetadataCollection;
2121
use ApiPlatform\Symfony\Bundle\DataCollector\RequestDataCollector;
2222
use ApiPlatform\Tests\Fixtures\DummyEntity;
23+
use Composer\InstalledVersions;
2324
use PackageVersions\Versions;
2425
use PHPUnit\Framework\MockObject\MockObject;
2526
use PHPUnit\Framework\TestCase;
@@ -163,7 +164,11 @@ public function testVersionCollection(): void
163164
$this->response
164165
);
165166

166-
$this->assertSame(null !== $dataCollector->getVersion(), class_exists(Versions::class));
167+
if (class_exists(InstalledVersions::class)) {
168+
$this->assertTrue(null !== $dataCollector->getVersion());
169+
} else {
170+
$this->assertSame(null !== $dataCollector->getVersion(), class_exists(Versions::class));
171+
}
167172
}
168173

169174
public function testWithPreviousData(): void

0 commit comments

Comments
 (0)