Skip to content

Commit

Permalink
feat: Added Redirections usage count to better analyze your app routes
Browse files Browse the repository at this point in the history
  • Loading branch information
ambroisemaupate committed Jun 15, 2023
1 parent d8d351b commit 3a6a38b
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 17 deletions.
38 changes: 38 additions & 0 deletions lib/RoadizCoreBundle/migrations/Version20230615122615.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

declare(strict_types=1);

namespace RZ\Roadiz\Migrations;

use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;

/**
* Auto-generated Migration: Please modify to your needs!
*/
final class Version20230615122615 extends AbstractMigration
{
public function getDescription(): string
{
return 'Added Redirection use count.';
}

public function up(Schema $schema): void
{
// this up() migration is auto-generated, please modify it to your needs
$this->addSql('ALTER TABLE redirections ADD use_count INT DEFAULT 0 NOT NULL');
$this->addSql('CREATE INDEX redirection_use_count ON redirections (use_count)');
}

public function down(Schema $schema): void
{
// this down() migration is auto-generated, please modify it to your needs
$this->addSql('DROP INDEX redirection_use_count ON redirections');
$this->addSql('ALTER TABLE redirections DROP use_count');
}

public function isTransactional(): bool
{
return false;
}
}
13 changes: 9 additions & 4 deletions lib/RoadizCoreBundle/src/Controller/RedirectionController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace RZ\Roadiz\CoreBundle\Controller;

use Doctrine\Persistence\ManagerRegistry;
use RZ\Roadiz\CoreBundle\Entity\Redirection;
use Symfony\Cmf\Component\Routing\RouteObjectInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
Expand All @@ -15,13 +16,12 @@
final class RedirectionController
{
private UrlGeneratorInterface $urlGenerator;
private ManagerRegistry $managerRegistry;

/**
* @param UrlGeneratorInterface $urlGenerator
*/
public function __construct(UrlGeneratorInterface $urlGenerator)
public function __construct(UrlGeneratorInterface $urlGenerator, ManagerRegistry $managerRegistry)
{
$this->urlGenerator = $urlGenerator;
$this->managerRegistry = $managerRegistry;
}

/**
Expand All @@ -32,6 +32,9 @@ public function __construct(UrlGeneratorInterface $urlGenerator)
public function redirectAction(Request $request, Redirection $redirection): RedirectResponse
{
if (null !== $redirection->getRedirectNodeSource()) {
$redirection->incrementUseCount();
$this->managerRegistry->getManagerForClass(Redirection::class)->flush();

return new RedirectResponse(
$this->urlGenerator->generate(
RouteObjectInterface::OBJECT_BASED_ROUTE_NAME,
Expand All @@ -45,6 +48,8 @@ public function redirectAction(Request $request, Redirection $redirection): Redi
null !== $redirection->getRedirectUri() &&
strlen($redirection->getRedirectUri()) > 0
) {
$redirection->incrementUseCount();
$this->managerRegistry->getManagerForClass(Redirection::class)->flush();
return new RedirectResponse($redirection->getRedirectUri(), $redirection->getType());
}

Expand Down
35 changes: 24 additions & 11 deletions lib/RoadizCoreBundle/src/Entity/Redirection.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,25 +18,24 @@
ORM\Entity(repositoryClass: RedirectionRepository::class),
ORM\Table(name: "redirections"),
ORM\HasLifecycleCallbacks,
UniqueEntity(fields: ["query"])
UniqueEntity(fields: ["query"]),
ORM\Index(columns: ["use_count"], name: 'redirection_use_count')
]
class Redirection extends AbstractDateTimed
{
/**
* @var string
*/
#[ORM\Column(type: 'string', length: 255, unique: true)]
#[Assert\NotBlank]
#[Assert\Length(max: 255)]
private string $query = "";

/**
* @var string|null
*/
#[ORM\Column(name: 'redirectUri', type: 'text', length: 2048, nullable: true)]
#[Assert\Length(max: 2048)]
private ?string $redirectUri = null;

#[ORM\Column(name: 'use_count', type: 'integer', nullable: false, options: ['default' => 0])]
#[Assert\Length(max: 2048)]
private int $useCount = 0;

/**
* @var NodesSources|null
*/
Expand All @@ -59,12 +58,12 @@ public function getQuery(): string
}

