Allow foreign code to add template variables

This commit is contained in:
Daniel Siepmann 2021-02-23 16:32:39 +01:00
parent 65f618647b
commit 0256665e82
Signed by: Daniel Siepmann
GPG key ID: 33D6629915560EF4
4 changed files with 160 additions and 4 deletions

View file

@ -21,6 +21,7 @@ namespace WerkraumMedia\Calendar\Controller\Frontend;
* 02110-1301, USA. * 02110-1301, USA.
*/ */
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Annotation as Extbase; use TYPO3\CMS\Extbase\Annotation as Extbase;
use TYPO3\CMS\Extbase\Mvc\Controller\ActionController; use TYPO3\CMS\Extbase\Mvc\Controller\ActionController;
use TYPO3\CMS\Extbase\Property\TypeConverter\DateTimeConverter; use TYPO3\CMS\Extbase\Property\TypeConverter\DateTimeConverter;
@ -28,6 +29,7 @@ use WerkraumMedia\Calendar\Domain\Model\Day;
use WerkraumMedia\Calendar\Domain\Model\Month; use WerkraumMedia\Calendar\Domain\Model\Month;
use WerkraumMedia\Calendar\Domain\Model\Week; use WerkraumMedia\Calendar\Domain\Model\Week;
use WerkraumMedia\Calendar\Domain\Model\Year; use WerkraumMedia\Calendar\Domain\Model\Year;
use WerkraumMedia\Calendar\Events\AssignTemplateVariables;
class CalendarController extends ActionController class CalendarController extends ActionController
{ {
@ -51,7 +53,7 @@ class CalendarController extends ActionController
*/ */
public function yearAction(Year $year) public function yearAction(Year $year)
{ {
$this->view->assignMultiple([ $this->assignVariables([
'year' => $year, 'year' => $year,
]); ]);
} }
@ -77,7 +79,7 @@ class CalendarController extends ActionController
*/ */
public function monthAction(Month $month) public function monthAction(Month $month)
{ {
$this->view->assignMultiple([ $this->assignVariables([
'month' => $month, 'month' => $month,
]); ]);
} }
@ -103,7 +105,7 @@ class CalendarController extends ActionController
*/ */
public function weekAction(Week $week) public function weekAction(Week $week)
{ {
$this->view->assignMultiple([ $this->assignVariables([
'week' => $week, 'week' => $week,
]); ]);
} }
@ -134,8 +136,15 @@ class CalendarController extends ActionController
*/ */
public function dayAction(Day $day) public function dayAction(Day $day)
{ {
$this->view->assignMultiple([ $this->assignVariables([
'day' => $day, 'day' => $day,
]); ]);
} }
private function assignVariables(array $variables): void
{
$event = GeneralUtility::makeInstance(AssignTemplateVariables::class, $variables);
$this->eventDispatcher->dispatch($event);
$this->view->assignMultiple($event->getVariables());
}
} }

View file

@ -0,0 +1,46 @@
<?php
namespace WerkraumMedia\Calendar\Events;
/*
* Copyright (C) 2021 Daniel Siepmann <coding@daniel-siepmann.de>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA.
*/
final class AssignTemplateVariables
{
/**
* @var array
*/
private $variables;
public function __construct(
array $variables
) {
$this->variables = $variables;
}
public function getVariables(): array
{
return $this->variables;
}
public function setVariables(array $variables): void
{
$this->variables = $variables;
}
}

View file

