Skip to content

Commit

Permalink
init commit
Browse files Browse the repository at this point in the history
  • Loading branch information
basz committed Nov 5, 2014
0 parents commit 485ada7
Show file tree
Hide file tree
Showing 47 changed files with 3,131 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
@@ -0,0 +1,4 @@
.project
.DS_Store
composer.lock
vendor
24 changes: 24 additions & 0 deletions .travis.yml
@@ -0,0 +1,24 @@
language: php

php:
- 5.3
- 5.4
- 5.5
- 5.6

before_script:
- composer self-update
- composer install --dev --prefer-source
- mkdir -p test/build

script:
- vendor/bin/phpunit -c ./phpunit.xml
- sh -c "if [ '$TRAVIS_PHP_VERSION' != '5.3' ]; then ./vendor/bin/phpcs --standard=PSR2 ./src/; fi"

after_script:
- wget https://scrutinizer-ci.com/ocular.phar
- php ocular.phar code-coverage:upload --format=php-clover test/build/logs/clover.xml

notifications:
irc: "irc.freenode.org#zftalk.modules"
email: false
22 changes: 22 additions & 0 deletions LICENSE.txt
@@ -0,0 +1,22 @@
Copyright (c) 2014 Bas Kamer

Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
21 changes: 21 additions & 0 deletions Module.php
@@ -0,0 +1,21 @@
<?php

namespace BsbFlysystem;

use Zend\ModuleManager\Feature\AutoloaderProviderInterface;

class Module implements AutoloaderProviderInterface
{
public function getAutoloaderConfig()
{
return array('Zend\Loader\StandardAutoloader' => array('namespaces' => array(
__NAMESPACE__ => __DIR__ . '/src/BsbFlysystem',
)));
}

public function getConfig()
{
return include __DIR__ . '/config/module.config.php';
}

}
182 changes: 182 additions & 0 deletions README.md
@@ -0,0 +1,182 @@
# BsbFlysystem

A simple Zend Framework 2 module that bridges the Flysystem filesystem.

Provides a way to configure the various filesystem adapters provided by the flysystem. These configured adapters are exposed via the service manager.

