-
-
Notifications
You must be signed in to change notification settings - Fork 199
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
10 changed files
with
588 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php | ||
|
||
namespace App\Storage; | ||
|
||
use Google\Cloud\Storage\StorageClient; | ||
use League\Flysystem\Adapter\Local; | ||
use League\Flysystem\Cached\CachedAdapter; | ||
use League\Flysystem\Cached\Storage\Memory; | ||
use Superbalist\Flysystem\GoogleStorage\GoogleStorageAdapter; | ||
|
||
class AdapterFactory | ||
{ | ||
public static function createLocal(string $localPath): Local | ||
{ | ||
return new Local($localPath); | ||
} | ||
|
||
public static function createGoogleStorage(string $gcloudBucket, string $pathPrefix = null): CachedAdapter | ||
{ | ||
$storage = new StorageClient(); | ||
|
||
return new CachedAdapter( | ||
new GoogleStorageAdapter($storage, $storage->bucket($gcloudBucket), $pathPrefix), | ||
new Memory(), | ||
); | ||
} | ||
} |
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,21 @@ | ||
<?php | ||
|
||
namespace App\Storage; | ||
|
||
/** | ||
* This service is here so that the injected services | ||
* are not removed from the container after compilation. | ||
* | ||
* To be removed. | ||
*/ | ||
class DummyService | ||
{ | ||
private StorageInterface $publicStorage; | ||
private StorageInterface $privateStorage; | ||
|
||
public function __construct(StorageInterface $publicStorage, StorageInterface $privateStorage) | ||
{ | ||
$this->publicStorage = $publicStorage; | ||
$this->privateStorage = $privateStorage; | ||
} | ||
} |
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,9 @@ | ||
<?php | ||
|
||
namespace App\Storage\Exception; | ||
|
||
use League\Flysystem\FileExistsException as BaseFileExistsException; | ||
|
||
class FileExistsException extends BaseFileExistsException | ||
{ | ||
} |
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,9 @@ | ||
<?php | ||
|
||
namespace App\Storage\Exception; | ||
|
||
use League\Flysystem\FileNotFoundException as BaseFileNotFoundException; | ||
|
||
class FileNotFoundException extends BaseFileNotFoundException | ||
{ | ||
} |
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,92 @@ | ||
<?php | ||
|
||
namespace App\Storage; | ||
|
||
use App\Storage\Exception\FileExistsException; | ||
use App\Storage\Exception\FileNotFoundException; | ||
use League\Flysystem\AdapterInterface; | ||
use League\Flysystem\FileExistsException as LeagueFileExistsException; | ||
use League\Flysystem\FileNotFoundException as LeagueFileNotFoundException; | ||
use League\Flysystem\Filesystem; | ||
|
||
class Storage implements StorageInterface | ||
{ | ||
private Filesystem $filesystem; | ||
|
||
public function __construct(AdapterInterface $adapter) | ||
{ | ||
$this->filesystem = new Filesystem($adapter); | ||
} | ||
|
||
public function has(string $path): bool | ||
{ | ||
return $this->filesystem->has($path); | ||
} | ||
|
||
public function getMimetype(string $path): string | ||
{ | ||
try { | ||
return $this->filesystem->getMimetype($path); | ||
} catch (LeagueFileNotFoundException $exception) { | ||
throw $this->createFileNotFoundException($path); | ||
} | ||
} | ||
|
||
public function read(string $path): string | ||
{ | ||
try { | ||
return $this->filesystem->read($path); | ||
} catch (LeagueFileNotFoundException $exception) { | ||
throw $this->createFileNotFoundException($path); | ||
} | ||
} | ||
|
||
public function readStream(string $path) | ||
{ | ||
try { | ||
return $this->filesystem->readStream($path); | ||
} catch (LeagueFileNotFoundException $exception) { | ||
throw $this->createFileNotFoundException($path); | ||
} | ||
} | ||
|
||
public function put(string $path, string $contents, array $config = []): bool | ||
{ | ||
return $this->filesystem->put($path, $contents, $config); | ||
} | ||
|
||
public function copy(string $path, string $newPath): bool | ||
{ | ||
try { | ||
return $this->filesystem->copy($path, $newPath); | ||
} catch (LeagueFileNotFoundException $exception) { | ||
throw $this->createFileNotFoundException($path); | ||
} catch (LeagueFileExistsException $exception) { | ||
throw $this->createFileExistsException($newPath); | ||
} | ||
} | ||
|
||
public function delete(string $path): bool | ||
{ | ||
try { | ||
return $this->filesystem->delete($path); | ||
} catch (LeagueFileNotFoundException $exception) { | ||
throw $this->createFileNotFoundException($path); | ||
} | ||
} | ||
|
||
public function listContents(string $directory, bool $recursive = false): array | ||
{ | ||
return $this->filesystem->listContents($directory, $recursive); | ||
} | ||
|
||
private function createFileNotFoundException(string $path): FileNotFoundException | ||
{ | ||
return new FileNotFoundException($path); | ||
} | ||
|
||
private function createFileExistsException(string $path): FileExistsException | ||
{ | ||
return new FileExistsException($path); | ||
} | ||
} |
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,43 @@ | ||
<?php | ||
|
||
namespace App\Storage; | ||
|
||
use App\Storage\Exception\FileExistsException; | ||
use App\Storage\Exception\FileNotFoundException; | ||
|
||
interface StorageInterface | ||
{ | ||
public function has(string $path): bool; | ||
|
||
/** | ||
* @throws FileNotFoundException | ||
*/ | ||
public function getMimetype(string $path): string; | ||
|
||
/** | ||
* @throws FileNotFoundException Thrown if $path does not exist | ||
*/ | ||
public function read(string $path): string; | ||
|
||
/** | ||
* @throws FileNotFoundException Thrown if $path does not exist | ||
* | ||
* @return resource | ||
*/ | ||
public function readStream(string $path); | ||
|
||
public function put(string $path, string $contents, array $config = []): bool; | ||
|
||
/** | ||
* @throws FileExistsException Thrown if $newpath exists | ||
* @throws FileNotFoundException Thrown if $path does not exist | ||
*/ | ||
public function copy(string $path, string $newpath): bool; | ||
|
||
/** | ||
* @throws FileNotFoundException | ||
*/ | ||
public function delete(string $path): bool; | ||
|
||
public function listContents(string $directory, bool $recursive = false): array; | ||
} |
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,37 @@ | ||
<?php | ||
|
||
namespace Tests\App\Storage; | ||
|
||
use App\Storage\AdapterFactory; | ||
use League\Flysystem\Adapter\Local; | ||
use League\Flysystem\AdapterInterface; | ||
use League\Flysystem\Cached\CachedAdapter; | ||
use PHPUnit\Framework\TestCase; | ||
use Superbalist\Flysystem\GoogleStorage\GoogleStorageAdapter; | ||
|
||
class AdapterFactoryTest extends TestCase | ||
{ | ||
public function testCreateLocal(): void | ||
{ | ||
$tmp = sys_get_temp_dir().'/storage-local/'; | ||
|
||
$adapter = AdapterFactory::createLocal($tmp); | ||
|
||
self::assertInstanceOf(Local::class, $adapter); | ||
self::assertInstanceOf(AdapterInterface::class, $adapter); | ||
self::assertSame($tmp, $adapter->getPathPrefix()); | ||
self::assertDirectoryExists($tmp); | ||
|
||
rmdir($tmp); | ||
} | ||
|
||
public function testCreateGoogleStorage(): void | ||
{ | ||
$adapter = AdapterFactory::createGoogleStorage('foo-bucket', 'path/to/dir'); | ||
|
||
self::assertInstanceOf(CachedAdapter::class, $adapter); | ||
self::assertInstanceOf(AdapterInterface::class, $adapter); | ||
self::assertInstanceOf(GoogleStorageAdapter::class, $adapter->getAdapter()); | ||
self::assertSame('path/to/dir/', $adapter->getPathPrefix()); | ||
} | ||
} |
Oops, something went wrong.