Skip to content

Commit

Permalink
v3.0.2
Browse files Browse the repository at this point in the history
* Add fallback support to Laravel 5 configuration.

Signed-off-by: crynobone <crynobone@gmail.com>
  • Loading branch information
crynobone committed Mar 18, 2015
1 parent b76e61a commit 79b4b9e
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 16 deletions.
4 changes: 4 additions & 0 deletions docs/changes.md
Expand Up @@ -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.
Expand Down
36 changes: 34 additions & 2 deletions src/TenantiManager.php
@@ -1,10 +1,18 @@
<?php namespace Orchestra\Tenanti;

use Illuminate\Support\Arr;
use InvalidArgumentException;
use Illuminate\Support\Manager;

class TenantiManager extends Manager
{
/**
* Configuration values.
*
* @var array
*/
protected $config = [];

/**
* Migration factory resolver.
*
Expand All @@ -23,8 +31,8 @@ class TenantiManager extends Manager
*/
protected function createDriver($driver)
{
$config = $this->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.");
Expand All @@ -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;
}
}
27 changes: 26 additions & 1 deletion src/TenantiServiceProvider.php
Expand Up @@ -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');
Expand All @@ -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'),
]);
}

/**
Expand Down
19 changes: 10 additions & 9 deletions tests/TenantiManagerTest.php
Expand Up @@ -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');

Expand All @@ -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');
}

/**
Expand Down
14 changes: 10 additions & 4 deletions tests/TenantiServiceProviderTest.php
Expand Up @@ -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) {
Expand All @@ -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());
}
Expand Down

0 comments on commit 79b4b9e

Please sign in to comment.