Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Api-Platform's version in debug bar #3235

Merged
merged 3 commits into from Sep 10, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -2,6 +2,7 @@

## 2.6.0

* Display the API Platform's version in the debug-bar
* MongoDB: Possibility to add execute options (aggregate command fields) for a resource, like `allowDiskUse` (#3144)
* MongoDB: Mercure support (#3290)
* GraphQL: Subscription support with Mercure (#3321)
Expand Down
1 change: 1 addition & 0 deletions composer.json
Expand Up @@ -96,6 +96,7 @@
"doctrine/mongodb-odm-bundle": "To support MongoDB. Only versions 4.0 and later are supported.",
"elasticsearch/elasticsearch": "To support Elasticsearch.",
"guzzlehttp/guzzle": "To use the HTTP cache invalidation system.",
"ocramius/package-versions": "To display the API Platform's version in the debug bar.",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@alanpoulain wouldn't it be more interesting to suggest https://github.com/composer/package-versions-deprecated for composer 1 & 2 compatibility?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure about this. If the user doesn't use PHP 7.4 or Composer 2, it will find this package anyway, don't you think?
And ocramius/package-versions is the original package, it makes sense to me to add it to the suggest list.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed 👍

"phpdocumentor/reflection-docblock": "To support extracting metadata from PHPDoc.",
"psr/cache-implementation": "To use metadata caching.",
"ramsey/uuid": "To support Ramsey's UUID identifiers.",
Expand Down
13 changes: 13 additions & 0 deletions src/Bridge/Symfony/Bundle/DataCollector/RequestDataCollector.php
Expand Up @@ -23,6 +23,7 @@
use ApiPlatform\Core\DataProvider\SubresourceDataProviderInterface;
use ApiPlatform\Core\Metadata\Resource\Factory\ResourceMetadataFactoryInterface;
use ApiPlatform\Core\Util\RequestAttributesExtractor;
use PackageVersions\Versions;
use Psr\Container\ContainerInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
Expand Down Expand Up @@ -158,6 +159,18 @@ public function getDataPersisters(): array
return $this->data['dataPersisters'] ?? ['responses' => []];
}

public function getVersion(): ?string
{
if (!class_exists(Versions::class)) {
return null;
}

$version = Versions::getVersion('api-platform/core');
preg_match('/^v(.*?)@/', $version, $output);

return $output[1] ?? strtok($version, '@');
}

/**
* {@inheritdoc}
*/
Expand Down
Expand Up @@ -80,10 +80,15 @@
{% set icon %}
{% set status_color = collector.counters.ignored_filters|default(false) ? 'yellow' : 'default' %}
{{ include('@ApiPlatform/DataCollector/api-platform-icon.svg') }}
<span class="sf-toolbar-value"></span>
{% endset %}

{% set text %}
{% if collector.version %}
<div class="sf-toolbar-info-piece">
<b>Version</b>
<span>{{ collector.version }}</span>
</div>
{% endif %}
<div class="sf-toolbar-info-piece">
<b>Resource Class</b>
<span>{{ collector.resourceClass|default('Not an API Platform resource') }}</span>
Expand Down
Expand Up @@ -30,6 +30,7 @@
use ApiPlatform\Core\Metadata\Resource\ResourceMetadata;
use ApiPlatform\Core\Tests\Fixtures\DummyEntity;
use ApiPlatform\Core\Tests\ProphecyTrait;
use PackageVersions\Versions;
use PHPUnit\Framework\TestCase;
use Psr\Container\ContainerInterface;
use Symfony\Component\HttpFoundation\ParameterBag;
Expand Down Expand Up @@ -212,6 +213,27 @@ public function testWithResourceWithTraceables()
}
}

public function testVersionCollection()
{
$this->apiResourceClassWillReturn(DummyEntity::class);

$dataCollector = new RequestDataCollector(
$this->metadataFactory->reveal(),
$this->filterLocator->reveal(),
$this->getUsedCollectionDataProvider(),
$this->getUsedItemDataProvider(),
$this->getUsedSubresourceDataProvider(),
$this->getUsedPersister()
);

$dataCollector->collect(
$this->request->reveal(),
$this->response
);

$this->assertSame(null !== $dataCollector->getVersion(), class_exists(Versions::class));
}

private function apiResourceClassWillReturn($data, $context = [])
{
$this->attributes->get('_api_resource_class')->shouldBeCalled()->willReturn($data);
Expand Down
Expand Up @@ -81,7 +81,7 @@ public function testDebugBarContentNotResourceClass()

// Check extra info content
$this->assertStringContainsString('sf-toolbar-status-default', $block->attr('class'), 'The toolbar block should have the default color.');
$this->assertSame('Not an API Platform resource', $block->filter('.sf-toolbar-info-piece span')->html());
$this->assertSame('Not an API Platform resource', $block->filterXPath('//div[@class="sf-toolbar-info-piece"][./b[contains(., "Resource Class")]]/span')->html());
}

public function testDebugBarContent()
Expand All @@ -99,7 +99,7 @@ public function testDebugBarContent()

// Check extra info content
$this->assertStringContainsString('sf-toolbar-status-default', $block->attr('class'), 'The toolbar block should have the default color.');
$this->assertSame('mongodb' === $this->env ? DocumentDummy::class : Dummy::class, $block->filter('.sf-toolbar-info-piece span')->html());
$this->assertSame('mongodb' === $this->env ? DocumentDummy::class : Dummy::class, $block->filterXPath('//div[@class="sf-toolbar-info-piece"][./b[contains(., "Resource Class")]]/span')->html());
}

public function testProfilerGeneralLayoutNotResourceClass()
Expand Down