Commit ed9e04db authored by Taylor Otwell's avatar Taylor Otwell

refactoring.

parent 16716097
......@@ -42,13 +42,13 @@ return array(
|
*/
'before' => function($laravel)
'before' => function()
{
// Do stuff before every request to your application.
},
'after' => function($laravel, $response)
'after' => function($response)
{
// Do stuff after every request to your application.
},
......
......@@ -16,28 +16,28 @@ return array(
|
| 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!';
| }
|
| You can even respond to more than one URI:
|
| 'GET /hello, GET /world' => function($laravel)
| 'GET /hello, GET /world' => function()
| {
| return 'Hello World!';
| }
|
| 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.";
| }
|
*/
'GET /' => function($laravel)
'GET /' => function()
{
return View::make('home.index');
},
......
......@@ -9,6 +9,14 @@ class Arr {
* also be accessed using JavaScript "dot" style notation. Retrieving items nested
* 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 string $key
* @param mixed $default
......@@ -38,6 +46,11 @@ class Arr {
* a variable depth, such as configuration arrays. Like the Arr::get
* 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 string $key
* @param mixed $value
......
......@@ -5,8 +5,6 @@ class Asset {
/**
* All of the instantiated asset containers.
*
* Asset containers are created through the container method, and are singletons.
*
* @var array
*/
public $containers = array();
......@@ -36,6 +34,14 @@ class Asset {
* Containers provide a convenient method of grouping assets while maintaining
* 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
* @return Asset_Container
*/
......@@ -54,6 +60,14 @@ class Asset {
*
* This provides a convenient API, allowing the develop to skip the "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)
{
......@@ -169,7 +183,7 @@ class Asset_Container {
* @param array $attributes
* @return void
*/
private function register($type, $name, $source, $dependencies, $attributes)
protected function register($type, $name, $source, $dependencies, $attributes)
{
$dependencies = (array) $dependencies;
......@@ -202,7 +216,7 @@ class Asset_Container {
* @param string $group
* @return string
*/
private function get_group($group)
protected function get_group($group)
{
if ( ! isset($this->assets[$group]) or count($this->assets[$group]) == 0) return '';
......@@ -245,7 +259,7 @@ class Asset_Container {
* @param string $name
* @return string
*/
private function get_asset($group, $name)
protected function get_asset($group, $name)
{
if ( ! isset($this->assets[$group][$name])) return '';
......@@ -260,7 +274,7 @@ class Asset_Container {
* @param array $assets
* @return array
*/
private function arrange($assets)
protected function arrange($assets)
{
list($original, $sorted) = array($assets, array());
......@@ -285,7 +299,7 @@ class Asset_Container {
* @param array $assets
* @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
// and remove it from the array of assets. Otherwise, we will not verify
......@@ -327,7 +341,7 @@ class Asset_Container {
* @param array $assets
* @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;
......
......@@ -60,7 +60,7 @@ class Caller {
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);
......@@ -83,7 +83,7 @@ class Caller {
{
$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 @@ class Caller {
{
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;
}
......
......@@ -52,17 +52,15 @@ class Route {
/**
* Call the route closure.
*
* If no closure is defined for the route, null will be returned. The IoC container instance will be
* passed to the route closure so it has access to all of the framework components.
* If no closure is defined for the route, null will be returned.
*
* @param Container $container
* @return mixed
*/
public function call(Container $container)
public function call()
{
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 {
/**
* @dataProvider getArray
*/
......
<?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;
}
}
\ No newline at end of file
......@@ -2,7 +2,12 @@
class ConfigTest extends PHPUnit_Framework_TestCase {
public function setUp()
public static function setUpBeforeClass()
{
IoC::container()->singletons = array();
}
public function tearDown()
{
IoC::container()->singletons = array();
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment