Skip to content

Commit

Permalink
Added documentation and some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jmartin82 committed Sep 7, 2016
1 parent 106bfa2 commit 33a351b
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 47 deletions.
76 changes: 50 additions & 26 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,32 +45,6 @@ composer require "cmp/storage"
```


##Functions available from storage

### Exists
Check whether a file exists.

### Get
Read a file and return the content.

### GetStream
Retrieves a read-stream for a file.

### Rename
Rename a file.

### Delete
Delete a file or directory (even if is not empty).

### Put
Create a file or update if exists. It will create the missing folders.

### PutStream
Create a file or update if exists. It will create the missing folders.

__*Note:*__ Use stream functions for big files.


##Adapters

It provides a generic API for handling common tasks across multiple file storage engines. If you want add a new one, you have to implements ``\Cmp\Storage\AdapterInterface``.
Expand Down Expand Up @@ -100,6 +74,30 @@ If you want create a custom call strategy you must extend ``\Cmp\Storage\Strateg

__*Note:*__ By default the lib uses the CallAllStrategy.

##Mountpoints

Some times you will want use different adapters or strategies depending of the path you are working. We solve this using the MountableVirtualStorage.
MountableVirtualStorage needs be constructed with one VirtualStorage implementation (Adapter or Strategy) and it binds this VirtualStorage to '/'.

After that you can register new mount points.

Example:

```php
$localMountPoint = new \Cmp\Storage\MountPoint('/tmp', $this->fileSystemStorage);
$publicMountPoint = new \Cmp\Storage\MountPoint('/var/www/app/public', $this->s3Adapter);
$vfs = new \Cmp\Storage\MountableVirtualStorage($this->fileSystemStorage); //bind to /
$vfs->registerMountPoint($localMountPoint);
$vfs->registerMountPoint($publicMountPoint);

$this->vfs->delete('/tmp/testfile'); //running over filesystem adapter
$this->vfs->put('/var/www/app/public/testfile', '..some content..')); //running over AWS S3 adapter
```

* Movement between mount points are also allowed.
* You can register adapters, strategies or any class that implements VirtualStorage


###Builtin

There are two ready to use strategies.
Expand Down Expand Up @@ -133,6 +131,32 @@ __Non fluid calls:__
* `hasLoadedAdapters()`Check if one or more adapters has been loaded


##Functions available from storage

### Exists
Check whether a file exists.

### Get
Read a file and return the content.

### GetStream
Retrieves a read-stream for a file.

### Rename
Rename a file.

### Delete
Delete a file or directory (even if is not empty).

### Put
Create a file or update if exists. It will create the missing folders.

### PutStream
Create a file or update if exists. It will create the missing folders.

__*Note:*__ Use stream functions for big files.


### Requirements

* php 5.6
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "cmp/storage",
"name": "pluggit/storage",
"description": "Virtual storage abstraction layer",
"type": "lib",
"require": {
Expand Down
71 changes: 51 additions & 20 deletions test/integration/Cmp/Storage/MountPointsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,12 @@ public function setUp()
$this->fileSystemStorage = new \Cmp\Storage\Adapter\FileSystemAdapter();

$paths = $this->getAvailablePath();


$localMountPoint = new \Cmp\Storage\MountPoint($paths['tmp'], $this->fileSystemStorage);
$secretMountPoint = new \Cmp\Storage\MountPoint($paths['secret'], $this->fileSystemStorage);
$publicMountPoint = new \Cmp\Storage\MountPoint($paths['public'], $this->s3Adapter);

$this->vfs = new \Cmp\Storage\MountableVirtualStorage($this->fileSystemStorage);
$this->vfs->registerMountPoint($localMountPoint);
$this->vfs->registerMountPoint($secretMountPoint);
Expand Down Expand Up @@ -120,34 +123,47 @@ public function testFilePutStream($path)
$this->assertEquals($content, $this->vfs->get($filename));
}

private function getTempFileNameInPath($localMountPoint)
public function moveFilesBetweenEndpoints()
{
$path = '';
while (true) {
$filename = uniqid('TestMountPoint', true).'.test';
$path = $localMountPoint.DIRECTORY_SEPARATOR.$filename;
if (!file_exists($path)) {
break;
}
}
$path = $this->getAvailablePath();
$filenameOld = $this->getTempFileNameInPath($path['tmp']);
$filenameNew = $this->getTempFileNameInPath($path['public']);

return $path;
$this->assertFalse($this->vfs->exists($filenameOld));
$this->assertFalse($this->vfs->exists($filenameNew));

$this->assertTrue($this->vfs->put($filenameOld, 'testFileRename'));
$this->assertFalse($this->vfs->exists($filenameNew));

$this->assertTrue($this->vfs->rename($filenameOld, $filenameNew));
$this->assertFalse($this->vfs->exists($filenameOld));
$this->assertTrue($this->vfs->exists($filenameNew));
}

private function getTempDirectoryPath($localMountPoint)

public function testFilePutWithStrategies()
{
$path = '';
while (true) {
$filename = uniqid('TestMountPoint', true);
$path = $localMountPoint.DIRECTORY_SEPARATOR.$filename;
if (!file_exists($path)) {
break;
}
}
$path = sys_get_temp_dir().DIRECTORY_SEPARATOR.'strategy';
$callAllStrategy = (new \Cmp\Storage\StorageBuilder())
->setStrategy(
new \Cmp\Storage\Strategy\CallAllStrategy()
)
->addAdapter($this->s3Adapter)
->addAdapter($this->fileSystemStorage)
->build();

return $path;

$this->vfs->registerMountPoint(new \Cmp\Storage\MountPoint($path,$callAllStrategy));

$content = 'This is a put test: '.rand(0, 1000)."\n";
$filename = $this->getTempFileNameInPath($path);
$this->assertFalse($this->vfs->exists($filename));
$this->assertTrue($this->vfs->put($filename, $content));
$this->assertTrue($this->vfs->exists($filename));
$this->assertEquals($content, $this->vfs->get($filename));
}


public function pathProvider()
{
$paths = $this->getAvailablePath();
Expand All @@ -160,6 +176,21 @@ public function pathProvider()
];
}

private function getTempFileNameInPath($localMountPoint)
{
$path = '';
while (true) {
$filename = uniqid('TestMountPoint', true).'.test';
$path = $localMountPoint.DIRECTORY_SEPARATOR.$filename;
if (!file_exists($path)) {
break;
}
}

return $path;
}


private function getAvailablePath()
{
return [
Expand Down

0 comments on commit 33a351b

Please sign in to comment.