Skip to content
This repository has been archived by the owner on Mar 1, 2023. It is now read-only.

Commit

Permalink
fixes #316 | cs fixes | bug fixes | added copy and move dir functions
Browse files Browse the repository at this point in the history
  • Loading branch information
prisis committed Sep 10, 2016
1 parent e000590 commit 6733484
Show file tree
Hide file tree
Showing 25 changed files with 580 additions and 228 deletions.
24 changes: 11 additions & 13 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
"defuse/php-encryption" : "^2.0",
"filp/whoops" : "^2.0",
"http-interop/http-factory" : "dev-master",
"league/flysystem" : "^1.0",
"league/flysystem" : "^1.0.27",
"monolog/monolog" : "^1.17",
"narrowspark/arr" : "^1.1",
"narrowspark/http-status" : "^2.0",
Expand Down Expand Up @@ -113,7 +113,7 @@
"cache/void-adapter" : "^0.3",
"guzzlehttp/guzzle" : "^6.0",
"league/flysystem-aws-s3-v3" : "^1.0",
"league/flysystem-cached-adapter" : "^1.0",
"league/flysystem-cached-adapter" : "^1.0.3",
"league/flysystem-dropbox" : "^1.0",
"league/flysystem-gridfs" : "^1.0",
"league/flysystem-rackspace" : "^1.0",
Expand Down Expand Up @@ -163,18 +163,16 @@
"cache/session-handler" : "Required to use the Session cache (^0.1).",
"cache/void-adapter" : "Required to use the Void cache (^0.3).",
"guzzlehttp/guzzle" : "Required to use the Mailgun and Mandrill mail drivers and the ping methods on schedules (^6.0).",
"league/flysystem" : "Required to use the Flysystem Local and FTP drivers (^1.3).",
"league/flysystem-aws-s3-v3" : "Required to use the Flysystem S3 driver (^1.0).",
"league/flysystem-azure" : "Required to use the Flysystem Azure adapter.",
"league/flysystem-cached-adapter" : "Required to use the Flysystem Caching adapter.",
"league/flysystem-dropbox" : "Required to use the Flysystem Dropbox adapter.",
"league/flysystem-gridfs" : "Required to use the Flysystem GridFS adapter.",
"league/flysystem-rackspace" : "Required to use the Flysystem Rackspace adapter.",
"league/flysystem-replicate-adapter" : "Required to use the Flysystem Replicate adapter.",
"league/flysystem-sftp" : "Required to use the Flysystem SFTP adapter.",
"league/flysystem-aws-s3-v3" : "Required to use the Flysystem S3 adapter (^1.0).",
"league/flysystem-cached-adapter" : "Required to use the Flysystem Caching adapter (^1.0.3).",
"league/flysystem-dropbox" : "Required to use the Flysystem Dropbox adapter (^1.0).",
"league/flysystem-gridfs" : "Required to use the Flysystem GridFS adapter (^1.0).",
"league/flysystem-rackspace" : "Required to use the Flysystem Rackspace adapter (^1.0).",
"league/flysystem-replicate-adapter" : "Required to use the Flysystem Replicate adapter (^1.0).",
"league/flysystem-sftp" : "Required to use the Flysystem SFTP adapter (^1.0).",
"league/flysystem-webdav" : "Required to use the Flysystem WebDav adapter (^1.0).",
"league/flysystem-vfs" : "Required to use the Flysystem VFS adapter (^1.0).",
"league/flysystem-webdav" : "Required to use the Flysystem WebDav adapter.",
"league/flysystem-ziparchive" : "Required to use the Flysystem ZipArchive adapter.",
"league/flysystem-ziparchive" : "Required to use the Flysystem ZipArchive adapter (^1.0).",
"league/plates" : "Required to use the League Plates view adapter (^3.1).",
"pda/pheanstalk" : "Required to use the Beanstalk queue driver (^3.0).",
"predis/predis" : "Required to use the Redis cache and Queue drivers (^1.0).",
Expand Down
35 changes: 18 additions & 17 deletions src/Viserio/Cache/CacheManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,10 @@
use MongoDB\Driver\Manager as MongoDBManager;
use Predis\Client as PredisClient;
use Redis;
use Viserio\Contracts\Cache\Manager as CacheManagerContract;
use Viserio\Support\AbstractManager;

