From f113b5c829f754d72f85cf725439af0b7582e90a Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Mon, 29 Aug 2011 22:02:32 -0500 Subject: [PATCH] refactoring for dependency injection. --- laravel/arr.php | 10 ---------- laravel/config.php | 24 ------------------------ laravel/config/container.php | 4 ++-- laravel/container.php | 33 --------------------------------- laravel/controller.php | 4 +--- laravel/file.php | 15 +-------------- laravel/input.php | 26 -------------------------- laravel/lang.php | 18 ------------------ laravel/redirect.php | 22 +++++++++++++++++++--- laravel/response.php | 10 ---------- laravel/view.php | 10 ---------- 11 files changed, 23 insertions(+), 153 deletions(-) diff --git a/laravel/arr.php b/laravel/arr.php index 1bd9030e..a31798af 100644 --- a/laravel/arr.php +++ b/laravel/arr.php @@ -9,11 +9,6 @@ class Arr { * also be accessed using JavaScript "dot" style notation. Retrieving items nested * in multiple arrays is also supported. * - * - * // Returns "taylor" - * Arr::get(array('name' => array('is' => 'Taylor')), 'name.is'); - * - * * @param array $array * @param string $key * @param mixed $default @@ -47,11 +42,6 @@ public static function get($array, $key, $default = null) * * Like the Arr::get method, JavaScript "dot" syntax is supported. * - * - * // Set "name.is" to "taylor" - * Arr::set(array('name' => array('is' => 'something')), 'name.is', 'taylor'); - * - * * @param array $array * @param string $key * @param mixed $value diff --git a/laravel/config.php b/laravel/config.php index dc8bf284..8afd84e7 100644 --- a/laravel/config.php +++ b/laravel/config.php @@ -14,14 +14,6 @@ class Config { /** * Determine if a configuration item or file exists. * - * - * // Determine if the "session" configuration file exists - * Config::has('session'); - * - * // Determine if the application timezone option exists - * Config::has('application.timezone'); - * - * * @param string $key * @return bool */ @@ -40,14 +32,6 @@ public function has($key) * If the name of a configuration file is passed without specifying an item, the * entire configuration array will be returned. * - * - * // Get the timezone option from the application configuration file - * $timezone = Config::get('application.timezone'); - * - * // Get the SQLite database connection configuration - * $sqlite = Config::get('database.connections.sqlite'); - * - * * @param string $key * @param string $default * @return array @@ -75,14 +59,6 @@ public function get($key, $default = null) * If a specific configuration item is not specified, the entire configuration * array will be replaced with the given value. * - * - * // Set the timezone option in the application configuration file - * Config::set('application.timezone', 'America/Chicago'); - * - * // Set the session configuration array - * Config::set('session', array()); - * - * * @param string $key * @param mixed $value * @return void diff --git a/laravel/config/container.php b/laravel/config/container.php index 426f4be4..de94b645 100644 --- a/laravel/config/container.php +++ b/laravel/config/container.php @@ -4,7 +4,7 @@ /* |-------------------------------------------------------------------------- - | Laravel Support Components + | Laravel Components |-------------------------------------------------------------------------- */ @@ -59,7 +59,7 @@ 'laravel.redirect' => array('singleton' => true, 'resolver' => function($container) { - return new Redirect($container->resolve('laravel.url')); + return new Redirect($container->resolve('laravel.session.driver'), $container->resolve('laravel.url')); }), diff --git a/laravel/container.php b/laravel/container.php index 2f33d6b9..594bb873 100644 --- a/laravel/container.php +++ b/laravel/container.php @@ -21,11 +21,6 @@ public static function container() /** * Magic Method for calling methods on the active container instance. - * - * - * // Get the request registered in the container - * $request = IoC::resolve('laravel.request'); - * */ public static function __callStatic($method, $parameters) { @@ -69,14 +64,6 @@ public function __construct($dependencies = array()) * * The resolver function when the registered dependency is requested. * - * - * // Register a simple dependency - * $container->register('name', function() { return 'Fred'; }); - * - * // Register a dependency as a singleton - * $container->register('name', function() { return new Name; }, true); - * - * * @param string $name * @param Closure $resolver * @return void @@ -89,11 +76,6 @@ public function register($name, $resolver, $singleton = false) /** * Determine if a dependency has been registered in the container. * - * - * // Determine if the "user" dependency is registered in the container - * $registered = $container->registered('user'); - * - * * @param string $name * @return bool */ @@ -108,11 +90,6 @@ public function registered($name) * 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 @@ -128,11 +105,6 @@ public function singleton($name, $resolver) * 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 @@ -147,11 +119,6 @@ public function instance($name, $instance) * * The dependency's resolver will be called and its result will be returned. * - * - * // Resolver the "name" dependency - * $name = $container->resolve('name'); - * - * * @param string $name * @return mixed */ diff --git a/laravel/controller.php b/laravel/controller.php index 48359f27..30bf2716 100644 --- a/laravel/controller.php +++ b/laravel/controller.php @@ -17,9 +17,7 @@ public function before() {} */ public function __get($key) { - $application = IoC::resolve('laravel.application'); - - return $application->$key; + return IoC::resolve('laravel.application')->$key; } /** diff --git a/laravel/file.php b/laravel/file.php index 27e14600..1b678810 100644 --- a/laravel/file.php +++ b/laravel/file.php @@ -124,13 +124,6 @@ public function modified($path) /** * Get a file MIME type by extension. * - * Any extension in the MIMEs configuration file may be passed to the method. - * - * - * // Returns "application/x-tar" - * $mime = $file->mime('tar'); - * - * * @param string $extension * @param string $default * @return string @@ -145,13 +138,7 @@ public function mime($extension, $default = 'application/octet-stream') /** * Determine if a file is a given type. * - * The Fileinfo PHP extension will be used to determine the MIME type of the file. Any extension - * in the MIMEs configuration file may be passed as a type. - * - * - * // Determine if the file is a JPG image - * $image = $file->is('jpg', 'path/to/image.jpg'); - * + * The Fileinfo PHP extension will be used to determine the MIME type of the file. * * @param string $extension * @param string $path diff --git a/laravel/input.php b/laravel/input.php index 60c46eb9..ff5a229f 100644 --- a/laravel/input.php +++ b/laravel/input.php @@ -80,14 +80,6 @@ public function has($key) * * This method should be used for all request methods (GET, POST, PUT, and DELETE). * - * - * // Get the "name" item from the input data - * $name = Request::active()->input->get('name'); - * - * // Get the "name" item and return "Fred" if it doesn't exist. - * $name = Request::active()->input->get('name', 'Fred'); - * - * * @param string $key * @param mixed $default * @return string @@ -111,11 +103,6 @@ public function had($key) /** * Get input data from the previous request. * - * - * // Get the "name" item from the old input data - * $name = Request::active()->input->old('name'); - * - * * @param string $key * @param mixed $default * @return string @@ -132,14 +119,6 @@ public function old($key = null, $default = null) * * "Dot" syntax may be used to get a specific item from the file array. * - * - * // Get the array of information regarding a given file - * $file = Request::active()->input->file('picture'); - * - * // Get the size of a given file - * $file = Request::active()->input->file('picture.size'); - * - * * @param string $key * @param mixed $default * @return array @@ -165,11 +144,6 @@ public function upload($key, $path) /** * Magic Method for retrieving items from the request input. - * - * - * // Retrieve the "name" item from the input data - * $name = Request::active()->input->name; - * */ public function __get($key) { diff --git a/laravel/lang.php b/laravel/lang.php index 305bbfd7..f336e140 100644 --- a/laravel/lang.php +++ b/laravel/lang.php @@ -52,14 +52,6 @@ private function __construct($key, $replacements = array()) * Language lines are retrieved using "dot" notation. So, asking for the "messages.required" langauge * line would return the "required" line from the "messages" language file. * - * - * // Get the "required" line from the "validation" language file - * $line = Lang::line('validation.required')->get(); - * - * // Specify a replacement for a language line - * $line = Lang::line('welcome.message', array('name' => 'Fred'))->get(); - * - * * @param string $key * @param array $replacements * @return Lang @@ -74,11 +66,6 @@ public static function line($key, $replacements = array()) * * A default value may also be specified, which will be returned in the language line doesn't exist. * - * - * // Get a validation line and return a default value if the line doesn't exist - * $line = Lang::line('welcome.message')->get('Hello!'); - * - * * @param string $language * @return string */ @@ -156,11 +143,6 @@ private function load($file) * * The language specified in this method should correspond to a language directory in your application. * - * - * // Get a "fr" language line - * $line = Lang::line('validation.required')->in('fr')->get(); - * - * * @param string $language * @return Lang */ diff --git a/laravel/redirect.php b/laravel/redirect.php index 47de8641..dd335fa9 100644 --- a/laravel/redirect.php +++ b/laravel/redirect.php @@ -2,15 +2,31 @@ class Redirect extends Response { + /** + * The URL generator instance. + * + * @var URL + */ + private $url; + + /** + * The active session driver instance. + * + * @var Session\Driver + */ + private $session; + /** * Create a new redirect generator instance. * - * @param URL $url + * @param Session\Driver $session + * @param URL $url * @return void */ - public function __construct(URL $url) + public function __construct(Session\Driver $session, URL $url) { $this->url = $url; + $this->session = $session; } /** @@ -62,7 +78,7 @@ public function to_secure($url, $status = 302, $method = 'location') */ public function with($key, $value) { - IoC::container()->resolve('laravel.session.driver')->flash($key, $value); + $this->session->flash($key, $value); return $this; } diff --git a/laravel/response.php b/laravel/response.php index 414d43ce..fcbc7996 100644 --- a/laravel/response.php +++ b/laravel/response.php @@ -223,14 +223,4 @@ public function status($status) return $this; } - /** - * Magic Method for passing undefined static methods to the Response_Factory instance - * registered in the application IoC container. This provides easy access to the - * response functions while still maintaining testability within the classes. - */ - public static function __callStatic($method, $parameters) - { - return call_user_func_array(array(IoC::container()->resolve('laravel.response'), $method), $parameters); - } - } \ No newline at end of file diff --git a/laravel/view.php b/laravel/view.php index a7a73ecb..ed83c3e7 100644 --- a/laravel/view.php +++ b/laravel/view.php @@ -296,14 +296,4 @@ public function __unset($key) unset($this->data[$key]); } - /** - * Magic Method for passing undefined static methods to the View_Factory instance - * registered in the application IoC container. This provides easy access to the - * view functions while still maintaining testability within the view classes. - */ - public static function __callStatic($method, $parameters) - { - return call_user_func_array(array(IoC::container()->resolve('laravel.view'), $method), $parameters); - } - } \ No newline at end of file