From 5201933b583bb37977e469e45a555b7a4f693762 Mon Sep 17 00:00:00 2001 From: Joshwin Zuidema Date: Mon, 9 Nov 2020 17:11:36 +0100 Subject: [PATCH 01/14] feature/auto-redirect --- .../Entity/AutoRedirectInterface.php | 7 + .../RedirectBundle/Entity/Redirect.php | 111 ++++------ .../EventSubscriber/RedirectSubscriber.php | 192 ++++++++++++++++++ .../Resources/config/services.yml | 4 + 4 files changed, 241 insertions(+), 73 deletions(-) create mode 100644 src/Kunstmaan/RedirectBundle/Entity/AutoRedirectInterface.php create mode 100644 src/Kunstmaan/RedirectBundle/EventSubscriber/RedirectSubscriber.php diff --git a/src/Kunstmaan/RedirectBundle/Entity/AutoRedirectInterface.php b/src/Kunstmaan/RedirectBundle/Entity/AutoRedirectInterface.php new file mode 100644 index 0000000000..9b0a7b810d --- /dev/null +++ b/src/Kunstmaan/RedirectBundle/Entity/AutoRedirectInterface.php @@ -0,0 +1,7 @@ +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; } /** @@ -176,7 +141,7 @@ public function setNote($note) * * @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') diff --git a/src/Kunstmaan/RedirectBundle/EventSubscriber/RedirectSubscriber.php b/src/Kunstmaan/RedirectBundle/EventSubscriber/RedirectSubscriber.php new file mode 100644 index 0000000000..04ecf71dd3 --- /dev/null +++ b/src/Kunstmaan/RedirectBundle/EventSubscriber/RedirectSubscriber.php @@ -0,0 +1,192 @@ + + */ + private $redirects = []; + + public function __construct(RequestStack $requestStack) + { + $this->requestStack = $requestStack; + } + + public function getSubscribedEvents(): array + { + return [ + Events::onFlush, + ]; + } + + public function onFlush(OnFlushEventArgs $onFlushEventArgs): void + { + $entityManager = $onFlushEventArgs->getEntityManager(); + $unitOfWork = $entityManager->getUnitOfWork(); + + if (!$entityManager instanceof EntityManager) { + return; + } + + foreach ($unitOfWork->getScheduledEntityUpdates() as $entity) { + if (!$entity instanceof NodeTranslation) { + continue; + } + + $this->createAutoRedirect( + $entity, + $entityManager, + $unitOfWork + ); + } + + $unitOfWork->computeChangeSets(); + } + + private function createAutoRedirect( + NodeTranslation $nodeTranslation, + EntityManager $entityManager, + UnitOfWork $unitOfWork + ): void { + $changeSet = $unitOfWork->getEntityChangeSet($nodeTranslation); + + if (!isset($changeSet['url'][0], $changeSet['url'][1])) { + return; + } + + $page = $nodeTranslation->getRef($entityManager); + + if (!$page instanceof AutoRedirectInterface) { + return; + } + + [ + $oldUrl, + $newUrl, + ] = $changeSet['url']; + + $this->processRedirect( + $entityManager, + $oldUrl, + $newUrl + ); + } + + private function processRedirect( + EntityManager $entityManager, + string $oldUrl, + string $newUrl + ): void { + $this->removeOriginRedirects( + $newUrl, + $entityManager + ); + + $this->updateTargetRedirects( + $oldUrl, + $newUrl, + $entityManager + ); + + $this->createRedirect( + $entityManager, + $oldUrl, + $newUrl + ); + } + + private function removeOriginRedirects( + string $newUrl, + EntityManager $entityManager + ): void { + $redirects = $entityManager->getRepository(Redirect::class)->findBy( + [ + 'origin' => $newUrl, + ] + ); + + /** @var Redirect $redirect */ + foreach ($redirects as $redirect) { + $entityManager->remove($redirect); + } + + if (isset($this->redirects[$newUrl])) { + foreach ($this->redirects[$newUrl] as $redirect) { + $entityManager->remove($redirect); + } + } + } + + private function updateTargetRedirects( + string $oldUrl, + string $newUrl, + EntityManager $entityManager + ): void { + $redirects = $entityManager->getRepository(Redirect::class)->findBy( + [ + 'target' => $oldUrl, + ] + ); + + /** @var Redirect $redirect */ + foreach ($redirects as $redirect) { + $redirect->setTarget($newUrl); + $redirect->setIsAutoRedirect(true); + } + } + + private function createRedirect( + EntityManager $entityManager, + string $oldUrl, + string $newUrl + ): void { + $redirect = new Redirect(); + $redirect->setOrigin($oldUrl); + $redirect->setTarget($newUrl); + $redirect->setPermanent(true); + $redirect->setDomain($this->getDomain()); + $redirect->setIsAutoRedirect(true); + + $entityManager->persist($redirect); + + $entityManager->getUnitOfWork()->computeChangeSet( + $entityManager->getClassMetadata(Redirect::class), + $redirect + ); + + if (!isset($this->redirects[$oldUrl])) { + $this->redirects[$oldUrl] = []; + } + + $this->redirects[$oldUrl][] = $redirect; + } + + private function getDomain(): ?string + { + $request = $this->requestStack->getCurrentRequest(); + + if (!$request instanceof Request) { + return null; + } + + return $request->getHost(); + } +} diff --git a/src/Kunstmaan/RedirectBundle/Resources/config/services.yml b/src/Kunstmaan/RedirectBundle/Resources/config/services.yml index a0a6fe4699..0925bb8f51 100644 --- a/src/Kunstmaan/RedirectBundle/Resources/config/services.yml +++ b/src/Kunstmaan/RedirectBundle/Resources/config/services.yml @@ -25,3 +25,7 @@ services: arguments: ['@kunstmaan_admin.domain_configuration'] tags: - { name: form.type, alias: kunstmaan_redirect_form_type } + + Kunstmaan\RedirectBundle\EventSubscriber\RedirectSubscriber: + arguments: + $requestStack: '@request_stack' From fc847af32506511dd0ecafbfc6364f54795bb065 Mon Sep 17 00:00:00 2001 From: Joshwin Zuidema Date: Mon, 9 Nov 2020 17:24:17 +0100 Subject: [PATCH 02/14] feature/auto-redirect --- .../EventSubscriber/RedirectSubscriber.php | 11 +++-------- .../RedirectBundle/Resources/config/services.yml | 2 ++ 2 files changed, 5 insertions(+), 8 deletions(-) diff --git a/src/Kunstmaan/RedirectBundle/EventSubscriber/RedirectSubscriber.php b/src/Kunstmaan/RedirectBundle/EventSubscriber/RedirectSubscriber.php index 04ecf71dd3..b382289dc9 100644 --- a/src/Kunstmaan/RedirectBundle/EventSubscriber/RedirectSubscriber.php +++ b/src/Kunstmaan/RedirectBundle/EventSubscriber/RedirectSubscriber.php @@ -68,7 +68,7 @@ private function createAutoRedirect( ): void { $changeSet = $unitOfWork->getEntityChangeSet($nodeTranslation); - if (!isset($changeSet['url'][0], $changeSet['url'][1])) { + if (!isset($changeSet['slug'])) { return; } @@ -78,15 +78,10 @@ private function createAutoRedirect( return; } - [ - $oldUrl, - $newUrl, - ] = $changeSet['url']; - $this->processRedirect( $entityManager, - $oldUrl, - $newUrl + $nodeTranslation->getUrl(), + $nodeTranslation->getFullSlug() ); } diff --git a/src/Kunstmaan/RedirectBundle/Resources/config/services.yml b/src/Kunstmaan/RedirectBundle/Resources/config/services.yml index 0925bb8f51..2e27087090 100644 --- a/src/Kunstmaan/RedirectBundle/Resources/config/services.yml +++ b/src/Kunstmaan/RedirectBundle/Resources/config/services.yml @@ -29,3 +29,5 @@ services: Kunstmaan\RedirectBundle\EventSubscriber\RedirectSubscriber: arguments: $requestStack: '@request_stack' + tags: + - { name: doctrine.event_subscriber, connection: default } From 53bc41a16c36bb0d36ddeb0470edf9f7c94e08ad Mon Sep 17 00:00:00 2001 From: Joshwin Zuidema Date: Mon, 9 Nov 2020 17:25:07 +0100 Subject: [PATCH 03/14] feature/auto-redirect --- src/Kunstmaan/RedirectBundle/Entity/Redirect.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Kunstmaan/RedirectBundle/Entity/Redirect.php b/src/Kunstmaan/RedirectBundle/Entity/Redirect.php index 2f95cdc801..b0223098a6 100644 --- a/src/Kunstmaan/RedirectBundle/Entity/Redirect.php +++ b/src/Kunstmaan/RedirectBundle/Entity/Redirect.php @@ -60,7 +60,7 @@ class Redirect extends AbstractEntity /** * @var bool * - * @ORM\Column(name="is_auto_redirect", type="boolean") + * @ORM\Column(name="is_auto_redirect", type="boolean", nullable=true) */ private $isAutoRedirect = false; From 01485255071b91c6923dc980a8d1802024891d78 Mon Sep 17 00:00:00 2001 From: Joshwin Zuidema Date: Tue, 10 Nov 2020 09:19:20 +0100 Subject: [PATCH 04/14] feature/auto-redirect --- .../RedirectAdminListConfigurator.php | 133 +++++++++++++----- .../Resources/translations/messages.en.yml | 22 +-- .../Resources/translations/messages.nl.yml | 1 + .../RedirectAdminListConfiguratorTest.php | 18 ++- 4 files changed, 120 insertions(+), 54 deletions(-) diff --git a/src/Kunstmaan/RedirectBundle/AdminList/RedirectAdminListConfigurator.php b/src/Kunstmaan/RedirectBundle/AdminList/RedirectAdminListConfigurator.php index da4bea083f..d75e71a975 100644 --- a/src/Kunstmaan/RedirectBundle/AdminList/RedirectAdminListConfigurator.php +++ b/src/Kunstmaan/RedirectBundle/AdminList/RedirectAdminListConfigurator.php @@ -17,13 +17,19 @@ class RedirectAdminListConfigurator extends AbstractDoctrineORMAdminListConfigur private $domainConfiguration; /** - * @param EntityManager $em The entity manager - * @param AclHelper $aclHelper The acl helper + * @param EntityManager $em The entity manager + * @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; @@ -34,65 +40,118 @@ 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' + ); } /** - * @param array|object $item The item - * @param string $columnName The column name + * @param array|object $item The item + * @param string $columnName The column name * * @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); + 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'; } diff --git a/src/Kunstmaan/RedirectBundle/Resources/translations/messages.en.yml b/src/Kunstmaan/RedirectBundle/Resources/translations/messages.en.yml index 6ee5eb6036..baaaeebd14 100644 --- a/src/Kunstmaan/RedirectBundle/Resources/translations/messages.en.yml +++ b/src/Kunstmaan/RedirectBundle/Resources/translations/messages.en.yml @@ -19,14 +19,16 @@ redirect: adminlist: header: - domain: Domain - origin: Origin - target: Target - permanent: Permanent? - note: Note + domain: Domain + origin: Origin + target: Target + permanent: Permanent? + note: Note + is_auto_redirect: Automatische redirect? filter: - domain: Domain - origin: Origin - target: Target - permanent: Permanent - note: Note + domain: Domain + origin: Origin + target: Target + permanent: Permanent + note: Note + is_auto_redirect: Automatische redirect? diff --git a/src/Kunstmaan/RedirectBundle/Resources/translations/messages.nl.yml b/src/Kunstmaan/RedirectBundle/Resources/translations/messages.nl.yml index 3fc39cf382..726dc59069 100644 --- a/src/Kunstmaan/RedirectBundle/Resources/translations/messages.nl.yml +++ b/src/Kunstmaan/RedirectBundle/Resources/translations/messages.nl.yml @@ -23,6 +23,7 @@ redirect: target: Bestemming permanent: Permanent? note: Notitie + is_auto_redirect: Automatische redirect? filter: domain: Domein origin: Herkomst diff --git a/src/Kunstmaan/RedirectBundle/Tests/unit/AdminList/RedirectAdminListConfiguratorTest.php b/src/Kunstmaan/RedirectBundle/Tests/unit/AdminList/RedirectAdminListConfiguratorTest.php index 0f5b47e9f6..12d09bc0a1 100644 --- a/src/Kunstmaan/RedirectBundle/Tests/unit/AdminList/RedirectAdminListConfiguratorTest.php +++ b/src/Kunstmaan/RedirectBundle/Tests/unit/AdminList/RedirectAdminListConfiguratorTest.php @@ -1,6 +1,6 @@ object = new RedirectAdminListConfigurator($this->em, $this->aclHelper, $domainConfiguration); } - public function testBuildFields() + public function testBuildFields(): void { $this->object->buildFields(); $fields = $this->object->getFields(); - $this->assertCount(4, $fields); + $this->assertCount(5, $fields); $fieldNames = array_map( function (Field $field) { return $field->getName(); }, $fields ); - $this->assertEquals(array('origin', 'target', 'permanent', 'note'), $fieldNames); + $this->assertEquals(['origin', 'target', 'permanent', 'note', 'isAutoRedirect'], $fieldNames); } - public function testBuildFilters() + public function testBuildFilters(): void { $filterBuilder = $this->createMock('Kunstmaan\AdminListBundle\AdminList\FilterBuilder'); $filterBuilder @@ -74,16 +74,20 @@ public function testBuildFilters() ->expects($this->at(3)) ->method('add') ->with('note'); + $filterBuilder + ->expects($this->at(4)) + ->method('add') + ->with('isAutoRedirect'); $this->object->setFilterBuilder($filterBuilder); $this->object->buildFilters(); } - public function testGetBundleName() + public function testGetBundleName(): void { $this->assertEquals('KunstmaanRedirectBundle', $this->object->getBundleName()); } - public function testGetEntityName() + public function testGetEntityName(): void { $this->assertEquals('Redirect', $this->object->getEntityName()); } From eef2a7ac0b6052c0ec3d54268cdcacb2caf8d982 Mon Sep 17 00:00:00 2001 From: Joshwin Zuidema Date: Tue, 10 Nov 2020 09:57:53 +0100 Subject: [PATCH 05/14] feature/auto-redirect --- .../AdminList/RedirectAdminListConfigurator.php | 4 ++++ src/Kunstmaan/RedirectBundle/Entity/Redirect.php | 4 ++-- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/src/Kunstmaan/RedirectBundle/AdminList/RedirectAdminListConfigurator.php b/src/Kunstmaan/RedirectBundle/AdminList/RedirectAdminListConfigurator.php index d75e71a975..a16efc30b4 100644 --- a/src/Kunstmaan/RedirectBundle/AdminList/RedirectAdminListConfigurator.php +++ b/src/Kunstmaan/RedirectBundle/AdminList/RedirectAdminListConfigurator.php @@ -140,6 +140,10 @@ public function getValue( return 'All domains'; } + if ($columnName === 'isAutoRedirect' && $item->isAutoRedirect() === null) { + return false; + } + return parent::getValue( $item, $columnName diff --git a/src/Kunstmaan/RedirectBundle/Entity/Redirect.php b/src/Kunstmaan/RedirectBundle/Entity/Redirect.php index b0223098a6..da8260936f 100644 --- a/src/Kunstmaan/RedirectBundle/Entity/Redirect.php +++ b/src/Kunstmaan/RedirectBundle/Entity/Redirect.php @@ -124,12 +124,12 @@ public function setPermanent(bool $permanent): Redirect return $this; } - public function isAutoRedirect(): bool + public function isAutoRedirect(): ?bool { return $this->isAutoRedirect; } - public function setIsAutoRedirect(bool $isAutoRedirect): Redirect + public function setIsAutoRedirect(?bool $isAutoRedirect): Redirect { $this->isAutoRedirect = $isAutoRedirect; From 8b16a3e5b151662b3c937f1065f3884ba2b8a642 Mon Sep 17 00:00:00 2001 From: Joshwin Zuidema Date: Tue, 10 Nov 2020 10:15:18 +0100 Subject: [PATCH 06/14] feature/auto-redirect --- .../EventSubscriber/RedirectSubscriber.php | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/src/Kunstmaan/RedirectBundle/EventSubscriber/RedirectSubscriber.php b/src/Kunstmaan/RedirectBundle/EventSubscriber/RedirectSubscriber.php index b382289dc9..2c6eaa2c17 100644 --- a/src/Kunstmaan/RedirectBundle/EventSubscriber/RedirectSubscriber.php +++ b/src/Kunstmaan/RedirectBundle/EventSubscriber/RedirectSubscriber.php @@ -81,10 +81,25 @@ private function createAutoRedirect( $this->processRedirect( $entityManager, $nodeTranslation->getUrl(), - $nodeTranslation->getFullSlug() + $this->getNewUrl($nodeTranslation) ); } + private function getNewUrl(NodeTranslation $nodeTranslation): string + { + $newUrl = $nodeTranslation->getFullSlug(); + + if (!is_string($newUrl)) { + return '/'; + } + + if ($newUrl[0] !== '/') { + return '/' . $newUrl; + } + + return $newUrl; + } + private function processRedirect( EntityManager $entityManager, string $oldUrl, From feaeac8b67b121bb21df1a017ce4b024f667ac8c Mon Sep 17 00:00:00 2001 From: Joshwin Zuidema Date: Tue, 10 Nov 2020 10:32:02 +0100 Subject: [PATCH 07/14] feature/auto-redirect --- .../EventSubscriber/RedirectSubscriber.php | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/src/Kunstmaan/RedirectBundle/EventSubscriber/RedirectSubscriber.php b/src/Kunstmaan/RedirectBundle/EventSubscriber/RedirectSubscriber.php index 2c6eaa2c17..d506ce96a2 100644 --- a/src/Kunstmaan/RedirectBundle/EventSubscriber/RedirectSubscriber.php +++ b/src/Kunstmaan/RedirectBundle/EventSubscriber/RedirectSubscriber.php @@ -93,11 +93,18 @@ private function getNewUrl(NodeTranslation $nodeTranslation): string return '/'; } - if ($newUrl[0] !== '/') { - return '/' . $newUrl; + return $this->startWithSlash($newUrl); + } + + private function startWithSlash(string $url): string + { + $firstCharacter = $url[0] ?? ''; + + if ($firstCharacter !== '/') { + return '/' . $url; } - return $newUrl; + return $url; } private function processRedirect( @@ -152,7 +159,7 @@ private function updateTargetRedirects( ): void { $redirects = $entityManager->getRepository(Redirect::class)->findBy( [ - 'target' => $oldUrl, + 'target' => $this->startWithSlash($oldUrl), ] ); From 6fa926075a7a1c06b21f84604d2ed765979c066c Mon Sep 17 00:00:00 2001 From: Joshwin Zuidema Date: Tue, 10 Nov 2020 10:53:09 +0100 Subject: [PATCH 08/14] feature/auto-redirect --- .../EventSubscriber/RedirectSubscriber.php | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/src/Kunstmaan/RedirectBundle/EventSubscriber/RedirectSubscriber.php b/src/Kunstmaan/RedirectBundle/EventSubscriber/RedirectSubscriber.php index d506ce96a2..dc043c95fe 100644 --- a/src/Kunstmaan/RedirectBundle/EventSubscriber/RedirectSubscriber.php +++ b/src/Kunstmaan/RedirectBundle/EventSubscriber/RedirectSubscriber.php @@ -107,6 +107,17 @@ private function startWithSlash(string $url): string return $url; } + private function removeSlashAtStart(string $url): string + { + $firstCharacter = $url[0] ?? ''; + + if ($firstCharacter !== '/') { + return $url; + } + + return substr($url, 1); + } + private function processRedirect( EntityManager $entityManager, string $oldUrl, @@ -134,9 +145,11 @@ private function removeOriginRedirects( string $newUrl, EntityManager $entityManager ): void { + $origin = $this->removeSlashAtStart($newUrl); + $redirects = $entityManager->getRepository(Redirect::class)->findBy( [ - 'origin' => $newUrl, + 'origin' => $origin, ] ); @@ -145,8 +158,8 @@ private function removeOriginRedirects( $entityManager->remove($redirect); } - if (isset($this->redirects[$newUrl])) { - foreach ($this->redirects[$newUrl] as $redirect) { + if (isset($this->redirects[$origin])) { + foreach ($this->redirects[$origin] as $redirect) { $entityManager->remove($redirect); } } From 75baa98304ddf2f8c070cfea6444495672630d37 Mon Sep 17 00:00:00 2001 From: Joshwin Zuidema Date: Tue, 10 Nov 2020 11:51:11 +0100 Subject: [PATCH 09/14] feature/auto-redirect --- .../AdminList/RedirectAdminListConfigurator.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Kunstmaan/RedirectBundle/AdminList/RedirectAdminListConfigurator.php b/src/Kunstmaan/RedirectBundle/AdminList/RedirectAdminListConfigurator.php index a16efc30b4..fe0ae02ea9 100644 --- a/src/Kunstmaan/RedirectBundle/AdminList/RedirectAdminListConfigurator.php +++ b/src/Kunstmaan/RedirectBundle/AdminList/RedirectAdminListConfigurator.php @@ -17,8 +17,8 @@ class RedirectAdminListConfigurator extends AbstractDoctrineORMAdminListConfigur private $domainConfiguration; /** - * @param EntityManager $em The entity manager - * @param AclHelper $aclHelper The acl helper + * @param EntityManager $em The entity manager + * @param AclHelper $aclHelper The acl helper * @param DomainConfigurationInterface $domainConfiguration */ public function __construct( @@ -127,8 +127,8 @@ public function buildFilters(): void } /** - * @param array|object $item The item - * @param string $columnName The column name + * @param array|object $item The item + * @param string $columnName The column name * * @return string */ From 48395322633d632cf3b4233001aa176c19cec861 Mon Sep 17 00:00:00 2001 From: Joshwin Zuidema Date: Tue, 10 Nov 2020 12:41:09 +0100 Subject: [PATCH 10/14] feature/auto-redirect --- .../AdminList/RedirectAdminListConfigurator.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Kunstmaan/RedirectBundle/AdminList/RedirectAdminListConfigurator.php b/src/Kunstmaan/RedirectBundle/AdminList/RedirectAdminListConfigurator.php index fe0ae02ea9..814102ae9e 100644 --- a/src/Kunstmaan/RedirectBundle/AdminList/RedirectAdminListConfigurator.php +++ b/src/Kunstmaan/RedirectBundle/AdminList/RedirectAdminListConfigurator.php @@ -17,8 +17,8 @@ class RedirectAdminListConfigurator extends AbstractDoctrineORMAdminListConfigur private $domainConfiguration; /** - * @param EntityManager $em The entity manager - * @param AclHelper $aclHelper The acl helper + * @param EntityManager $em The entity manager + * @param AclHelper $aclHelper The acl helper * @param DomainConfigurationInterface $domainConfiguration */ public function __construct( From c5a8cdf150cb5c798755d4f22e1417d42a3b0596 Mon Sep 17 00:00:00 2001 From: Joshwin Zuidema Date: Tue, 10 Nov 2020 13:12:48 +0100 Subject: [PATCH 11/14] feature/auto-redirect --- .../RedirectBundle/EventSubscriber/RedirectSubscriber.php | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/Kunstmaan/RedirectBundle/EventSubscriber/RedirectSubscriber.php b/src/Kunstmaan/RedirectBundle/EventSubscriber/RedirectSubscriber.php index dc043c95fe..e7787c0e03 100644 --- a/src/Kunstmaan/RedirectBundle/EventSubscriber/RedirectSubscriber.php +++ b/src/Kunstmaan/RedirectBundle/EventSubscriber/RedirectSubscriber.php @@ -78,9 +78,15 @@ private function createAutoRedirect( return; } + $oldUrl = $nodeTranslation->getUrl(); + + if (!is_string($oldUrl)) { + return; + } + $this->processRedirect( $entityManager, - $nodeTranslation->getUrl(), + $oldUrl, $this->getNewUrl($nodeTranslation) ); } From 145c832b59c91237b18efe1c17b5070f05854830 Mon Sep 17 00:00:00 2001 From: Joshwin Zuidema Date: Tue, 10 Nov 2020 16:18:41 +0100 Subject: [PATCH 12/14] feature/auto-redirect --- .../EventSubscriber/RedirectSubscriber.php | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/src/Kunstmaan/RedirectBundle/EventSubscriber/RedirectSubscriber.php b/src/Kunstmaan/RedirectBundle/EventSubscriber/RedirectSubscriber.php index e7787c0e03..49e8df6212 100644 --- a/src/Kunstmaan/RedirectBundle/EventSubscriber/RedirectSubscriber.php +++ b/src/Kunstmaan/RedirectBundle/EventSubscriber/RedirectSubscriber.php @@ -78,15 +78,9 @@ private function createAutoRedirect( return; } - $oldUrl = $nodeTranslation->getUrl(); - - if (!is_string($oldUrl)) { - return; - } - $this->processRedirect( $entityManager, - $oldUrl, + $nodeTranslation->getUrl(), $this->getNewUrl($nodeTranslation) ); } @@ -126,7 +120,7 @@ private function removeSlashAtStart(string $url): string private function processRedirect( EntityManager $entityManager, - string $oldUrl, + ?string $oldUrl, string $newUrl ): void { $this->removeOriginRedirects( @@ -134,6 +128,10 @@ private function processRedirect( $entityManager ); + if (!is_string($oldUrl)) { + return; + } + $this->updateTargetRedirects( $oldUrl, $newUrl, From 7a77d0731b2f86bb8512b6eb0a7912be905df33c Mon Sep 17 00:00:00 2001 From: Joshwin Zuidema Date: Wed, 11 Nov 2020 13:52:46 +0100 Subject: [PATCH 13/14] feature/auto-redirect --- src/Kunstmaan/RedirectBundle/Entity/Redirect.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Kunstmaan/RedirectBundle/Entity/Redirect.php b/src/Kunstmaan/RedirectBundle/Entity/Redirect.php index da8260936f..f1a75642f3 100644 --- a/src/Kunstmaan/RedirectBundle/Entity/Redirect.php +++ b/src/Kunstmaan/RedirectBundle/Entity/Redirect.php @@ -100,7 +100,7 @@ public function setNote(?string $note): Redirect return $this; } - public function getTarget(): string + public function getTarget(): ?string { return $this->target; } From 9a87bd7b8681be97c5c1959ca25977e6cb86afd5 Mon Sep 17 00:00:00 2001 From: Joshwin Zuidema Date: Wed, 18 Nov 2020 09:04:25 +0100 Subject: [PATCH 14/14] feature/auto-redirect --- src/Kunstmaan/RedirectBundle/Entity/Redirect.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Kunstmaan/RedirectBundle/Entity/Redirect.php b/src/Kunstmaan/RedirectBundle/Entity/Redirect.php index f1a75642f3..9e81a948fa 100644 --- a/src/Kunstmaan/RedirectBundle/Entity/Redirect.php +++ b/src/Kunstmaan/RedirectBundle/Entity/Redirect.php @@ -55,7 +55,7 @@ class Redirect extends AbstractEntity * * @ORM\Column(name="permanent", type="boolean") */ - private $permanent; + private $permanent = false; /** * @var bool