@ -25,6 +25,7 @@ use PHPUnit\Framework\TestCase;
use Prophecy\Argument as ProphetArgument; use Prophecy\Argument as ProphetArgument;
use Prophecy\PhpUnit\ProphecyTrait; use Prophecy\PhpUnit\ProphecyTrait;
use Prophecy\Prophecy\ObjectProphecy; use Prophecy\Prophecy\ObjectProphecy;
use Psr\EventDispatcher\EventDispatcherInterface;
use TYPO3\CMS\Extbase\Mvc\Controller\Argument; use TYPO3\CMS\Extbase\Mvc\Controller\Argument;
use TYPO3\CMS\Extbase\Mvc\Controller\Arguments; use TYPO3\CMS\Extbase\Mvc\Controller\Arguments;
use TYPO3\CMS\Extbase\Mvc\View\ViewInterface; use TYPO3\CMS\Extbase\Mvc\View\ViewInterface;
@ -35,6 +36,7 @@ use WerkraumMedia\Calendar\Domain\Model\Day;
use WerkraumMedia\Calendar\Domain\Model\Month; use WerkraumMedia\Calendar\Domain\Model\Month;
use WerkraumMedia\Calendar\Domain\Model\Week; use WerkraumMedia\Calendar\Domain\Model\Week;
use WerkraumMedia\Calendar\Domain\Model\Year; use WerkraumMedia\Calendar\Domain\Model\Year;
use WerkraumMedia\Calendar\Events\AssignTemplateVariables;
use WerkraumMedia\Calendar\Tests\ForcePropertyTrait; use WerkraumMedia\Calendar\Tests\ForcePropertyTrait;
/** /**
@ -96,6 +98,10 @@ class CalendarControllerTest extends TestCase
{ {
$subject = new CalendarController(); $subject = new CalendarController();
$eventDispatcher = $this->prophesize(EventDispatcherInterface::class);
$eventDispatcher->dispatch(ProphetArgument::type(AssignTemplateVariables::class))->shouldBeCalled();
$subject->injectEventDispatcher($eventDispatcher->reveal());
$year = $this->prophesize(Year::class); $year = $this->prophesize(Year::class);
$view = $this->prophesize(ViewInterface::class); $view = $this->prophesize(ViewInterface::class);
$view->assignMultiple([ $view->assignMultiple([
@ -159,6 +165,10 @@ class CalendarControllerTest extends TestCase
{ {
$subject = new CalendarController(); $subject = new CalendarController();
$eventDispatcher = $this->prophesize(EventDispatcherInterface::class);
$eventDispatcher->dispatch(ProphetArgument::type(AssignTemplateVariables::class))->shouldBeCalled();
$subject->injectEventDispatcher($eventDispatcher->reveal());
$month = $this->prophesize(Month::class); $month = $this->prophesize(Month::class);
$view = $this->prophesize(ViewInterface::class); $view = $this->prophesize(ViewInterface::class);
$view->assignMultiple([ $view->assignMultiple([
@ -222,6 +232,10 @@ class CalendarControllerTest extends TestCase
{ {
$subject = new CalendarController(); $subject = new CalendarController();
$eventDispatcher = $this->prophesize(EventDispatcherInterface::class);
$eventDispatcher->dispatch(ProphetArgument::type(AssignTemplateVariables::class))->shouldBeCalled();
$subject->injectEventDispatcher($eventDispatcher->reveal());
$week = $this->prophesize(Week::class); $week = $this->prophesize(Week::class);
$view = $this->prophesize(ViewInterface::class); $view = $this->prophesize(ViewInterface::class);
$view->assignMultiple([ $view->assignMultiple([
@ -306,6 +320,10 @@ class CalendarControllerTest extends TestCase
{ {
$subject = new CalendarController(); $subject = new CalendarController();
$eventDispatcher = $this->prophesize(EventDispatcherInterface::class);
$eventDispatcher->dispatch(ProphetArgument::type(AssignTemplateVariables::class))->shouldBeCalled();
$subject->injectEventDispatcher($eventDispatcher->reveal());
$day = $this->prophesize(Day::class); $day = $this->prophesize(Day::class);
$view = $this->prophesize(ViewInterface::class); $view = $this->prophesize(ViewInterface::class);
$view->assignMultiple([ $view->assignMultiple([

View file

@ -0,0 +1,83 @@
<?php
namespace WerkraumMedia\Calendar\Tests\Unit\Events;
/*
* Copyright (C) 2021 Daniel Siepmann <coding@daniel-siepmann.de>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA.
*/
use PHPUnit\Framework\TestCase;
use WerkraumMedia\Calendar\Events\AssignTemplateVariables;
/**
* @covers WerkraumMedia\Calendar\Events\AssignTemplateVariables
*/
class AssignTemplateVariablesTest extends TestCase
{
/**
* @test
*/
public function canBeCreated(): void
{
$subject = new AssignTemplateVariables(
[]
);
self::assertInstanceOf(AssignTemplateVariables::class, $subject);
}
/**
* @test
*/
public function returnsVariables(): void
{
$subject = new AssignTemplateVariables(
[
'variable1' => 'value1',
]
);
$result = $subject->getVariables();
self::assertSame([
'variable1' => 'value1',
], $result);
}
/**
* @test
*/
public function newVariablesOverwriteExistingVariables(): void
{
$subject = new AssignTemplateVariables(
[
'variable1' => 'value1',
]
);
$subject->setVariables([
'variable2' => 'value2',
]);
$result = $subject->getVariables();
self::assertSame([
'variable2' => 'value2',
], $result);
}
}