Skip to content

Commit

Permalink
Merge branch 'master' into 2.1
Browse files Browse the repository at this point in the history
  • Loading branch information
Gravitano committed Dec 11, 2015
2 parents 8e2cbe5 + 282dddd commit 8c88c4c
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 4 deletions.
9 changes: 5 additions & 4 deletions Menu.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,10 @@ public function modify($name, Closure $callback)
*
* @return string|null
*/
public function get($name, $presenter = null)
public function get($name, $presenter = null, $bindings = array())
{
return $this->has($name) ? $this->menus[$name]->render($presenter) : null;
return $this->has($name) ?
$this->menus[$name]->setBindings($bindings)->render($presenter) : null;
}

/**
Expand All @@ -121,9 +122,9 @@ public function get($name, $presenter = null)
*
* @return string
*/
public function render($name, $presenter = null)
public function render($name, $presenter = null, $bindings = array())
{
return $this->get($name, $presenter);
return $this->get($name, $presenter, $bindings);
}

/**
Expand Down
61 changes: 61 additions & 0 deletions MenuBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,13 @@ class MenuBuilder implements Countable
*/
protected $ordering = false;

/**
* Resolved item binding map.
*
* @var array
*/
protected $bindings = array();

/**
* Constructor.
*
Expand Down Expand Up @@ -251,6 +258,58 @@ public function setPresenterFromStyle($name)
$this->setPresenter($this->getStyle($name));
}

/**
* Set the resolved item bindings
*
* @param array $arr
*/
public function setBindings(array $bindings)
{
$this->bindings = $bindings;
return $this;
}

/**
* Resolves a key from the bindings array.
*
* @param string|array $key
* @return mixed
*/
public function resolve($key)
{
if (is_array($key)) {
foreach ($key as $k => $v) {
$key[$k] = $this->resolve($v);
}
} elseif (is_string($key)) {
$matches = array();
preg_match_all('/{[\s]*?([^\s]+)[\s]*?}/i', $key, $matches, PREG_SET_ORDER);
foreach ($matches as $match) {
if (array_key_exists($match[1], $this->bindings)) {
$key = preg_replace('/' . $match[0] . '/', $this->bindings[$match[1]], $key, 1);
}
}
}
return $key;
}

/**
* Resolves an array of menu items properties.
*
* @param array &$items
* @return void
*/
protected function resolveItems(array &$items)
{
$resolver = function ($property) {
return $this->resolve($property) ?: $property;
};

for ($i = 0; $i < count($items); $i++) {
$items[$i]->fill(array_map($resolver, $items[$i]->getProperties()));
}
}

/**
* Add new child menu.
*
Expand Down Expand Up @@ -455,6 +514,8 @@ public function destroy()
*/
public function render($presenter = null)
{
$this->resolveItems($this->items);

if (!is_null($this->view)) {
return $this->renderView($presenter);
}
Expand Down

0 comments on commit 8c88c4c

Please sign in to comment.