Commit 82045e20 authored by Taylor Otwell's avatar Taylor Otwell

more refactoring for dependency injection.

parent 6a8aafc2
...@@ -2,6 +2,17 @@ ...@@ -2,6 +2,17 @@
return array( return array(
/*
|--------------------------------------------------------------------------
| Laravel URL Writer
|--------------------------------------------------------------------------
*/
'laravel.url' => array('singleton' => true, 'resolver' => function()
{
return new URL;
}),
/* /*
|-------------------------------------------------------------------------- |--------------------------------------------------------------------------
| Laravel File Cache Driver | Laravel File Cache Driver
...@@ -19,7 +30,7 @@ return array( ...@@ -19,7 +30,7 @@ return array(
|-------------------------------------------------------------------------- |--------------------------------------------------------------------------
*/ */
'laravel.cache.file_engine' => array('resolver' => function($container) 'laravel.cache.file_engine' => array('resolver' => function()
{ {
return new Cache\File_Engine; return new Cache\File_Engine;
}), }),
...@@ -41,7 +52,7 @@ return array( ...@@ -41,7 +52,7 @@ return array(
|-------------------------------------------------------------------------- |--------------------------------------------------------------------------
*/ */
'laravel.cache.apc_engine' => array('resolver' => function($container) 'laravel.cache.apc_engine' => array('resolver' => function()
{ {
return new Cache\APC_Engine; return new Cache\APC_Engine;
}), }),
...@@ -63,7 +74,7 @@ return array( ...@@ -63,7 +74,7 @@ return array(
|-------------------------------------------------------------------------- |--------------------------------------------------------------------------
*/ */
'laravel.memcache' => array('singleton' => true, 'resolver' => function($container) 'laravel.memcache' => array('singleton' => true, 'resolver' => function()
{ {
if ( ! class_exists('Memcache')) if ( ! class_exists('Memcache'))
{ {
......
...@@ -41,6 +41,14 @@ class Container { ...@@ -41,6 +41,14 @@ class Container {
/** /**
* Register a dependency as a singleton. * Register a dependency as a singleton.
* *
* Singletons will only be instantiated the first time they are resolved. On subsequent
* requests for the object, the original instance will be returned.
*
* <code>
* // Register a dependency as a singleton
* $container->singleton('user', function() { return new User; })
* </code>
*
* @param string $name * @param string $name
* @param Closure $resolver * @param Closure $resolver
* @return void * @return void
...@@ -53,6 +61,14 @@ class Container { ...@@ -53,6 +61,14 @@ class Container {
/** /**
* Register an instance as a singleton. * Register an instance as a singleton.
* *
* This method allows you to register an already existing object instance with the
* container as a singleton instance.
*
* <code>
* // Register an object instance as a singleton in the container
* $container->instance('user', new User);
* </code>
*
* @param string $name * @param string $name
* @param mixed $instance * @param mixed $instance
* @return void * @return void
......
...@@ -75,7 +75,9 @@ class Form { ...@@ -75,7 +75,9 @@ class Form {
*/ */
private static function action($action, $https) private static function action($action, $https)
{ {
return HTML::entities(URL::to(((is_null($action)) ? Request::uri() : $action), $https)); $url = IoC::container()->resolve('laravel.url');
return HTML::entities($url->to(((is_null($action)) ? IoC::resolve('laravel.request')->uri() : $action), $https));
} }
/** /**
...@@ -447,7 +449,7 @@ class Form { ...@@ -447,7 +449,7 @@ class Form {
*/ */
public static function image($url, $name = null, $attributes = array()) public static function image($url, $name = null, $attributes = array())
{ {
$attributes['src'] = URL::to_asset($url); $attributes['src'] = IoC::container()->resolve('laravel.url')->to_asset($url);
return static::input('image', $name, null, $attributes); return static::input('image', $name, null, $attributes);
} }
......
...@@ -24,7 +24,9 @@ class HTML { ...@@ -24,7 +24,9 @@ class HTML {
*/ */
public static function script($url, $attributes = array()) public static function script($url, $attributes = array())
{ {
return '<script type="text/javascript" src="'.static::entities(URL::to_asset($url)).'"'.static::attributes($attributes).'></script>'.PHP_EOL; $url = IoC::container()->resolve('laravel.url');
return '<script type="text/javascript" src="'.static::entities($url->to_asset($url)).'"'.static::attributes($attributes).'></script>'.PHP_EOL;
} }
/** /**
...@@ -40,7 +42,9 @@ class HTML { ...@@ -40,7 +42,9 @@ class HTML {
$attributes = array_merge($attributes, array('rel' => 'stylesheet', 'type' => 'text/css')); $attributes = array_merge($attributes, array('rel' => 'stylesheet', 'type' => 'text/css'));
return '<link href="'.static::entities(URL::to_asset($url)).'"'.static::attributes($attributes).'>'.PHP_EOL; $url = IoC::container()->resolve('laravel.url');
return '<link href="'.static::entities($url->to_asset($url)).'"'.static::attributes($attributes).'>'.PHP_EOL;
} }
/** /**
...@@ -67,7 +71,9 @@ class HTML { ...@@ -67,7 +71,9 @@ class HTML {
*/ */
public static function link($url, $title, $attributes = array(), $https = false, $asset = false) public static function link($url, $title, $attributes = array(), $https = false, $asset = false)
{ {
return '<a href="'.static::entities(URL::to($url, $https, $asset)).'"'.static::attributes($attributes).'>'.static::entities($title).'</a>'; $url = IoC::container()->resolve('laravel.url');
return '<a href="'.static::entities($url->to($url, $https, $asset)).'"'.static::attributes($attributes).'>'.static::entities($title).'</a>';
} }
/** /**
...@@ -130,7 +136,7 @@ class HTML { ...@@ -130,7 +136,7 @@ class HTML {
*/ */
public static function link_to_route($name, $title, $parameters = array(), $attributes = array(), $https = false) public static function link_to_route($name, $title, $parameters = array(), $attributes = array(), $https = false)
{ {
return static::link(URL::to_route($name, $parameters, $https), $title, $attributes); return static::link(IoC::resolve('laravel.url')->to_route($name, $parameters, $https), $title, $attributes);
} }
/** /**
...@@ -189,7 +195,7 @@ class HTML { ...@@ -189,7 +195,7 @@ class HTML {
{ {
$attributes['alt'] = static::entities($alt); $attributes['alt'] = static::entities($alt);
return '<img src="'.static::entities(URL::to_asset($url)).'"'.static::attributes($attributes).'>'; return '<img src="'.static::entities(IoC::resolve('laravel.url')->to_asset($url)).'"'.static::attributes($attributes).'>';
} }
/** /**
......
...@@ -136,6 +136,14 @@ class Inflector { ...@@ -136,6 +136,14 @@ class Inflector {
/** /**
* Convert a word to its plural form. * Convert a word to its plural form.
* *
* <code>
* // Returns "friends"
* Inflector::plural('friend');
*
* // Returns "children"
* Inflector::plural('child');
* </code>
*
* @param string $value * @param string $value
* @return string * @return string
*/ */
...@@ -147,6 +155,14 @@ class Inflector { ...@@ -147,6 +155,14 @@ class Inflector {
/** /**
* Convert a word to its singular form. * Convert a word to its singular form.
* *
* <code>
* // Returns "friend"
* Inflector::singular('friends');
*
* // Returns "child"
* Inflector::singular('children');
* </code>
*
* @param string $value * @param string $value
* @return string * @return string
*/ */
......
...@@ -45,11 +45,6 @@ Loader::bootstrap(Config::get('aliases'), array(APP_PATH.'libraries/', APP_PATH. ...@@ -45,11 +45,6 @@ Loader::bootstrap(Config::get('aliases'), array(APP_PATH.'libraries/', APP_PATH.
spl_autoload_register(array('Laravel\\Loader', 'load')); spl_autoload_register(array('Laravel\\Loader', 'load'));
// --------------------------------------------------------------
// Bootstrap the IoC container.
// --------------------------------------------------------------
IoC::bootstrap(Config::get('dependencies'));
// -------------------------------------------------------------- // --------------------------------------------------------------
// Set the error reporting and display levels. // Set the error reporting and display levels.
// -------------------------------------------------------------- // --------------------------------------------------------------
...@@ -98,21 +93,25 @@ register_shutdown_function(function() use ($error_dependencies) ...@@ -98,21 +93,25 @@ register_shutdown_function(function() use ($error_dependencies)
// -------------------------------------------------------------- // --------------------------------------------------------------
date_default_timezone_set(Config::get('application.timezone')); date_default_timezone_set(Config::get('application.timezone'));
// --------------------------------------------------------------
// Load the session.
// --------------------------------------------------------------
if (Config::get('session.driver') != '') Session::driver()->start(Cookie::get('laravel_session'));
// -------------------------------------------------------------- // --------------------------------------------------------------
// Load all of the core routing and response classes. // Load all of the core routing and response classes.
// -------------------------------------------------------------- // --------------------------------------------------------------
require SYS_PATH.'renderable'.EXT;
require SYS_PATH.'response'.EXT; require SYS_PATH.'response'.EXT;
require SYS_PATH.'routing/route'.EXT; require SYS_PATH.'routing/route'.EXT;
require SYS_PATH.'routing/router'.EXT; require SYS_PATH.'routing/router'.EXT;
require SYS_PATH.'routing/loader'.EXT; require SYS_PATH.'routing/loader'.EXT;
require SYS_PATH.'routing/filter'.EXT; require SYS_PATH.'routing/filter'.EXT;
// --------------------------------------------------------------
// Bootstrap the IoC container.
// --------------------------------------------------------------
IoC::bootstrap(Config::get('dependencies'));
// --------------------------------------------------------------
// Load the session.
// --------------------------------------------------------------
if (Config::get('session.driver') != '') Session::driver()->start(Cookie::get('laravel_session'));
// -------------------------------------------------------------- // --------------------------------------------------------------
// Load the packages that are in the auto-loaded packages array. // Load the packages that are in the auto-loaded packages array.
// -------------------------------------------------------------- // --------------------------------------------------------------
...@@ -133,6 +132,11 @@ $request = new Request($_SERVER); ...@@ -133,6 +132,11 @@ $request = new Request($_SERVER);
// -------------------------------------------------------------- // --------------------------------------------------------------
$request->input = new Input($request, $_GET, $_POST, $_COOKIE, $_FILES); $request->input = new Input($request, $_GET, $_POST, $_COOKIE, $_FILES);
// --------------------------------------------------------------
// Register the request as a singleton in the IoC container.
// --------------------------------------------------------------
IoC::container()->instance('laravel.request', $request);
// -------------------------------------------------------------- // --------------------------------------------------------------
// Register the filters for the default module. // Register the filters for the default module.
// -------------------------------------------------------------- // --------------------------------------------------------------
......
...@@ -24,7 +24,7 @@ class Redirect extends Response { ...@@ -24,7 +24,7 @@ class Redirect extends Response {
*/ */
public static function to($url, $status = 302, $method = 'location', $https = false) public static function to($url, $status = 302, $method = 'location', $https = false)
{ {
$url = URL::to($url, $https); $url = IoC::container()->resolve('laravel.url')->to($url, $https);
if ($method == 'location') if ($method == 'location')
{ {
...@@ -93,14 +93,16 @@ class Redirect extends Response { ...@@ -93,14 +93,16 @@ class Redirect extends Response {
{ {
$parameters = (isset($parameters[0])) ? $parameters[0] : array(); $parameters = (isset($parameters[0])) ? $parameters[0] : array();
$url = IoC::container()->resolve('laravel.url');
if (strpos($method, 'to_secure_') === 0) if (strpos($method, 'to_secure_') === 0)
{ {
return static::to(URL::to_route(substr($method, 10), $parameters, true)); return static::to($url->to_route(substr($method, 10), $parameters, true));
} }
if (strpos($method, 'to_') === 0) if (strpos($method, 'to_') === 0)
{ {
return static::to(URL::to_route(substr($method, 3), $parameters)); return static::to($url->to_route(substr($method, 3), $parameters));
} }
throw new \Exception("Method [$method] is not defined on the Redirect class."); throw new \Exception("Method [$method] is not defined on the Redirect class.");
......
<?php namespace Laravel;
interface Renderable {
/**
* Get the evaluated string contents of the object.
*
* @return string
*/
public function render();
}
\ No newline at end of file
<?php namespace Laravel; <?php namespace Laravel;
interface Renderable {
/**
* Get the evaluated string contents of the object.
*
* @return string
*/
public function render();
}
class Response implements Renderable { class Response implements Renderable {
/** /**
......
...@@ -11,7 +11,7 @@ class URL { ...@@ -11,7 +11,7 @@ class URL {
* @param bool $https * @param bool $https
* @return string * @return string
*/ */
public static function to($url = '', $https = false) public function to($url = '', $https = false)
{ {
if (filter_var($url, FILTER_VALIDATE_URL) !== false) return $url; if (filter_var($url, FILTER_VALIDATE_URL) !== false) return $url;
...@@ -31,9 +31,9 @@ class URL { ...@@ -31,9 +31,9 @@ class URL {
* @param string $url * @param string $url
* @return string * @return string
*/ */
public static function to_secure($url = '') public function to_secure($url = '')
{ {
return static::to($url, true); return $this->to($url, true);
} }
/** /**
...@@ -44,9 +44,9 @@ class URL { ...@@ -44,9 +44,9 @@ class URL {
* @param string $url * @param string $url
* @return string * @return string
*/ */
public static function to_asset($url) public function to_asset($url)
{ {
return str_replace('index.php/', '', static::to($url, Request::active()->is_secure())); return str_replace('index.php/', '', $this->to($url, IoC::resolve('laravel.request')->is_secure()));
} }
/** /**
...@@ -57,10 +57,10 @@ class URL { ...@@ -57,10 +57,10 @@ class URL {
* *
* <code> * <code>
* // Generate a URL for the "profile" named route * // Generate a URL for the "profile" named route
* $url = URL::to_route('profile'); * $url = $url->to_route('profile');
* *
* // Generate a URL for the "profile" named route with parameters. * // Generate a URL for the "profile" named route with parameters.
* $url = URL::to_route('profile', array('fred')); * $url = $url->to_route('profile', array('fred'));
* </code> * </code>
* *
* @param string $name * @param string $name
...@@ -68,7 +68,7 @@ class URL { ...@@ -68,7 +68,7 @@ class URL {
* @param bool $https * @param bool $https
* @return string * @return string
*/ */
public static function to_route($name, $parameters = array(), $https = false) public function to_route($name, $parameters = array(), $https = false)
{ {
if ( ! is_null($route = Routing\Finder::find($name, Routing\Loader::all()))) if ( ! is_null($route = Routing\Finder::find($name, Routing\Loader::all())))
{ {
...@@ -83,7 +83,7 @@ class URL { ...@@ -83,7 +83,7 @@ class URL {
$uri = str_replace(array('/(:any?)', '/(:num?)'), '', $uri); $uri = str_replace(array('/(:any?)', '/(:num?)'), '', $uri);
return static::to($uri, $https); return $this->to($uri, $https);
} }
throw new \Exception("Error generating named route for route [$name]. Route is not defined."); throw new \Exception("Error generating named route for route [$name]. Route is not defined.");
...@@ -94,16 +94,16 @@ class URL { ...@@ -94,16 +94,16 @@ class URL {
* *
* <code> * <code>
* // Generate a HTTPS URL for the "profile" named route * // Generate a HTTPS URL for the "profile" named route
* $url = URL::to_secure_route('profile'); * $url = $url->to_secure_route('profile');
* </code> * </code>
* *
* @param string $name * @param string $name
* @param array $parameters * @param array $parameters
* @return string * @return string
*/ */
public static function to_secure_route($name, $parameters = array()) public function to_secure_route($name, $parameters = array())
{ {
return static::to_route($name, $parameters, true); return $this->to_route($name, $parameters, true);
} }
/** /**
...@@ -111,17 +111,17 @@ class URL { ...@@ -111,17 +111,17 @@ class URL {
* *
* <code> * <code>
* // Returns "my-first-post" * // Returns "my-first-post"
* $slug = URL::slug('My First Post!!'); * $slug = $url->slug('My First Post!!');
* *
* // Returns "my_first_post" * // Returns "my_first_post"
* $slug = URL::slug('My First Post!!', '_'); * $slug = $url->slug('My First Post!!', '_');
* </code> * </code>
* *
* @param string $title * @param string $title
* @param string $separator * @param string $separator
* @return string * @return string
*/ */
public static function slug($title, $separator = '-') public function slug($title, $separator = '-')
{ {
$title = Str::ascii($title); $title = Str::ascii($title);
...@@ -139,27 +139,27 @@ class URL { ...@@ -139,27 +139,27 @@ class URL {
* *
* <code> * <code>
* // Generate a URL for the "profile" named route * // Generate a URL for the "profile" named route
* $url = URL::to_profile(); * $url = $url->to_profile();
* *
* // Generate a URL for the "profile" named route using HTTPS * // Generate a URL for the "profile" named route using HTTPS
* $url = URL::to_secure_profile(); * $url = $url->to_secure_profile();
* *
* // Generate a URL for the "profile" named route with parameters. * // Generate a URL for the "profile" named route with parameters.
* $url = URL::to_profile(array('fred')); * $url = $url->to_profile(array('fred'));
* </code> * </code>
*/ */
public static function __callStatic($method, $parameters) public function __call($method, $parameters)
{ {
$parameters = (isset($parameters[0])) ? $parameters[0] : array(); $parameters = (isset($parameters[0])) ? $parameters[0] : array();
if (strpos($method, 'to_secure_') === 0) if (strpos($method, 'to_secure_') === 0)
{ {
return static::to_route(substr($method, 10), $parameters, true); return $this->to_route(substr($method, 10), $parameters, true);
} }
if (strpos($method, 'to_') === 0) if (strpos($method, 'to_') === 0)
{ {
return static::to_route(substr($method, 3), $parameters); return $this->to_route(substr($method, 3), $parameters);
} }
throw new \Exception("Method [$method] is not defined on the URL class."); throw new \Exception("Method [$method] is not defined on the URL class.");
......
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