Skip to content

Commit

Permalink
feat(domain): add aliases to a domain name
Browse files Browse the repository at this point in the history
Aliases are mapped to a simple php array, there is a little hack to overcome an issue with Doctrine's `simple_array` which maps the `[]` to `null` in database, hence the oddities you can see in the `DomainName` entity.

This also update the "big" regexp we expose with the possible aliases of domain. (see updated tests for example of generated regexp).

The same validation rule as a `Domain` `name` has been added to each `aliases` element but both seems to be ignored by __ZiziAdmin__...

> This fix issue #429 💋
  • Loading branch information
lutangar committed Sep 7, 2021
1 parent 66f133f commit c3f5fd3
Show file tree
Hide file tree
Showing 7 changed files with 101 additions and 5 deletions.
1 change: 1 addition & 0 deletions config/packages/easy_admin.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ easy_admin:
fields:
- { property: name, label: domains.name }
- { property: path, label: domains.path }
- { property: aliases, label: domains.aliases, help: domains.aliases.help }
show:
max_results: 100
fields:
Expand Down
25 changes: 25 additions & 0 deletions migrations/Version20210907130721.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

declare(strict_types=1);

namespace Application\Migrations;

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

final class Version20210907130721 extends AbstractMigration
{
public function up(Schema $schema): void
{
$this->abortIf('mysql' !== $this->connection->getDatabasePlatform()->getName(), 'Migration can only be executed safely on \'mysql\'.');

$this->addSql('ALTER TABLE domain_name ADD aliases LONGTEXT NOT NULL COMMENT \'(DC2Type:simple_array)\'');
}

public function down(Schema $schema): void
{
$this->abortIf('mysql' !== $this->connection->getDatabasePlatform()->getName(), 'Migration can only be executed safely on \'mysql\'.');

$this->addSql('ALTER TABLE domain_name DROP aliases');
}
}
6 changes: 6 additions & 0 deletions src/DataFixtures/DomainFixtures.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,13 @@ public function load(ObjectManager $manager): void
$manager->persist($disMoiDomain);
$this->addReference('dismoi_domain', $disMoiDomain);

$youtubeDomain = new DomainName('youtube.com');
$youtubeDomain->addAlias('m.youtube.com');
$manager->persist($disMoiDomain);
$this->addReference('com_youtube_www', $youtubeDomain);

$domainName1 = new DomainName('first.domainname.fr');
$domainName1->addAlias('alias.first.domainname.fr');
$manager->persist($domainName1);
$this->addReference('first_domain', $domainName1);

Expand Down
56 changes: 56 additions & 0 deletions src/Entity/DomainName.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ class DomainName
{
use ORMBehaviors\Timestampable\Timestampable;

/**
* @see https://github.com/doctrine/orm/issues/4673
*/
public const EMPTY_SIMPLE_ARRAY = [''];

/**
* @var int
*
Expand Down Expand Up @@ -67,6 +72,17 @@ class DomainName
*/
private $sets;

/**
* @var string[]
*
* @ORM\Column(type="simple_array")
*
* @Assert\All({
* @Assert\Regex("/^(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9\-]*[A-Za-z0-9])$/")
* })
*/
private $aliases;

/**
* Domain constructor.
*/
Expand All @@ -75,6 +91,7 @@ public function __construct(string $name = '')
$this->name = $name;
$this->matchingContexts = new ArrayCollection();
$this->sets = new ArrayCollection();
$this->aliases = self::EMPTY_SIMPLE_ARRAY;
}

public function __toString(): string
Expand Down Expand Up @@ -118,6 +135,45 @@ public function getSets(): Collection
return $this->sets;
}

/**
* @param string[] $aliases
*/
public function setAliases(array $aliases = []): self
{
$this->aliases = array_filter($aliases, static function ($alias) {
return '' !== $alias;
}) ?: self::EMPTY_SIMPLE_ARRAY;

return $this;
}

/**
* @return string[]
*/
public function getAliases(): array
{
return array_filter($this->aliases, static function ($alias) {
return '' !== $alias;
});
}

/**
* @return DomainName
*/
public function addAlias(string $alias): self
{
$this->aliases = array_merge($this->aliases, [$alias]);

return $this;
}

public function removeAlias(string $alias): self
{
$this->aliases = array_diff($this->aliases, [$alias]);

return $this;
}

/**
* @return DomainName
*/
Expand Down
12 changes: 9 additions & 3 deletions src/Entity/MatchingContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -266,9 +266,15 @@ public function getFullUrlRegex(Escaper $escaper = null): string

return '('.implode(
'|',
array_map(static function (DomainName $dn) use ($escaper) {
return escape($dn->getFullName(), $escaper);
}, $domains)
array_reduce($domains, static function ($accumulator, DomainName $dn) use ($escaper) {
return array_merge(
$accumulator,
[escape($dn->getFullName(), $escaper)],
array_map(static function (string $alias) use ($escaper) {
return escape($alias, $escaper);
}, $dn->getAliases())
);
}, [])
).')'.$this->urlRegex;
}

Expand Down
4 changes: 2 additions & 2 deletions tests/Entity/MatchingContextTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ public function testItGetFullUrlRegex(): void
/** @var MatchingContext $mc */
$mc = $this->referenceRepository->getReference('mc_with_domain_name');
$regex = $mc->getFullUrlRegex($escaper);
self::assertEquals('(duckduckgo.com|www.bing.com|www.google.fr|www.qwant.com|www.yahoo.com|first.domainname.fr|second.domainname.fr)'.$mc->getUrlRegex(), $regex);
self::assertEquals('(duckduckgo.com|www.bing.com|www.google.fr|www.qwant.com|www.yahoo.com|first.domainname.fr|alias.first.domainname.fr|second.domainname.fr)'.$mc->getUrlRegex(), $regex);

$regex = $mc->getFullUrlRegex();
self::assertEquals('(duckduckgo.com|www.bing.com|www.google.fr|www.qwant.com|www.yahoo.com|first.domainname.fr|second.domainname.fr)'.$mc->getUrlRegex(), $regex);
self::assertEquals('(duckduckgo.com|www.bing.com|www.google.fr|www.qwant.com|www.yahoo.com|first.domainname.fr|alias.first.domainname.fr|second.domainname.fr)'.$mc->getUrlRegex(), $regex);

/** @var MatchingContext $mc */
$mc = $this->referenceRepository->getReference('mc_without_domain_name');
Expand Down
2 changes: 2 additions & 0 deletions translations/messages.fr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ domains:
name: Nom du domaine
path: Chemin
sets: Groupes de domaines liés
aliases: Aliases
aliases.help: "Par exemple <i>m.youtube.com</i> pour <i>www.youtube.com</i>."
sets.nb: Nb de groupes de domaines liés
matchingContexts: Pages cibles liées
matchingContexts.nb: Nb d’ensemble de pages cibles liées
Expand Down

0 comments on commit c3f5fd3

Please sign in to comment.