class CacheManager extends AbstractManager
class CacheManager extends AbstractManager implements CacheManagerContract
{
/**
* {@inheritdoc}
Expand Down Expand Up @@ -60,6 +61,22 @@ public function chain(array $pools, array $options = null): CachePoolChain
);
}

/**
* {@inheritdoc}
*/
public function createDriver(array $config)
{
$driver = parent::createDriver($config);

$namespace = $this->config->get($this->getConfigName() . '.namespace');

if ($namespace !== null && $driver instanceof HierarchicalPoolInterface) {
return $this->namespacedPool($driver, $namespace);
}

return $driver;
}

/**
* Create an instance of the Apc cache driver.
*
Expand Down Expand Up @@ -239,22 +256,6 @@ protected function createSessionDriver(array $config): Psr6SessionHandler
return new Psr6SessionHandler($pool, $config['config']);
}

/**
* {@inheritdoc}
*/
protected function createDriver(array $config)
{
$driver = parent::createDriver($config);

$namespace = $this->config->get($this->getConfigName() . '.namespace');

if ($namespace !== null && $driver instanceof HierarchicalPoolInterface) {
return $this->namespacedPool($driver, $namespace);
}

return $driver;
}

/**
* Create a prefixed cache pool with a namespace.
*
Expand Down
4 changes: 4 additions & 0 deletions src/Viserio/Cache/Providers/CacheServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Interop\Container\ServiceProvider;
use Viserio\Cache\CacheManager;
use Viserio\Config\Manager as ConfigManager;
use Viserio\Contracts\Cache\Manager as CacheManagerContract;

class CacheServiceProvider implements ServiceProvider
{
Expand All @@ -16,6 +17,9 @@ public function getServices()
{
return [
CacheManager::class => [self::class, 'registerCacheFactory'],
CacheManagerContract::class => function (ContainerInterface $container) {
return $container->get(CacheManager::class);
},
'cache' => function (ContainerInterface $container) {
return $container->get(CacheManager::class);
},
Expand Down
7 changes: 7 additions & 0 deletions src/Viserio/Contracts/Cache/Manager.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php
declare(strict_types=1);
namespace Viserio\Contracts\Cache;

interface Manager
{
}
46 changes: 46 additions & 0 deletions src/Viserio/Contracts/Cache/Traits/CacheAwareTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php
declare(strict_types=1);
namespace Viserio\Contracts\Cache\Traits;

use RuntimeException;
use Viserio\Contracts\Cache\Manager;

trait CacheAwareTrait
{
/**
* Cache Manager instance.
*
* @var \Viserio\Contracts\Cache\Manager|null
*/
protected $cache;

/**
* Set a Cache Manager.
*
* @param \Viserio\Contracts\Cache\Manager $cache
*
* @return $this
*/
public function setCacheManager(Manager $cache)
{
$this->cache = $cache;

return $this;
}

/**
* Get the Cache Manager.
*
* @throws \RuntimeException
*
* @return \Viserio\Contracts\Cache\Manager
*/
public function getCacheManager(): Manager
{
if (! $this->cache) {
throw new RuntimeException('Cache Manager is not set up.');
}

return $this->cache;
}
}
2 changes: 1 addition & 1 deletion src/Viserio/Exception/Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -480,7 +480,7 @@ protected function getFiltered(
*/
protected function getTransformed(Throwable $exception): Throwable
{
$container = $this->getContainer();
$container = $this->getContainer();
$transformers = array_merge(
$this->transformers,
$container->get(ConfigManagerContract::class)->get('exception.transformers', [])
Expand Down
90 changes: 90 additions & 0 deletions src/Viserio/Filesystem/Cache/CachedFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<?php
declare(strict_types=1);
namespace Viserio\Filesystem\Cache;

use InvalidArgumentException;
use League\Flysystem\Cached\CacheInterface;
use League\Flysystem\Cached\Storage\Adapter;
use League\Flysystem\Cached\Storage\Psr6Cache;
use Viserio\Contracts\Cache\Manager as CacheManagerContract;
use Viserio\Filesystem\FilesystemManager;

class CachedFactory
{
/**
* Instance of FilesystemManager.
*
* @var \Viserio\Filesystem\FilesystemManager
*/
protected $manager;

/**
* Instance of CacheManager.
*
* @var \Viserio\Contracts\Cache\Manager
*/
protected $cacheManager;

/**
* Create a new cached factory instance.
*
* @param \Viserio\Filesystem\FilesystemManager $manager
*/
public function __construct(FilesystemManager $manager, CacheManagerContract $cacheManager = null)
{
$this->manager = $manager;
$this->cacheManager = $cacheManager;
}

/**
* Establish a cache connection.
*
* @param array $config
*
* @throws \InvalidArgumentException
*
* @return \League\Flysystem\Cached\CacheInterface
*/
public function connection(array $config): CacheInterface
{
if (! isset($config['cache'], $config['cache']['driver'])) {
throw new InvalidArgumentException('A driver must be specified.');
}

return $this->createConnector($config);
}

/**
* Create a connector instance based on the configuration.
*
* @param array $config
*
* @throws \InvalidArgumentException
*
* @return \League\Flysystem\Cached\CacheInterface
*/
protected function createConnector(array $config): CacheInterface
{
$cacheConfig = $config['cache'];

if (($cache = $this->cacheManager) !== null) {
if ($cache->hasDriver($cacheConfig['driver'])) {
return new Psr6Cache(
$cache->driver($cacheConfig['driver']),
$cacheConfig['key'],
$cacheConfig['expire']
);
}
}

if ($this->manager->hasConnection($cacheConfig['driver'])) {
return new Adapter(
$this->manager->createConnection($config),
$cacheConfig['key'],
$cacheConfig['expire']
);
}

throw new InvalidArgumentException(sprintf('Unsupported driver [%s].', $cacheConfig['driver']));
}
}
36 changes: 27 additions & 9 deletions src/Viserio/Filesystem/FilesystemAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ public function write(string $path, $contents, array $config = []): bool
public function put(string $path, $contents, array $config = []): bool
{
$config['visibility'] = $this->parseVisibility($config['visibility'] ?? null) ?: [];

$flyConfig = new FlyConfig($config);

if (is_resource($contents)) {
Expand Down Expand Up @@ -388,7 +389,7 @@ public function cleanDirectory(string $dirname): bool
*/
public function isDirectory(string $dirname): bool
{
return $this->driver->getMetadata($dirname)['type'] === 'dir';
return is_dir($this->driver->getPathPrefix() . $dirname);
}

/**
Expand All @@ -400,21 +401,31 @@ public function copyDirectory(string $directory, string $destination, array $opt
return false;
}

if (! $this->isDirectory($destination)) {
if (! is_dir($destination)) {
$this->createDirectory($destination, ['visibility' => 'public']);
}

$recursive = true;

if (isset($options['recursive'])) {
$recursive = $options['recursive'];
}
$recursive = $options['recursive'] ?? true;

$contents = $this->driver->listContents($directory, $recursive);

foreach ($contents as $item) {
// code...
if ($item['type'] == 'dir') {
$this->createDirectory(
$destination . str_replace($directory, '', $item['path']),
['visibility' => $this->getVisibility($item['path'])]
);
}

if ($item['type'] == 'file') {
$this->copy(
$item['path'],
$destination . str_replace($directory, '', $item['path'])
);
}
}

return true;
}

/**
Expand All @@ -430,7 +441,14 @@ public function moveDirectory(string $directory, string $destination, array $opt
}
}

if (@rename($directory, $destination) !== true) {
$copy = $this->copyDirectory(
$directory,
$destination,
['visibility' => $this->getVisibility($directory)]
);
$delete = $this->deleteDirectory($directory);

if (!$copy && !$delete) {
return false;
}

Expand Down

0 comments on commit 6733484

Please sign in to comment.