fly-crud / fly-crud
Simple crud system build on top of flysystem
Installs: 9 934
Dependents: 1
Suggesters: 0
Security: 0
Stars: 13
Watchers: 5
Forks: 3
Open Issues: 0
Requires
- php: >=7.1
- league/flysystem: ^1.0
Requires (Dev)
- phpunit/phpunit: ^7.0
- symfony/yaml: ^4.2
Suggests
- symfony/yaml: To use YamlRepository
README
Sometimes you don't need a database, just some data in yaml/json/etc files to build a website.
This library provides a simple way to manage data stored in files using flysystem as engine.
Installation
The library is compatible with PHP >= 7.1 and installable and autoloadable via Composer as fly-crud/fly-crud.
$ composer require fly-crud/fly-crud
Usage example:
use FlyCrud\Directory; use FlyCrud\Formats\Json; //Create a repository to store the data as json $repo = Directory::make('/path/to/files', new Json()); //Create a new document $document = new Document([ 'title' => 'Title post', 'intro' => 'This is the new post' ]); //Get/set/edit data $document->title = 'The new title post'; //Save the document $repo->saveDocument('first-post', $document); //Get the document again $document = $repo->getDocument('first-post'); //or delete it $repo->deleteDocument('first-post');
Working with directories
Let's say we have the following structure with yaml files:
_ site
|_ posts
|_ first-post.yml
|_ second-post.yml
|_ articles
|_ first-article.yml
|_ second-article.yml
|_ third-article.yml
use FlyCrud\Directory; use FlyCrud\Document; use FlyCrud\Formats\Yaml; //Create a repository pointing to our site data using Yaml format: $site = Directory::make(__DIR__.'/site', new Yaml()); //Get the posts directory $posts = $site->getDirectory('posts'); //And the first post document $post = $posts->getDocument('first-post'); //Or store a new document $posts->saveDocument('third-post', new Document([ 'title' => 'My awesome third post', 'intro' => 'This is the third post' ]));
Array access and property access
To ease the work with documents and directories:
- Use properties to access to directories (ex:
$site->posts
) - Use array-like syntax to access to documents (ex:
$posts['first-post']
)
Example with the same structure used previously:
//Access to the first-article document $article = $site->articles['first-article']; //Save a new article $site->articles['other-article'] = new Document([ 'title' => 'I like tomatoes' 'intro' => 'Yes, they are red, rounded and tasty!' ]);
API
Working with documents
Documents are clases extending ArrayObject with some additions:
- Implements the JsonSerializable interface, so you can convert the document to json easily
json_encode($document)
- Implements the magic methods
__get()
,__set()
,__isset()
and__unset()
, so you can manipulate the values like properties. - The data is converted to
stdClass
objects, this allows to manipulate it easily. Example:
use FlyCrud\Document; //Create a document $post = new Document([ 'title' => 'My post', 'tags' => ['php', 'code'], 'sections' => [ [ 'title' => 'Section one', 'body' => 'This is the first section of the document' ],[ 'title' => 'Section two', 'body' => 'This is the second section of the document' ] ] ]); //Use the properties to access to the data: echo $post->title; // "My post" echo $post->tags[0]; // "php" echo $post->sections[0]->title; // "Section one" //Modify the data $post->section[1]->title = 'New title of the second section';