Skip to content

Commit

Permalink
feat: Added Roadiz custom data-collector for web-profiler
Browse files Browse the repository at this point in the history
  • Loading branch information
ambroisemaupate committed Jun 20, 2023
1 parent 84a611b commit d0e01fa
Show file tree
Hide file tree
Showing 4 changed files with 126 additions and 2 deletions.
16 changes: 14 additions & 2 deletions lib/RoadizCoreBundle/config/services.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
---
parameters:
roadiz_core.cms_version: '2.1.16'
roadiz_core.cms_version_prefix: 'main'
env(APP_NAMESPACE): "roadiz"
env(APP_VERSION): "0.1.0"
env(APP_USE_ACCEPT_LANGUAGE_HEADER): 'false'
Expand All @@ -22,9 +24,9 @@ services:
autowire: true # Automatically injects dependencies in your services.
autoconfigure: true # Automatically registers your services as commands, event subscribers, etc.
bind:
$cmsVersion: '2.1.16'
$cmsVersion: '%roadiz_core.cms_version%'
$appVersion: '%roadiz_core.app_version%'
$cmsVersionPrefix: 'main'
$cmsVersionPrefix: '%roadiz_core.cms_version_prefix%'
$staticDomain: '%roadiz_core.static_domain_name%'
$hideRoadizVersion: '%roadiz_core.hide_roadiz_version%'
$inheritanceType: '%roadiz_core.inheritance_type%'
Expand Down Expand Up @@ -140,6 +142,16 @@ services:
decorates: 'api_platform.serializer.normalizer.item'
decoration_priority: 21

RZ\Roadiz\CoreBundle\DataCollector\RequestDataCollector:
autoconfigure: false
arguments: [ '%roadiz_core.cms_version%', '%roadiz_core.cms_version_prefix%']
tags:
- name: data_collector
template: '@RoadizCore/DataCollector/request.html.twig'
# must match the value returned by the getName() method
id: 'roadiz.data_collector.request'
priority: 400

# Document
RZ\Roadiz\CoreBundle\Serializer\Normalizer\DocumentNormalizer:
# By default, .inner is passed as argument
Expand Down
71 changes: 71 additions & 0 deletions lib/RoadizCoreBundle/src/DataCollector/RequestDataCollector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php

declare(strict_types=1);

namespace RZ\Roadiz\CoreBundle\DataCollector;

use PackageVersions\Versions;
use Symfony\Bundle\FrameworkBundle\DataCollector\AbstractDataCollector;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

final class RequestDataCollector extends AbstractDataCollector
{
private ?string $cmsVersion = null;
private ?string $cmsVersionPrefix = null;

public function __construct(string $cmsVersion, string $cmsVersionPrefix)
{
$this->cmsVersion = $cmsVersion;
$this->cmsVersionPrefix = $cmsVersionPrefix;
}

/**
* {@inheritdoc}
*/
public function collect(Request $request, Response $response, \Throwable $exception = null): void
{
$this->data = [];
}

public function getVersion(): ?string
{
$fallback = implode(' - ', array_filter([$this->cmsVersionPrefix, $this->cmsVersion]));
if (!class_exists(Versions::class)) {
return $fallback;
}

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

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

public static function getTemplate(): ?string
{
return '@RoadizCore/DataCollector/request.html.twig';
}

public function getMethod()
{
return $this->data['method'];
}

public function getAcceptableContentTypes()
{
return $this->data['acceptable_content_types'];
}

/**
* {@inheritdoc}
*/
public function getName(): string
{
return 'roadiz.data_collector.request';
}

public function reset(): void
{
$this->data = [];
}
}
18 changes: 18 additions & 0 deletions lib/RoadizCoreBundle/templates/DataCollector/request.html.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{% extends '@WebProfiler/Profiler/layout.html.twig' %}

{% block toolbar %}
{% set icon %}
{{ include('@RoadizCore/DataCollector/roadiz-icon.svg') }}
{% endset %}

{% set text %}
<div class="sf-toolbar-info-piece">
<b>Roadiz CMS</b>
{% if collector.version %}
<span>{{ collector.version }}</span>
{% endif %}
</div>
{% endset %}

{{ include('@WebProfiler/Profiler/toolbar_item.html.twig', { 'link': false }) }}
{% endblock %}
23 changes: 23 additions & 0 deletions lib/RoadizCoreBundle/templates/DataCollector/roadiz-icon.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit d0e01fa

Please sign in to comment.