-
Notifications
You must be signed in to change notification settings - Fork 194
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] [Outdated] Added a first implementation of a BreadcrumbRenderer #45
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,160 @@ | ||
<?php | ||
|
||
namespace Knp\Menu\Renderer; | ||
|
||
use Knp\Menu\ItemInterface; | ||
|
||
/** | ||
* Renders MenuItem tree as a breadcrumb | ||
*/ | ||
class BreadcrumbRenderer extends Renderer implements RendererInterface | ||
{ | ||
private $defaultOptions; | ||
|
||
/** | ||
* @param array $defaultOptions | ||
* @param string $charset | ||
*/ | ||
public function __construct(array $defaultOptions = array(), $charset = null) | ||
{ | ||
$this->defaultOptions = array_merge(array( | ||
'current_as_link' => true, | ||
'current_class' => 'current', | ||
'additional_path' => null, | ||
'compressed' => false, | ||
'allow_safe_labels' => false, | ||
'root_attributes' => array(), | ||
), $defaultOptions); | ||
|
||
parent::__construct($charset); | ||
} | ||
|
||
/** | ||
* Renders a menu with the specified renderer. | ||
* | ||
* @param \Knp\Menu\ItemInterface $item | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No need for full namespace as this is added in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This way is used everywhere in KnpMenu because PhpStorm 3 does not support use statement fully in phpdoc (it looks at them when generating the phpdoc but not when reading it), and I wrote the library using PhpStorm 3. I think the version 4 fixes it but I haven't switched to it yet. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (now works pretty good in phpstorm) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I know. The code in master has already been updated to use short class names a long time ago |
||
* @param array $options | ||
* @return string | ||
*/ | ||
public function render(ItemInterface $item, array $options = array()) | ||
{ | ||
$options = array_merge($this->defaultOptions, $options); | ||
|
||
$breadcrumb = $item->getBreadcrumbsArray($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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just curious, why not allowing There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is the reason why there is a bunch of protected methods: you can extend the class to customize it. |
||
} | ||
|
||
/** | ||
* 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 \Knp\Menu\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['current_class']) : 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['current_as_link'])) { | ||
$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 \Knp\Menu\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"; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is just about readability of the html source code, right? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. indeed. This is used to render some nice HTML (which can be changed by setting the |
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
<?php | ||
|
||
namespace Knp\Menu\Tests\Renderer; | ||
|
||
use Knp\Menu\Renderer\BreadcrumbRenderer; | ||
use Knp\Menu\Tests\TestCase; | ||
|
||
class BreadcrumbRendererTest extends TestCase | ||
{ | ||
public function testBreadcrumbRendering() | ||
{ | ||
$renderer = new BreadcrumbRenderer(array('compressed' => true)); | ||
|
||
$expected = '<ul><li>Root li</li><li>Parent 1</li><li>Child 1</li></ul>'; | ||
|
||
$this->assertEquals($expected, $renderer->render($this->ch1)); | ||
} | ||
|
||
public function testAdditionalPath() | ||
{ | ||
$renderer = new BreadcrumbRenderer(array('compressed' => true)); | ||
|
||
$expected = '<ul><li>Root li</li><li>Parent 1</li><li>Child 1</li><li><a href="http://example.com">Foo</a></li></ul>'; | ||
|
||
$this->assertEquals($expected, $renderer->render($this->ch1, array('additional_path' => array('Foo' => 'http://example.com')))); | ||
} | ||
|
||
public function testCurrentLink() | ||
{ | ||
$this->pt1->setUri('foobar')->setCurrent(true); | ||
|
||
$renderer = new BreadcrumbRenderer(array('compressed' => true)); | ||
|
||
$expected = '<ul><li>Root li</li><li class="current"><a href="foobar">Parent 1</a></li><li>Child 1</li></ul>'; | ||
|
||
$this->assertEquals($expected, $renderer->render($this->ch1)); | ||
} | ||
|
||
public function testCurrentNoLink() | ||
{ | ||
$this->pt1->setUri('foobar')->setCurrent(true); | ||
|
||
$renderer = new BreadcrumbRenderer(array('compressed' => true, 'current_as_link' => false)); | ||
|
||
$expected = '<ul><li>Root li</li><li class="current">Parent 1</li><li>Child 1</li></ul>'; | ||
|
||
$this->assertEquals($expected, $renderer->render($this->ch1)); | ||
} | ||
|
||
public function testCurrentCustomClass() | ||
{ | ||
$this->pt1->setCurrent(true); | ||
|
||
$renderer = new BreadcrumbRenderer(array('compressed' => true, 'current_class' => 'foo')); | ||
|
||
$expected = '<ul><li>Root li</li><li class="foo">Parent 1</li><li>Child 1</li></ul>'; | ||
|
||
$this->assertEquals($expected, $renderer->render($this->ch1)); | ||
} | ||
|
||
public function testEscapedLabel() | ||
{ | ||
$this->pt1->setExtra('safe_label', true)->setLabel('<strong>Foo</strong>'); | ||
|
||
$renderer = new BreadcrumbRenderer(array('compressed' => true)); | ||
|
||
$expected = '<ul><li>Root li</li><li><strong>Foo</strong></li><li>Child 1</li></ul>'; | ||
|
||
$this->assertEquals($expected, $renderer->render($this->ch1)); | ||
} | ||
|
||
public function testUnsafeLabel() | ||
{ | ||
$this->pt1->setLabel('<strong>Foo</strong>'); | ||
|
||
$renderer = new BreadcrumbRenderer(array('compressed' => true, 'allow_safe_labels' => true)); | ||
|
||
$expected = '<ul><li>Root li</li><li><strong>Foo</strong></li><li>Child 1</li></ul>'; | ||
|
||
$this->assertEquals($expected, $renderer->render($this->ch1)); | ||
} | ||
|
||
public function testSafeLabel() | ||
{ | ||
$this->pt1->setExtra('safe_label', true)->setLabel('<strong>Foo</strong>'); | ||
|
||
$renderer = new BreadcrumbRenderer(array('compressed' => true, 'allow_safe_labels' => true)); | ||
|
||
$expected = '<ul><li>Root li</li><li><strong>Foo</strong></li><li>Child 1</li></ul>'; | ||
|
||
$this->assertEquals($expected, $renderer->render($this->ch1)); | ||
} | ||
|
||
public function testPrettyRendering() | ||
{ | ||
$renderer = new BreadcrumbRenderer(); | ||
|
||
$rendered = <<<HTML | ||
<ul> | ||
<li> | ||
Root li | ||
</li> | ||
<li> | ||
Parent 1 | ||
</li> | ||
<li> | ||
Child 1 | ||
</li> | ||
</ul> | ||
|
||
HTML; | ||
|
||
$this->assertEquals($rendered, $renderer->render($this->ch1)); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it intended to have those options with underscore? The other renderers have their options called
currentAsLink
,currentClass
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The only place where the library does not follow the Symfony2 standards is for array keys, where underscores should be used (which also leads to a violation of the Twig CS for the same reason). So I'm wondering about changing them to underscores in the v2 of the library, but I'm not sure yet (it would be a BC break)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Imho it should be compatible with the current renderers and break BC with all at once at some version.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well, the master branch of the library is the dev branch for 2.0. I want to think a bit more about this renaming, and about the issues related to the breadcrumb (see other comments) but such renaming would be done not long after the merge. But I will open a separate issue to discuss it