more refactoring for dependency injection.
This commit is contained in:
parent
6a8aafc259
commit
82045e20d9
|
@ -2,6 +2,17 @@
|
|||
|
||||
return array(
|
||||
|
||||
/*
|
||||
|--------------------------------------------------------------------------
|
||||
| Laravel URL Writer
|
||||
|--------------------------------------------------------------------------
|
||||
*/
|
||||
|
||||
'laravel.url' => array('singleton' => true, 'resolver' => function()
|
||||
{
|
||||
return new URL;
|
||||
}),
|
||||
|
||||
/*
|
||||
|--------------------------------------------------------------------------
|
||||
| Laravel File Cache Driver
|
||||
|
@ -19,7 +30,7 @@
|
|||
|--------------------------------------------------------------------------
|
||||
*/
|
||||
|
||||
'laravel.cache.file_engine' => array('resolver' => function($container)
|
||||
'laravel.cache.file_engine' => array('resolver' => function()
|
||||
{
|
||||
return new Cache\File_Engine;
|
||||
}),
|
||||
|
@ -41,7 +52,7 @@
|
|||
|--------------------------------------------------------------------------
|
||||
*/
|
||||
|
||||
'laravel.cache.apc_engine' => array('resolver' => function($container)
|
||||
'laravel.cache.apc_engine' => array('resolver' => function()
|
||||
{
|
||||
return new Cache\APC_Engine;
|
||||
}),
|
||||
|
@ -63,7 +74,7 @@
|
|||
|--------------------------------------------------------------------------
|
||||
*/
|
||||
|
||||
'laravel.memcache' => array('singleton' => true, 'resolver' => function($container)
|
||||
'laravel.memcache' => array('singleton' => true, 'resolver' => function()
|
||||
{
|
||||
if ( ! class_exists('Memcache'))
|
||||
{
|
||||
|
|
|
@ -41,6 +41,14 @@ public function register($name, $resolver, $singleton = false)
|
|||
/**
|
||||
* 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 Closure $resolver
|
||||
* @return void
|
||||
|
@ -53,6 +61,14 @@ public function singleton($name, $resolver)
|
|||
/**
|
||||
* 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 mixed $instance
|
||||
* @return void
|
||||
|
|
|
@ -30,10 +30,10 @@ class Form {
|
|||
* containing the request method. PUT and DELETE are not supported by HTML forms, so the
|
||||
* hidden field will allow us to "spoof" PUT and DELETE requests.
|
||||
*
|
||||
* @param string $action
|
||||
* @param string $method
|
||||
* @param array $attributes
|
||||
* @param bool $https
|
||||
* @param string $action
|
||||
* @param string $method
|
||||
* @param array $attributes
|
||||
* @param bool $https
|
||||
* @return string
|
||||
*/
|
||||
public static function open($action = null, $method = 'POST', $attributes = array(), $https = false)
|
||||
|
@ -69,13 +69,15 @@ private static function method($method)
|
|||
*
|
||||
* If no action is specified, the current request URI will be used.
|
||||
*
|
||||
* @param string $action
|
||||
* @param bool $https
|
||||
* @param string $action
|
||||
* @param bool $https
|
||||
* @return string
|
||||
*/
|
||||
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 @@ public static function reset($value, $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);
|
||||
}
|
||||
|
|
|
@ -24,7 +24,9 @@ public static function entities($value)
|
|||
*/
|
||||
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 @@ public static function style($url, $attributes = array())
|
|||
|
||||
$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 @@ public static function span($value, $attributes = array())
|
|||
*/
|
||||
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 @@ public static function link_to_secure_asset($url, $title, $attributes = array())
|
|||
*/
|
||||
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 @@ public static function image($url, $alt = '', $attributes = array())
|
|||
{
|
||||
$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 @@ public static function plural_if($value, $count)
|
|||
/**
|
||||
* Convert a word to its plural form.
|
||||
*
|
||||
* <code>
|
||||
* // Returns "friends"
|
||||
* Inflector::plural('friend');
|
||||
*
|
||||
* // Returns "children"
|
||||
* Inflector::plural('child');
|
||||
* </code>
|
||||
*
|
||||
* @param string $value
|
||||
* @return string
|
||||
*/
|
||||
|
@ -147,6 +155,14 @@ public static function plural($value)
|
|||
/**
|
||||
* Convert a word to its singular form.
|
||||
*
|
||||
* <code>
|
||||
* // Returns "friend"
|
||||
* Inflector::singular('friends');
|
||||
*
|
||||
* // Returns "child"
|
||||
* Inflector::singular('children');
|
||||
* </code>
|
||||
*
|
||||
* @param string $value
|
||||
* @return string
|
||||
*/
|
||||
|
|
|
@ -45,11 +45,6 @@
|
|||
|
||||
spl_autoload_register(array('Laravel\\Loader', 'load'));
|
||||
|
||||
// --------------------------------------------------------------
|
||||
// Bootstrap the IoC container.
|
||||
// --------------------------------------------------------------
|
||||
IoC::bootstrap(Config::get('dependencies'));
|
||||
|
||||
// --------------------------------------------------------------
|
||||
// Set the error reporting and display levels.
|
||||
// --------------------------------------------------------------
|
||||
|
@ -98,21 +93,25 @@
|
|||
// --------------------------------------------------------------
|
||||
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.
|
||||
// --------------------------------------------------------------
|
||||
require SYS_PATH.'renderable'.EXT;
|
||||
require SYS_PATH.'response'.EXT;
|
||||
require SYS_PATH.'routing/route'.EXT;
|
||||
require SYS_PATH.'routing/router'.EXT;
|
||||
require SYS_PATH.'routing/loader'.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.
|
||||
// --------------------------------------------------------------
|
||||
|
@ -133,6 +132,11 @@
|
|||
// --------------------------------------------------------------
|
||||
$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.
|
||||
// --------------------------------------------------------------
|
||||
|
|
|
@ -24,7 +24,7 @@ class Redirect extends Response {
|
|||
*/
|
||||
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')
|
||||
{
|
||||
|
@ -93,14 +93,16 @@ public static function __callStatic($method, $parameters)
|
|||
{
|
||||
$parameters = (isset($parameters[0])) ? $parameters[0] : array();
|
||||
|
||||
$url = IoC::container()->resolve('laravel.url');
|
||||
|
||||
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)
|
||||
{
|
||||
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.");
|
||||
|
|
|
@ -1,12 +0,0 @@
|
|||
<?php namespace Laravel;
|
||||
|
||||
interface Renderable {
|
||||
|
||||
/**
|
||||
* Get the evaluated string contents of the object.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function render();
|
||||
|
||||
}
|
|
@ -1,5 +1,16 @@
|
|||
<?php namespace Laravel;
|
||||
|
||||
interface Renderable {
|
||||
|
||||
/**
|
||||
* Get the evaluated string contents of the object.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function render();
|
||||
|
||||
}
|
||||
|
||||
class Response implements Renderable {
|
||||
|
||||
/**
|
||||
|
|
|
@ -11,7 +11,7 @@ class URL {
|
|||
* @param bool $https
|
||||
* @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;
|
||||
|
||||
|
@ -31,9 +31,9 @@ public static function to($url = '', $https = false)
|
|||
* @param string $url
|
||||
* @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 @@ public static function to_secure($url = '')
|
|||
* @param string $url
|
||||
* @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 @@ public static function to_asset($url)
|
|||
*
|
||||
* <code>
|
||||
* // 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.
|
||||
* $url = URL::to_route('profile', array('fred'));
|
||||
* $url = $url->to_route('profile', array('fred'));
|
||||
* </code>
|
||||
*
|
||||
* @param string $name
|
||||
|
@ -68,7 +68,7 @@ public static function to_asset($url)
|
|||
* @param bool $https
|
||||
* @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())))
|
||||
{
|
||||
|
@ -83,7 +83,7 @@ public static function to_route($name, $parameters = array(), $https = false)
|
|||
|
||||
$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.");
|
||||
|
@ -94,16 +94,16 @@ public static function to_route($name, $parameters = array(), $https = false)
|
|||
*
|
||||
* <code>
|
||||
* // Generate a HTTPS URL for the "profile" named route
|
||||
* $url = URL::to_secure_route('profile');
|
||||
* $url = $url->to_secure_route('profile');
|
||||
* </code>
|
||||
*
|
||||
* @param string $name
|
||||
* @param array $parameters
|
||||
* @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 @@ public static function to_secure_route($name, $parameters = array())
|
|||
*
|
||||
* <code>
|
||||
* // Returns "my-first-post"
|
||||
* $slug = URL::slug('My First Post!!');
|
||||
* $slug = $url->slug('My First Post!!');
|
||||
*
|
||||
* // Returns "my_first_post"
|
||||
* $slug = URL::slug('My First Post!!', '_');
|
||||
* $slug = $url->slug('My First Post!!', '_');
|
||||
* </code>
|
||||
*
|
||||
* @param string $title
|
||||
* @param string $separator
|
||||
* @return string
|
||||
*/
|
||||
public static function slug($title, $separator = '-')
|
||||
public function slug($title, $separator = '-')
|
||||
{
|
||||
$title = Str::ascii($title);
|
||||
|
||||
|
@ -139,27 +139,27 @@ public static function slug($title, $separator = '-')
|
|||
*
|
||||
* <code>
|
||||
* // 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
|
||||
* $url = URL::to_secure_profile();
|
||||
* $url = $url->to_secure_profile();
|
||||
*
|
||||
* // Generate a URL for the "profile" named route with parameters.
|
||||
* $url = URL::to_profile(array('fred'));
|
||||
* $url = $url->to_profile(array('fred'));
|
||||
* </code>
|
||||
*/
|
||||
public static function __callStatic($method, $parameters)
|
||||
public function __call($method, $parameters)
|
||||
{
|
||||
$parameters = (isset($parameters[0])) ? $parameters[0] : array();
|
||||
|
||||
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)
|
||||
{
|
||||
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.");
|
||||
|
|
Loading…
Reference in New Issue