Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 26 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,38 @@ 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 (false === $version) {
try {
$version = strtok(Versions::getVersion('api-platform/core'), '@');
} catch (\OutOfBoundsException) {
$version = false;
}
}

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

preg_match('/^v(.*?)$/', $version, $output);

return $output[1] ?? strtok($version, '@');
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
Loading