From d63d5e99445936bfa8ad77f5cbcd0de2700f8231 Mon Sep 17 00:00:00 2001 From: Westin Shafer Date: Thu, 14 Sep 2017 16:48:33 -0600 Subject: [PATCH] Fix Adaptor Cache and Clean up ReadMe --- README.md | 113 +++++++++++----------- src/Cache/AdaptorCacheFactory.php | 16 ++-- src/FlySystemManager.php | 20 ++++ tests/Cache/AdaptorCacheFactoryTest.php | 120 ++++++------------------ 4 files changed, 112 insertions(+), 157 deletions(-) diff --git a/README.md b/README.md index dd88093..23455b3 100644 --- a/README.md +++ b/README.md @@ -480,7 +480,7 @@ return [ 'adaptors' => [ // Array Keys are the names used for the adaptor 'default' => [ - 'type' => 'local', #A daptor name or pre-configured service from the container + 'type' => 'local', # Adaptor name or pre-configured service from the container // Adaptor specific options. See adaptors below 'options' => [ @@ -516,7 +516,7 @@ return [ 'adaptorTwo' => [ 'type' => 'null', // Adaptor name or pre-configured service from the container - 'options' => [], // Adaptor specific options. See adaptors below + 'options' => [], // Adaptor specific options. See adaptors below ], ], @@ -535,7 +535,7 @@ return [ ], ], - 'cache_two' => [ + 'cacheTwo' => [ 'type' => 'psr6', // Cache specific options. See caches below 'options' => [ @@ -554,8 +554,8 @@ return [ // default file system 'default' => [ 'adaptor' => 'default', // Adaptor name from adaptor configuration - 'cache' => 'default', // Cache name from adaptor configuration - 'plugins' => [] // User defined plugins to be injected into the file system + 'cache' => 'default', // Cache name from adaptor configuration + 'plugins' => [] // User defined plugins to be injected into the file system ], // Mount Manager Config @@ -564,14 +564,14 @@ return [ 'fileSystems' => [ 'local' => [ 'adaptor' => 'default', // Adaptor name from adaptor configuration - 'cache' => 'cache_one', // PSR-6 pre-configured service - 'plugins' => [] // User defined plugins to be injected into the file system + 'cache' => 'default', // PSR-6 pre-configured service + 'plugins' => [] // User defined plugins to be injected into the file system ], 'anotherFileSystem' => [ - 'adaptor' => 'adaptor_two', // Adaptor name from adaptor configuration - 'cache' => 'cache_two', // PSR-6 pre-configured service - 'plugins' => [] // User defined plugins to be injected into the file system + 'adaptor' => 'adaptorTwo', // Adaptor name from adaptor configuration + 'cache' => 'cacheTwo', // PSR-6 pre-configured service + 'plugins' => [] // User defined plugins to be injected into the file system ], ], ], @@ -590,9 +590,9 @@ return [ 'fileSystems' => [ // Array Keys are the file systems identifiers 'myFileSystemName' => [ - 'adaptor' => 'default', // Adaptor name from adaptor configuration - 'cache' => 'default', // Cache name from adaptor configuration - 'plugins' => [] // User defined plugins to be injected into the file system + 'adaptor' => 'default', // Required : Adaptor name from adaptor configuration + 'cache' => 'default', // Optional : Cache name from adaptor configuration + 'plugins' => [] // Optional : User defined plugins to be injected into the file system ], ], ], @@ -631,9 +631,11 @@ return [ 'default' => [ 'type' => 'local', 'options' => [ - 'root' => '/path/to/root', // Path on local filesystem - 'writeFlags' => LOCK_EX, // PHP flags. See: file_get_contents for more info - 'linkBehavior' => League\Flysystem\Adapter\Local::DISALLOW_LINKS, #Link behavior + 'root' => '/path/to/root', // Required : Path on local filesystem + 'writeFlags' => LOCK_EX, // Optional : PHP flags. See: file_get_contents for more info + 'linkBehavior' => League\Flysystem\Adapter\Local::DISALLOW_LINKS, // Optional : Link behavior + + // Optional: Optional set of permissions to set for files 'permissions' => [ 'file' => [ 'public' => 0644, @@ -664,11 +666,11 @@ return [ 'default' => [ 'type' => 'ftp', 'options' => [ - 'host' => 'ftp.example.com', - 'username' => 'username', - 'password' => 'password', + 'host' => 'ftp.example.com', // Required : Host + 'username' => 'username', // Required : Username + 'password' => 'password', // Required : Password - /** optional config settings */ + // optional config settings 'port' => 21, 'root' => '/path/to/root', 'passive' => true, @@ -697,15 +699,15 @@ return [ 'flysystem' => [ 'adaptors' => [ 'default' => [ - 'type' => 'ftp', + 'type' => 'sftp', 'options' => [ - 'host' => 'example.com', - 'port' => 21, - 'username' => 'username', - 'password' => 'password', - 'privateKey' => 'path/to/or/contents/of/privatekey', - 'root' => '/path/to/root', - 'timeout' => 10, + 'host' => 'example.com', // Required : Host + 'port' => 21, // Optional : Port + 'username' => 'username', // Optional : Username + 'password' => 'password', // Optional : Password + 'privateKey' => 'path/to/or/contents/of/privatekey', // Optional : Host + 'root' => '/path/to/root', // Required : Root Path + 'timeout' => 10, // Optional : Timeout ], ], ], @@ -731,7 +733,7 @@ return [ 'adaptors' => [ 'default' => [ 'type' => 'memory', - 'options' => [], + 'options' => [], // No options available ], ], ], @@ -757,7 +759,7 @@ return [ 'default' => [ 'type' => 'zip', 'options' => [ - 'path' => '/some/path/to/file.zip' + 'path' => '/some/path/to/file.zip' // Required : File name and path to use for zip file ], ], ], @@ -784,10 +786,10 @@ return [ 'default' => [ 'type' => 'azure', 'options' => [ - 'accountName' => 'account-name', - 'apiKey' => 'api-key', - 'container' => 'container-name', - 'prefix' => 'prefix_', // Optional + 'accountName' => 'account-name', // Required : Account Name + 'apiKey' => 'api-key', // Required : API Key + 'container' => 'container-name', // Required : Container name + 'prefix' => 'prefix_', // Optional ], ], ], @@ -814,12 +816,12 @@ return [ 'default' => [ 'type' => 's3', 'options' => [ - 'key' => 'aws-key', - 'secret' => 'aws-secret', - 'region' => 'us-east-1', - 'bucket' => 'bucket-name', - 'prefix' => 'some/prefix', #optional - 'version' => 'latest' // Default: 'latest' + 'key' => 'aws-key', // Required : Key + 'secret' => 'aws-secret', // Required : Secret + 'region' => 'us-east-1', // Required : Region + 'bucket' => 'bucket-name', // Required : Bucket Name + 'prefix' => 'some/prefix', // Optional : Prefix + 'version' => 'latest' // Optional : Api Version. Default: 'latest' ], ], ], @@ -845,8 +847,8 @@ return [ 'default' => [ 'type' => 'dropbox', 'options' => [ - 'token' => 'my-token', - 'prefix' => 'prefix', #optional + 'token' => 'my-token', // Required : API Token + 'prefix' => 'prefix', // Optional : Prefix ], ], ], @@ -868,7 +870,7 @@ return [ 'caches' => [ 'default' => [ 'type' => 'memory', - 'options' => [], #No options available + 'options' => [], // No options available ], ], ], @@ -890,10 +892,9 @@ return [ 'default' => [ 'type' => 'adaptor', 'options' => [ - 'managerServiceName' => \WShafer\PSR11FlySystem\FlySystemManager::class, // Optional. Only needed if you change the service name of the Fly Manager - 'fileSystem' => 'MyFileSystemName', // Filesystem name found in the FileSystems config - 'fileName' => 'my_cache_file', // File name for cache file - 'ttl' => 300 + 'adaptor' => 'MyAdaptorName', // Required : Adaptor name found in the adaptor config + 'fileName' => 'my_cache_file', // Required : File name for cache file + 'ttl' => 300 // Optional : Time to live ], ], ], @@ -913,9 +914,9 @@ return [ 'default' => [ 'type' => 'psr6', 'options' => [ - 'service' => 'my_psr6_service_from_container', // Service to be used from the container - 'key' => 'my_key_', // Cache Key - 'ttl' => 3000 // Expires + 'service' => 'my_psr6_service_from_container', // Required : Service to be used from the container + 'key' => 'my_key_', // Required : Cache Key + 'ttl' => 3000 // Optional : Time to live ], ], ], @@ -935,9 +936,9 @@ return [ 'default' => [ 'type' => 'predis', 'options' => [ - 'service' => 'my_predis_client_from_container', // Configured Predis Client Service to pull from container - 'key' => 'my_key_', // Cache Key - 'ttl' => 3000 // Expires + 'service' => 'my_predis_client_from_container', // Required : Configured Predis Client Service to pull from container + 'key' => 'my_key_', // Required : Cache Key + 'ttl' => 3000 // Optional : Time to live ], ], ], @@ -957,9 +958,9 @@ return [ 'default' => [ 'type' => 'memcached', 'options' => [ - 'service' => 'my_memcached_client_from_container', // Configured Memcached Client Service to pull from container - 'key' => 'my_key_', // Cache Key - 'ttl' => 3000 // Expires + 'service' => 'my_memcached_client_from_container', // Required : Configured Memcached Client Service to pull from container + 'key' => 'my_key_', // Required : Cache Key + 'ttl' => 3000 // Optional : Time to live ], ], ], diff --git a/src/Cache/AdaptorCacheFactory.php b/src/Cache/AdaptorCacheFactory.php index c4b465e..0f90f33 100644 --- a/src/Cache/AdaptorCacheFactory.php +++ b/src/Cache/AdaptorCacheFactory.php @@ -6,33 +6,33 @@ use League\Flysystem\Cached\Storage\Adapter; use WShafer\PSR11FlySystem\Exception\MissingConfigException; use WShafer\PSR11FlySystem\Exception\MissingServiceException; +use WShafer\PSR11FlySystem\FlySystemFactory; use WShafer\PSR11FlySystem\FlySystemManager; class AdaptorCacheFactory extends ContainerAwareCacheAbstract { public function __invoke(array $options) { - $managerServiceName = $options['managerServiceName'] ?? FlySystemManager::class; - - if (empty($options['fileSystem'])) { + if (empty($options['adaptor'])) { throw new MissingConfigException( 'Unable to locate cache file adaptor in config' ); } - $fileSystem = $options['fileSystem']; + $adaptor = $options['adaptor']; $fileName = $options['fileName'] ?? 'file_cache'; $ttl = $options['ttl'] ?? null; /** @var FlySystemManager $manager */ - $manager = $this->getService($managerServiceName); + $manager = FlySystemFactory::getFlySystemManager($this->getContainer()); + $adaptorManager = $manager->getAdaptorManager(); - if (!$manager->has($fileSystem)) { + if (!$adaptorManager->has($adaptor)) { throw new MissingServiceException( - 'Unable to locate file system: '.$fileSystem + 'Unable to locate file system: '.$adaptor ); } - return new Adapter($manager->get($fileSystem), $fileName, $ttl); + return new Adapter($adaptorManager->get($adaptor), $fileName, $ttl); } } diff --git a/src/FlySystemManager.php b/src/FlySystemManager.php index e21994d..b8388bb 100644 --- a/src/FlySystemManager.php +++ b/src/FlySystemManager.php @@ -138,4 +138,24 @@ public function has($id) { return $this->config->hasFileSystemConfig($id); } + + /** + * Get the Adaptor manager + * + * @return ContainerInterface + */ + public function getAdaptorManager() + { + return $this->adaptorManager; + } + + /** + * Get the Cache Manager + * + * @return mixed + */ + public function getCacheManager() + { + return $this->cacheManager; + } } diff --git a/tests/Cache/AdaptorCacheFactoryTest.php b/tests/Cache/AdaptorCacheFactoryTest.php index 5d6bad6..82a00b6 100644 --- a/tests/Cache/AdaptorCacheFactoryTest.php +++ b/tests/Cache/AdaptorCacheFactoryTest.php @@ -9,7 +9,9 @@ use Psr\Container\ContainerInterface; use WShafer\PSR11FlySystem\Cache\AdaptorCacheFactory; use WShafer\PSR11FlySystem\Cache\Psr6CacheFactory; +use WShafer\PSR11FlySystem\FlySystemFactory; use WShafer\PSR11FlySystem\FlySystemManager; +use WShafer\PSR11FlySystem\Service\AdaptorManager; /** * @covers \WShafer\PSR11FlySystem\Cache\AdaptorCacheFactory @@ -33,36 +35,36 @@ public function setup() public function testInvoke() { - $mockService = $this->getMockBuilder(FlySystemManager::class) + $mockAdaptorManager = $this->getMockBuilder(AdaptorManager::class) ->disableOriginalConstructor() ->getMock(); - $mockFileAdaptor = $this->getMockBuilder(Local::class) + $mockService = $this->getMockBuilder(FlySystemManager::class) ->disableOriginalConstructor() ->getMock(); $mockService->expects($this->once()) - ->method('get') - ->with('mockFileAdaptor') - ->willReturn($mockFileAdaptor); + ->method('getAdaptorManager') + ->willReturn($mockAdaptorManager); - $mockService->expects($this->once()) - ->method('has') - ->with('mockFileAdaptor') - ->willReturn(true); + FlySystemFactory::setFlySystemManager($mockService); - $this->containerMock->expects($this->once()) + $mockFileAdaptor = $this->getMockBuilder(Local::class) + ->disableOriginalConstructor() + ->getMock(); + + $mockAdaptorManager->expects($this->once()) ->method('get') - ->with(FlySystemManager::class) - ->willReturn($mockService); + ->with('myAdaptor') + ->willReturn($mockFileAdaptor); - $this->containerMock->expects($this->once()) + $mockAdaptorManager->expects($this->once()) ->method('has') - ->with(FlySystemManager::class) + ->with('myAdaptor') ->willReturn(true); $options = [ - 'fileSystem' => 'mockFileAdaptor', + 'adaptor' => 'myAdaptor', 'fileName' => 'cache_file', 'ttl' => 300 ]; @@ -78,7 +80,7 @@ public function testInvoke() public function testInvokeServiceNotFound() { $options = [ - 'fileSystem' => null, + 'adaptor' => null, 'fileName' => 'cache_file', 'ttl' => 300 ]; @@ -91,98 +93,30 @@ public function testInvokeServiceNotFound() */ public function testInvokeFileSystemNotFound() { - $mockService = $this->getMockBuilder(FlySystemManager::class) + $mockAdaptorManager = $this->getMockBuilder(AdaptorManager::class) ->disableOriginalConstructor() ->getMock(); - $mockService->expects($this->once()) - ->method('has') - ->with('mockFileAdaptor') - ->willReturn(false); - - $mockService->expects($this->never()) - ->method('get') - ->with('mockFileAdaptor'); - - $this->containerMock->expects($this->once()) - ->method('get') - ->with(FlySystemManager::class) - ->willReturn($mockService); - - $this->containerMock->expects($this->once()) - ->method('has') - ->with(FlySystemManager::class) - ->willReturn(true); - - $options = [ - 'fileSystem' => 'mockFileAdaptor', - 'fileName' => 'cache_file', - 'ttl' => 300 - ]; - - /** @var Adapter $cache */ - $cache = call_user_func($this->factory, $options); - } - - public function testInvokeWithCustomServiceName() - { $mockService = $this->getMockBuilder(FlySystemManager::class) ->disableOriginalConstructor() ->getMock(); - $mockFileAdaptor = $this->getMockBuilder(Local::class) - ->disableOriginalConstructor() - ->getMock(); - - $mockService->expects($this->once()) - ->method('get') - ->with('mockFileAdaptor') - ->willReturn($mockFileAdaptor); - $mockService->expects($this->once()) - ->method('has') - ->with('mockFileAdaptor') - ->willReturn(true); - - $this->containerMock->expects($this->once()) - ->method('get') - ->with('MyRenamedService') - ->willReturn($mockService); - - $this->containerMock->expects($this->once()) - ->method('has') - ->with('MyRenamedService') - ->willReturn(true); - - $options = [ - 'managerServiceName' => 'MyRenamedService', - 'fileSystem' => 'mockFileAdaptor', - 'fileName' => 'cache_file', - 'ttl' => 300 - ]; + ->method('getAdaptorManager') + ->willReturn($mockAdaptorManager); - /** @var Adapter $cache */ - $cache = call_user_func($this->factory, $options); - $this->assertInstanceOf(Adapter::class, $cache); - } + FlySystemFactory::setFlySystemManager($mockService); - /** - * @expectedException \WShafer\PSR11FlySystem\Exception\MissingServiceException - */ - public function testCantFindCustomServiceName() - { - $this->containerMock->expects($this->never()) - ->method('get') - ->with('notHere'); + $mockAdaptorManager->expects($this->never()) + ->method('get'); - $this->containerMock->expects($this->once()) + $mockAdaptorManager->expects($this->once()) ->method('has') - ->with('notHere') + ->with('myAdaptor') ->willReturn(false); $options = [ - 'managerServiceName' => 'notHere', - 'fileSystem' => 'mockFileAdaptor', + 'adaptor' => 'myAdaptor', 'fileName' => 'cache_file', 'ttl' => 300 ];