Skip to content

Commit

Permalink
Merge 4796102 into a64c134
Browse files Browse the repository at this point in the history
  • Loading branch information
Westin Shafer committed Sep 14, 2017
2 parents a64c134 + 4796102 commit a72a253
Show file tree
Hide file tree
Showing 14 changed files with 539 additions and 204 deletions.
394 changes: 245 additions & 149 deletions README.md

Large diffs are not rendered by default.

3 changes: 1 addition & 2 deletions Vagrantfile
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ fi
# Install dependencies
add-apt-repository ppa:ondrej/php
apt-get update
apt-get install -y git curl php7.1 php7.1-bcmath php7.1-bz2 php7.1-cli php7.1-curl php7.1-intl php7.1-json php7.1-mbstring php7.1-opcache php7.1-soap php7.1-sqlite3 php7.1-xml php7.1-xsl php7.1-zip libapache2-mod-php7.1
apt-get install -y git curl php7.1 php7.1-bcmath php7.1-bz2 php7.1-cli php7.1-curl php7.1-intl php7.1-json php7.1-mbstring php7.1-opcache php7.1-soap php7.1-sqlite3 php7.1-xml php7.1-xsl php7.1-zip
if [ -e /usr/local/bin/composer ]; then
/usr/local/bin/composer self-update
Expand All @@ -31,7 +31,6 @@ SCRIPT

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
config.vm.box = 'ubuntu/xenial64'
config.vm.network "forwarded_port", guest: 80, host: 8080
config.vm.synced_folder '.', '/var/www'
config.vm.provision 'shell', inline: @script

Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
{fdddsadasda
"name": "wshafer/psr11-flysystem",
"type": "library",
"description": "Flysystem Facotories for PSR-11",
Expand Down
23 changes: 1 addition & 22 deletions src/Config/FileSystemConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,42 +20,21 @@ class FileSystemConfig
*/
public function __construct(array $config)
{
$this->validateConfig($config);
$this->config = $config;

if ($this->isManager()) {
$this->buildManagerFileSystemConfigs();
}
}

/**
* Validate the config array
*
* @param $config
*/
public function validateConfig($config)
{
if (empty($config)) {
throw new MissingConfigException(
'No config found'
);
}

if (empty($config['adaptor'])) {
throw new MissingConfigException(
'No config key of "type" found in adaptor config array.'
);
}
}

/**
* Get the adaptor
*
* @return string
*/
public function getAdaptor()
{
return $this->config['adaptor'];
return $this->config['adaptor'] ?? 'default';
}

/**
Expand Down
14 changes: 8 additions & 6 deletions src/Config/MainConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@ class MainConfig

public function __construct(array $config)
{
$this->validateConfig($config);
$this->validateConfigAndSetDefaults($config);
$this->config = $config;
$this->buildAdaptorConfigs();
$this->buildFileSystemConfigs();
$this->buildCacheConfigs();
}

public function validateConfig($config)
public function validateConfigAndSetDefaults(&$config)
{
if (empty($config)
|| empty($config['flysystem'])
Expand All @@ -40,10 +40,12 @@ public function validateConfig($config)
);
}

if (empty($config['flysystem']['fileSystems'])) {
throw new MissingConfigException(
'No config key of "adaptors" found in flysystem config array.'
);
if (empty($config['flysystem']['fileSystems']['default'])) {
$config['flysystem']['fileSystems']['default'] = [];
}

if (empty($config['flysystem']['caches']['default'])) {
$config['flysystem']['caches']['default']['type'] = 'memory';
}
}

Expand Down
8 changes: 8 additions & 0 deletions src/Exception/InvalidContainerException.php
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
{
}
86 changes: 86 additions & 0 deletions src/FlySystemFactory.php
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;
}
}
2 changes: 1 addition & 1 deletion src/Service/CacheManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public function __construct(
*/
public function get($id)
{
if ($id == 'default') {
if (empty($id)) {
return $this->cacheMapper->get('memory', []);
}

Expand Down
64 changes: 64 additions & 0 deletions test.php
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();
14 changes: 0 additions & 14 deletions tests/Config/FileSystemConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,18 +106,4 @@ public function testManagerMissingFileSystems()
'adaptor' => 'manager'
]);
}

public function testFailWithNoConfig()
{
$this->expectException(MissingConfigException::class);
new FileSystemConfig([]);
}

public function testFailWithNoType()
{
$this->expectException(MissingConfigException::class);
$this->setupFileSystemConfig();
unset($this->settings['adaptor']);
new FileSystemConfig($this->settings);
}
}
7 changes: 0 additions & 7 deletions tests/Config/MainConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -179,13 +179,6 @@ public function testFailWithNoMainKey()
new MainConfig($this->settings);
}

public function testFailWithNoFileSystems()
{
$this->expectException(MissingConfigException::class);
unset($this->settings['flysystem']['fileSystems']);
new MainConfig($this->settings);
}

public function testFailWithNoAdaptors()
{
$this->expectException(MissingConfigException::class);
Expand Down

0 comments on commit a72a253

Please sign in to comment.