-
Notifications
You must be signed in to change notification settings - Fork 9
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
3 changed files
with
181 additions
and
1 deletion.
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
118 changes: 118 additions & 0 deletions
118
library/Xi/Filelib/Storage/Adapter/FlysystemStorageAdapter.php
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,118 @@ | ||
<?php | ||
|
||
/** | ||
* This file is part of the Xi Filelib package. | ||
* | ||
* For copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Xi\Filelib\Storage\Adapter; | ||
|
||
use League\Flysystem\AdapterInterface; | ||
use League\Flysystem\Filesystem; | ||
use Xi\Filelib\Identifiable; | ||
use Xi\Filelib\Resource\Resource; | ||
use Xi\Filelib\Storage\Adapter\Filesystem\PathCalculator\PathCalculator; | ||
use Xi\Filelib\Storage\Adapter\Filesystem\PathCalculator\LegacyPathCalculator; | ||
use Xi\Filelib\Storage\Retrieved; | ||
use Xi\Filelib\Version; | ||
use Xi\Filelib\Versionable; | ||
|
||
/** | ||
* Stores files in a filesystem | ||
* | ||
* @author pekkis | ||
*/ | ||
class FlysystemStorageAdapter extends BaseTemporaryRetrievingStorageAdapter | ||
{ | ||
/** | ||
* @var Filesystem | ||
*/ | ||
private $filesystem; | ||
|
||
/** | ||
* @var PathCalculator | ||
*/ | ||
private $pathCalculator; | ||
|
||
/** | ||
* @param Filesystem $filesystem | ||
* @param PathCalculator $pathCalculator | ||
* @param string $tempDir | ||
*/ | ||
public function __construct( | ||
Filesystem $filesystem, | ||
PathCalculator $pathCalculator, | ||
$tempDir = null | ||
) { | ||
|
||
$this->filesystem = $filesystem; | ||
$this->pathCalculator = ($pathCalculator) ?: new LegacyPathCalculator(); | ||
} | ||
|
||
private function getPathName(Resource $resource) | ||
{ | ||
return $this->pathCalculator->getPath($resource); | ||
} | ||
|
||
private function getVersionPathName(Versionable $versionable, Version $version) | ||
{ | ||
return $this->pathCalculator->getPathVersion($versionable, $version); | ||
} | ||
|
||
public function store(Resource $resource, $tempFile) | ||
{ | ||
$pathName = $this->getPathName($resource); | ||
$this->filesystem->write($pathName, file_get_contents($tempFile)); | ||
} | ||
|
||
public function storeVersion(Versionable $versionable, Version $version, $tempFile) | ||
{ | ||
$pathName = $this->getVersionPathName($versionable, $version); | ||
$this->filesystem->write($pathName, file_get_contents($tempFile)); | ||
} | ||
|
||
public function retrieve(Resource $resource) | ||
{ | ||
$tmp = $this->getTemporaryFilename(); | ||
$file = | ||
file_put_contents( | ||
$tmp, | ||
$this->filesystem->get($this->getPathName($resource))->read() | ||
); | ||
return new Retrieved($tmp, true); | ||
} | ||
|
||
public function retrieveVersion(Versionable $versionable, Version $version) | ||
{ | ||
$tmp = $this->getTemporaryFilename(); | ||
file_put_contents( | ||
$tmp, | ||
$this->filesystem->get( | ||
$this->getVersionPathName($versionable, $version) | ||
)->read() | ||
); | ||
return new Retrieved($tmp, true); | ||
} | ||
|
||
public function delete(Resource $resource) | ||
{ | ||
$this->filesystem->delete($this->getPathName($resource)); | ||
} | ||
|
||
public function deleteVersion(Versionable $versionable, Version $version) | ||
{ | ||
$this->filesystem->delete($this->getVersionPathName($versionable, $version)); | ||
} | ||
|
||
public function exists(Resource $resource) | ||
{ | ||
return $this->filesystem->has($this->getPathName($resource)); | ||
} | ||
|
||
public function versionExists(Versionable $versionable, Version $version) | ||
{ | ||
return $this->filesystem->has($this->getVersionPathName($versionable, $version)); | ||
} | ||
} |
61 changes: 61 additions & 0 deletions
61
tests/Xi/Filelib/Tests/Storage/Adapter/FlysystemStorageAdapterTest.php
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,61 @@ | ||
<?php | ||
|
||
/** | ||
* This file is part of the Xi Filelib package. | ||
* | ||
* For copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Xi\Filelib\Tests\Storage\Adapter; | ||
|
||
use League\Flysystem\Plugin\ListFiles; | ||
use Xi\Filelib\Storage\Adapter\Filesystem\DirectoryIdCalculator\TimeDirectoryIdCalculator; | ||
use League\Flysystem\Filesystem; | ||
use League\Flysystem\Adapter\Local as LocalAdapter; | ||
use Xi\Filelib\Storage\Adapter\Filesystem\PathCalculator\LegacyPathCalculator; | ||
use Xi\Filelib\Storage\Adapter\FlysystemStorageAdapter; | ||
|
||
/** | ||
* @group storage | ||
*/ | ||
class FlysystemStorageAdapterTest extends TestCase | ||
{ | ||
private $filesystem; | ||
|
||
/** | ||
* @return Filesystem | ||
*/ | ||
private function getFilesystem() | ||
{ | ||
if (!$this->filesystem) { | ||
$adapter = new LocalAdapter(ROOT_TESTS . '/data/files'); | ||
$this->filesystem = new Filesystem($adapter); | ||
} | ||
|
||
return $this->filesystem; | ||
} | ||
|
||
protected function tearDown() | ||
{ | ||
$filesystem = $this->getFilesystem(); | ||
foreach ($filesystem->listFiles('', true) as $file) { | ||
|
||
if (!preg_match('#\.gitignore#', $file['path'])) { | ||
$filesystem->delete($file['path']); | ||
} | ||
} | ||
} | ||
|
||
protected function getStorage() | ||
{ | ||
$filesystem = $this->getFilesystem(); | ||
$filesystem->addPlugin(new ListFiles()); | ||
|
||
$dc = new TimeDirectoryIdCalculator(); | ||
$pc = new LegacyPathCalculator($dc); | ||
$storage = new FlysystemStorageAdapter($filesystem, $pc, false); | ||
return array($storage, true); | ||
} | ||
|
||
} |