Skip to content

Instantly share code, notes, and snippets.

@uestla
Created March 9, 2012 22:12
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save uestla/2008990 to your computer and use it in GitHub Desktop.
Save uestla/2008990 to your computer and use it in GitHub Desktop.
common:
factories:
navigationControl:
class: NavigationControl( %appDir%/config/menu.neon, { gallery: @gallery } )
<?php
interface INavigationLoader
{
/** @return Nette\ArrayHash */
function loadNavigation();
}
':Homepage:uvod': Úvod
':Gallery:default':
text: Galerie
items: gallery # navigationLoader
<?php
use Nette\ArrayHash;
class NavigationControl extends BaseControl
{
/** @var INavigationLoader[] */
private $loaders;
/** @var ArrayHash */
private $items;
// ...
function __construct($file, array $loaders = NULL)
{
parent::__construct();
if ($loaders) {
foreach ($loaders as $name => $loader) {
$this->addLoader($name, $loader);
}
}
$this->processItems(NULL, $items = ArrayHash::from( Nette\Utils\Neon::decode( file_get_contents( $file ) ) ));
$this->items = $items;
}
function addLoader($name, INavigationLoader $loader)
{
$this->loaders[$name] = $loader;
}
// ...
private function processItems($parent, ArrayHash $items)
{
foreach ($items as $dest => $item) {
if ($item instanceof ArrayHash) {
$item['parent'] = $parent;
$item['dest'] = $dest;
if (isset($item['items'])) {
if (is_string($item['items'])) {
if (!isset($this->loaders[$item['items']])) {
throw new Nette\InvalidStateException("Unknown loader '{$item['items']}'.");
}
$item['items'] = $this->loaders[ $item['items'] ]->loadNavigation();
if (!count($item['items'])) {
unset($item['items']);
} else {
$this->processItems($item, $item['items']);
}
} elseif ($item['items'] instanceof ArrayHash) {
if (!count($item['items'])) {
unset($item['items']);
} else {
$this->processItems($item, $item['items']);
}
} else {
throw new Nette\InvalidStateException("Either string or Nette\ArrayHash expected, but '" . gettype($item['items']) . "' given.");
}
}
} else {
$items[$dest] = ArrayHash::from(array(
'parent' => $parent,
'dest' => $dest,
'text' => $item,
));
}
}
}
// ...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment