Skip to content

Commit

Permalink
Added support for flysystem.
Browse files Browse the repository at this point in the history
  • Loading branch information
pekkis committed Feb 16, 2015
1 parent c5f5703 commit 01d6c5b
Show file tree
Hide file tree
Showing 3 changed files with 181 additions and 1 deletion.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@
"symfony/security": "~2.2",
"symfony/console": "~2.2",
"symfony/stopwatch": "~2.2",
"knplabs/gaufrette": "~0.1"
"knplabs/gaufrette": "~0.1",
"league/flysystem": "~1.0"
},

"suggest": {
Expand Down
118 changes: 118 additions & 0 deletions library/Xi/Filelib/Storage/Adapter/FlysystemStorageAdapter.php
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));
}
}
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);
}

}

0 comments on commit 01d6c5b

Please sign in to comment.