Skip to content

Commit

Permalink
Added readf()/writef() methods and tempdir configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
aimeos committed Dec 2, 2015
1 parent 11d3c4d commit 3864d9a
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 12 deletions.
55 changes: 48 additions & 7 deletions lib/custom/src/MW/Filesystem/Laravel.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,19 @@
class Laravel implements Iface, DirIface, MetaIface
{
private $fs;
private $tempdir;


/**
* Initializes the object
*
* @param \Illuminate\Contracts\Filesystem\Filesystem $fs Laravel file system object
* @param string $tempdir Directory for storing temporary files
*/
public function __construct( \Illuminate\Contracts\Filesystem\Filesystem $fs )
public function __construct( \Illuminate\Contracts\Filesystem\Filesystem $fs, $tempdir )
{
$this->fs = $fs;
$this->tempdir = $tempdir;
}


Expand Down Expand Up @@ -181,6 +184,27 @@ public function read( $path )
}


/**
* Reads the content of the remote file and writes it to a local one
*
* @param string $path Path to the remote file
* @return string Path of the local file
* @throws \Aimeos\MW\Filesystem\Exception If an error occurs
*/
public function readf( $path )
{
if( ( $filename = tempnam( $this->tempdir, 'ai-' ) ) === false ) {
throw new Exception( sprintf( 'Unable to create file in "%1$s"', $this->tempdir ) );
}

if( @file_put_contents( $filename, $this->fs->get( $path ) ) === false ) {
throw new Exception( sprintf( 'Couldn\'t write file "%1$s"', $filename ) );
}

return $filename;
}


/**
* Returns the stream descriptor for the file
*
Expand All @@ -199,18 +223,15 @@ public function reads( $path )
}

if( ( $stream = tmpfile() ) === false ) {
$error = error_get_last();
throw new Exception( $error['message'] );
throw new Exception( 'Couldn\'t create temporary file' );
}

if( fwrite( $stream, $content ) === false ) {
$error = error_get_last();
throw new Exception( $error['message'] );
throw new Exception( 'Couldn\'t write to temporary file' );
}

if( rewind( $stream ) === false ) {
$error = error_get_last();
throw new Exception( $error['message'] );
throw new Exception( 'Couldn\'t rewind temporary file' );
}

return $stream;
Expand All @@ -237,6 +258,26 @@ public function write( $path, $content )
}


/**
* Writes the content of the local file to the remote path
*
* {@inheritDoc}
*
* @param string $path Path to the remote file
* @param string $file Path to the local file
* @return void
* @throws \Aimeos\MW\Filesystem\Exception If an error occurs
*/
public function writef( $path, $local )
{
if( ( $content = @file_get_contents( $local ) ) === false ) {
throw new Exception( sprintf( 'Couldn\'t read file "%1$s"', $local ) );
}

$this->write( $path, $content );
}


/**
* Write the content of the stream descriptor into the remote file
*
Expand Down
9 changes: 6 additions & 3 deletions lib/custom/src/MW/Filesystem/Manager/Laravel.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,24 @@
*/
class Laravel extends Standard implements Iface
{
private $objects = array();
private $fsm;
private $objects = array();
private $tempdir;


/**
* Initializes the object
*
* @param \Illuminate\Filesystem\FilesystemManager $fsm Laravel file system manager object
* @param \Aimeos\MW\Config\Iface $config Configuration object
* @param string $tempdir Directory for storing temporary files
*/
public function __construct( \Illuminate\Filesystem\FilesystemManager $fsm, \Aimeos\MW\Config\Iface $config )
public function __construct( \Illuminate\Filesystem\FilesystemManager $fsm, \Aimeos\MW\Config\Iface $config, $tempdir )
{
parent::__construct( $config );

$this->fsm = $fsm;
$this->tempdir = $tempdir;
}


Expand All @@ -51,7 +54,7 @@ public function get( $name )
if( is_string( $key ) )
{
if( !isset( $this->objects[$key] ) ) {
$this->objects[$key] = new \Aimeos\MW\Filesystem\Laravel( $this->fsm->disk( $key ) );
$this->objects[$key] = new \Aimeos\MW\Filesystem\Laravel( $this->fsm->disk( $key ), $this->tempdir );
}

return $this->objects[$key];
Expand Down
34 changes: 33 additions & 1 deletion lib/custom/tests/MW/Filesystem/LaravelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ protected function setUp()
->disableOriginalConstructor()
->getMock();

$this->object = new \Aimeos\MW\Filesystem\Laravel( $this->mock );
$this->object = new \Aimeos\MW\Filesystem\Laravel( $this->mock, sys_get_temp_dir() );
}


Expand Down Expand Up @@ -156,6 +156,18 @@ public function testReadException()
}


public function testReadf()
{
$this->mock->expects( $this->once() )->method( 'get' )
->will( $this->returnValue( 'test' ) );

$result = $this->object->readf( 'file' );

$this->assertEquals( 'test', file_get_contents( $result ) );
unlink( $result );
}


public function testReads()
{
$this->mock->expects( $this->once() )->method( 'get' )
Expand Down Expand Up @@ -198,6 +210,26 @@ public function testWriteException()
}


public function testWritef()
{
$file = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'file99';
file_put_contents( $file, 'test' );

$this->mock->expects( $this->once() )->method( 'put' );

$this->object->writef( 'file', $file );

unlink( $file );
}


public function testWritefException()
{
$this->setExpectedException( '\Aimeos\MW\Filesystem\Exception' );
$this->object->writef( '', 'invalid' );
}


public function testWrites()
{
$this->mock->expects( $this->once() )->method( 'put' );
Expand Down
2 changes: 1 addition & 1 deletion lib/custom/tests/MW/Filesystem/Manager/LaravelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ protected function setUp()
->getMock();

$this->config = new \Aimeos\MW\Config\PHPArray( array(), array() );
$this->object = new \Aimeos\MW\Filesystem\Manager\Laravel( $this->storage, $this->config );
$this->object = new \Aimeos\MW\Filesystem\Manager\Laravel( $this->storage, $this->config, sys_get_temp_dir() );
}


Expand Down

0 comments on commit 3864d9a

Please sign in to comment.