Skip to content

Commit

Permalink
Update manager.
Browse files Browse the repository at this point in the history
Signed-off-by: Mior Muhammad Zaki <crynobone@gmail.com>
  • Loading branch information
crynobone committed Aug 25, 2019
1 parent 202e627 commit 378d1bf
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 59 deletions.
3 changes: 2 additions & 1 deletion .php_cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
<?php

$finder = PhpCsFixer\Finder::create()
->in(__DIR__.'/resources')
->in(__DIR__.'/config')
->in(__DIR__.'/database')
->in(__DIR__.'/src')
->in(__DIR__.'/tests');

Expand Down
76 changes: 22 additions & 54 deletions src/MemoryManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,21 @@

namespace Orchestra\Memory;

use Exception;
use RuntimeException;
use Orchestra\Support\Manager;
use Orchestra\Memory\Handlers\Cache;
use Orchestra\Memory\Handlers\Fluent;
use Orchestra\Memory\Handlers\Runtime;
use Orchestra\Memory\Handlers\Eloquent;
use Illuminate\Contracts\Container\Container;
use Orchestra\Support\Concerns\WithConfiguration;
use Orchestra\Contracts\Memory\Handler as HandlerContract;
use Orchestra\Contracts\Memory\Provider as ProviderContract;
use Illuminate\Contracts\Cache\Repository as CacheRepository;

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

/**
* The encrypter implementation.
Expand All @@ -32,14 +28,14 @@ class MemoryManager extends Manager
/**
* Create a new manager instance.
*
* @param \Illuminate\Foundation\Application $app
* @param \Illuminate\Contracts\Container\Container $container
*/
public function __construct($app)
public function __construct(Container $container)
{
parent::__construct($app);
parent::__construct($container);

try {
$this->encrypter = $app->make('encrypter');
$this->encrypter = $container->make('encrypter');
} catch (RuntimeException $e) {
$this->encrypter = null;
}
Expand All @@ -54,10 +50,10 @@ public function __construct($app)
*/
protected function createFluentDriver(string $name): ProviderContract
{
$config = $this->config['fluent'][$name] ?? [];
$config = $this->configurations['fluent'][$name] ?? [];
$cache = $this->getCacheRepository($config);

return $this->createProvider(new Fluent($name, $config, $this->app->make('db'), $cache));
return $this->createProvider(new Fluent($name, $config, $this->container->make('db'), $cache));
}

/**
Expand All @@ -69,7 +65,7 @@ protected function createFluentDriver(string $name): ProviderContract
*/
protected function createEloquentDriver(string $name): ProviderContract
{
$config = $this->config['eloquent'][$name] ?? [];
$config = $this->configurations['eloquent'][$name] ?? [];
$cache = $this->getCacheRepository($config);

return $this->createProvider(new Eloquent($name, $config, $this->app, $cache));
Expand All @@ -84,7 +80,7 @@ protected function createEloquentDriver(string $name): ProviderContract
*/
protected function createCacheDriver(string $name): ProviderContract
{
$config = $this->config['cache'][$name] ?? [];
$config = $this->configurations['cache'][$name] ?? [];
$cache = $this->getCacheRepository($config);

return $this->createProvider(new Cache($name, $config, $cache));
Expand All @@ -99,7 +95,7 @@ protected function createCacheDriver(string $name): ProviderContract
*/
protected function createRuntimeDriver(string $name): ProviderContract
{
$config = $this->config['runtime'][$name] ?? [];
$config = $this->configurations['runtime'][$name] ?? [];

return $this->createProvider(new Runtime($name, $config));
}
Expand All @@ -123,7 +119,7 @@ protected function createProvider(HandlerContract $handler): ProviderContract
*/
public function getDefaultDriver()
{
return $this->config['driver'] ?? 'fluent.default';
return $this->configurations['driver'] ?? 'fluent.default';
}

/**
Expand All @@ -135,31 +131,7 @@ public function getDefaultDriver()
*/
public function setDefaultDriver($name)
{
$this->config['driver'] = $name;
}

/**
* 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;
$this->configurations['driver'] = $name;
}

/**
Expand All @@ -171,15 +143,11 @@ public function setConfig(array $config)
*/
public function makeOrFallback(string $fallbackName = 'orchestra'): ProviderContract
{
$fallback = null;

try {
$fallback = $this->make();
} catch (Exception $e) {
$fallback = $this->driver("runtime.{$fallbackName}");
}

return $fallback;
return \rescue(function () {
return $this->make();
}, function () use ($fallbackName) {
return $this->driver("runtime.{$fallbackName}");
}, false);
}

/**
Expand All @@ -206,8 +174,8 @@ public function finish(): void
*/
protected function getCacheRepository(array $config): CacheRepository
{
$connection = $config['connections']['cache'] ?? null;

return $this->app->make('cache')->driver($connection);
return $this->container->make('cache')->driver(
$config['connections']['cache'] ?? null
);
}
}
2 changes: 1 addition & 1 deletion src/MemoryServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public function register()
$manager = new MemoryManager($app);
$namespace = $this->hasPackageRepository() ? 'orchestra/memory::' : 'orchestra.memory';

$manager->setConfig($app->make('config')->get($namespace));
$manager->setConfiguration($app->make('config')->get($namespace));

return $manager;
});
Expand Down
6 changes: 3 additions & 3 deletions tests/Feature/MemoryManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public function it_can_resolve_driver_using_fallback()
'orchestra' => [],
],
];
Memory::setConfig($config);
Memory::setConfiguration($config);

$provider = Memory::makeOrFallback();
$handler = $provider->getHandler();
Expand Down Expand Up @@ -73,7 +73,7 @@ public function it_can_make_with_default_driver()
{
$config = ['driver' => 'runtime.default'];

Memory::setConfig($config);
Memory::setConfiguration($config);
$provider = Memory::make();
$handler = $provider->getHandler();

Expand All @@ -88,7 +88,7 @@ public function it_can_set_default_driver()
{
Memory::setDefaultDriver('foo');

$this->assertSame('foo', Memory::getConfig()['driver']);
$this->assertSame('foo', Memory::getConfiguration()['driver']);
}

/** @test */
Expand Down

0 comments on commit 378d1bf

Please sign in to comment.