From d08210b58ee8201ede58290d3a9e5af57cf1ae4b Mon Sep 17 00:00:00 2001 From: Patrick Berenschot Date: Thu, 9 Sep 2021 07:43:13 +0000 Subject: [PATCH 1/3] [SeoBundle] Added event to populate robots.txt --- .../SeoBundle/Controller/RobotsController.php | 32 ++++----- src/Kunstmaan/SeoBundle/Event/RobotsEvent.php | 25 +++++++ .../EventListener/AdminRobotsTxtListener.php | 42 ++++++++++++ .../EventListener/FileRobotsTxtListener.php | 32 +++++++++ .../ParameterRobotsTxtListener.php | 32 +++++++++ .../SeoBundle/Resources/config/services.yml | 23 +++++++ .../SeoBundle/Tests/Event/RobotsEventTest.php | 33 +++++++++ .../AdminRobotsTxtListenerTest.php | 68 +++++++++++++++++++ .../FileRobotsTxtListenerTest.php | 43 ++++++++++++ .../ParameterRobotsTxtListenerTest.php | 33 +++++++++ 10 files changed, 346 insertions(+), 17 deletions(-) create mode 100644 src/Kunstmaan/SeoBundle/Event/RobotsEvent.php create mode 100644 src/Kunstmaan/SeoBundle/EventListener/AdminRobotsTxtListener.php create mode 100644 src/Kunstmaan/SeoBundle/EventListener/FileRobotsTxtListener.php create mode 100644 src/Kunstmaan/SeoBundle/EventListener/ParameterRobotsTxtListener.php create mode 100644 src/Kunstmaan/SeoBundle/Tests/Event/RobotsEventTest.php create mode 100644 src/Kunstmaan/SeoBundle/Tests/EventListener/AdminRobotsTxtListenerTest.php create mode 100644 src/Kunstmaan/SeoBundle/Tests/EventListener/FileRobotsTxtListenerTest.php create mode 100644 src/Kunstmaan/SeoBundle/Tests/EventListener/ParameterRobotsTxtListenerTest.php diff --git a/src/Kunstmaan/SeoBundle/Controller/RobotsController.php b/src/Kunstmaan/SeoBundle/Controller/RobotsController.php index e1e86f6449..b83a9e7e65 100644 --- a/src/Kunstmaan/SeoBundle/Controller/RobotsController.php +++ b/src/Kunstmaan/SeoBundle/Controller/RobotsController.php @@ -2,36 +2,34 @@ namespace Kunstmaan\SeoBundle\Controller; -use Kunstmaan\SeoBundle\Entity\Robots; +use Kunstmaan\SeoBundle\Event\RobotsEvent; use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template; -use Symfony\Bundle\FrameworkBundle\Controller\Controller; +use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\Routing\Annotation\Route; +use Symfony\Contracts\EventDispatcher\EventDispatcherInterface; -class RobotsController extends Controller +final class RobotsController extends AbstractController { + private $dispatcher; + + public function __construct(EventDispatcherInterface $dispatcher) + { + $this->dispatcher = $dispatcher; + } + /** - * Generates the robots.txt content when available in the database and falls back to normal robots.txt if exists - * * @Route(path="/robots.txt", name="KunstmaanSeoBundle_robots", defaults={"_format": "txt"}) * @Template(template="@KunstmaanSeo/Admin/Robots/index.html.twig") * * @return array */ - public function indexAction(Request $request) + public function __invoke(Request $request) { - $entity = $this->getDoctrine()->getRepository(Robots::class)->findOneBy([]); - $robots = $this->getParameter('robots_default'); + $event = new RobotsEvent(); - if ($entity && $entity->getRobotsTxt()) { - $robots = $entity->getRobotsTxt(); - } else { - $file = $request->getBasePath() . 'robots.txt'; - if (file_exists($file)) { - $robots = file_get_contents($file); - } - } + $event = $this->dispatcher->dispatch($event); - return ['robots' => $robots]; + return ['robots' => $event->getContent()]; } } diff --git a/src/Kunstmaan/SeoBundle/Event/RobotsEvent.php b/src/Kunstmaan/SeoBundle/Event/RobotsEvent.php new file mode 100644 index 0000000000..770a9e28b8 --- /dev/null +++ b/src/Kunstmaan/SeoBundle/Event/RobotsEvent.php @@ -0,0 +1,25 @@ +content = $content; + } + + public function setContent(string $content): void + { + $this->content = $content; + } + + public function getContent(): string + { + return $this->content; + } +} diff --git a/src/Kunstmaan/SeoBundle/EventListener/AdminRobotsTxtListener.php b/src/Kunstmaan/SeoBundle/EventListener/AdminRobotsTxtListener.php new file mode 100644 index 0000000000..f32780ba19 --- /dev/null +++ b/src/Kunstmaan/SeoBundle/EventListener/AdminRobotsTxtListener.php @@ -0,0 +1,42 @@ +repository = $repository; + } + + public static function getSubscribedEvents(): array + { + return [ + RobotsEvent::class => ['__invoke', 100], + ]; + } + + public function __invoke(RobotsEvent $event): void + { + $entity = $this->repository->findOneBy([]); + if (!$entity instanceof Robots) { + return; + } + + $content = $entity->getRobotsTxt(); + if ($content === null) { + return; + } + + $event->setContent($content); + } +} diff --git a/src/Kunstmaan/SeoBundle/EventListener/FileRobotsTxtListener.php b/src/Kunstmaan/SeoBundle/EventListener/FileRobotsTxtListener.php new file mode 100644 index 0000000000..28ec63b5c1 --- /dev/null +++ b/src/Kunstmaan/SeoBundle/EventListener/FileRobotsTxtListener.php @@ -0,0 +1,32 @@ +path = $path; + } + + public static function getSubscribedEvents(): array + { + return [ + RobotsEvent::class => ['__invoke', 0], + ]; + } + + public function __invoke(RobotsEvent $event): void + { + if (empty($event->getContent()) && file_exists($this->path)) { + $event->setContent(file_get_contents($this->path)); + } + } +} diff --git a/src/Kunstmaan/SeoBundle/EventListener/ParameterRobotsTxtListener.php b/src/Kunstmaan/SeoBundle/EventListener/ParameterRobotsTxtListener.php new file mode 100644 index 0000000000..2cc0663aec --- /dev/null +++ b/src/Kunstmaan/SeoBundle/EventListener/ParameterRobotsTxtListener.php @@ -0,0 +1,32 @@ +fallback = $fallback; + } + + public static function getSubscribedEvents(): array + { + return [ + RobotsEvent::class => ['__invoke', -100], + ]; + } + + public function __invoke(RobotsEvent $event): void + { + if (empty($event->getContent())) { + $event->setContent($this->fallback); + } + } +} diff --git a/src/Kunstmaan/SeoBundle/Resources/config/services.yml b/src/Kunstmaan/SeoBundle/Resources/config/services.yml index bdc7f1a8d0..cfcfdcdaa9 100644 --- a/src/Kunstmaan/SeoBundle/Resources/config/services.yml +++ b/src/Kunstmaan/SeoBundle/Resources/config/services.yml @@ -39,3 +39,26 @@ services: arguments: ['@security.authorization_checker'] tags: - { name: 'kunstmaan_admin.menu.adaptor' } + + kunstmaanseobundle.repository.robots: + class: Doctrine\ORM\EntityRepository + factory: ["@doctrine.orm.entity_manager", getRepository] + arguments: ['Kunstmaan\SeoBundle\Entity\Robots'] + + Kunstmaan\SeoBundle\EventListener\AdminRobotsTxtListener: + autoconfigure: true + arguments: + $repository: '@kunstmaanseobundle.repository.robots' + + Kunstmaan\SeoBundle\EventListener\FileRobotsTxtListener: + autoconfigure: true + arguments: ["robots.txt", '@request_stack'] + + Kunstmaan\SeoBundle\EventListener\ParameterRobotsTxtListener: + autoconfigure: true + arguments: ['%robots_default%'] + + Kunstmaan\SeoBundle\Controller\RobotsController: + autowire: true + tags: ['controller.service_arguments'] + diff --git a/src/Kunstmaan/SeoBundle/Tests/Event/RobotsEventTest.php b/src/Kunstmaan/SeoBundle/Tests/Event/RobotsEventTest.php new file mode 100644 index 0000000000..0004df12bf --- /dev/null +++ b/src/Kunstmaan/SeoBundle/Tests/Event/RobotsEventTest.php @@ -0,0 +1,33 @@ +getContent(); + $this->assertEquals($initialContent, $result); + + $newContent = "$result\nAdded"; + $object->setContent($newContent); + + $this->assertEquals($newContent, $object->getContent()); + } + + public function testShouldDefaultToEmptyContent(): void + { + $object = new RobotsEvent(); + + $result = $object->getContent(); + $this->assertEquals('', $result); + } +} diff --git a/src/Kunstmaan/SeoBundle/Tests/EventListener/AdminRobotsTxtListenerTest.php b/src/Kunstmaan/SeoBundle/Tests/EventListener/AdminRobotsTxtListenerTest.php new file mode 100644 index 0000000000..e1a9babf2b --- /dev/null +++ b/src/Kunstmaan/SeoBundle/Tests/EventListener/AdminRobotsTxtListenerTest.php @@ -0,0 +1,68 @@ +setRobotsTxt(self::CONTENT); + + $this->repoMock = $this->createMock(EntityRepository::class); + $this->repoMock->expects($this->any()) + ->method('findOneBy') + ->with([]) + ->willReturn($filled); + + $event = new RobotsEvent(); + $listener = new AdminRobotsTxtListener($this->repoMock); + $listener->__invoke($event); + + $this->assertEquals(self::CONTENT, $event->getContent()); + } + + public function testShouldDoNothingWhenEntityMissing() + { + $this->repoMock = $this->createMock(EntityRepository::class); + $this->repoMock->expects($this->any()) + ->method('findOneBy') + ->with([]) + ->willReturn(null); + + $event = new RobotsEvent('untouched'); + $listener = new AdminRobotsTxtListener($this->repoMock); + $listener->__invoke($event); + + $this->assertEquals('untouched', $event->getContent()); + } + + public function testShouldDoNothingWhenEntityEmpty() + { + $empty = new Robots(); + + $this->repoMock = $this->createMock(EntityRepository::class); + $this->repoMock->expects($this->any()) + ->method('findOneBy') + ->with([]) + ->willReturn($empty); + + $event = new RobotsEvent('untouched'); + $listener = new AdminRobotsTxtListener($this->repoMock); + $listener->__invoke($event); + + $this->assertEquals('untouched', $event->getContent()); + } +} diff --git a/src/Kunstmaan/SeoBundle/Tests/EventListener/FileRobotsTxtListenerTest.php b/src/Kunstmaan/SeoBundle/Tests/EventListener/FileRobotsTxtListenerTest.php new file mode 100644 index 0000000000..561481e2e6 --- /dev/null +++ b/src/Kunstmaan/SeoBundle/Tests/EventListener/FileRobotsTxtListenerTest.php @@ -0,0 +1,43 @@ +setContent(self::CONTENT); + $listener = new FileRobotsTxtListener(__FILE__); + $listener->__invoke($event); + + $this->assertEquals(self::CONTENT, $event->getContent()); + } + + public function testShouldSetContentFromFileWhenEmpty(): void + { + $event = new RobotsEvent(); + $listener = new FileRobotsTxtListener(__FILE__); + $listener->__invoke($event); + + $this->assertEquals(file_get_contents(__FILE__), $event->getContent()); + } + + public function testShouldDoNothingWhenFileDoesNotExists(): void + { + $event = new RobotsEvent(); + $listener = new FileRobotsTxtListener('/some/none/existing/file'); + $listener->__invoke($event); + + $this->assertEquals('', $event->getContent()); + } +} diff --git a/src/Kunstmaan/SeoBundle/Tests/EventListener/ParameterRobotsTxtListenerTest.php b/src/Kunstmaan/SeoBundle/Tests/EventListener/ParameterRobotsTxtListenerTest.php new file mode 100644 index 0000000000..e08acd9f97 --- /dev/null +++ b/src/Kunstmaan/SeoBundle/Tests/EventListener/ParameterRobotsTxtListenerTest.php @@ -0,0 +1,33 @@ +__invoke($event); + + $this->assertEquals('fallback content', $event->getContent()); + } + + public function testShouldSetDoNothingWhenFilled() + { + $event = new RobotsEvent(self::CONTENT); + $listener = new ParameterRobotsTxtListener('fallback content'); + $listener->__invoke($event); + + $this->assertEquals(self::CONTENT, $event->getContent()); + } +} From b57049b5d8741f578a9eaf48b7590bcaa97b6688 Mon Sep 17 00:00:00 2001 From: Patrick Berenschot Date: Fri, 10 Sep 2021 10:16:57 +0000 Subject: [PATCH 2/3] [SeoBundle] Fix accidental BC break --- src/Kunstmaan/SeoBundle/Controller/RobotsController.php | 6 +++--- src/Kunstmaan/SeoBundle/Resources/config/services.yml | 2 +- .../Tests/EventListener/AdminRobotsTxtListenerTest.php | 2 +- .../Tests/EventListener/FileRobotsTxtListenerTest.php | 2 +- 4 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/Kunstmaan/SeoBundle/Controller/RobotsController.php b/src/Kunstmaan/SeoBundle/Controller/RobotsController.php index b83a9e7e65..2034be09c5 100644 --- a/src/Kunstmaan/SeoBundle/Controller/RobotsController.php +++ b/src/Kunstmaan/SeoBundle/Controller/RobotsController.php @@ -4,12 +4,12 @@ use Kunstmaan\SeoBundle\Event\RobotsEvent; use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template; -use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; +use Symfony\Bundle\FrameworkBundle\Controller\Controller; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\Routing\Annotation\Route; use Symfony\Contracts\EventDispatcher\EventDispatcherInterface; -final class RobotsController extends AbstractController +class RobotsController extends Controller { private $dispatcher; @@ -24,7 +24,7 @@ public function __construct(EventDispatcherInterface $dispatcher) * * @return array */ - public function __invoke(Request $request) + public function indexAction(Request $request) { $event = new RobotsEvent(); diff --git a/src/Kunstmaan/SeoBundle/Resources/config/services.yml b/src/Kunstmaan/SeoBundle/Resources/config/services.yml index cfcfdcdaa9..b029397ad8 100644 --- a/src/Kunstmaan/SeoBundle/Resources/config/services.yml +++ b/src/Kunstmaan/SeoBundle/Resources/config/services.yml @@ -52,7 +52,7 @@ services: Kunstmaan\SeoBundle\EventListener\FileRobotsTxtListener: autoconfigure: true - arguments: ["robots.txt", '@request_stack'] + arguments: ['robots.txt', '@request_stack'] Kunstmaan\SeoBundle\EventListener\ParameterRobotsTxtListener: autoconfigure: true diff --git a/src/Kunstmaan/SeoBundle/Tests/EventListener/AdminRobotsTxtListenerTest.php b/src/Kunstmaan/SeoBundle/Tests/EventListener/AdminRobotsTxtListenerTest.php index e1a9babf2b..50aae7d0bd 100644 --- a/src/Kunstmaan/SeoBundle/Tests/EventListener/AdminRobotsTxtListenerTest.php +++ b/src/Kunstmaan/SeoBundle/Tests/EventListener/AdminRobotsTxtListenerTest.php @@ -10,7 +10,7 @@ use Kunstmaan\SeoBundle\EventListener\AdminRobotsTxtListener; use PHPUnit\Framework\TestCase; -class AdminRobotsTxtListenerTest extends TestCase +final class AdminRobotsTxtListenerTest extends TestCase { private $repoMock; private const CONTENT = 'User-agent: * diff --git a/src/Kunstmaan/SeoBundle/Tests/EventListener/FileRobotsTxtListenerTest.php b/src/Kunstmaan/SeoBundle/Tests/EventListener/FileRobotsTxtListenerTest.php index 561481e2e6..35a731aa54 100644 --- a/src/Kunstmaan/SeoBundle/Tests/EventListener/FileRobotsTxtListenerTest.php +++ b/src/Kunstmaan/SeoBundle/Tests/EventListener/FileRobotsTxtListenerTest.php @@ -8,7 +8,7 @@ use Kunstmaan\SeoBundle\EventListener\FileRobotsTxtListener; use PHPUnit\Framework\TestCase; -class FileRobotsTxtListenerTest extends TestCase +final class FileRobotsTxtListenerTest extends TestCase { private const CONTENT = 'User-agent: * Allow: /'; From 35e90c5caf0b64f098210d59f7ade21edd657ed5 Mon Sep 17 00:00:00 2001 From: Patrick Berenschot Date: Wed, 15 Sep 2021 09:33:53 +0000 Subject: [PATCH 3/3] [SeoBundle] Make RobotsEvent final --- src/Kunstmaan/SeoBundle/Event/RobotsEvent.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Kunstmaan/SeoBundle/Event/RobotsEvent.php b/src/Kunstmaan/SeoBundle/Event/RobotsEvent.php index 770a9e28b8..5b3e6f6e2f 100644 --- a/src/Kunstmaan/SeoBundle/Event/RobotsEvent.php +++ b/src/Kunstmaan/SeoBundle/Event/RobotsEvent.php @@ -4,7 +4,7 @@ namespace Kunstmaan\SeoBundle\Event; -class RobotsEvent +final class RobotsEvent { private $content;