From c7ddbbb018742714374f6adee78e238df75aae21 Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Wed, 31 Aug 2011 00:07:45 -0500 Subject: [PATCH] more dependency injection! --- laravel/application.php | 46 ++++++++++++++++++------------- laravel/bootstrap.php | 9 ++---- laravel/cache/apc.php | 10 ------- laravel/cache/driver.php | 23 ---------------- laravel/cache/manager.php | 13 --------- laravel/cache/memcached.php | 10 ------- laravel/config/container.php | 34 +++++++++++++++++------ laravel/container.php | 3 ++ laravel/controller.php | 17 ++---------- laravel/download.php | 23 +++++++++------- laravel/form.php | 53 ++++++++++-------------------------- laravel/html.php | 16 ----------- laravel/loader.php | 4 +-- laravel/package.php | 2 +- laravel/request.php | 15 ++++++++-- laravel/response.php | 9 ++++-- laravel/session/cookie.php | 4 +-- laravel/session/driver.php | 46 +++---------------------------- laravel/session/manager.php | 2 +- laravel/str.php | 2 +- 20 files changed, 118 insertions(+), 223 deletions(-) diff --git a/laravel/application.php b/laravel/application.php index c272e6c1..cf7f07b5 100644 --- a/laravel/application.php +++ b/laravel/application.php @@ -1,29 +1,37 @@ registered('laravel.'.$key)) + { + return IoC::container()->resolve('laravel.'.$key); + } + elseif (IoC::container()->registered($key)) + { + return IoC::container()->resolve($key); + } + + throw new \Exception("Attempting to access undefined property [$key]."); + } + +} + +class Application extends Resolver { + + /** + * The IoC container instance for the application. * * @var Container */ public $container; - /** - * Magic Method for resolving core classes out of the IoC container. - */ - public function __get($key) - { - if ($this->container->registered('laravel.'.$key)) - { - return $this->container->resolve('laravel.'.$key); - } - elseif ($this->container->registered($key)) - { - return $this->container->resolve($key); - } - - throw new \Exception("Attempting to access undefined property [$key] on application instance."); - } - } \ No newline at end of file diff --git a/laravel/bootstrap.php b/laravel/bootstrap.php index 20ec4d6c..e9f650ad 100644 --- a/laravel/bootstrap.php +++ b/laravel/bootstrap.php @@ -65,6 +65,8 @@ $application->container = new Container($dependencies); +IoC::$container = $application->container; + // -------------------------------------------------------------- // Load the auto-loader. // -------------------------------------------------------------- @@ -73,9 +75,4 @@ // -------------------------------------------------------------- // Register the application in the container. // -------------------------------------------------------------- -$application->container->instance('laravel.application', $application); - -// -------------------------------------------------------------- -// Set the IoC container instance for use as a service locator. -// -------------------------------------------------------------- -IoC::$container = $application->container; \ No newline at end of file +IoC::container()->instance('laravel.application', $application); \ No newline at end of file diff --git a/laravel/cache/apc.php b/laravel/cache/apc.php index f6e34d17..eba53725 100644 --- a/laravel/cache/apc.php +++ b/laravel/cache/apc.php @@ -75,11 +75,6 @@ public function __construct(APC_Engine $apc, $key) /** * Determine if an item exists in the cache. * - * - * // Determine if the "name" item exists in the cache - * $exists = Cache::driver()->has('name'); - * - * * @param string $key * @return bool */ @@ -102,11 +97,6 @@ protected function retrieve($key) /** * Write an item to the cache for a given number of minutes. * - * - * // Write the "name" item to the cache for 30 minutes - * Cache::driver()->put('name', 'Fred', 30); - * - * * @param string $key * @param mixed $value * @param int $minutes diff --git a/laravel/cache/driver.php b/laravel/cache/driver.php index 267a2eda..9e4c4def 100644 --- a/laravel/cache/driver.php +++ b/laravel/cache/driver.php @@ -5,11 +5,6 @@ abstract class Driver { /** * Determine if an item exists in the cache. * - * - * // Determine if the "name" item exists in the cache - * $exists = Cache::driver()->has('name'); - * - * * @param string $key * @return bool */ @@ -21,14 +16,6 @@ abstract public function has($key); * A default value may also be specified, and will be returned in the requested * item does not exist in the cache. * - * - * // Get the "name" item from the cache - * $name = Cache::driver()->get('name'); - * - * // Get the "name" item from the cache or return "Fred" - * $name = Cache::driver()->get('name', 'Fred'); - * - * * @param string $key * @param mixed $default * @param string $driver @@ -52,11 +39,6 @@ abstract protected function retrieve($key); /** * Write an item to the cache for a given number of minutes. * - * - * // Write the "name" item to the cache for 30 minutes - * Cache::driver()->put('name', 'Fred', 30); - * - * * @param string $key * @param mixed $value * @param int $minutes @@ -68,11 +50,6 @@ abstract public function put($key, $value, $minutes); * Get an item from the cache. If the item doesn't exist in the cache, store * the default value in the cache and return it. * - * - * // Get the "name" item from the cache or store "Fred" for 30 minutes - * $name = Cache::driver()->remember('name', 'Fred', 30); - * - * * @param string $key * @param mixed $default * @param int $minutes diff --git a/laravel/cache/manager.php b/laravel/cache/manager.php index 665bdccc..3581dfef 100644 --- a/laravel/cache/manager.php +++ b/laravel/cache/manager.php @@ -43,14 +43,6 @@ public function __construct(Container $container, $default) * If no driver name is specified, the default cache driver will be returned * as defined in the cache configuration file. * - * - * // Get the default cache driver - * $driver = $application->cache->driver(); - * - * // Get the APC cache driver - * $apc = $application->cache->driver('apc'); - * - * * @param string $driver * @return Cache\Driver */ @@ -76,11 +68,6 @@ public function driver($driver = null) * * Passing method calls to the driver instance provides a convenient API for the developer * when always using the default cache driver. - * - * - * // Get an item from the default cache driver - * $name = $application->cache->get('name'); - * */ public function __call($method, $parameters) { diff --git a/laravel/cache/memcached.php b/laravel/cache/memcached.php index f06c2042..158ca886 100644 --- a/laravel/cache/memcached.php +++ b/laravel/cache/memcached.php @@ -34,11 +34,6 @@ public function __construct(Memcache $memcache, $key) /** * Determine if an item exists in the cache. * - * - * // Determine if the "name" item exists in the cache - * $exists = Cache::driver()->has('name'); - * - * * @param string $key * @return bool */ @@ -61,11 +56,6 @@ protected function retrieve($key) /** * Write an item to the cache for a given number of minutes. * - * - * // Write the "name" item to the cache for 30 minutes - * Cache::driver()->put('name', 'Fred', 30); - * - * * @param string $key * @param mixed $value * @param int $minutes diff --git a/laravel/config/container.php b/laravel/config/container.php index 7d7d5064..55530ce7 100644 --- a/laravel/config/container.php +++ b/laravel/config/container.php @@ -47,23 +47,41 @@ }), + 'laravel.form' => array('resolver' => function($container) + { + list($request, $html, $url) = array( + $container->resolve('laravel.request'), + $container->resolve('laravel.html'), + $container->resolve('laravel.url'), + ); + + return new Form($request, $html, $url); + }), + + + 'laravel.html' => array('resolver' => function($container) + { + return new HTML($container->resolve('laravel.url'), $container->resolve('laravel.config')->get('application.encoding')); + }), + + 'laravel.input' => array('singleton' => true, 'resolver' => function($container) { - $application = $container->resolve('laravel.application'); + $request = $container->resolve('laravel.request'); $input = array(); - if ($application->request->method() == 'GET') + if ($request->method() == 'GET') { $input = $_GET; } - elseif ($application->request->method() == 'POST') + elseif ($request->method() == 'POST') { $input = $_POST; } - elseif ($application->request->method() == 'PUT' or $application->request->method == 'DELETE') + elseif ($request->method() == 'PUT' or $request->method == 'DELETE') { - ($application->request->spoofed()) ? $input = $_POST : parse_str(file_get_contents('php://input'), $input); + ($request->spoofed()) ? $input = $_POST : parse_str(file_get_contents('php://input'), $input); } return new Input($input, $_FILES, $container->resolve('laravel.cookie')); @@ -98,7 +116,7 @@ 'laravel.request' => array('singleton' => true, 'resolver' => function($container) { - return new Request($_SERVER, $container->resolve('laravel.config')->get('application.url')); + return new Request($_SERVER, $_POST, $container->resolve('laravel.config')->get('application.url')); }), @@ -265,7 +283,7 @@ }), - 'laravel.cache.memcache.connection' => array('singleton' => true, 'resolver' => function() + 'laravel.cache.memcache.connection' => array('singleton' => true, 'resolver' => function($container) { if ( ! class_exists('Memcache')) { @@ -274,7 +292,7 @@ $memcache = new \Memcache; - foreach (Config::get('cache.servers') as $server) + foreach ($container->resolve('laravel.config')->get('cache.servers') as $server) { $memcache->addServer($server['host'], $server['port'], true, $server['weight']); } diff --git a/laravel/container.php b/laravel/container.php index 594bb873..eb8505ec 100644 --- a/laravel/container.php +++ b/laravel/container.php @@ -12,6 +12,9 @@ class IoC { /** * Get the active container instance. * + * The container is set early in the request cycle and can be access here for + * use as a service locator if dependency injection is not practical. + * * @return Container */ public static function container() diff --git a/laravel/controller.php b/laravel/controller.php index 30bf2716..4412b345 100644 --- a/laravel/controller.php +++ b/laravel/controller.php @@ -1,31 +1,20 @@ $key; - } - /** * Magic Method to handle calls to undefined functions on the controller. */ - public function __call($method, $parameters) - { - return IoC::resolve('laravel.application')->responder->error('404'); - } + public function __call($method, $parameters) { return $this->response->error('404'); } } \ No newline at end of file diff --git a/laravel/download.php b/laravel/download.php index f059326f..23042828 100644 --- a/laravel/download.php +++ b/laravel/download.php @@ -25,22 +25,25 @@ public function __construct(File $file) * * @param string $path * @param string $name + * @param array $headers * @return Response */ - public function of($path, $name = null) + public function of($path, $name = null, $headers = array()) { if (is_null($name)) $name = basename($path); - $response = parent::__construct($this->file->get($path)); + $headers = array_merge(array( + 'Content-Description' => 'File Transfer', + 'Content-Type' => $this->mime($this->file->extension($path)), + 'Content-Disposition' => 'attachment; filename="'.$name.'"', + 'Content-Transfer-Encoding' => 'binary', + 'Expires' = => 0, + 'Cache-Control' => 'must-revalidate, post-check=0, pre-check=0', + 'Pragma' => 'public', + 'Content-Length' => $this->file-size($path), + ), $headers); - $response->header('Content-Description', 'File Transfer'); - $response->header('Content-Type', $this->file->mime($this->file->extension($path))); - $response->header('Content-Disposition', 'attachment; filename="'.$name.'"'); - $response->header('Content-Transfer-Encoding', 'binary'); - $response->header('Expires', 0); - $response->header('Cache-Control', 'must-revalidate, post-check=0, pre-check=0'); - $response->header('Pragma', 'public'); - $response->header('Content-Length', $this->file->size($path)); + $response = parent::__construct($this->file->get($path), 200, $headers); return $response; } diff --git a/laravel/form.php b/laravel/form.php index 8828be30..e9e7c98e 100644 --- a/laravel/form.php +++ b/laravel/form.php @@ -23,13 +23,6 @@ class Form { */ private $url; - /** - * The CSRF token for the session. - * - * @var string - */ - public $token; - /** * All of the label names that have been created. * @@ -44,31 +37,20 @@ class Form { * Create a new form writer instance. * * @param Request $request - * @param string $token + * @param HTML $html + * @param URL $url * @return void */ - public function __construct(Request $request, HTML $html, URL $url, $token) + public function __construct(Request $request, HTML $html, URL $url) { $this->url = $url; $this->html = $html; - $this->token = $token; $this->request = $request; } /** * Open a HTML form. * - * - * // Open a POST form for the current URI - * echo Form::open(); - * - * // Open a POST form to a specified URI - * echo Form::open('user/login'); - * - * // Open a PUT form to a specified URI - * echo Form::open('user/profile', 'put'); - * - * * Note: If PUT or DELETE is specified as the form method, a hidden input field will be generated * 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. @@ -180,16 +162,22 @@ public function close() */ public function token() { - return $this->input('hidden', 'csrf_token', $this->token); + return $this->input('hidden', 'csrf_token', $this->raw_token()); + } + + /** + * Get the CSRF token for the current session. + * + * @return string + */ + public function raw_token() + { + return IoC::container()->resolve('laravel.session')->get('csrf_token'); } /** * Create a HTML label element. * - * - * echo Form::label('email', 'E-Mail Address'); - * - * * @param string $name * @param string $value * @param array $attributes @@ -208,14 +196,6 @@ public function label($name, $value, $attributes = array()) * If an ID attribute is not specified and a label has been generated matching the input * element name, the label name will be used as the element ID. * - * - * // Generate a text type input element - * echo Form::input('text', 'email'); - * - * // Generate a hidden type input element with a specified value - * echo Form::input('hidden', 'secret', 'This is a secret.'); - * - * * @param string $name * @param mixed $value * @param array $attributes @@ -365,11 +345,6 @@ public function textarea($name, $value = '', $attributes = array()) /** * Create a HTML select element. * - * - * // Generate a drop-down with the "S" item selected - * echo Form::select('sizes', array('L' => 'Large', 'S' => 'Small'), 'S'); - * - * * @param string $name * @param array $options * @param string $selected diff --git a/laravel/html.php b/laravel/html.php index ca40e78e..966daeff 100644 --- a/laravel/html.php +++ b/laravel/html.php @@ -144,14 +144,6 @@ public function link_to_secure_asset($url, $title, $attributes = array()) * * An array of parameters may be specified to fill in URI segment wildcards. * - * - * // Link to the "login" route - * echo HTML::link_to_route('login', 'Login'); - * - * // Link to the "profile" route, which has a URI of "/profile/(:any)" - * echo HTML::link_to_route('profile', 'Profile', array('taylor')); - * - * * @param string $name * @param string $title * @param array $parameters @@ -330,14 +322,6 @@ public function obfuscate($value) * Magic Method for handling dynamic static methods. * * This method primarily handles dynamic calls to create links to named routes. - * - * - * // Link to the "login" route - * echo HTML::link_to_login('Login'); - * - * // Link to the "profile" route, which has a URI of "/profile/(:any)" - * echo HTML::link_to_profile('Profile', array('taylor')); - * */ public function __call($method, $parameters) { diff --git a/laravel/loader.php b/laravel/loader.php index 895f1213..9e7481bb 100644 --- a/laravel/loader.php +++ b/laravel/loader.php @@ -7,14 +7,14 @@ class Loader { * * @var array */ - public $paths; + private $paths; /** * All of the class aliases. * * @var array */ - public $aliases; + private $aliases; /** * Bootstrap the auto-loader. diff --git a/laravel/package.php b/laravel/package.php index 042a678a..9839b2f6 100644 --- a/laravel/package.php +++ b/laravel/package.php @@ -7,7 +7,7 @@ class Package { * * @var array */ - public $loaded = array(); + private $loaded = array(); /** * Load a package or set of packages. diff --git a/laravel/request.php b/laravel/request.php index a5aedf5c..65daec37 100644 --- a/laravel/request.php +++ b/laravel/request.php @@ -9,6 +9,13 @@ class Request { */ public $server; + /** + * The $_POST array for the request. + * + * @var array + */ + private $post; + /** * The route handling the current request. * @@ -27,12 +34,14 @@ class Request { * Create a new request instance. * * @param array $server + * @param array $post * @param string $url * @return void */ - public function __construct($server, $url) + public function __construct($server, $post, $url) { $this->url = $url; + $this->post = $post; $this->server = $server; } @@ -84,7 +93,7 @@ public function uri() */ public function method() { - return ($this->spoofed()) ? $_POST['REQUEST_METHOD'] : $this->server['REQUEST_METHOD']; + return ($this->spoofed()) ? $this->post['REQUEST_METHOD'] : $this->server['REQUEST_METHOD']; } /** @@ -96,7 +105,7 @@ public function method() */ public function spoofed() { - return is_array($_POST) and array_key_exists('REQUEST_METHOD', $_POST); + return is_array($this->post) and array_key_exists('REQUEST_METHOD', $this->post); } /** diff --git a/laravel/response.php b/laravel/response.php index d542d1ca..46ade53f 100644 --- a/laravel/response.php +++ b/laravel/response.php @@ -25,11 +25,12 @@ public function __construct(View_Factory $view) * * @param mixed $content * @param int $status + * @param array $headers * @return Response */ - public function make($content, $status = 200) + public function make($content, $status = 200, $headers = array()) { - return new Response($content, $status); + return new Response($content, $status, $headers); } /** @@ -144,11 +145,13 @@ class Response { * * @param mixed $content * @param int $status + * @param array $headers * @return void */ - public function __construct($content, $status = 200) + public function __construct($content, $status = 200, $headers = array()) { $this->content = $content; + $this->headers = $headers; $this->status = $status; } diff --git a/laravel/session/cookie.php b/laravel/session/cookie.php index 9e8dc735..dc7cc17a 100644 --- a/laravel/session/cookie.php +++ b/laravel/session/cookie.php @@ -28,9 +28,9 @@ class Cookie extends Driver { /** * Create a new Cookie session driver instance. * - * @param Crypter $crypter + * @param Crypter $crypter * @param Laravel\Cookie $cookie - * @param array $config + * @param array $config * @return void */ public function __construct(Crypter $crypter, \Laravel\Cookie $cookie, $config) diff --git a/laravel/session/driver.php b/laravel/session/driver.php index ac8b3943..20d19b0c 100644 --- a/laravel/session/driver.php +++ b/laravel/session/driver.php @@ -1,7 +1,7 @@ - * // Determine if "name" item exists in the session - * $exists = Session::driver()->has('name'); - * - * * @param string $key * @return bool */ @@ -86,14 +81,6 @@ public function has($key) * A default value may also be specified, and will be returned in the requested * item does not exist in the session. * - * - * // Get the "name" item from the session - * $name = Session::driver()->get('name'); - * - * // Get the "name" item from the session or return "Fred" - * $name = Session::driver()->get('name', 'Fred'); - * - * * @param string $key * @param mixed $default * @return mixed @@ -111,11 +98,6 @@ public function get($key, $default = null) /** * Write an item to the session. * - * - * // Write the "name" item to the session - * Session::driver()->put('name', 'Fred'); - * - * * @param string $key * @param mixed $value * @return Driver @@ -133,11 +115,6 @@ public function put($key, $value) * Flash data only exists for the next request. After that, it will be removed from * the session. Flash data is useful for temporary status or welcome messages. * - * - * // Write the "name" item to the session flash data - * Session::driver()->flash('name', 'Fred'); - * - * * @param string $key * @param mixed $value * @return Driver @@ -152,11 +129,6 @@ public function flash($key, $value) /** * Remove an item from the session. * - * - * // Remove the "name" item from the session - * Session::driver()->forget('name'); - * - * * @param string $key * @return Driver */ @@ -196,11 +168,11 @@ public function regenerate() * The input of the current request will also be flashed to the session so it is * available for the next request via the "old" method on the input class. * - * @param Laravel\Input_Engine $input - * @param array $config + * @param Laravel\Input $input + * @param array $config * @return void */ - public function close(Input_Engine $input, $config) + public function close(Input $input, $config) { $this->flash('laravel_old_input', $input->get())->age(); @@ -259,11 +231,6 @@ protected function write_cookie(Cookie $cookies, $config) /** * Magic Method for retrieving items from the session. - * - * - * // Get the "name" item from the session - * $name = $application->session->name; - * */ public function __get($key) { @@ -272,11 +239,6 @@ public function __get($key) /** * Magic Method for writings items to the session. - * - * - * // Write "Fred" to the session "name" item - * $application->session->name = 'Fred'; - * */ public function __set($key, $value) { diff --git a/laravel/session/manager.php b/laravel/session/manager.php index 688410b3..1faaf9ab 100644 --- a/laravel/session/manager.php +++ b/laravel/session/manager.php @@ -32,7 +32,7 @@ public function __construct(Container $container) * @param string $driver * @return Session\Driver */ - public static function driver($driver) + public function driver($driver) { if (in_array($driver, array('cookie', 'file', 'database', 'apc', 'memcached'))) { diff --git a/laravel/str.php b/laravel/str.php index 9ef75ee9..1d682611 100644 --- a/laravel/str.php +++ b/laravel/str.php @@ -107,7 +107,7 @@ private static function pool($type = 'alpha_num') * * @return string */ - private static function encoding() + public static function encoding() { return IoC::container()->resolve('laravel.config')->get('application.encoding'); }