From b0d3443448174037128f6f4a664bc1ad2acadb84 Mon Sep 17 00:00:00 2001 From: Travis Miller Date: Tue, 2 Sep 2014 12:32:59 -0500 Subject: [PATCH 1/2] Update service provider to use `bindShared()`. --- lib/Spot/Provider/Laravel.php | 74 ++++++++++++++++++++++++----------- 1 file changed, 51 insertions(+), 23 deletions(-) diff --git a/lib/Spot/Provider/Laravel.php b/lib/Spot/Provider/Laravel.php index a29ab2a..25c7202 100644 --- a/lib/Spot/Provider/Laravel.php +++ b/lib/Spot/Provider/Laravel.php @@ -7,35 +7,63 @@ class Laravel extends ServiceProvider { - protected $config = []; - - public function __construct($app) - { - $this->app = $app; - - $configObject = $this->app['config']; - $connections = $configObject->get('database.connections'); - $config = $connections[$configObject->get('database.default')]; - - // Munge Laravel array structure to match expected Doctrine DBAL's - $config = [ - 'dbname' => $config['database'], - 'user' => isset($config['username']) ? $config['username'] : null, - 'password' => isset($config['password']) ? $config['password'] : null, - 'host' => isset($config['host']) ? $config['host'] : null, - 'driver' => 'pdo_' . $config['driver'] - ]; - $this->config = $config; - } + /** + * Indicates if loading of the provider is deferred. + * + * @var bool + */ + protected $defer = false; + /** + * Register the service provider. + * + * The register method is called immediately when the service provider + * is registered, there is no promise that services created by other + * providers are available when this method is called. + * + * @return void + */ public function register() { - $this->app['spot'] = function () { + $this->app->bindShared('spot', function ($app) { + + $connection = $app['config']->get('database.default'); + $credentials = $app['config']->get('database.connections.' . $connection); + $config = new Config(); - $config->addConnection('default', $this->config); + $config->addConnection('default', [ + 'dbname' => $credentials['database'], + 'user' => $credentials['username'], + 'password' => $credentials['password'], + 'host' => $credentials['host'], + 'driver' => 'pdo_' . $credentials['driver'] + ]); return new Locator($config); - }; + }); } + /** + * Bootstrap the application events. + * + * This method is called right before a request is routed. If actions + * in this provider rely on another service being registered, or you + * are overriding services bound by another provider, you should + * use this method. + * + * @return void + */ + public function boot() + { + } + + /** + * Get the services provided by the provider. + * + * @return array + */ + public function provides() + { + return array('spot'); + } } From 8d792828c9bdc941568f6a4592deabccd0b2e018 Mon Sep 17 00:00:00 2001 From: Travis Miller Date: Tue, 2 Sep 2014 14:45:16 -0500 Subject: [PATCH 2/2] Add checks on $credentials to prevent notices. The default laravel configuration does not define all config options for the sqlite connection: https://github.com/laravel/laravel/blob/master/app/config/database.php Add an `isset()` check on `$credentials` for the following index to avoid PHP undefined index notices: $credentials['username'] $credentials['password'] $credentials['host'] --- lib/Spot/Provider/Laravel.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/Spot/Provider/Laravel.php b/lib/Spot/Provider/Laravel.php index 25c7202..e50161a 100644 --- a/lib/Spot/Provider/Laravel.php +++ b/lib/Spot/Provider/Laravel.php @@ -33,9 +33,9 @@ public function register() $config = new Config(); $config->addConnection('default', [ 'dbname' => $credentials['database'], - 'user' => $credentials['username'], - 'password' => $credentials['password'], - 'host' => $credentials['host'], + 'user' => isset($credentials['username']) ? $credentials['username'] : null, + 'password' => isset($credentials['password']) ? $credentials['password'] : null, + 'host' => isset($credentials['host']) ? $credentials['host'] : null, 'driver' => 'pdo_' . $credentials['driver'] ]);