Permalink
Browse files

Merge pull request #2367 from anlutro-pull-requests/feature/composer-…

…priority

Sample implementation of composer prioritity
  • Loading branch information...
taylorotwell committed Sep 30, 2013
2 parents 6ea8d8b + 809b1f2 commit fb4fe45a9c795e250d046e87f6c3c86a6d01f850
Showing with 37 additions and 7 deletions.
  1. +26 −7 src/Illuminate/View/Environment.php
  2. +11 −0 tests/View/ViewEnvironmentTest.php
@@ -292,13 +292,13 @@ public function creator($views, $callback)
* @param \Closure|string $callback
* @return array
*/
public function composer($views, $callback)
public function composer($views, $callback, $priority = null)
{
$composers = array();
foreach ((array) $views as $view)
{
$composers[] = $this->addViewEvent($view, $callback);
$composers[] = $this->addViewEvent($view, $callback, 'composing: ', $priority);
}
return $composers;
@@ -312,17 +312,17 @@ public function composer($views, $callback)
* @param string $prefix
* @return Closure
*/
protected function addViewEvent($view, $callback, $prefix = 'composing: ')
protected function addViewEvent($view, $callback, $prefix = 'composing: ', $priority = null)
{
if ($callback instanceof Closure)
{
$this->events->listen($prefix.$view, $callback);
$this->addEventListener($prefix.$view, $callback, $priority);
return $callback;
}
elseif (is_string($callback))
{
return $this->addClassEvent($view, $callback, $prefix);
return $this->addClassEvent($view, $callback, $prefix, $priority);
}
}
@@ -334,7 +334,7 @@ protected function addViewEvent($view, $callback, $prefix = 'composing: ')
* @param string $prefix
* @return \Closure
*/
protected function addClassEvent($view, $class, $prefix)
protected function addClassEvent($view, $class, $prefix, $priority = null)
{
$name = $prefix.$view;
@@ -343,11 +343,30 @@ protected function addClassEvent($view, $class, $prefix)
// on the instance. This allows for convenient, testable view composers.
$callback = $this->buildClassEventCallback($class, $prefix);
$this->events->listen($name, $callback);
$this->addEventListener($name, $callback, $priority);
return $callback;
}
/**
* Add a listener to the event dispatcher.
*
* @param string $name
* @param \Closure $callback
* @param integer $priority
*/
protected function addEventListener($name, $callback, $priority = null)
{
if (is_null($priority))
{
$this->events->listen($name, $callback);
}
else
{
$this->events->listen($name, $callback, $priority);
}
}
/**
* Build a class based container callback Closure.
*
@@ -154,6 +154,17 @@ public function testComposersAreProperlyRegistered()
}
public function testComposersAreProperlyRegisteredWithPriority()
{
$env = $this->getEnvironment();
$env->getDispatcher()->shouldReceive('listen')->once()->with('composing: foo', m::type('Closure'), 1);
$callback = $env->composer('foo', function() { return 'bar'; }, 1);
$callback = $callback[0];
$this->assertEquals('bar', $callback());
}
public function testClassCallbacks()
{
$env = $this->getEnvironment();

0 comments on commit fb4fe45

Please sign in to comment.