Commit ead7b1aa authored by Tino Goratsch's avatar Tino Goratsch

Refactor/breadcrumb (#38)

* first part at refactoring the breadcrumb
- renamed the Breadcrumb class to Steps and moved it into the namespace ACP3\Core\Breadcrumb

* -minor improvement

* reworked the breadcrumb Steps class

* reworked the breadcrumb Steps class (part 2)

* covered the ACP3\Core\Breadcrumb\Title with unit tests
parent d5365cb4
......@@ -10,9 +10,13 @@ use ACP3\Core;
class PageCssClasses
{
/**
* @var \ACP3\Core\Breadcrumb
* @var \ACP3\Core\Breadcrumb\Steps
*/
protected $breadcrumb;
/**
* @var \ACP3\Core\Breadcrumb\Title
*/
protected $title;
/**
* @var \ACP3\Core\Http\RequestInterface
*/
......@@ -24,14 +28,17 @@ class PageCssClasses
protected $details;
/**
* @param \ACP3\Core\Breadcrumb $breadcrumb
* @param \ACP3\Core\Breadcrumb\Steps $breadcrumb
* @param \ACP3\Core\Breadcrumb\Title $title
* @param \ACP3\Core\Http\RequestInterface $request
*/
public function __construct(
Core\Breadcrumb $breadcrumb,
Core\Breadcrumb\Steps $breadcrumb,
Core\Breadcrumb\Title $title,
Core\Http\RequestInterface $request
) {
$this->breadcrumb = $breadcrumb;
$this->title = $title;
$this->request = $request;
}
......@@ -65,7 +72,7 @@ class PageCssClasses
str_replace(
' ',
'-',
strtolower($this->breadcrumb->getPageTitle())
strtolower($this->title->getPageTitle())
),
ENT_QUOTES,
'UTF-8'
......
<?php
/**
* Copyright (c) 2016 by the ACP3 Developers.
* See the LICENCE file at the top-level module directory for licencing details.
*/
namespace ACP3\Core\Breadcrumb\Event;
use ACP3\Core\Breadcrumb\Title;
use Symfony\Component\EventDispatcher\Event;
/**
* Class GetSiteAndPageTitleBeforeEvent
* @package ACP3\Core\Breadcrumb\Event
*/
class GetSiteAndPageTitleBeforeEvent extends Event
{
/**
* @var \ACP3\Core\Breadcrumb\Title
*/
private $title;
/**
* GetSiteAndPageTitleBeforeEvent constructor.
*
* @param \ACP3\Core\Breadcrumb\Title $title
*/
public function __construct(Title $title)
{
$this->title = $title;
}
/**
* @return \ACP3\Core\Breadcrumb\Title
*/
public function getTitle()
{
return $this->title;
}
}
<?php
/**
* Copyright (c) 2016 by the ACP3 Developers.
* See the LICENCE file at the top-level module directory for licencing details.
*/
namespace ACP3\Core\Breadcrumb\Event;
use ACP3\Core\Breadcrumb\Steps;
use Symfony\Component\EventDispatcher\Event;
/**
* Class StepsBuildCacheEvent
* @package ACP3\Core\Breadcrumb\Event
*/
class StepsBuildCacheEvent extends Event
{
/**
* @var \ACP3\Core\Breadcrumb\Steps
*/
private $steps;
/**
* BreadcrumbStepsBuildCacheEvent constructor.
*
* @param \ACP3\Core\Breadcrumb\Steps $steps
*/
public function __construct(Steps $steps)
{
$this->steps = $steps;
}
/**
* @return \ACP3\Core\Breadcrumb\Steps
*/
public function getSteps()
{
return $this->steps;
}
}
......@@ -6,12 +6,24 @@
namespace ACP3\Core\Breadcrumb;
use ACP3\Core\Breadcrumb\Event\GetSiteAndPageTitleBeforeEvent;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
/**
* Class Title
* @package ACP3\Core\Breadcrumb
* @package ACP3\Core\Breadcrumb\Breadcrumb
*/
class Title
{
/**
* @var \ACP3\Core\Breadcrumb\Steps
*/
protected $steps;
/**
* @var \Symfony\Component\EventDispatcher\EventDispatcherInterface
*/
protected $eventDispatcher;
/**
* @var string
*/
......@@ -28,12 +40,23 @@ class Title
* @var string
*/
protected $pageTitleSeparator = '-';
/**
* @var string
*/
protected $siteTitle = '';
/**
* Title constructor.
*
* @param \ACP3\Core\Breadcrumb\Steps $steps
* @param \Symfony\Component\EventDispatcher\EventDispatcherInterface $eventDispatcher
*/
public function __construct(Steps $steps, EventDispatcherInterface $eventDispatcher)
{
$this->steps = $steps;
$this->eventDispatcher = $eventDispatcher;
}
/**
* @return string
*/
......@@ -42,11 +65,30 @@ class Title
return $this->siteTitle;
}
/**
* @param string $title
*
* @return $this
*/
public function setSiteTitle($title)
{
$this->siteTitle = $title;
return $this;
}
/**
* @return string
*/
public function getPageTitle()
{
if (empty($this->pageTitle)) {
$steps = $this->steps->getBreadcrumb();
$lastCrumb = end($steps);
$this->pageTitle = $lastCrumb['title'];
}
return $this->pageTitle;
}
......@@ -121,16 +163,23 @@ class Title
*/
public function getSiteAndPageTitle()
{
$this->eventDispatcher->dispatch(
'core.breadcrumb.title.get_site_and_page_title_before',
new GetSiteAndPageTitleBeforeEvent($this)
);
$title = $this->getPageTitle();
$separator = $this->getPageTitleSeparator();
if (!empty($this->pageTitlePrefix)) {
$title = $this->pageTitlePrefix . $separator . $title;
}
if (!empty($this->pageTitlePostfix)) {
$title .= $separator . $this->pageTitlePostfix;
if (!empty($this->getPageTitlePostfix())) {
$title .= $separator . $this->getPageTitlePostfix();
}
if (!empty($this->getSiteTitle())) {
$title .= ' | ' . $this->getSiteTitle();
}
$title .= ' | ' . $this->getSiteTitle();
return $title;
}
......
......@@ -32,6 +32,7 @@ class AdminContext extends FrontendContext
$context,
$context->getAssets(),
$context->getBreadcrumb(),
$context->getTitle(),
$context->getActionHelper(),
$context->getResponse()
);
......
......@@ -18,31 +18,37 @@ class FrontendContext extends Core\Controller\Context\WidgetContext
/**
* @var \ACP3\Core\Assets
*/
protected $assets;
private $assets;
/**
* @var \ACP3\Core\Breadcrumb
* @var \ACP3\Core\Breadcrumb\Steps
*/
protected $breadcrumb;
private $breadcrumb;
/**
* @var \ACP3\Core\Breadcrumb\Title
*/
private $title;
/**
* @var \ACP3\Core\Modules\Helper\Action
*/
protected $actionHelper;
private $actionHelper;
/**
* @var \Symfony\Component\HttpFoundation\Response
*/
protected $response;
private $response;
/**
* @param \ACP3\Core\Controller\Context\WidgetContext $context
* @param \ACP3\Core\Assets $assets
* @param \ACP3\Core\Breadcrumb $breadcrumb
* @param \ACP3\Core\Breadcrumb\Steps $breadcrumb
* @param \ACP3\Core\Breadcrumb\Title $title
* @param \ACP3\Core\Modules\Helper\Action $actionHelper
* @param \Symfony\Component\HttpFoundation\Response $response
*/
public function __construct(
Core\Controller\Context\WidgetContext $context,
Core\Assets $assets,
Core\Breadcrumb $breadcrumb,
Core\Breadcrumb\Steps $breadcrumb,
Core\Breadcrumb\Title $title,
Core\Modules\Helper\Action $actionHelper,
Response $response
) {
......@@ -63,6 +69,7 @@ class FrontendContext extends Core\Controller\Context\WidgetContext
$this->assets = $assets;
$this->breadcrumb = $breadcrumb;
$this->title = $title;
$this->actionHelper = $actionHelper;
$this->response = $response;
}
......@@ -76,13 +83,21 @@ class FrontendContext extends Core\Controller\Context\WidgetContext
}
/**
* @return \ACP3\Core\Breadcrumb
* @return \ACP3\Core\Breadcrumb\Steps
*/
public function getBreadcrumb()
{
return $this->breadcrumb;
}
/**
* @return \ACP3\Core\Breadcrumb\Title
*/
public function getTitle()
{
return $this->title;
}
/**
* @return Core\Modules\Helper\Action
*/
......
......@@ -21,9 +21,13 @@ abstract class FrontendAction extends Core\Controller\WidgetAction
*/
protected $assets;
/**
* @var \ACP3\Core\Breadcrumb
* @var \ACP3\Core\Breadcrumb\Steps
*/
protected $breadcrumb;
/**
* @var \ACP3\Core\Breadcrumb\Title
*/
protected $title;
/**
* @var Core\Helpers\RedirectMessages
*/
......@@ -46,6 +50,7 @@ abstract class FrontendAction extends Core\Controller\WidgetAction
$this->assets = $context->getAssets();
$this->breadcrumb = $context->getBreadcrumb();
$this->title = $context->getTitle();
$this->actionHelper = $context->getActionHelper();
$this->response = $context->getResponse();
}
......
......@@ -10,9 +10,9 @@ use ACP3\Core;
class TableOfContents
{
/**
* @var \ACP3\Core\Breadcrumb
* @var \ACP3\Core\Breadcrumb\Title
*/
protected $breadcrumb;
protected $title;
/**
* @var \ACP3\Core\I18n\Translator
*/
......@@ -37,7 +37,7 @@ class TableOfContents
/**
* TableOfContents constructor.
*
* @param \ACP3\Core\Breadcrumb $breadcrumb
* @param \ACP3\Core\Breadcrumb\Title $title
* @param \ACP3\Core\I18n\Translator $translator
* @param \ACP3\Core\Http\RequestInterface $request
* @param \ACP3\Core\RouterInterface $router
......@@ -45,14 +45,14 @@ class TableOfContents
* @param \ACP3\Core\View $view
*/
public function __construct(
Core\Breadcrumb $breadcrumb,
Core\Breadcrumb\Title $title,
Core\I18n\Translator $translator,
Core\Http\RequestInterface $request,
Core\RouterInterface $router,
Core\Validation\ValidationRules\IntegerValidationRule $integerValidationRule,
Core\View $view
) {
$this->breadcrumb = $breadcrumb;
$this->title = $title;
$this->translator = $translator;
$this->request = $request;
$this->router = $router;
......@@ -83,7 +83,7 @@ class TableOfContents
$toc[$i]['selected'] = $this->isCurrentPage($customUris, $page, $pageNumber, $i);
if ($toc[$i]['selected'] === true) {
$this->breadcrumb->setPageTitlePostfix($toc[$i]['title']);
$this->title->setPageTitlePostfix($toc[$i]['title']);
}
++$i;
}
......
<?php
namespace ACP3\Core;
use ACP3\Core\Breadcrumb\Title;
use ACP3\Core\Controller\AreaEnum;
use ACP3\Core\Http\RequestInterface;
use ACP3\Core\I18n\Translator;
......@@ -16,9 +17,9 @@ class Pagination
*/
protected $user;
/**
* @var \ACP3\Core\Breadcrumb
* @var \ACP3\Core\Breadcrumb\Title
*/
protected $breadcrumb;
protected $title;
/**
* @var \ACP3\Core\I18n\Translator
*/
......@@ -72,20 +73,20 @@ class Pagination
* Pagination constructor.
*
* @param \ACP3\Core\User $user
* @param \ACP3\Core\Breadcrumb $breadcrumb
* @param \ACP3\Core\Breadcrumb\Title $title
* @param \ACP3\Core\I18n\Translator $translator
* @param \ACP3\Core\Http\RequestInterface $request
* @param \ACP3\Core\RouterInterface $router
*/
public function __construct(
User $user,
Breadcrumb $breadcrumb,
Title $title,
Translator $translator,
RequestInterface $request,
RouterInterface $router
) {
$this->user = $user;
$this->breadcrumb = $breadcrumb;
$this->title = $title;
$this->translator = $translator;
$this->request = $request;
$this->router = $router;
......@@ -177,7 +178,7 @@ class Pagination
{
if ($this->currentPage > 1) {
$postfix = $this->translator->t('system', 'page_x', ['%page%' => $this->currentPage]);
$this->breadcrumb->setPageTitlePostfix($postfix);
$this->title->setPageTitlePostfix($postfix);
}
}
......
<?php
/**
* Copyright (c) 2016 by the ACP3 Developers.
* See the LICENCE file at the top-level module directory for licencing details.
*/
namespace ACP3\Core\Test\Breadcrumb;
use ACP3\Core\Breadcrumb\Steps;
use ACP3\Core\Breadcrumb\Title;
use Symfony\Component\EventDispatcher\EventDispatcher;
class TitleTest extends \PHPUnit_Framework_TestCase
{
/**
* @var \ACP3\Core\Breadcrumb\Title
*/
private $title;
/**
* @var \PHPUnit_Framework_MockObject_MockObject
*/
private $stepsMock;
/**
* @var \PHPUnit_Framework_MockObject_MockObject
*/
private $eventDispatcherMock;
protected function setUp()
{
$this->initializeMockObjects();
$this->title = new Title(
$this->stepsMock,
$this->eventDispatcherMock
);
}
private function initializeMockObjects()
{
$this->stepsMock = $this->getMockBuilder(Steps::class)
->disableOriginalConstructor()
->getMock();
$this->eventDispatcherMock = $this->getMockBuilder(EventDispatcher::class)
->disableOriginalConstructor()
->getMock();
}
public function testGetSiteAndPageTitleWithEmptySiteTitle()
{
$this->setUpStepsExpectations(1);
$this->assertEquals('Foo', $this->title->getSiteAndPageTitle());
}
private function setUpStepsExpectations($callCount)
{
$steps = [
[
'title' => 'Foo',
'uri' => '/foo/bar/baz/'
]
];
$this->stepsMock->expects($this->exactly($callCount))
->method('getBreadcrumb')
->willReturn($steps);
}
public function testGetSiteAndPageTitleWithNotEmptySiteTitle()
{
$this->setUpStepsExpectations(1);
$this->title->setSiteTitle('Lorem Ipsum');
$this->assertEquals('Foo | Lorem Ipsum', $this->title->getSiteAndPageTitle());
}
public function testGetSiteAndPageTitleWithPrefixAndPostfixAndSeparator()
{
$this->setUpStepsExpectations(1);
$this->title
->setSiteTitle('Lorem Ipsum')
->setPageTitlePrefix('ACP')
->setPageTitlePostfix('Page 1')
->setPageTitleSeparator('::');
$expected = 'ACP :: Foo :: Page 1 | Lorem Ipsum';
$this->assertEquals($expected, $this->title->getSiteAndPageTitle());
}
public function testGetSiteAndPageTitleWithCustomPageTitle()
{
$this->setUpStepsExpectations(0);
$this->title
->setSiteTitle('Lorem Ipsum')
->setPageTitle('FooBar');
$expected = 'FooBar | Lorem Ipsum';
$this->assertEquals($expected, $this->title->getSiteAndPageTitle());
}
}
......@@ -12,7 +12,7 @@ class PageTitle extends SiteTitle
*/
public function process(array $params, \Smarty_Internal_Template $smarty)
{
return $this->breadcrumb->getPageTitle();
return $this->title->getPageTitle();
}
/**
......
......@@ -12,7 +12,7 @@ class SiteAndPageTitle extends SiteTitle
*/
public function process(array $params, \Smarty_Internal_Template $smarty)
{
return $this->breadcrumb->getSiteAndPageTitle();
return $this->title->getSiteAndPageTitle();
}
/**
......
......@@ -6,7 +6,7 @@
namespace ACP3\Core\View\Renderer\Smarty\Functions;
use ACP3\Core\Breadcrumb;
use ACP3\Core\Breadcrumb\Title;
/**
* Class SiteTitle
......@@ -15,16 +15,16 @@ use ACP3\Core\Breadcrumb;
class SiteTitle extends AbstractFunction
{
/**
* @var \ACP3\Core\Breadcrumb
* @var \ACP3\Core\Breadcrumb\Title
*/
protected $breadcrumb;
protected $title;
/**
* @param \ACP3\Core\Breadcrumb $breadcrumb
* @param \ACP3\Core\Breadcrumb\Title $title
*/
public function __construct(Breadcrumb $breadcrumb)
public function __construct(Title $title)
{
$this->breadcrumb = $breadcrumb;
$this->title = $title;
}
/**
......@@ -35,7 +35,7 @@ class SiteTitle extends AbstractFunction
*/
public function process(array $params, \Smarty_Internal_Template $smarty)
{
return $this->breadcrumb->getSiteTitle();
return $this->title->getSiteTitle();
}
/**
......
......@@ -73,19 +73,22 @@ services:
smarty.plugin.page_title:
class: ACP3\Core\View\Renderer\Smarty\Functions\PageTitle
arguments: ['@core.breadcrumb']
arguments:
- '@core.breadcrumb.title'
tags:
- { name: core.view.extension }
smarty.plugin.site_title:
class: ACP3\Core\View\Renderer\Smarty\Functions\SiteTitle
arguments: ['@core.breadcrumb']
arguments:
- '@core.breadcrumb.title'
tags:
- { name: core.view.extension }
smarty.plugin.site_and_page_title:
class: ACP3\Core\View\Renderer\Smarty\Functions\SiteAndPageTitle
arguments: ['@core.breadcrumb']
arguments:
- '@core.breadcrumb.title'
tags:
- { name: core.view.extension }
......
......@@ -40,4 +40,5 @@ services:
class: ACP3\Core\Assets\PageCssClasses
arguments:
- '@core.breadcrumb'
- '@core.breadcrumb.title'
- '@core.request'
services:
core.breadcrumb:
class: ACP3\Core\Breadcrumb
class: ACP3\Core\Breadcrumb\Steps
arguments:
- '@service_container'
- '@core.lang'
- '@core.request'
- '@core.router'
- '@core.breadcrumb.title'
- '@core.eventDispatcher'
core.breadcrumb.title:
class: ACP3\Core\Breadcrumb\Title
arguments:
- '@core.eventDispatcher'
- '@core.breadcrumb'
......@@ -29,6 +29,7 @@ services:
- '@core.context.widget'
- '@core.assets'
- '@core.breadcrumb'
- '@core.breadcrumb.title'
- '@core.modules.helper.action'
- '@core.response'
public: false
......
......@@ -67,7 +67,7 @@ services:
core.helpers.toc:
class: ACP3\Core\Helpers\TableOfContents
arguments:
- '@core.breadcrumb'
- '@core.breadcrumb.title'
- '@core.lang'
- '@core.request'
- '@core.router'
......
......@@ -82,7 +82,7 @@ services:
class: ACP3\Core\Pagination
arguments:
- '@core.user'
- '@core.breadcrumb'
- '@core.breadcrumb.title'
- '@core.lang'
- '@core.request'
- '@core.router'
......
<?php
/**
* Copyright (c) 2016 by the ACP3 Developers.
* See the LICENCE file at the top-level module directory for licencing details.
*/
namespace ACP3\Modules\ACP3\Acp\Event\Listener;
use ACP3\Core\Breadcrumb\Event\StepsBuildCacheEvent;
use ACP3\Core\I18n\Translator;
/**
* Class OnBuildAdminCacheEmptyStepsBeforeListener
* @package ACP3\Modules\ACP3\Acp\Event\Listener
*/
class OnBuildAdminCacheEmptyStepsBeforeListener
{
/**
* @var \ACP3\Core\I18n\Translator
*/
private $translator;
/**
* OnBreadcrumbStepsBuildCacheListener constructor.
*
* @param \ACP3\Core\I18n\Translator $translator
*/
public function __construct(Translator $translator)