save
This commit is contained in:
@@ -0,0 +1,67 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Acme PHP project.
|
||||
*
|
||||
* (c) Titouan Galopin <galopintitouan@gmail.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace AcmePhp\Core\Filesystem\Adapter;
|
||||
|
||||
use AcmePhp\Core\Filesystem\FilesystemInterface;
|
||||
use League\Flysystem\FilesystemInterface as FlysystemFilesystemInterface;
|
||||
|
||||
class FlysystemAdapter implements FilesystemInterface
|
||||
{
|
||||
/**
|
||||
* @var FlysystemFilesystemInterface
|
||||
*/
|
||||
private $filesystem;
|
||||
|
||||
public function __construct(FlysystemFilesystemInterface $filesystem)
|
||||
{
|
||||
$this->filesystem = $filesystem;
|
||||
}
|
||||
|
||||
public function write(string $path, string $content)
|
||||
{
|
||||
$isOnRemote = $this->filesystem->has($path);
|
||||
if ($isOnRemote && !$this->filesystem->update($path, $content)) {
|
||||
throw $this->createRuntimeException($path, 'updated');
|
||||
}
|
||||
if (!$isOnRemote && !$this->filesystem->write($path, $content)) {
|
||||
throw $this->createRuntimeException($path, 'created');
|
||||
}
|
||||
}
|
||||
|
||||
public function delete(string $path)
|
||||
{
|
||||
$isOnRemote = $this->filesystem->has($path);
|
||||
if ($isOnRemote && !$this->filesystem->delete($path)) {
|
||||
throw $this->createRuntimeException($path, 'delete');
|
||||
}
|
||||
}
|
||||
|
||||
public function createDir(string $path)
|
||||
{
|
||||
$isOnRemote = $this->filesystem->has($path);
|
||||
if (!$isOnRemote && !$this->filesystem->createDir($path)) {
|
||||
throw $this->createRuntimeException($path, 'created');
|
||||
}
|
||||
}
|
||||
|
||||
private function createRuntimeException(string $path, string $action): \RuntimeException
|
||||
{
|
||||
return new \RuntimeException(
|
||||
sprintf(
|
||||
'File %s could not be %s because: %s',
|
||||
$path,
|
||||
$action,
|
||||
error_get_last()
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Acme PHP project.
|
||||
*
|
||||
* (c) Titouan Galopin <galopintitouan@gmail.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace AcmePhp\Core\Filesystem\Adapter;
|
||||
|
||||
use AcmePhp\Core\Filesystem\FilesystemFactoryInterface;
|
||||
use AcmePhp\Core\Filesystem\FilesystemInterface;
|
||||
use League\Flysystem\Adapter\Ftp;
|
||||
use League\Flysystem\Filesystem;
|
||||
|
||||
class FlysystemFtpFactory implements FilesystemFactoryInterface
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function create(array $config): FilesystemInterface
|
||||
{
|
||||
return new FlysystemAdapter(new Filesystem(new Ftp($config)));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Acme PHP project.
|
||||
*
|
||||
* (c) Titouan Galopin <galopintitouan@gmail.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace AcmePhp\Core\Filesystem\Adapter;
|
||||
|
||||
use AcmePhp\Core\Filesystem\FilesystemFactoryInterface;
|
||||
use AcmePhp\Core\Filesystem\FilesystemInterface;
|
||||
use League\Flysystem\Adapter\Local;
|
||||
use League\Flysystem\Filesystem;
|
||||
use Webmozart\Assert\Assert;
|
||||
|
||||
class FlysystemLocalFactory implements FilesystemFactoryInterface
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function create(array $config): FilesystemInterface
|
||||
{
|
||||
Assert::keyExists($config, 'root', 'create::$config expected an array with the key %s.');
|
||||
|
||||
return new FlysystemAdapter(new Filesystem(new Local($config['root'])));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Acme PHP project.
|
||||
*
|
||||
* (c) Titouan Galopin <galopintitouan@gmail.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace AcmePhp\Core\Filesystem\Adapter;
|
||||
|
||||
use AcmePhp\Core\Filesystem\FilesystemFactoryInterface;
|
||||
use AcmePhp\Core\Filesystem\FilesystemInterface;
|
||||
use League\Flysystem\Filesystem;
|
||||
use League\Flysystem\Sftp\SftpAdapter;
|
||||
|
||||
class FlysystemSftpFactory implements FilesystemFactoryInterface
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function create(array $config): FilesystemInterface
|
||||
{
|
||||
return new FlysystemAdapter(new Filesystem(new SftpAdapter($config)));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Acme PHP project.
|
||||
*
|
||||
* (c) Titouan Galopin <galopintitouan@gmail.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace AcmePhp\Core\Filesystem\Adapter;
|
||||
|
||||
use League\Flysystem\Filesystem;
|
||||
use League\Flysystem\Memory\MemoryAdapter;
|
||||
|
||||
class NullAdapter extends FlysystemAdapter
|
||||
{
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct(new Filesystem(new MemoryAdapter()));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Acme PHP project.
|
||||
*
|
||||
* (c) Titouan Galopin <galopintitouan@gmail.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace AcmePhp\Core\Filesystem;
|
||||
|
||||
interface FilesystemFactoryInterface
|
||||
{
|
||||
/**
|
||||
* Create a new Filesystem.
|
||||
*/
|
||||
public function create(array $config): FilesystemInterface;
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Acme PHP project.
|
||||
*
|
||||
* (c) Titouan Galopin <galopintitouan@gmail.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace AcmePhp\Core\Filesystem;
|
||||
|
||||
interface FilesystemInterface
|
||||
{
|
||||
/**
|
||||
* Write content to a file.
|
||||
*/
|
||||
public function write(string $path, string $content);
|
||||
|
||||
/**
|
||||
* Delete a file.
|
||||
*/
|
||||
public function delete(string $path);
|
||||
|
||||
/**
|
||||
* Delete a directory.
|
||||
*/
|
||||
public function createDir(string $path);
|
||||
}
|
||||
Reference in New Issue
Block a user