dev-master
9999999-devStoring data in the file system with PHP
GLP-3.0
The Requires
- php >=5.4
json files data file system wrinting files reading files storing data
Wallogit.com
2017 © Pedro PelĂĄez
Storing data in the file system with PHP
The Papyrus package can store information in files, perform queries, updates, and deletes data . All this, based on a primary key or identifier. The information is stored in JSON format., (*1)
To install via composer (http://getcomposer.org/), place the following in your composer.json file:, (*2)
{
"require": {
"barbosa/papyrus": "dev-master"
}
}
or download package from github.com:, (*3)
http://github.com/barbosa89/papyrus
Papyrus need to run an array of configurations and a path to the folder containing the files storage., (*4)
Create an array of configurations in a file or in the file which instances to Papyrus. The array must have two key, extension and files:, (*5)
<?php
/**
* Example in a configurations file.
*/
return [
'extension' => '.project',
'files' => [
'users' => ['dni(int#)', 'name', 'lastName']
]
];
Other way, in the file which instances to Papyrus:, (*6)
<?php
require 'vendor/autoload.php';
use \Barbosa\Papyrus\Papyrus;
$config = [
'extension' => '.project',
'files' => [
'users' => ['dni(int#)', 'name', 'lastName']
]
];
$papyrus = new Papyrus($config);
This extension is for all files:, (*7)
'extension' => '.project'
The file structure represents fields in a SQL table , what is in brackets, are the fields of each file:, (*8)
'files' => [
'fileName' => ['primaryKey(int#)', 'field', 'otherField'],
'otherFileName' => ['primaryKey(str#)', 'field', 'otherField']
]
The options for primary key:, (*9)
Auto increment integer (int++) Unique integer (int#) Unique string (str#)
Storage files that are configured in the array must be created in a folder chosen by the user, the folder path should be passed to Papyrus:, (*10)
<?php
require 'vendor/autoload.php';
use \Barbosa\Papyrus\Papyrus;
/**
* Complete example.
*/
$config = [
'extension' => '.project',
'files' => [
'users' => ['dni(int#)', 'name', 'lastName']
]
];
$path = realpath(__DIR__ . '/storageFolder/');
$papyrus = new Papyrus($config, $path);
You can also switch configurations through methods:, (*11)
<?php
require 'vendor/autoload.php';
use \Barbosa\Papyrus\Papyrus;
/**
* Complete example with setters.
*/
$papyrus = new Papyrus();
$papyrus->setStoragePath(realpath(__DIR__ . '/storageFolder/'));
$config = [
'extension' => '.project',
'files' => [
'users' => ['dni(int#)', 'name', 'lastName']
]
];
$papyrus->loadConfigurations($config);
As example, a file will be created with registers name and the extension .data:, (*12)
$ touch path/to/storageFolder/registers.data
In the console:, (*13)
$ ls -l -rwx---rw- 1 user user 494 jul 8 21:10 registers.data
You can add every files that you need., (*14)
Files in the storage folder (path/to/storageFolder/):, (*15)
chmod 706 fileName.extension
Example:, (*16)
chmod 706 registers.data
For purposes of explaining with examples , it is assumed that there is a file called users with the following structure:, (*17)
'files' => [
'users' => ['dni(int#)', 'name', 'lastName']
]
Field dni(int#), indicates that is the primary key and a unique integer., (*18)
Require the autoload file:, (*19)
require 'vendor/autoload.php'; use \Barbosa\Papyrus\Papyrus;
$values = ['dni' => 123456, 'name' => 'Tony', 'lastName' => 'Stark'];
$papyrus = new Papyrus();
$papyrus->insertInto('users')->values($values)->runQuery();
The where method can only receive a value of array type, which should be the primary key or identifier., (*20)
$papyrus->select()->from('users')->runQuery();
$papyrus->select('dni, name')->from('users')->runQuery();
$papyrus->select()->from('users')->where(['dni' => 123456])->runQuery();
$papyrus->select('dni, name')->from('users')->where(['dni' => 123456])->runQuery();
$papyrus->select()->from('users')->orderBy(['name' => 'ASC'])->runQuery();
$papyrus->select('dni, lastName')->from('users')->orderBy(['lastName' => 'ASC'])->runQuery();
$papyrus->select()->from('users')->limit(3)->runQuery();
$papyrus->select()->from('users')->limit(3)->orderBy(['name' => 'DESC'])->runQuery();
$papyrus->deleteFrom('users')->runQuery();
$papyrus->deleteFrom('users')->where(['dni' => 123456])->runQuery();
The primary key of a record can not be modified., (*21)
$data = ['name' => 'Tony', 'lastname' => 'Stark'];
$papyrus->update('users')->set($data)->runQuery();
$data = ['name' => 'Tony', 'lastName' => 'The Iron Man'];
$papyrus->update('users')->set($data)->where(['dni' => 123456])->runQuery();
$papyrus->getRecords();
$papyrus->getStatus();
Papyrus::select($fields); Papyrus::deleteFrom($file = ''); Papyrus::update($file = ''); Papyrus::from($file = ''); Papyrus::insertInto($file = ''); Papyrus::set(array $data = null); Papyrus::values(array $data = null); Papyrus::where(array $conditions = null); Papyrus::orderBy(array $order = null); Papyrus::limit($limit = 0); Papyrus::runQuery(); Papyrus::getRecords(); Papyrus::getStatus(); $papyrus->loadConfigurations(array $config = null); $papyrus->setStoragePath($path = '');
Thanks..., (*22)
Storing data in the file system with PHP
GLP-3.0
json files data file system wrinting files reading files storing data