Visit [flysystem](http://flysystem.thephpleague.com) for detailed usage of the Flysystem library.

Note : WIP do not use in production!

This module allows to retrieve fully configured filesystems by name from the ServiceLocator. These filesystems will be fully configured. This allows your code be agnostic the configuration of adpaters and filesystems. Whether the filesystem is a local filesystem or remote dropbox account, whether you enable caching or use the replicate adapter becomes a configuration detail.

## Installation

```sh
php composer.phar require "bushbaby/flysystem:~0.1"
```

Then add `BsbFlysystem` to the `config/application.config.php` modules list.

Copy the `config/bsb_flysystem.global.php.dist` to the `config/autoload` directory to jump start configuration.

## Configuration

All configuration regarding BsbFlysystem life in the `bsb_flysystem` config key.

The configuration consists of the following base elements;

- *Adapters* are consumed by a Filesystem.
- *Caches* are consumed by a Filesystem.
- *Filesystems* filesystem are consumed in userland.

### Adapters

To configure an adapter you add a key to `bsb_flysystem->adapters` with a associative array containing the following options;

- type \<string\> Type of adapter
- shared \<boolean\> (optional) Defines the shared option of a [ZF2 service](http://framework.zend.com/manual/2.0/en/modules/zend.service-manager.quick-start.html#using-configuration).
- options \<array\> Options specific per adapter (see [flysystem](http://flysystem.thephpleague.com) or config/bsb_flysystem.global.php.dist)


example: a local adapter pointing to ./data/files

```
<?php
'bsb_flysystem' => array(
'adapters' => array(
'local_files' => array(
'type' => 'local',
'options' => array(
'root' => './data/files'
),
),
),
),
```

### Filesystems

Configure a filesystems by adding to `bsb_flysystem->filesystems`. Each filesystem may containing the following options;

- adapter \<string\> Name of adapter service.
- cache \<string\> (optional) If defined a name of a cache service. Defaults to false.
- eventable \<boolean\> When true returns an EventableFGilesystem instance. (see [flysystem](http://flysystem.thephpleague.com).

example: Filesystem called 'files' with the previously defined 'local_files' adapter.

```php
'bsb_flysystem' => array(
'filesystem' => array(
'files' => array(
'adapter' => 'local_files',
'cache' => false,
'eventable => false,
)
)
),
```

### Caches

- **Not yet implemented** -

Configure a caches by adding to `bsb_flysystem->caches`. Each cache may containing the following options;

example: Cache called 'memcached'.

```php
'bsb_flysystem' => array(
'caches' => array(
'memcached' => array(

)
)
),
```


## Usage

By default BsbFlysystem provides one pre-configured filesystem. This is a local filesystem (uncached) and exposes the data directory of a default ZF2 application.

Both the filesystems and adapters are ZF2 plugin managers and stored within the global service manager.

### Filesystem Manager

In its simplest form this is how we would retrieve a filesystem. We get the filesystem service from the main service manager and fetch from that a filesystem instance.

example: Fetch a 'default' filesystem. In this case a 'local' filesystem with a root of 'data'.

```
$filesystem = $serviceLocator->get('BsbFlysystemManager')->get('default');
$contents = $filesystem->read('file.txt');
```

If we at some point decide we need to store these files on a different system. Rackspace for example, we simply reconfigure the named filesystem service to use a different named adapter service. No need to change the userland implementation.

### Adapter Manager

Direct access to the Adapter service is possible by via the `BsbFlysystemAdapterManager` service registered in the main service locator. This is useful to setup `Mount` Filesystems or to use runtime configuration. See the advanced section below.

```
$adapter = $serviceLocator->get('BsbFlysystemAdapterManager')->get('local.data');
$filesystem = new Filesystem($adapter);
$contents = $filesystem->read('file.txt');
```

## Provided Factories

I have tried to provide factories (and tests) for each of the adapters that come with the Flysystem. Each come with there own setof required and optional options. I refer to the Flysystem documentation for more inforation.

### Adapters

- Aws3S
- Copy
- Dropbox
- Ftp
- Local
- Null
- Rackspace
- the ObjectStore Container must exist before usage
- Won't connect until actual usage by Filesystem (thanks to [ProxyManager](https://github.com/Ocramius/ProxyManager)) and uses the same lazy loading configuration ZF2 provides.
- Replicate
- Sftp
- WebDav
- Zip

### Cache



## Advanced Usage

A feature of ZF2 service managers is the ability to create an instance of a service each time you request it from the service manager (shared vs unshared). For adapters this can be easily accomplished by setting 'shared' to false/true. This in combination with the create options that can be provided to the get method of the service manager is useful to override certain configuration keys.

Consider the following configuration.

```
'adapters' => array(
'dropbox_user' => array(
'type' => 'dropbox',
'shared' => false,
'options' => array(
'client_identifier' => 'app_id',
'access_token' => 'xxxxx',
),
),
),
'filesystems' => array(
'dropbox_user' => array(
'adapter' => 'dropbox_user'
)
),
```

```
$adapter = $serviceLocator->get('BsbFlysystemAdapterManager')
->get('dropbox_user', ['access_token' => $accessToken]);
$filesystem = new Filesystem($adapter);
```


53 changes: 53 additions & 0 deletions composer.json
@@ -0,0 +1,53 @@
{
"name": "bushbaby/flysystem",
"description": "Zend Framework 2 module bridge for flysystem filesystem.",
"license": "MIT",
"type": "library",
"authors": [
{
"name": "Bas Kamer",
"email": "bas@bushbaby.nl"
}
],
"require": {
"zendframework/zendframework": "~2.2",
"league/flysystem": "~0.5.7"
},
"require-dev": {
"league/event": "~1.0",
"rackspace/php-opencloud": "~1.10.0",
"dropbox/dropbox-sdk": "~1.1.3",
"barracuda/copy": "~1.1.4",
"phpseclib/phpseclib" : "~0.3.5",
"aws/aws-sdk-php": "~2.4",
"squizlabs/php_codesniffer": "~2.0RC1",
"phpunit/phpunit": "~4.1",
"ocramius/proxy-manager": "~1.0.0@beta",
"sabre/dav" : "*"
},
"autoload": {
"psr-0": {
"BsbFlysystem\\": "src/",
"BsbFlysystemTest\\": "test/"
},
"classmap": [
"Module.php"
]
},
"suggest": {
"league/event": "Required for EventableFilesystem",
"aws/aws-sdk-php": "Allows you to use AWS S3 storage",
"predis/predis": "Allows you to use Predis for caching",
"dropbox/dropbox-sdk": "Allows you to use Dropbox storage",
"phpseclib/phpseclib": "Allows SFTP server storage",
"sabre/dav": "Enables WebDav support",
"rackspace/php-opencloud": "Allows you to use Rackspace Cloud Files",
"barracuda/copy": "Allows you to use Copy.com storage",
"guzzlehttp/guzzle": "Allows you to use http files (reading only)"
},
"extra": {
"branch-alias": {
"dev-master": "0.1.x-dev"
}
}
}
74 changes: 74 additions & 0 deletions config/bsb_flysystem.global.php.dist
@@ -0,0 +1,74 @@
<?php

return array(
'bsb_flysystem' => array(
'connection' => array(
'local_default' => array(
'type' => 'local',
'options' => array(
'root' => './data/files'
),
),
'sftp_default' => array(
'type' => 'sftp',
'options' => array(
'host' => 'xxxxx.xxx',
'port' => 21,
'username' => 'xxxxx',
'password' => 'xxxxx',
// 'privateKey' => 'path/to/or/contents/of/privatekey',
// 'root' => '/path/to/root',
// 'timeout' => 10,
),
),
'zip_default' => array(
'type' => 'zip',
'options' => array(
'archive' => './data/files.zip',
'prefix' => '/'
),
),
'rackspace_default' => array(
'type' => 'rackspace',
'options' => array(
'url' => "http://api-uri",
'secret' => array(
'username' => "xxxxx",
'password' => "xxxxx",
),
'objectstore' => array(
'name' => 'xxxxx',
'region' => 'XX',
'url_type' => 'publicURL',
'container' => 'xxxxx'
),
'prefix' => '/'
),
),
'dropbox_default' => array(
'type' => 'dropbox',
'options' => array(
'client_identifier' => 'app_id',
'access_token' => 'xxxxx',
'prefix' => '/'
),
),
'awss3_default' => array(
'type' => 'awss3',
'options' => array(
'key' => 'your-app-id',
'secret' => 'xxxxx',
'bucket' => 'xxxxx'
),
),
'webdav_default' => array(
'type' => 'webdav',
'options' => array(
'baseUri' => 'http://demo.sabredav.org/',
'userName' => 'testuser',
'password' => 'test'
),
),
),
),
);

0 comments on commit 485ada7

Please sign in to comment.