Skip to content

Commit

Permalink
restructured plugin to work better with adapters and switched adapter…
Browse files Browse the repository at this point in the history
… to return closures so filters work
  • Loading branch information
cgarvis committed Jan 22, 2012
1 parent 2ace284 commit b58668d
Show file tree
Hide file tree
Showing 5 changed files with 169 additions and 130 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* @license http://opensource.org/licenses/bsd-license.php The BSD License
*/

namespace li3_filesystem\extensions\storage\filesystem\adapter;
namespace li3_filesystem\extensions\adapter\storage\filesystem;

use SplFileInfo;
use RecursiveIteratorIterator;
Expand Down Expand Up @@ -46,7 +46,7 @@ class File extends \lithium\core\Object {
*/
public function __construct(array $config = array()) {
$defaults = array(
'path' => Libraries::get(true, 'path') . 'webroot/img/',
'path' => Libraries::get(true, 'path') . '/webroot/uploads',
);
parent::__construct($config + $defaults);
}
Expand All @@ -55,34 +55,45 @@ public function __construct(array $config = array()) {
* @param string $filename
* @param string $data
* @param array $options
* @return boolean
* @return closure Function returning boolean `true` on successful write, `false` otherwise.
*/
public function write($filename, $data = null, array $options = array()) {
return (file_put_contents($filename, $data) ? true : false);
public function write($filename, $data, array $options = array()) {
$path = $this->_config['path'];
return function($self, $params) use (&$path) {
$data = $params['data'];
$path = "{$path}/{$params['filename']}";
return file_put_contents($path, $data);
};
}

/**
* @param string $filename
* @return string|boolean
*/
public function read($filename) {
if(file_exists($filename)) {
return file_get_contents($filename);
}

return false;
$path = $this->_config['path'];
return function($self, $params) use (&$path) {
$path = "{$path}/{$params['filename']}";
if(file_exists($path)) {
return file_get_contents($path);
}
return false;
};
}

/**
* @param string $filename
* @return boolean
*/
public function delete($filename) {
if(file_exists($filename)) {
return unlink($filename);
}

return false;
$path = $this->_config['path'];
return function($self, $params) use (&$path) {
$path = "{$path}/{$params['filename']}";
if(file_exists($path)) {
return unlink($path);
}
return false;
};
}
}
?>
26 changes: 14 additions & 12 deletions extensions/storage/FileSystem.php → storage/FileSystem.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* @license http://opensource.org/licenses/bsd-license.php The BSD License
*/

namespace li3_filesystem\extensions\storage;
namespace li3_filesystem\storage;

/**
* The `FileSystem` static class provides a consistent interface to configure and utilize the different
Expand All @@ -32,8 +32,6 @@
* @see lithium\core\Adaptable
* @see app\extensions\storage\filesystem\adapter
*/


class FileSystem extends \lithium\core\Adaptable {

/**
Expand All @@ -48,51 +46,55 @@ class FileSystem extends \lithium\core\Adaptable {
*
* @var string Dot-delimited path.
*/
protected static $_adapters = 'storage.filesystem.adapter';
protected static $_adapters = 'adapter.storage.filesystem';

/**
* Writes file from tmp to the specified filesystem configuration.
*
* @param string $name Configuration to be used for writing
* @param string $filePath a full path with filename and extension to be written
* @param string $filename a full path with filename and extension to be written
* @param mixed $data usualy an output of file field
* @param mixed $options Options for the method, filters and strategies.
* @return boolean True on successful FileSystem write, false otherwise
* @filter This method may be filtered.
* @TODO implement configurations
*/
public static function write($name, $filePath, $data, array $options = array()) {
public static function write($name, $filename, $data, array $options = array()) {
$settings = static::config();

$method = static::adapter($name)->write($key, $data, $options);
$params = compact('filePath', 'data');
if (!isset($settings[$name])) {
return false;
}

$method = static::adapter($name)->write($filename, $data, $options);
$params = compact('filename', 'data');
return static::_filter(__FUNCTION__, $params, $method, $settings[$name]['filters']);
}

/**
* Reads file from the specified filesystem configuration
*
* @param string $name Configuration to be used for reading
* @param mixed $filePath a full path with filename and extension to be retrieved
* @param mixed $filename a full path with filename and extension to be retrieved
* @param mixed $options Options for the method and strategies.
* @return mixed Read results on successful filesystem read, null otherwise
* @filter This method may be filtered.
* @TODO implement
*/
public static function read($name, $filePath, array $options = array()) {
public static function read($name, $filename, array $options = array()) {
}

/**
* Deletes file from the specified filesystem configuration
*
* @param string $name Configuration to be used for deletion
* @param mixed $filePath a full path with filename and extension to be deleted
* @param mixed $filename a full path with filename and extension to be deleted
* @param mixed $options Options for the method and strategies.
* @return boolean True on successful deletion, false otherwise
* @filter This method may be filtered.
* @TODO implement
*/
public static function delete($name, $filePath, array $options = array()) {
public static function delete($name, $filename, array $options = array()) {

}
}
Expand Down
124 changes: 124 additions & 0 deletions tests/cases/extensions/adapter/storage/filesystem/FileTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
<?php
/**
* Lithium Filesystem: managing file uploads the easy way
*
* @copyright Copyright 2012, Little Boy Genius (http://www.littleboygenius.org)
* @license http://opensource.org/licenses/bsd-license.php The BSD License
*/

namespace li3_filesystem\tests\cases\extensions\adapter\storage\filesystem;

use SplFileInfo;
use lithium\core\Libraries;
use li3_filesystem\extensions\adapter\storage\filesystem\File;

class FileTest extends \lithium\test\Unit {
protected $configuration = array(
'path' => '/tmp'
);

/**
* Skip the test if the default File adapter read/write path is not read/write-able.
*
* @return void
*/
public function skip() {
$directory = new SplFileInfo($this->configuration['path']);
$accessible = ($directory->isDir() && $directory->isReadable() && $directory->isWritable());
$message = 'The File filesystem adapter path does not have the proper permissions.';
$this->skipIf(!$accessible, $message);
}

public function setUp() {
$this->file = new File($this->configuration);
}

public function tearDown() {
if(file_exists($this->configuration['path'] . '/test_file')) {
unlink($this->configuration['path'] . '/test_file');
}
unset($this->file);
}

public function testSimpleWrite() {
$filename = 'test_file';
$data = 'data';

$closure = $this->file->write($filename, $data);
$this->assertTrue(is_callable($closure));

$params = compact('filename', 'data');
$result = $closure($this->file, $params, null);
$this->assertTrue($result);

$result = file_get_contents($this->configuration['path'] . '/' . $filename);
$this->assertEqual($data, $result);
}

public function testReadNonexistentFile() {
$filename = 'thisfileshouldnotexist_file';

$closure = $this->file->read($filename);
$this->assertTrue(is_callable($closure));

$params = compact('filename');
$result = $closure($this->file, $params, null);
$this->assertFalse($result);
}

public function testReadEmptyFile() {
$filename = 'test_file';
$data = '';

file_put_contents($this->configuration['path'] . '/' . $filename, $data);

$closure = $this->file->read($filename);
$this->assertTrue(is_callable($closure));

$params = compact('filename');
$result = $closure($this->file, $params);
$this->assertEqual($data, $result);
}

public function testReadExistingFile() {
$filename = 'test_file';
$data = 'Some test content';

file_put_contents($this->configuration['path'] . '/' . $filename, $data);

$closure = $this->file->read($filename);
$this->assertTrue(is_callable($closure));

$params = compact('filename');
$result = $closure($this->file, $params);
$this->assertEqual($data, $result);
}

public function testDeleteNonexistentFile() {
$filename = 'no_file';

$closure = $this->file->delete($filename);
$this->assertTrue(is_callable($closure));

$params = compact('filename');
$result = $closure($this->file, $params);
$this->assertTrue($result === FALSE);
}

public function testDeleteExistingFile() {
$filename = 'test_file';
$data = 'Some content';

file_put_contents($this->configuration['path'] . '/' . $filename, $data);

$closure = $this->file->delete($filename);
$this->assertTrue(is_callable($closure));

$params = compact('filename');
$this->assertTrue(file_exists($this->configuration['path'] . '/' . $filename));

$result = $closure($this->file, $params);
$this->assertFalse(file_exists($this->configuration['path'] . '/'. $filename));
}
}
?>
97 changes: 0 additions & 97 deletions tests/cases/extensions/storage/filesystem/adapter/FileTest.php

This file was deleted.

Loading

0 comments on commit b58668d

Please sign in to comment.