/**
* @param string $query
* @param string|null $query
* @return Redirection
*/
public function setQuery($query): Redirection
public function setQuery(?string $query): Redirection
{
$this->query = $query;
$this->query = $query ?? '';
return $this;
}

Expand All @@ -80,7 +79,7 @@ public function getRedirectUri(): ?string
* @param string|null $redirectUri
* @return Redirection
*/
public function setRedirectUri($redirectUri): Redirection
public function setRedirectUri(?string $redirectUri): Redirection
{
$this->redirectUri = $redirectUri;
return $this;
Expand Down Expand Up @@ -140,4 +139,18 @@ public function __construct()
$this->type = Response::HTTP_MOVED_PERMANENTLY;
$this->initAbstractDateTimed();
}

/**
* @return int
*/
public function getUseCount(): int
{
return $this->useCount;
}

public function incrementUseCount(): self
{
$this->useCount++;
return $this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ final class RedirectableUrlMatcher extends BaseMatcher
*
* @return array An array of parameters
*/
public function redirect($path, $route, $scheme = null): array
public function redirect(string $path, string $route, ?string $scheme = null): array
{
return [
'_controller' => RedirectionController::class . '::redirectToRouteAction',
Expand Down
2 changes: 1 addition & 1 deletion lib/RoadizCoreBundle/src/Routing/RedirectionMatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public function __construct(
/**
* {@inheritdoc}
*/
public function match($pathinfo): array
public function match(string $pathinfo): array
{
$this->stopwatch->start('findRedirection');
$decodedUrl = rawurldecode($pathinfo);
Expand Down
1 change: 1 addition & 0 deletions lib/Rozier/src/Resources/translations/messages.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -1177,6 +1177,7 @@
<trans-unit xml:space="preserve" id="redirection.type"><source>redirection.type</source><note>Redirection field which stands for HTTP code: 301 or 302</note></trans-unit>
<trans-unit xml:space="preserve" id="redirection.moved_permanently"><source>redirection.moved_permanently</source><note>Redirection type description for 301 Moved Permanently</note></trans-unit>
<trans-unit xml:space="preserve" id="redirection.moved_temporarily"><source>redirection.moved_temporarily</source><note>Redirection type description for 302 Moved Temporarily</note></trans-unit>
<trans-unit xml:space="preserve" id="redirection.use_count"><source>redirection.use_count</source><note>Redirection usage count</note></trans-unit>
<trans-unit xml:space="preserve" id="login_image"><source>login_image</source><note>Global parameter to customize login screen background image</note></trans-unit>
<trans-unit xml:space="preserve" id="publishedAt"><source>publishedAt</source><note>Field label for node-source publication date and time.</note></trans-unit>
<trans-unit xml:space="preserve" id="publishable"><source>publishable</source><note>Field label for node-type publishable status.</note></trans-unit>
Expand Down
8 changes: 8 additions & 0 deletions lib/Rozier/src/Resources/views/redirections/list.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,13 @@
} only %}
</th>
<th>{% trans %}redirection.type{% endtrans %}</th>
<th>
{% trans %}redirection.use_count{% endtrans %}
{% include '@RoadizRozier/includes/column_ordering.html.twig' with {
'field': 'useCount',
'filters': filters,
} only %}
</th>
<th class="table-actions-row table-actions-row-3">{% trans %}actions{% endtrans %}</th>
</tr>
</thead>
Expand All @@ -66,6 +73,7 @@
{% endif %}
</td>
<td>{{ item.typeAsString|trans }}</td>
<td>{{ item.useCount }}</td>
<td class="table-actions-row">
{% apply spaceless %}
<a class="uk-button uk-button-content uk-button-small"
Expand Down

0 comments on commit 3a6a38b

Please sign in to comment.