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

[RedirectBundle] Automatically generate redirects #2831

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,15 @@ class RedirectAdminListConfigurator extends AbstractDoctrineORMAdminListConfigur
* @param AclHelper $aclHelper The acl helper
* @param DomainConfigurationInterface $domainConfiguration
*/
public function __construct(EntityManager $em, AclHelper $aclHelper = null, DomainConfigurationInterface $domainConfiguration)
{
parent::__construct($em, $aclHelper);
public function __construct(
EntityManager $em,
AclHelper $aclHelper = null,
DomainConfigurationInterface $domainConfiguration
) {
parent::__construct(
$em,
$aclHelper
);

$this->domainConfiguration = $domainConfiguration;

Expand All @@ -34,32 +40,90 @@ public function __construct(EntityManager $em, AclHelper $aclHelper = null, Doma
/**
* Configure the visible columns
*/
public function buildFields()
public function buildFields(): void
{
if ($this->domainConfiguration->isMultiDomainHost()) {
$this->addField('domain', 'redirect.adminlist.header.domain', true);
$this->addField(
'domain',
'redirect.adminlist.header.domain',
true
);
}
$this->addField('origin', 'redirect.adminlist.header.origin', true);
$this->addField('target', 'redirect.adminlist.header.target', true);
$this->addField('permanent', 'redirect.adminlist.header.permanent', true);
$this->addField('note', 'redirect.adminlist.header.note', true);
$this->addField(
'origin',
'redirect.adminlist.header.origin',
true
);
$this->addField(
'target',
'redirect.adminlist.header.target',
true
);
$this->addField(
'permanent',
'redirect.adminlist.header.permanent',
true
);
$this->addField(
'note',
'redirect.adminlist.header.note',
true
);
$this->addField(
'isAutoRedirect',
'redirect.adminlist.header.is_auto_redirect',
true
);
}

/**
* Build filters for admin list
*/
public function buildFilters()
public function buildFilters(): void
{
if ($this->domainConfiguration->isMultiDomainHost()) {
$hosts = $this->domainConfiguration->getHosts();
$domains = array_combine($hosts, $hosts);
$domains = array_merge(array('' => 'redirect.all'), $domains);
$this->addFilter('domain', new ORM\EnumerationFilterType('domain'), 'redirect.adminlist.filter.domain', $domains);
$domains = array_combine(
$hosts,
$hosts
);
$domains = array_merge(
['' => 'redirect.all'],
$domains
);
$this->addFilter(
'domain',
new ORM\EnumerationFilterType('domain'),
'redirect.adminlist.filter.domain',
$domains
);
}
$this->addFilter('origin', new ORM\StringFilterType('origin'), 'redirect.adminlist.filter.origin');
$this->addFilter('target', new ORM\StringFilterType('target'), 'redirect.adminlist.filter.target');
$this->addFilter('permanent', new ORM\BooleanFilterType('permanent'), 'redirect.adminlist.filter.permanent');
$this->addFilter('note', new ORM\StringFilterType('note'), 'redirect.adminlist.filter.note');

$this->addFilter(
'origin',
new ORM\StringFilterType('origin'),
'redirect.adminlist.filter.origin'
);
$this->addFilter(
'target',
new ORM\StringFilterType('target'),
'redirect.adminlist.filter.target'
);
$this->addFilter(
'permanent',
new ORM\BooleanFilterType('permanent'),
'redirect.adminlist.filter.permanent'
);
$this->addFilter(
'note',
new ORM\StringFilterType('note'),
'redirect.adminlist.filter.note'
);
$this->addFilter(
'isAutoRedirect',
new ORM\BooleanFilterType('isAutoRedirect'),
'redirect.adminlist.filter.is_auto_redirect'
);
}

/**
Expand All @@ -68,31 +132,30 @@ public function buildFilters()
*
* @return string
*/
public function getValue($item, $columnName)
{
if ($columnName == 'domain' && !$item->getDomain()) {
public function getValue(
$item,
$columnName
) {
if ($columnName === 'domain' && !$item->getDomain()) {
return 'All domains';
}

return parent::getValue($item, $columnName);
if ($columnName === 'isAutoRedirect' && $item->isAutoRedirect() === null) {
return false;
}

return parent::getValue(
$item,
$columnName
);
}

/**
* Get bundle name
*
* @return string
*/
public function getBundleName()
public function getBundleName(): string
{
return 'KunstmaanRedirectBundle';
}

/**
* Get entity name
*
* @return string
*/
public function getEntityName()
public function getEntityName(): string
{
return 'Redirect';
}
Expand Down
7 changes: 7 additions & 0 deletions src/Kunstmaan/RedirectBundle/Entity/AutoRedirectInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace Kunstmaan\RedirectBundle\Entity;

interface AutoRedirectInterface
{
}
113 changes: 39 additions & 74 deletions src/Kunstmaan/RedirectBundle/Entity/Redirect.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,29 +21,29 @@
class Redirect extends AbstractEntity
{
/**
* @var string
* @var string|null
*
* @ORM\Column(name="domain", type="string", length=255, nullable=true)
*/
private $domain;

/**
* @var string
* @var string|null
*
* @ORM\Column(name="origin", type="string", length=255)
* @Assert\NotBlank()
*/
private $origin;

/**
* @var string
* @var string|null
*
* @ORM\Column(name="note", type="string", length=255, nullable=true)
*/
private $note;

/**
* @var string
* @var string|null
*
* @ORM\Column(name="target", type="text")
* @Assert\NotBlank()
Expand All @@ -55,128 +55,93 @@ class Redirect extends AbstractEntity
*
* @ORM\Column(name="permanent", type="boolean")
*/
private $permanent;
private $permanent = false;

/**
* Get domain
* @var bool
*
* @return string
* @ORM\Column(name="is_auto_redirect", type="boolean", nullable=true)
*/
public function getDomain()
private $isAutoRedirect = false;

public function getDomain(): ?string
{
return $this->domain;
}

/**
* Set domain
*
* @param string $domain
*
* @return Redirect
*/
public function setDomain($domain)
public function setDomain(?string $domain): Redirect
{
$this->domain = $domain;

return $this;
}

/**
* Set origin
*
* @param string $origin
*
* @return Redirect
*/
public function setOrigin($origin)
public function getOrigin(): ?string
{
return $this->origin;
}

public function setOrigin(?string $origin): Redirect
{
$this->origin = $origin;

return $this;
}

/**
* Get origin
*
* @return string
*/
public function getOrigin()
public function getNote(): ?string
{
return $this->origin;
return $this->note;
}

/**
* Set target
*
* @param string $target
*
* @return Redirect
*/
public function setTarget($target)
public function setNote(?string $note): Redirect
{
$this->target = $target;
$this->note = $note;

return $this;
}

/**
* Get target
*
* @return string
*/
public function getTarget()
public function getTarget(): ?string
{
return $this->target;
}

/**
* Set permanent
*
* @param bool $permanent
*
* @return Redirect
*/
public function setPermanent($permanent)
public function setTarget(?string $target): Redirect
{
$this->permanent = $permanent;
$this->target = $target;

return $this;
}

/**
* Get permanent
*
* @return bool
*/
public function isPermanent()
public function isPermanent(): bool
{
return $this->permanent;
}

/**
* @return string
*/
public function getNote()
public function setPermanent(bool $permanent): Redirect
{
return $this->note;
$this->permanent = $permanent;

return $this;
}

/**
* @param string $note
*
* @return Redirect
*/
public function setNote($note)
public function isAutoRedirect(): ?bool
{
$this->note = $note;
return $this->isAutoRedirect;
}

public function setIsAutoRedirect(?bool $isAutoRedirect): Redirect
{
$this->isAutoRedirect = $isAutoRedirect;

return $this;
}

/**
* @Assert\Callback
*
* @param ExecutionContextInterface $context
*/
public function validate(ExecutionContextInterface $context)
public function validate(ExecutionContextInterface $context): void
{
if ($this->getOrigin() === $this->getTarget()) {
$context->buildViolation('errors.redirect.origin_same_as_target')
Expand Down
Loading