Skip to content

Commit

Permalink
fix(symfony): fetch api-platform/symfony version debug bar (#6722)
Browse files Browse the repository at this point in the history
fixes #6709
  • Loading branch information
soyuka authored Oct 16, 2024
1 parent d34cd7b commit e7fb04f
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 4 deletions.
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

0 comments on commit e7fb04f

Please sign in to comment.