2017 © Pedro Peláez
 

library phpjstore

image

tlucas/phpjstore

  • Friday, May 19, 2017
  • by tlucas
  • Repository
  • 1 Watchers
  • 1 Stars
  • 138 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 8 Versions
  • 0 % Grown

The README.md

phpjstore

Latest Stable Version Latest Unstable Version License, (*1)

Phpjstore provides a class to save and access schemaless data in a flatfile JSON backend., (*2)

Requirements

Phpjstore iself doesn't require any dependencies., (*3)

The admin interface uses the Jeremy Dorn's json-editor (included in the package), jquery, and Bootstrap 3. Both of these can be included directly in your page (using CDN), (*4)

Installation/Setup

Using composer:, (*5)

composer require tlucas/phpjstore

In your project file (e.g. project.php) you wish to use it in make sure you have, (*6)

require_once('vendor/autoload.php');
use jstore\jstore;

Then you can instantiate a data store object with, (*7)

$store = new jstore('mydata');

Which will store all the data in the mydata directory (relative to your current script)., (*8)

Basic usage

To interact with a data object, first you need to create a data object. This is done, using the $store object defined above, by calling the get() method:, (*9)

$data = $store->get('somekey');

This has now created an object $data containing the data stored using the somekey key. If that key doesn't yet exist, this will be an empty data object., (*10)

Once you have the object in $object you can set some values using set():, (*11)

$data->set(['firstkey' => 'firstvalue', 'secondkey' => 'secondvalue']);

This will only set the specified values (it will not affect the other values stored in the object) so if we then do:, (*12)

$data->set(['thirdkey' => 'thirdvalue']);

This is also equivalent to, (*13)

$data->thirdkey = 'thirdvalue';

All three values will now be held on that object., (*14)

If we want to delete the first variable we set earlier:, (*15)

$data->delete('firstkey');

So now, our stored object looks like this (if we do echo $data;):, (*16)

{
    "firstkey": "firstvalue",
    "secondkey": "secondvalue",
    "thirdkey": "thirdvalue"
}

To access these values:, (*17)

echo $data->firstkey;

Will print firstvalue., (*18)

If you prefer to work with arrays,, (*19)

$data->toArray();

will return the array:, (*20)

Array
(
    [firstkey] => firstvalue
    [secondkey] => secondvalue
    [thirdkey] => thirdvalue
)

At the moment, the object has only been modified in memory, so to save the changes permanently, just call:, (*21)

$data->save();

Globals

Phpjstore includes some shortcuts to the above functions for storing and retrieving global variables:, (*22)

To set a global variable:, (*23)

$store->setGlobal(['varname' => 'varvalue']);

(Note: this will immediately save the variable to the storage backend), (*24)

And to retrieve the variable we just set:, (*25)

$store->getGlobal('varname');

To list all Globals that are stored in this way:, (*26)

$store->getGlobals();

Which will return a list of the keys., (*27)

To delete one of these stored variables:, (*28)

$store->deleteGlobal('varname');

(Note: again, this will immediately save the variable to the storage backend), (*29)

Admin

Phpjstore includes a data management interface based around jdorn's fantastic json-editor., (*30)

Before using it, it requires a little setup., (*31)

Scripts/styles

First, the interface uses a javascript file to be included once, along with jquery, somewhere before it is called. It also uses Bootstrap 3 for styling, so that must also be available on the page., (*32)

Phpjstore includes some helper fuctions to do these for you, if you don't already use them in your page:, (*33)

echo jstore::script();

Will include jsut the json-editor script. If you also need to include jquery, use:, (*34)

echo jstore::script($jquery=True);

To include Bootstrap:, (*35)

echo jstore::bootstrap3();

Endpoint

In order to save the data provided in the form you will need to set an endpoint for submission. This is done by adding, (*36)

$store->registerEndpoint();

To the top of the file which is acting as your endpoint. This can be the same file, or a different file, but it must be placed before any output is sent., (*37)

If you placed this endpoing in a separate file (e.g. /path/to/submit.php), you will need to point your admin scripts to send their submissions to that file:, (*38)

$store->adminpost = '/path/to/submit.php';

Important: Be aware that anyone who has access to the endpoint you set here will be able to modify your data, so make sure you protect it, either by putting the file in a restricted directory (Using .htaccess on Apache, for instance) or eclosing the method call in an authentication check, using whatever authentication system you choose):, (*39)

if( /* User is authenticated */ ){
    $store->registerEndpoint();
}

Data schema

The final step before adding the interface is setting up the schema for your data. While the stored data don't depend on any schema (you can just save variables straight to them, and it will create the fields as required). The json-editor interface requires a schema to build the apppropriate forms., (*40)

To set up a schema, simply go to your mydata directory (as defined when initialising the jstore instance, and inside the schema directory create a file called somekey.json. In that file, define a schema as defined here. (When you first set up jstore, there will be an example.json schema already there, for reference)., (*41)

Forms

Now we have done all that, we are ready to display the admin form!, (*42)

To display the form for the somekey schema you created above:, (*43)

echo $store->admin('somekey');

(Or to use the example schema provided: echo $store->admin('example');), (*44)

And you're done! You should have a fully functional form to modify your stored data., (*45)

You can list all the schemas you have defined by running:, (*46)

$store->getSchemas();

So if you wanted an admin page which automatically gave you a form to allow you to edit all of your defined schemas, you could run:, (*47)

foreach($store->getSchemas() as $schema){
    echo $store->admin($schema);
}

The Versions

19/05 2017

dev-master

9999999-dev

  Sources   Download

MIT

The Requires

  • php ^7.0

 

The Development Requires

  • php ^7.0

by Thomas Lucas

19/05 2017

v1.1.4

1.1.4.0

  Sources   Download

MIT

The Requires

  • php ^7.0

 

The Development Requires

  • php ^7.0

by Thomas Lucas

08/05 2017

v1.1.3

1.1.3.0

  Sources   Download

MIT

The Requires

  • php ^7.0

 

The Development Requires

  • php ^7.0

by Thomas Lucas

03/05 2017

v1.1.2

1.1.2.0

  Sources   Download

MIT

The Requires

  • php ^7.0

 

The Development Requires

  • php ^7.0

by Thomas Lucas

26/04 2017

v1.1.1

1.1.1.0

  Sources   Download

MIT

The Requires

  • php ^7.0

 

The Development Requires

  • php ^7.0

by Thomas Lucas

26/04 2017

v1.1.0

1.1.0.0

  Sources   Download

MIT

The Requires

  • php ^7.0

 

The Development Requires

  • php ^7.0

by Thomas Lucas

25/04 2017

v1.1.0-beta1

1.1.0.0-beta1

  Sources   Download

MIT

The Requires

  • php ^7.0

 

The Development Requires

  • php ^7.0

by Thomas Lucas

22/04 2017

v1.0.0

1.0.0.0

  Sources   Download

MIT

The Requires

  • php ^7.0

 

The Development Requires

  • php ^7.0

by Thomas Lucas