refactoring.
This commit is contained in:
parent
16716097c6
commit
ed9e04db6f
|
@ -42,13 +42,13 @@
|
||||||
|
|
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
'before' => function($laravel)
|
'before' => function()
|
||||||
{
|
{
|
||||||
// Do stuff before every request to your application.
|
// Do stuff before every request to your application.
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|
||||||
'after' => function($laravel, $response)
|
'after' => function($response)
|
||||||
{
|
{
|
||||||
// Do stuff after every request to your application.
|
// Do stuff after every request to your application.
|
||||||
},
|
},
|
||||||
|
|
|
@ -16,28 +16,28 @@
|
||||||
|
|
|
|
||||||
| Here is how to respond to a simple GET request to http://example.com/hello:
|
| Here is how to respond to a simple GET request to http://example.com/hello:
|
||||||
|
|
|
|
||||||
| 'GET /hello' => function($laravel)
|
| 'GET /hello' => function()
|
||||||
| {
|
| {
|
||||||
| return 'Hello World!';
|
| return 'Hello World!';
|
||||||
| }
|
| }
|
||||||
|
|
|
|
||||||
| You can even respond to more than one URI:
|
| You can even respond to more than one URI:
|
||||||
|
|
|
|
||||||
| 'GET /hello, GET /world' => function($laravel)
|
| 'GET /hello, GET /world' => function()
|
||||||
| {
|
| {
|
||||||
| return 'Hello World!';
|
| return 'Hello World!';
|
||||||
| }
|
| }
|
||||||
|
|
|
|
||||||
| It's easy to allow URI wildcards using the (:num) or (:any) place-holders:
|
| It's easy to allow URI wildcards using the (:num) or (:any) place-holders:
|
||||||
|
|
|
|
||||||
| 'GET /hello/(:any)' => function($laravel, $name)
|
| 'GET /hello/(:any)' => function($name)
|
||||||
| {
|
| {
|
||||||
| return "Welcome, $name.";
|
| return "Welcome, $name.";
|
||||||
| }
|
| }
|
||||||
|
|
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
'GET /' => function($laravel)
|
'GET /' => function()
|
||||||
{
|
{
|
||||||
return View::make('home.index');
|
return View::make('home.index');
|
||||||
},
|
},
|
||||||
|
|
|
@ -9,6 +9,14 @@ class Arr {
|
||||||
* also be accessed using JavaScript "dot" style notation. Retrieving items nested
|
* also be accessed using JavaScript "dot" style notation. Retrieving items nested
|
||||||
* in multiple arrays is supported.
|
* in multiple arrays is supported.
|
||||||
*
|
*
|
||||||
|
* <code>
|
||||||
|
* // Get the "name" item from the array
|
||||||
|
* $name = Arr::get(array('name' => 'Fred'), 'name');
|
||||||
|
*
|
||||||
|
* // Get the "age" item from the array, or return 25 if it doesn't exist
|
||||||
|
* $name = Arr::get(array('name' => 'Fred'), 'age', 25);
|
||||||
|
* </code>
|
||||||
|
*
|
||||||
* @param array $array
|
* @param array $array
|
||||||
* @param string $key
|
* @param string $key
|
||||||
* @param mixed $default
|
* @param mixed $default
|
||||||
|
@ -38,6 +46,11 @@ public static function get($array, $key, $default = null)
|
||||||
* a variable depth, such as configuration arrays. Like the Arr::get
|
* a variable depth, such as configuration arrays. Like the Arr::get
|
||||||
* method, JavaScript "dot" syntax is supported.
|
* method, JavaScript "dot" syntax is supported.
|
||||||
*
|
*
|
||||||
|
* <code>
|
||||||
|
* // Set the "name" item to "Fred" in the array
|
||||||
|
* Arr::set($array, 'name', 'Fred');
|
||||||
|
* </code>
|
||||||
|
*
|
||||||
* @param array $array
|
* @param array $array
|
||||||
* @param string $key
|
* @param string $key
|
||||||
* @param mixed $value
|
* @param mixed $value
|
||||||
|
|
|
@ -5,8 +5,6 @@ class Asset {
|
||||||
/**
|
/**
|
||||||
* All of the instantiated asset containers.
|
* All of the instantiated asset containers.
|
||||||
*
|
*
|
||||||
* Asset containers are created through the container method, and are singletons.
|
|
||||||
*
|
|
||||||
* @var array
|
* @var array
|
||||||
*/
|
*/
|
||||||
public $containers = array();
|
public $containers = array();
|
||||||
|
@ -36,6 +34,14 @@ public function __construct(HTML $html)
|
||||||
* Containers provide a convenient method of grouping assets while maintaining
|
* Containers provide a convenient method of grouping assets while maintaining
|
||||||
* expressive code and a clean API.
|
* expressive code and a clean API.
|
||||||
*
|
*
|
||||||
|
* <code>
|
||||||
|
* // Get the default asset container
|
||||||
|
* $container = Asset::container();
|
||||||
|
*
|
||||||
|
* // Get the "footer" asset container
|
||||||
|
* $container = Asset::container('footer');
|
||||||
|
* </code>
|
||||||
|
*
|
||||||
* @param string $container
|
* @param string $container
|
||||||
* @return Asset_Container
|
* @return Asset_Container
|
||||||
*/
|
*/
|
||||||
|
@ -54,6 +60,14 @@ public function container($container = 'default')
|
||||||
*
|
*
|
||||||
* This provides a convenient API, allowing the develop to skip the "container"
|
* This provides a convenient API, allowing the develop to skip the "container"
|
||||||
* method when using the default container.
|
* method when using the default container.
|
||||||
|
*
|
||||||
|
* <code>
|
||||||
|
* // Add a JavaScript file to the default container
|
||||||
|
* Asset::script('jquery', 'js/jquery.js');
|
||||||
|
*
|
||||||
|
* // Get all of the styles from the default container
|
||||||
|
* echo Asset::styles();
|
||||||
|
* </code>
|
||||||
*/
|
*/
|
||||||
public function __call($method, $parameters)
|
public function __call($method, $parameters)
|
||||||
{
|
{
|
||||||
|
@ -169,7 +183,7 @@ public function script($name, $source, $dependencies = array(), $attributes = ar
|
||||||
* @param array $attributes
|
* @param array $attributes
|
||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
private function register($type, $name, $source, $dependencies, $attributes)
|
protected function register($type, $name, $source, $dependencies, $attributes)
|
||||||
{
|
{
|
||||||
$dependencies = (array) $dependencies;
|
$dependencies = (array) $dependencies;
|
||||||
|
|
||||||
|
@ -202,7 +216,7 @@ public function scripts()
|
||||||
* @param string $group
|
* @param string $group
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
private function get_group($group)
|
protected function get_group($group)
|
||||||
{
|
{
|
||||||
if ( ! isset($this->assets[$group]) or count($this->assets[$group]) == 0) return '';
|
if ( ! isset($this->assets[$group]) or count($this->assets[$group]) == 0) return '';
|
||||||
|
|
||||||
|
@ -245,7 +259,7 @@ public function get_script($name)
|
||||||
* @param string $name
|
* @param string $name
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
private function get_asset($group, $name)
|
protected function get_asset($group, $name)
|
||||||
{
|
{
|
||||||
if ( ! isset($this->assets[$group][$name])) return '';
|
if ( ! isset($this->assets[$group][$name])) return '';
|
||||||
|
|
||||||
|
@ -260,7 +274,7 @@ private function get_asset($group, $name)
|
||||||
* @param array $assets
|
* @param array $assets
|
||||||
* @return array
|
* @return array
|
||||||
*/
|
*/
|
||||||
private function arrange($assets)
|
protected function arrange($assets)
|
||||||
{
|
{
|
||||||
list($original, $sorted) = array($assets, array());
|
list($original, $sorted) = array($assets, array());
|
||||||
|
|
||||||
|
@ -285,7 +299,7 @@ private function arrange($assets)
|
||||||
* @param array $assets
|
* @param array $assets
|
||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
private function evaluate_asset($asset, $value, $original, &$sorted, &$assets)
|
protected function evaluate_asset($asset, $value, $original, &$sorted, &$assets)
|
||||||
{
|
{
|
||||||
// If the asset has no more dependencies, we can add it to the sorted list
|
// If the asset has no more dependencies, we can add it to the sorted list
|
||||||
// and remove it from the array of assets. Otherwise, we will not verify
|
// and remove it from the array of assets. Otherwise, we will not verify
|
||||||
|
@ -327,7 +341,7 @@ private function evaluate_asset($asset, $value, $original, &$sorted, &$assets)
|
||||||
* @param array $assets
|
* @param array $assets
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
private function dependency_is_valid($asset, $dependency, $original, $assets)
|
protected function dependency_is_valid($asset, $dependency, $original, $assets)
|
||||||
{
|
{
|
||||||
if ( ! isset($original[$dependency])) return false;
|
if ( ! isset($original[$dependency])) return false;
|
||||||
|
|
||||||
|
|
|
@ -60,7 +60,7 @@ public function call(Route $route)
|
||||||
return $this->finish($route, $response);
|
return $this->finish($route, $response);
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( ! is_null($response = $route->call($this->container)))
|
if ( ! is_null($response = $route->call()))
|
||||||
{
|
{
|
||||||
if (is_array($response)) $response = $this->delegator->delegate($route, $response);
|
if (is_array($response)) $response = $this->delegator->delegate($route, $response);
|
||||||
|
|
||||||
|
@ -83,7 +83,7 @@ protected function before(Route $route)
|
||||||
{
|
{
|
||||||
$before = array_merge(array('before'), $route->filters('before'));
|
$before = array_merge(array('before'), $route->filters('before'));
|
||||||
|
|
||||||
return $this->filterer->filter($before, array($this->container), true);
|
return $this->filterer->filter($before, array(), true);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -99,7 +99,7 @@ protected function finish(Route $route, $response)
|
||||||
{
|
{
|
||||||
if ( ! $response instanceof Response) $response = new Response($response);
|
if ( ! $response instanceof Response) $response = new Response($response);
|
||||||
|
|
||||||
$this->filterer->filter(array_merge($route->filters('after'), array('after')), array($this->container, $response));
|
$this->filterer->filter(array_merge($route->filters('after'), array('after')), array($response));
|
||||||
|
|
||||||
return $response;
|
return $response;
|
||||||
}
|
}
|
||||||
|
|
|
@ -52,17 +52,15 @@ public function __construct($key, $callback, $parameters = array())
|
||||||
/**
|
/**
|
||||||
* Call the route closure.
|
* Call the route closure.
|
||||||
*
|
*
|
||||||
* If no closure is defined for the route, null will be returned. The IoC container instance will be
|
* If no closure is defined for the route, null will be returned.
|
||||||
* passed to the route closure so it has access to all of the framework components.
|
|
||||||
*
|
*
|
||||||
* @param Container $container
|
|
||||||
* @return mixed
|
* @return mixed
|
||||||
*/
|
*/
|
||||||
public function call(Container $container)
|
public function call()
|
||||||
{
|
{
|
||||||
if (is_null($closure = $this->find_closure())) return;
|
if (is_null($closure = $this->find_closure())) return;
|
||||||
|
|
||||||
return call_user_func_array($closure, array_merge($this->parameters, array($container)));
|
return call_user_func_array($closure, $this->parameters);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -2,7 +2,6 @@
|
||||||
|
|
||||||
class ArrTest extends PHPUnit_Framework_TestCase {
|
class ArrTest extends PHPUnit_Framework_TestCase {
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @dataProvider getArray
|
* @dataProvider getArray
|
||||||
*/
|
*/
|
||||||
|
|
|
@ -0,0 +1,121 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
class AssetTest extends PHPUnit_Framework_TestCase {
|
||||||
|
|
||||||
|
public function testContainerMethodReturnsContainer()
|
||||||
|
{
|
||||||
|
$asset = Laravel\IoC::resolve('laravel.asset');
|
||||||
|
|
||||||
|
$this->assertInstanceOf('Laravel\\Asset_Container', $asset->container());
|
||||||
|
$this->assertInstanceOf('Laravel\\Asset_Container', $asset->container('footer'));
|
||||||
|
|
||||||
|
$this->assertEquals($asset->container()->name, 'default');
|
||||||
|
$this->assertEquals($asset->container('footer')->name, 'footer');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testAssetManagerMagicallyCallsDefaultContainer()
|
||||||
|
{
|
||||||
|
$asset = Laravel\IoC::resolve('laravel.asset');
|
||||||
|
|
||||||
|
$mock = $this->getMockBuilder('Laravel\\Asset_Container')->disableOriginalConstructor()->getMock();
|
||||||
|
|
||||||
|
$mock->expects($this->any())->method('styles')->will($this->returnValue('styles'));
|
||||||
|
|
||||||
|
$asset->containers['default'] = $mock;
|
||||||
|
|
||||||
|
$this->assertEquals($asset->styles(), 'styles');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testAddMethodAddsAssetBasedOnExtension()
|
||||||
|
{
|
||||||
|
$container = $this->getContainer();
|
||||||
|
|
||||||
|
$container->add('jquery', 'js/jquery.js');
|
||||||
|
$container->add('jquery-css', 'css/jquery.css');
|
||||||
|
|
||||||
|
$this->assertEquals($container->assets['script']['jquery']['source'], 'js/jquery.js');
|
||||||
|
$this->assertEquals($container->assets['style']['jquery-css']['source'], 'css/jquery.css');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @dataProvider assetProvider
|
||||||
|
*/
|
||||||
|
public function testStyleMethodRegistersStylesheetAsset($type, $source, $attributes, $testAttributes)
|
||||||
|
{
|
||||||
|
$container = $this->getContainer();
|
||||||
|
|
||||||
|
$dependencies = array('jquery');
|
||||||
|
|
||||||
|
$container->$type('reset', $source, $dependencies, $attributes);
|
||||||
|
|
||||||
|
$this->assertEquals($container->assets[$type]['reset']['source'], $source);
|
||||||
|
$this->assertEquals($container->assets[$type]['reset']['dependencies'], $dependencies);
|
||||||
|
$this->assertEquals($container->assets[$type]['reset']['attributes'], $testAttributes);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function assetProvider()
|
||||||
|
{
|
||||||
|
$attributes = array('test' => 'test');
|
||||||
|
|
||||||
|
return array(
|
||||||
|
array('style', 'css/reset.css', $attributes, array_merge($attributes, array('media' => 'all'))),
|
||||||
|
array('script', 'js/jquery.js', $attributes, $attributes),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testStylesCanBeRetrieved()
|
||||||
|
{
|
||||||
|
$container = new Laravel\Asset_Container('default', new HTMLAssetStub);
|
||||||
|
|
||||||
|
$container->style('reset', 'css/reset.css');
|
||||||
|
$container->style('jquery', 'css/jquery.css');
|
||||||
|
|
||||||
|
$this->assertEquals($container->get_style('reset'), 'css/reset.css media:all');
|
||||||
|
$this->assertEquals($container->styles(), 'css/reset.css media:allcss/jquery.css media:all');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testScriptsCanBeRetrieved()
|
||||||
|
{
|
||||||
|
$container = new Laravel\Asset_Container('default', new HTMLAssetStub);
|
||||||
|
|
||||||
|
$container->script('jquery-ui', 'js/jquery-ui.js');
|
||||||
|
$container->script('jquery', 'js/jquery.js', array(), array('test' => 'value'));
|
||||||
|
|
||||||
|
$this->assertEquals($container->get_script('jquery-ui'), 'js/jquery-ui.js ');
|
||||||
|
$this->assertEquals($container->scripts(), 'js/jquery-ui.js js/jquery.js test:value');
|
||||||
|
}
|
||||||
|
|
||||||
|
private function getContainer()
|
||||||
|
{
|
||||||
|
return new Laravel\Asset_Container('default', Laravel\IoC::resolve('laravel.html'));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
class HTMLAssetStub extends Laravel\HTML {
|
||||||
|
|
||||||
|
public function __construct() {}
|
||||||
|
|
||||||
|
public function style($source, $attributes)
|
||||||
|
{
|
||||||
|
return $source.' '.$this->getAttributes($attributes);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function script($source, $attributes)
|
||||||
|
{
|
||||||
|
return $source.' '.$this->getAttributes($attributes);
|
||||||
|
}
|
||||||
|
|
||||||
|
private function getAttributes($attributes)
|
||||||
|
{
|
||||||
|
$html = '';
|
||||||
|
|
||||||
|
foreach ($attributes as $key => $value)
|
||||||
|
{
|
||||||
|
$html .= $key.':'.$value;
|
||||||
|
}
|
||||||
|
|
||||||
|
return $html;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -2,7 +2,12 @@
|
||||||
|
|
||||||
class ConfigTest extends PHPUnit_Framework_TestCase {
|
class ConfigTest extends PHPUnit_Framework_TestCase {
|
||||||
|
|
||||||
public function setUp()
|
public static function setUpBeforeClass()
|
||||||
|
{
|
||||||
|
IoC::container()->singletons = array();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function tearDown()
|
||||||
{
|
{
|
||||||
IoC::container()->singletons = array();
|
IoC::container()->singletons = array();
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue