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

[WIP] Breadcrumb renderer #161

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
30 changes: 30 additions & 0 deletions src/Knp/Menu/Renderer/BaseRenderer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace Knp\Menu\Renderer;

use Knp\Menu\ItemInterface;

abstract class BaseRenderer implements RendererInterface
{
protected $defaultOptions;

public function __construct($defaultOptions)
{
$this->defaultOptions = array_merge(array(
'depth' => null,
'matchingDepth' => null,
'currentAsLink' => true,
'currentClass' => 'current',
'ancestorClass' => 'current_ancestor',
'firstClass' => 'first',
'lastClass' => 'last',
'compressed' => false,
'allow_safe_labels' => false,
'clear_matcher' => true,
'leaf_class' => null,
'branch_class' => null,
), $defaultOptions);
}

public abstract function render(ItemInterface $item, array $options = array());
}
159 changes: 159 additions & 0 deletions src/Knp/Menu/Renderer/BreadcrumbRenderer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
<?php

namespace Knp\Menu\Renderer;

use Knp\Menu\ItemInterface;
use Knp\Menu\Util\MenuManipulator;

/**
* Renders MenuItem tree as a breadcrumb
*/
class BreadcrumbRenderer extends Renderer implements RendererInterface
{
private $itemManipulator;

/**
* @param array $defaultOptions
* @param string $charset
* @param MenuManipulator $menuManipulator
*/
public function __construct(MenuManipulator $itemManipulator, array $defaultOptions = array(), $charset = null)
{
$this->itemManipulator = $itemManipulator;
$defaultOptions = array_merge(array(
'additional_path' => null,
'root_attributes' => array(),
), $defaultOptions);

parent::__construct($defaultOptions, $charset);
}

/**
* Renders a menu with the specified renderer.
*
* @param ItemInterface $item
* @param array $options
* @return string
*/
public function render(ItemInterface $item, array $options = array())
{
$options = array_merge($this->defaultOptions, $options);

$breadcrumb = $this->itemManipulator->getBreadcrumbsArray($item, $options['additional_path']);

if (empty($breadcrumb)) {
return '';
}

return $this->renderBreadcrumb($breadcrumb, $options);
}

/**
* Renders the breadcrumb
*
* @param array $breadcrumb
* @param array $options
* @return string
*/
protected function renderBreadcrumb(array $breadcrumb, array $options)
{
$html = $this->format('<ul'.$this->renderHtmlAttributes($options['root_attributes']).'>', 'ul', 0, $options);
$html .= $this->renderList($breadcrumb, $options);
$html .= $this->format('</ul>', 'ul', 0, $options);

return $html;
}

/**
* Renders the breadcrumb list
*
* @param array $breadcrumb
* @param array $options
* @return string
*/
protected function renderList(array $breadcrumb, array $options)
{
$html = '';
foreach ($breadcrumb as $element) {
$element = array_replace(array('label' => null, 'uri' => null, 'item' => null), $element);
$html .= $this->renderItem($element['label'], $element['uri'], $options, $element['item']);
}

return $html;
}

/**
* @param string $label
* @param string $uri
* @param array $options
* @param ItemInterface|null $item
* @return string
*/
protected function renderItem($label, $uri, array $options, ItemInterface $item = null)
{
$isCurrent = null !== $item && $item->isCurrent();
$attributes = $isCurrent ? array('class' => $options['currentClass']) : array();

// opening li tag
$html = $this->format('<li'.$this->renderHtmlAttributes($attributes).'>', 'li', 1, $options);

// render the text/link inside the li tag
if (null === $uri || ($isCurrent && !$options['currentAsLink'])) {
$content = $this->renderLabel($label, $options, $item);
} else {
$content = sprintf('<a href="%s">%s</a>', $this->escape($uri), $this->renderLabel($label, $options, $item));
}
$html .= $this->format($content, 'link', 1, $options);

// closing li tag
$html .= $this->format('</li>', 'li', 1, $options);

return $html;
}

/**
* @param string $label
* @param array $options
* @param ItemInterface|null $item
* @return string
*/
protected function renderLabel($label, array $options, ItemInterface $item = null)
{
if ($options['allow_safe_labels'] && null !== $item && $item->getExtra('safe_label', false)) {
return $item->getLabel();
}

return $this->escape($label);
}

/**
* If $this->renderCompressed is on, this will apply the necessary
* spacing and line-breaking so that the particular thing being rendered
* makes up its part in a fully-rendered and spaced menu.
*
* @param string $html The html to render in an (un)formatted way
* @param string $type The type [ul,link,li] of thing being rendered
* @param integer $level
* @param array $options
* @return string
*/
protected function format($html, $type, $level, array $options)
{
if ($options['compressed']) {
return $html;
}

switch ($type) {
case 'ul':
case 'link':
$spacing = $level * 4;
break;

case 'li':
$spacing = $level * 4 - 2;
break;
}

return str_repeat(' ', $spacing).$html."\n";
}
}
21 changes: 3 additions & 18 deletions src/Knp/Menu/Renderer/ListRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,9 @@
/**
* Renders MenuItem tree as unordered list
*/
class ListRenderer extends Renderer implements RendererInterface
class ListRenderer extends Renderer
{
protected $matcher;
protected $defaultOptions;

/**
* @param MatcherInterface $matcher
Expand All @@ -21,22 +20,8 @@ class ListRenderer extends Renderer implements RendererInterface
public function __construct(MatcherInterface $matcher, array $defaultOptions = array(), $charset = null)
{
$this->matcher = $matcher;
$this->defaultOptions = array_merge(array(
'depth' => null,
'matchingDepth' => null,
'currentAsLink' => true,
'currentClass' => 'current',
'ancestorClass' => 'current_ancestor',
'firstClass' => 'first',
'lastClass' => 'last',
'compressed' => false,
'allow_safe_labels' => false,
'clear_matcher' => true,
'leaf_class' => null,
'branch_class' => null,
), $defaultOptions);

parent::__construct($charset);

parent::__construct($defaultOptions, $charset);
}

public function render(ItemInterface $item, array $options = array())
Expand Down
6 changes: 4 additions & 2 deletions src/Knp/Menu/Renderer/Renderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,20 @@
define('ENT_SUBSTITUTE', 8);
}

abstract class Renderer
abstract class Renderer extends BaseRenderer
{
protected $charset = 'UTF-8';

/**
* @param string $charset
*/
public function __construct($charset = null)
public function __construct($defaultOptions = array(), $charset = null)
{
if (null !== $charset) {
$this->charset = (string) $charset;
}

parent::__construct($defaultOptions);
}

/**
Expand Down
22 changes: 6 additions & 16 deletions src/Knp/Menu/Renderer/TwigRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,13 @@
use Knp\Menu\ItemInterface;
use Knp\Menu\Matcher\MatcherInterface;

class TwigRenderer implements RendererInterface
class TwigRenderer extends BaseRenderer
{
/**
* @var \Twig_Environment
*/
private $environment;
private $matcher;
private $defaultOptions;

/**
* @param \Twig_Environment $environment
Expand All @@ -24,21 +23,12 @@ public function __construct(\Twig_Environment $environment, $template, MatcherIn
{
$this->environment = $environment;
$this->matcher = $matcher;
$this->defaultOptions = array_merge(array(
'depth' => null,
'matchingDepth' => null,
'currentAsLink' => true,
'currentClass' => 'current',
'ancestorClass' => 'current_ancestor',
'firstClass' => 'first',
'lastClass' => 'last',
'template' => $template,
'compressed' => false,
'allow_safe_labels' => false,
'clear_matcher' => true,
'leaf_class' => null,
'branch_class' => null,

$defaultOptions = array_merge(array(
'template' => $template
), $defaultOptions);

parent::__construct($defaultOptions);
}

public function render(ItemInterface $item, array $options = array())
Expand Down