diff --git a/docs/changes.md b/docs/changes.md index 64b4f29..4f10064 100644 --- a/docs/changes.md +++ b/docs/changes.md @@ -5,6 +5,10 @@ title: Tenanti Change Log ## Version 3.0 {#3-0} +### v3.0.2 {#v3-0-2} + +* Add fallback support to Laravel 5 configuration. + ### v3.0.1 {#v3-0-1} * Allow different connection name to be used when resolving migration. diff --git a/src/TenantiManager.php b/src/TenantiManager.php index 3151c3c..3082ef9 100644 --- a/src/TenantiManager.php +++ b/src/TenantiManager.php @@ -1,10 +1,18 @@ app['config']->get("orchestra/tenanti::drivers.{$driver}"); - $chunk = $this->app['config']->get('orchestra/tenanti::chunk', 100); + $config = Arr::get($this->config, "drivers.{$driver}"); + $chunk = Arr::get($this->config, 'chunk', 100); if (is_null($config)) { throw new InvalidArgumentException("Driver [$driver] not supported."); @@ -42,4 +50,28 @@ public function getDefaultDriver() { throw new InvalidArgumentException("Default driver not implemented."); } + + /** + * Get configuration values. + * + * @return array + */ + public function getConfig() + { + return $this->config; + } + + /** + * Set configuration. + * + * @param array $config + * + * @return $this + */ + public function setConfig(array $config) + { + $this->config = $config; + + return $this; + } } diff --git a/src/TenantiServiceProvider.php b/src/TenantiServiceProvider.php index 5b4d280..89f31c6 100644 --- a/src/TenantiServiceProvider.php +++ b/src/TenantiServiceProvider.php @@ -19,7 +19,12 @@ class TenantiServiceProvider extends ServiceProvider public function register() { $this->app->singleton('orchestra.tenanti', function ($app) { - return new TenantiManager($app); + $manager = new TenantiManager($app); + $namespace = $this->hasPackageRepository() ? 'orchestra/tenanti::' : 'orchestra.tenanti'; + + $manager->setConfig($app['config'][$namespace]); + + return $manager; }); $this->app->alias('orchestra.tenanti', 'Orchestra\Tenanti\TenantiManager'); @@ -35,6 +40,26 @@ public function boot() $path = realpath(__DIR__.'/../resources'); $this->addConfigComponent('orchestra/tenanti', 'orchestra/tenanti', $path.'/config'); + + if (! $this->hasPackageRepository()) { + $this->bootUsingLaravel($path); + } + } + + /** + * Boot using Laravel setup. + * + * @param string $path + * + * @return void + */ + protected function bootUsingLaravel($path) + { + $this->mergeConfigFrom("{$path}/config/config.php", 'orchestra.tenanti'); + + $this->publishes([ + "{$path}/config/config.php" => config_path('orchestra/tenanti.php'), + ]); } /** diff --git a/tests/TenantiManagerTest.php b/tests/TenantiManagerTest.php index 57152d0..88f865e 100644 --- a/tests/TenantiManagerTest.php +++ b/tests/TenantiManagerTest.php @@ -22,14 +22,14 @@ public function tearDown() public function testDriverMethod() { $app = new Container(); - $app['config'] = $config = m::mock('\Illuminate\Config\Repository'); - $option = ['model' => 'User']; - - $config->shouldReceive('get')->once()->with('orchestra/tenanti::drivers.user')->andReturn($option) - ->shouldReceive('get')->once()->with('orchestra/tenanti::chunk', 100)->andReturn(100); + $config = [ + 'drivers' => ['user' => ['model' => 'User']], + 'chunk' => 100, + ]; $stub = new TenantiManager($app); + $stub->setConfig($config); $resolver = $stub->driver('user'); @@ -47,12 +47,13 @@ public function testDriverMethod() public function testDriverMethodGivenDriverNotAvailable() { $app = new Container(); - $app['config'] = $config = m::mock('\Illuminate\Config\Repository'); - $config->shouldReceive('get')->once()->with('orchestra/tenanti::drivers.user')->andReturnNull() - ->shouldReceive('get')->once()->with('orchestra/tenanti::chunk', 100)->andReturn(100); + $config = [ + 'drivers' => [], + 'chunk' => 100, + ]; - with(new TenantiManager($app))->driver('user'); + with(new TenantiManager($app))->setConfig($config)->driver('user'); } /** diff --git a/tests/TenantiServiceProviderTest.php b/tests/TenantiServiceProviderTest.php index 86c7b6e..d1683a8 100644 --- a/tests/TenantiServiceProviderTest.php +++ b/tests/TenantiServiceProviderTest.php @@ -24,7 +24,10 @@ public function testServiceProviderIsDeferred() */ public function testRegisterMethod() { - $app = m::mock('\Illuminate\Container\Container[singleton]'); + $app = m::mock('\Illuminate\Container\Container[singleton]'); + $app['config'] = $config = m::mock('\Illuminate\Contracts\Config\Repository', '\ArrayAccess'); + + $config->shouldReceive('offsetGet')->once()->with('orchestra.tenanti')->andReturn([]); $app->shouldReceive('singleton')->once()->with('orchestra.tenanti', m::type('Closure')) ->andReturnUsing(function ($n, $c) use ($app) { @@ -44,11 +47,14 @@ public function testRegisterMethod() */ public function testBootMethod() { - $stub = m::mock('\Orchestra\Tenanti\TenantiServiceProvider[addConfigComponent]', [null]); - $path = realpath(__DIR__.'/../resources/config'); + $stub = m::mock('\Orchestra\Tenanti\TenantiServiceProvider[addConfigComponent,bootUsingLaravel]', [null]) + ->shouldAllowMockingProtectedMethods(); + $path = realpath(__DIR__.'/../resources'); $stub->shouldReceive('addConfigComponent')->once() - ->with('orchestra/tenanti', 'orchestra/tenanti', $path)->andReturnNull(); + ->with('orchestra/tenanti', 'orchestra/tenanti', $path.'/config')->andReturnNull() + ->shouldReceive('bootUsingLaravel')->once() + ->with($path)->andReturnNull(); $this->assertNull($stub->boot()); }