-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
539 additions
and
204 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace WShafer\PSR11FlySystem\Exception; | ||
|
||
class InvalidContainerException extends \InvalidArgumentException | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace WShafer\PSR11FlySystem; | ||
|
||
use League\Flysystem\Filesystem; | ||
use League\Flysystem\MountManager; | ||
use Psr\Container\ContainerInterface; | ||
use WShafer\PSR11FlySystem\Exception\InvalidContainerException; | ||
|
||
class FlySystemFactory | ||
{ | ||
/** @var string */ | ||
protected $fileSystemName = 'default'; | ||
|
||
/** @var FlySystemManager */ | ||
protected static $flySystemManager; | ||
|
||
/** | ||
* @param ContainerInterface $container | ||
* | ||
* @return Filesystem|MountManager | ||
*/ | ||
public function __invoke(ContainerInterface $container) | ||
{ | ||
$manager = static::getFlySystemManager($container); | ||
$fileSystemName = $this->getFileSystemName(); | ||
return $manager->get($fileSystemName); | ||
} | ||
|
||
/** | ||
* Magic method for constructing FileSystems by service name | ||
* | ||
* @param $name | ||
* @param $arguments | ||
* | ||
* @return Filesystem|MountManager | ||
*/ | ||
public static function __callStatic($name, $arguments) | ||
{ | ||
if (empty($arguments[0]) | ||
|| !$arguments[0] instanceof ContainerInterface | ||
) { | ||
throw new InvalidContainerException( | ||
'Argument 0 must be an instance of a PSR-11 container' | ||
); | ||
} | ||
|
||
$factory = new static(); | ||
$factory->setFileSystemName($name); | ||
return $factory($arguments[0]); | ||
} | ||
|
||
/** | ||
* @return string | ||
*/ | ||
public function getFileSystemName(): string | ||
{ | ||
return $this->fileSystemName; | ||
} | ||
|
||
/** | ||
* @param string $fileSystemName | ||
*/ | ||
public function setFileSystemName(string $fileSystemName) | ||
{ | ||
$this->fileSystemName = $fileSystemName; | ||
} | ||
|
||
public static function getFlySystemManager(ContainerInterface $container) : FlySystemManager | ||
{ | ||
// @codeCoverageIgnoreStart | ||
if (!static::$flySystemManager) { | ||
$factory = new FlySystemManagerFactory(); | ||
static::setFlySystemManager($factory($container)); | ||
} | ||
// @codeCoverageIgnoreEnd | ||
|
||
return static::$flySystemManager; | ||
} | ||
|
||
public static function setFlySystemManager(FlySystemManager $flySystemManager) | ||
{ | ||
static::$flySystemManager = $flySystemManager; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
<?php | ||
require 'vendor/autoload.php'; | ||
|
||
$serviceManager = new \Zend\ServiceManager\ServiceManager([ | ||
'factories' => [ | ||
'fileSystem' => \WShafer\PSR11FlySystem\FlySystemFactory::class, | ||
'other' => [\WShafer\PSR11FlySystem\FlySystemFactory::class, 'other'], | ||
], | ||
]); | ||
|
||
$serviceManager->setService('config', [ | ||
'flysystem' => [ | ||
'adaptors' => [ | ||
'default' => [ | ||
'type' => 'local', | ||
'options' => [ | ||
'root' => '/tmp/zend' | ||
], | ||
], | ||
], | ||
|
||
'fileSystems' => [ | ||
// Array Keys are the file systems identifiers | ||
'other' => [ | ||
'adaptor' => 'default' | ||
], | ||
], | ||
], | ||
]); | ||
|
||
$container = new \Xtreamwayz\Pimple\Container([ | ||
'fileSystem' => new \WShafer\PSR11FlySystem\FlySystemFactory(), | ||
'other' => function($c) { | ||
return \WShafer\PSR11FlySystem\FlySystemFactory::other($c); | ||
}, | ||
'config' => [ | ||
'flysystem' => [ | ||
'adaptors' => [ | ||
'myFiles' => [ | ||
'type' => 'local', | ||
'options' => [ | ||
'root' => '/tmp/pimple' | ||
], | ||
], | ||
], | ||
|
||
'fileSystems' => [ | ||
// Array Keys are the file systems identifiers | ||
'default' => [ | ||
'adaptor' => 'myFiles', # Adaptor name from adaptor configuration | ||
], | ||
|
||
'other' => [ | ||
'adaptor' => 'myFiles' | ||
], | ||
], | ||
], | ||
] | ||
]); | ||
|
||
/** @var \League\Flysystem\FilesystemInterface $fileSystem */ | ||
$fileSystem = $serviceManager->get('other'); | ||
$fileSystem->put('test1.txt', 'this is also test 2'); | ||
print $fileSystem->get('test1.txt')->read(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.