Data

Bear Framework provides a simple yet powerful object storage API. You can use it to store, modify, request and list any data (text, images, videos, etc.). Initialize it by setting a data driver. The default data driver (FileDataDriver) stores the objects in files using atomic operations. Every data item is stored in a separate file.

use BearFramework\App;

$app App::get();

// Enables a file based storage for the data items.
$app->data->useFileDriver('/data/storage/path/');

// Creates a new data item.
$item $app->data->make('users/1''{"name":"John Smith","email":"john@example.com"}');

// Stores the data item.
$app->data->set($item);

// Retrieve a data item.
$item $app->data->get('users/1');
echo 
$item->key// Will output 'users/1'
echo $item->value// Will output '{"name":"John Smith","email":"john@example.com"}'

// Append data to a data item (will be created if missing).
$app->data->append('visits/ip.log'"123.123.123.123\n");

// Duplicatea a data item
$app->data->duplicate('users/1''users/2');

// Renames a data item
$app->data->rename('users/2''users/3');

// Deletes a data item
$app->data->delete('users/3');

// Retrieves a list of all data items.
$app->data->getList();

Metadata

Metadata support is available too. It is a great way to store sizes, dates or other object details.

use BearFramework\App;

$app App::get();

// Creates a new data item.
$item $app->data->make('users/1''{"name":"John Smith","email":"john@example.com"}');
$item->metadata['lastLoginDate'] = '2019-12-12';

// Stores the data item.
$app->data->set($item);

// Retrieve the data item and then access the metadata.
$item $app->data->get('users/1');
echo 
$item->metadata['lastLoginDate']; // Will output '2019-12-12'

// Request only the metadata
echo $app->data->getMetadata('users/1''lastLoginDate'); // Will output '2019-12-12'

// Set new metadata value
echo $app->data->setMetadata('users/1''lastLoginDate''2019-12-20');

// Delete metadata
echo $app->data->deleteMetadata('users/1''lastLoginDate');

File access

The data objects can be accessed and modified using the build in PHP file functions. Just prefix the keys with 'appdata://'

// Stores or updates a data item.
file_put_contents('appdata://users/1''{"name":"John Smith","email":"john@example.com"}');

// Retrieves the content of a data item.
echo file_get_contents('appdata://users/1');

Allow public access

Object's public URLs can be easily requested too.

use BearFramework\App;

$app App::get();

// Makes the appdata://products/images public.
$app->assets->addDir('appdata://products/images');

// Creates a new image.
file_put_contents('appdata://products/images/1.jpg''...');

// Generates a new public URL for the data item specified.
echo $app->assets->getURL('appdata://products/images/1.jpg'); // Will output 'http://example.com/assets/...'

Learn more

Handle HTTP requests
Retrieve information about the HTTP request
Store frequently used data for later usage

The information on this page is created for version 1.1 of Bear Framework and may not be applicable for other versions of the framework.