From 82045e20d98956a4ed6602a191e3530f2a3dce3f Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Wed, 24 Aug 2011 23:44:50 -0500 Subject: [PATCH] more refactoring for dependency injection. --- laravel/config/dependencies.php | 17 ++++++++++--- laravel/container.php | 16 +++++++++++++ laravel/form.php | 18 +++++++------- laravel/html.php | 16 +++++++++---- laravel/inflector.php | 16 +++++++++++++ laravel/laravel.php | 26 +++++++++++--------- laravel/redirect.php | 8 ++++--- laravel/renderable.php | 12 ---------- laravel/response.php | 11 +++++++++ laravel/url.php | 42 ++++++++++++++++----------------- 10 files changed, 119 insertions(+), 63 deletions(-) delete mode 100644 laravel/renderable.php diff --git a/laravel/config/dependencies.php b/laravel/config/dependencies.php index 9f7abcd1..42d68654 100644 --- a/laravel/config/dependencies.php +++ b/laravel/config/dependencies.php @@ -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')) { diff --git a/laravel/container.php b/laravel/container.php index a3ffeaab..2f76eb28 100644 --- a/laravel/container.php +++ b/laravel/container.php @@ -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. + * + * + * // Register a dependency as a singleton + * $container->singleton('user', function() { return new User; }) + * + * * @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. + * + * + * // Register an object instance as a singleton in the container + * $container->instance('user', new User); + * + * * @param string $name * @param mixed $instance * @return void diff --git a/laravel/form.php b/laravel/form.php index 63a25f55..03732c59 100644 --- a/laravel/form.php +++ b/laravel/form.php @@ -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); } diff --git a/laravel/html.php b/laravel/html.php index f4a51977..c665bc1a 100644 --- a/laravel/html.php +++ b/laravel/html.php @@ -24,7 +24,9 @@ public static function entities($value) */ public static function script($url, $attributes = array()) { - return ''.PHP_EOL; + $url = IoC::container()->resolve('laravel.url'); + + return ''.PHP_EOL; } /** @@ -40,7 +42,9 @@ public static function style($url, $attributes = array()) $attributes = array_merge($attributes, array('rel' => 'stylesheet', 'type' => 'text/css')); - return ''.PHP_EOL; + $url = IoC::container()->resolve('laravel.url'); + + return ''.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 ''.static::entities($title).''; + $url = IoC::container()->resolve('laravel.url'); + + return ''.static::entities($title).''; } /** @@ -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 ''; + return ''; } /** diff --git a/laravel/inflector.php b/laravel/inflector.php index 231a09a7..8fdb977e 100644 --- a/laravel/inflector.php +++ b/laravel/inflector.php @@ -136,6 +136,14 @@ public static function plural_if($value, $count) /** * Convert a word to its plural form. * + * + * // Returns "friends" + * Inflector::plural('friend'); + * + * // Returns "children" + * Inflector::plural('child'); + * + * * @param string $value * @return string */ @@ -147,6 +155,14 @@ public static function plural($value) /** * Convert a word to its singular form. * + * + * // Returns "friend" + * Inflector::singular('friends'); + * + * // Returns "child" + * Inflector::singular('children'); + * + * * @param string $value * @return string */ diff --git a/laravel/laravel.php b/laravel/laravel.php index 8e9d27a1..40e79b61 100644 --- a/laravel/laravel.php +++ b/laravel/laravel.php @@ -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. // -------------------------------------------------------------- diff --git a/laravel/redirect.php b/laravel/redirect.php index 61e3b472..98d71832 100644 --- a/laravel/redirect.php +++ b/laravel/redirect.php @@ -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."); diff --git a/laravel/renderable.php b/laravel/renderable.php deleted file mode 100644 index 5a095d9e..00000000 --- a/laravel/renderable.php +++ /dev/null @@ -1,12 +0,0 @@ -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) * * * // 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')); * * * @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) * * * // Generate a HTTPS URL for the "profile" named route - * $url = URL::to_secure_route('profile'); + * $url = $url->to_secure_route('profile'); * * * @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()) * * * // 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!!', '_'); * * * @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 = '-') * * * // 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')); * */ - 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.");