2017 © Pedro Peláez
 

library eloquent-external-storage

Adds transparent external storage to an eloquent model

image

inakianduaga/eloquent-external-storage

Adds transparent external storage to an eloquent model

  • Monday, September 21, 2015
  • by inakianduaga
  • Repository
  • 1 Watchers
  • 3 Stars
  • 79 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 2 Forks
  • 2 Open issues
  • 11 Versions
  • 8 % Grown

The README.md

eloquent-external-storage

![Build Status][travis-image] ![Coverage Status][coveralls-image] Code Climate HHVM Status Dependency Status, (*1)

Adds external storage capabilities to an eloquent model., (*2)

Features

  • Save/retrieve model-related content to an external storage using setContent(), getContent() methods on the eloquent model. Now you can move all that binary data out of the database without having to move a finger.
  • Storage supports different drivers, currently file and Amazon AWS S3 are implemented
  • Different models can implement different storage drivers, separate configurations
  • Storage drivers can be updated on the fly.

Installation

Add package as a composer dependency

In your composer.json file, include, (*3)

"require": {
        "inakianduaga/eloquent-external-storage" : "dev-master",
    },

and then run, (*4)

composer update --no-scripts inakianduaga/laravel-html-builder-extensions, (*5)

to install the package, (*6)

Database configuration

The storage driver needs three additional fields, where it stores the content path, an md5 checksum of the contents, and the driver class used to store the contents., (*7)

  • A migration example is provided under src/migration.
  • The database field names can be custom-named, simply modify the model's $databaseFields property

class ActualModel extends InakiAnduaga\EloquentExternalStorage\Models\AbstractModelWithExternalStorage { /** * Under what db field we store the content path/md5/storage_driver_class for this model */ protected $databaseFields = array( 'contentPath' => 'content_path', 'contentMD5' => 'content_md5', 'storage_driver' => 'storage_driver', ); }

Configuration

Each model (class) that needs external storage must have a configuration set, controlled by model properties:, (*8)


class ActualModel extends InakiAnduaga\EloquentExternalStorage\Models\AbstractModelWithExternalStorage { /** * This is the path to the driver configuration that will be used for this model class, independently of other classes */ protected static $storageDriverConfigPath; }

Choosing / changing a storage driver dynamically

If you want to switch storage drivers dynamically for a given model, you can do so by using the model's setStorageDriver(StorageDriver $driver, $storageDriverConfigurationPath = null) method. This will use the given driver with the chosen configuration path (or leave the current config path if null), (*9)

Drivers configuration

The package provides placeholder configurations for the different included drivers. In the laravel installation root folder, run, (*10)

php artisan config:publish inakianduaga/eloquent-external-storage, (*11)

You can then modify the placeholder values in the files - Aws S3 driver: app/config/packages/inakianduaga/eloquent-external-storage/awsS3.php - File driver app/config/packages/inakianduaga/eloquent-external-storage/file.php, (*12)

Note that you should set the models $storageDriverConfigPath property to point to inakianduaga/eloquent-external-storage::awsS3 for the example above, when using S3., (*13)

Usage:

  • Simply extend InakiAnduaga\EloquentExternalStorage\Models\AbstractModelWithExternalStorage in your eloquent model (or use the trait InakiAnduaga\EloquentExternalStorage\Models\ModelWithExternalStorageTrait if you can't use class extension., (*14)

    • In the extended model, set driver/database config properties (see above).
  • To attach/retrieve external content associated to a model, (*15)

    • Use setContent method on the model to set the content (will be actually saved on update/save/creation)
    • Use getContent to retrieve the actual contents

Development & Testing

Requirements

  • Install node & npm, afterwards run npm install to install development tools
  • Install sqlite driver for running tests, (*16)

    • Ubuntu: sudo apt-get install sqlite3 libsqlite3-dev and afterwards sudo apt-get install php5-sqlite

Tools

There are several commands related to testing, (*17)

  • gulp test : runs unit tests once
  • gulp tdd : runs unit tests continuously once changes are detected

Both options have the ability to generate a code coverage report in different formats. When this is selected, a local server will be launched to visualize the coverage report, (*18)

Testing AWS S3 Driver

  • Locally: Valid AWS S3 credentials can be provided via tests/.env file, see tests/.env.example as an example. If no credentials are set, the AWS S3 tests will be skipped
  • CI: Travis CI integration is performed by passing encrypted credentials in travis.yml (valid only for inakianduaga/eloquent-external-storage repo).
  • Amazon S3 testing in Travis CI

Tests Coverage report

  • gulp test --generateCoverage=coverageHtml to generate html code coverage report (under ./coverage folder)

Documentation generation

  • The package code documentation can be generated by running gulp docs, which generates the docs in the ./docs folder

The Versions

28/01 2015

dev-24_automatic_driver_injection

dev-24_automatic_driver_injection

Adds transparent external storage to an eloquent model

  Sources   Download

MIT

The Requires

 

The Development Requires

by Inaki Anduaga

eloquent

20/01 2015

dev-9_setup_travis_build

dev-9_setup_travis_build

Adds transparent external storage to an eloquent model

  Sources   Download

MIT

The Requires

 

The Development Requires

by Inaki Anduaga

eloquent

20/01 2015

dev-7_add_tests

dev-7_add_tests

Adds transparent external storage to an eloquent model

  Sources   Download

MIT

The Requires

 

The Development Requires

by Inaki Anduaga

eloquent

15/01 2015

dev-9_add_gulp_common_tasks

dev-9_add_gulp_common_tasks

Adds transparent external storage to an eloquent model

  Sources   Download

MIT

The Requires

 

The Development Requires

by Inaki Anduaga

eloquent

15/01 2015

dev-6_setup_tests

dev-6_setup_tests

Adds transparent external storage to an eloquent model

  Sources   Download

MIT

The Requires

 

The Development Requires

by Inaki Anduaga

eloquent

14/01 2015

dev-6_setup_test_environment

dev-6_setup_test_environment

Adds transparent external storage to an eloquent model

  Sources   Download

MIT

The Requires

 

The Development Requires

by Inaki Anduaga

eloquent

11/01 2015

dev-1_boilerplate_package

dev-1_boilerplate_package

Adds transparent external storage to an eloquent model

  Sources   Download

MIT

The Requires

 

The Development Requires

by Inaki Anduaga

eloquent