Skip to content

Commit

Permalink
Fix a Windows compatibility issue regarding URLs including namespaced…
Browse files Browse the repository at this point in the history
… entity class name (so backslashes) (#62) (#63)
  • Loading branch information
DamienHarper authored Apr 18, 2019
1 parent 326f67b commit ce7def6
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 7 deletions.
4 changes: 4 additions & 0 deletions src/DoctrineAuditBundle/Controller/AuditController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace DH\DoctrineAuditBundle\Controller;

use DH\DoctrineAuditBundle\Helper\AuditHelper;
use DH\DoctrineAuditBundle\Reader\AuditReader;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
Expand Down Expand Up @@ -36,6 +37,7 @@ public function listAuditsAction(): Response
public function showEntityHistoryAction(Request $request, string $entity, $id = null): Response
{
$page = (int) $request->query->get('page', 1);
$entity = AuditHelper::paramToNamespace($entity);

$reader = $this->container->get('dh_doctrine_audit.reader');
$entries = $reader->getAuditsPager($entity, $id, $page, AuditReader::PAGE_SIZE);
Expand All @@ -57,6 +59,8 @@ public function showEntityHistoryAction(Request $request, string $entity, $id =
*/
public function showAuditEntryAction(string $entity, $id): Response
{
$entity = AuditHelper::paramToNamespace($entity);

$reader = $this->container->get('dh_doctrine_audit.reader');
$data = $reader->getAudit($entity, $id);

Expand Down
10 changes: 10 additions & 0 deletions src/DoctrineAuditBundle/Helper/AuditHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -342,4 +342,14 @@ public function getAuditTableIndices(string $tablename): array

return $indices;
}

public static function paramToNamespace(string $entity): string
{
return str_replace('-', '\\', $entity);
}

public static function namespaceToParam(string $entity): string
{
return str_replace('\\', '-', $entity);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{% extends "@DHDoctrineAudit/layout.html.twig" %}

{% import '@DHDoctrineAudit/Audit/bootstrap.html.twig' as bootstrap %}
{% import '@DHDoctrineAudit/Audit/helper.html.twig' as helper %}

{% block dh_doctrine_audit_content %}
<div class="card border-0">
Expand All @@ -25,7 +26,7 @@
<td>{{ table|escape }}</td>
<td>{{ bootstrap.badge(reader.getAuditsCount(entity) ~ ' operations') }}</td>
<td>
<a href="{{ path('dh_doctrine_audit_show_entity_history', { 'entity': entity }) }}">View audits</a>
<a href="{{ path('dh_doctrine_audit_show_entity_history', {'entity': helper.namespaceToParam(entity)}) }}">View audits</a>
</td>
</tr>
{% endfor %}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
<nav aria-label="breadcrumb">
<ol class="breadcrumb">
<li class="breadcrumb-item"><a href="{{ path('dh_doctrine_audit_list_audits') }}">Home</a></li>
<li class="breadcrumb-item {% if id is null %}active{% endif %}" aria-current="page"><a href="{{ path('dh_doctrine_audit_show_entity_history', { 'entity': entity }) }}">{{ entity }}</a></li>
<li class="breadcrumb-item {% if id is null %}active{% endif %}" aria-current="page"><a href="{{ path('dh_doctrine_audit_show_entity_history', { 'entity': helper.namespaceToParam(entity) }) }}">{{ entity }}</a></li>
{% if id is not null %}
<li class="breadcrumb-item active" aria-current="page"><a href="{{ path('dh_doctrine_audit_show_entity_history', { 'entity': entity, 'id': id }) }}">{{ entity }}#{{ id }}</a></li>
<li class="breadcrumb-item active" aria-current="page"><a href="{{ path('dh_doctrine_audit_show_entity_history', { 'entity': helper.namespaceToParam(entity), 'id': id }) }}">{{ entity }}#{{ id }}</a></li>
{% endif %}
</ol>
</nav>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
<nav aria-label="breadcrumb">
<ol class="breadcrumb">
<li class="breadcrumb-item"><a href="{{ path('dh_doctrine_audit_list_audits') }}">Home</a></li>
<li class="breadcrumb-item"><a href="{{ path('dh_doctrine_audit_show_entity_history', {'entity': entity}) }}">{{ entity }}</a></li>
<li class="breadcrumb-item active" aria-current="page"><a href="{{ path('dh_doctrine_audit_show_audit_entry', {'entity': entity, 'id': entry.getObjectId()}) }}">{{ entity }}#{{ entry.getObjectId() }}</a></li>
<li class="breadcrumb-item"><a href="{{ path('dh_doctrine_audit_show_entity_history', {'entity': helper.namespaceToParam(entity)}) }}">{{ entity }}</a></li>
<li class="breadcrumb-item active" aria-current="page"><a href="{{ path('dh_doctrine_audit_show_audit_entry', {'entity': helper.namespaceToParam(entity), 'id': entry.getObjectId()}) }}">{{ entity }}#{{ entry.getObjectId() }}</a></li>
</ol>
</nav>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@
{% endfilter %}
{% endmacro dump %}

{% macro namespaceToParam(entity) %}
{{- entity|replace({'\\': '-'}) -}}
{% endmacro namespaceToParam %}

{% macro humanize(entity, entry) %}
{% import _self as helper %}

Expand Down Expand Up @@ -40,14 +44,14 @@
{% set action = '???' %}
{% endif %}

<code><a href="{{ path('dh_doctrine_audit_show_entity_history', { 'entity': entity, 'id': entry.object_id }) }}" class="code">{{ subject }}</a></code>
<code><a href="{{ path('dh_doctrine_audit_show_entity_history', { 'entity': helper.namespaceToParam(entity), 'id': entry.object_id }) }}" class="code">{{ subject }}</a></code>
{% if source is defined and subject != source.label %}
<em>({{ source.label }})</em>
{% endif %}
has been <b>{{ action }}</b>
{% if target is defined %}
{% set subject = target.class~'#'~target.id %}
{{ direction }} <code><a href="{{ path('dh_doctrine_audit_show_entity_history', { 'entity': target.class, 'id': target.id }) }}">{{ subject }}</a></code>
{{ direction }} <code><a href="{{ path('dh_doctrine_audit_show_entity_history', { 'entity': helper.namespaceToParam(target.class), 'id': target.id }) }}">{{ subject }}</a></code>
{% if subject != target.label %}
(<em>{{ helper.dump(target) }}</em>)
{% endif %}
Expand Down
10 changes: 10 additions & 0 deletions tests/DoctrineAuditBundle/Helper/AuditHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,16 @@ public function testBlameWhenNoUser(): void
$this->assertSame($expected, $helper->blame(), 'AuditHelper::blame() is ok.');
}

public function testParamToNamespace(): void
{
$this->assertSame(Author::class, AuditHelper::paramToNamespace('DH-DoctrineAuditBundle-Tests-Fixtures-Core-Author'), 'AuditHelper::paramToNamespace() is ok.');
}

public function testNamespaceToParam(): void
{
$this->assertSame('DH-DoctrineAuditBundle-Tests-Fixtures-Core-Author', AuditHelper::namespaceToParam(Author::class), 'AuditHelper::namespaceToParam() is ok.');
}

public function testGetConfiguration(): void
{
$em = $this->getEntityManager();
Expand Down

0 comments on commit ce7def6

Please sign in to comment.