diff --git a/.php_cs b/.php_cs index a976ab5..9c499ac 100644 --- a/.php_cs +++ b/.php_cs @@ -1,7 +1,8 @@ in(__DIR__.'/resources') + ->in(__DIR__.'/config') + ->in(__DIR__.'/database') ->in(__DIR__.'/src') ->in(__DIR__.'/tests'); diff --git a/src/MemoryManager.php b/src/MemoryManager.php index 591d0da..e55d1a2 100644 --- a/src/MemoryManager.php +++ b/src/MemoryManager.php @@ -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. @@ -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; } @@ -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)); } /** @@ -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)); @@ -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)); @@ -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)); } @@ -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'; } /** @@ -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; } /** @@ -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); } /** @@ -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 + ); } } diff --git a/src/MemoryServiceProvider.php b/src/MemoryServiceProvider.php index 29e99c5..51dcc69 100644 --- a/src/MemoryServiceProvider.php +++ b/src/MemoryServiceProvider.php @@ -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; }); diff --git a/tests/Feature/MemoryManagerTest.php b/tests/Feature/MemoryManagerTest.php index 2941f6a..9954ee2 100644 --- a/tests/Feature/MemoryManagerTest.php +++ b/tests/Feature/MemoryManagerTest.php @@ -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(); @@ -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(); @@ -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 */