mnapoli/blackbox

0.6.1 2015-01-10 01:47 UTC

This package is auto-updated.

Last update: 2024-09-19 21:06:08 UTC


README

BlackBox

BlackBox 是一个存储库,它通过简单的接口抽象了后端和数据转换。

Build Status Latest Version

存储数据。 "在哪里" 和 "如何" 可以稍后决定。

实验性:此项目仍处于实验阶段。请谨慎使用。

用法

API 由接口定义,非常简单。

namespace BlackBox;

interface Storage extends Traversable
{
    public function get(string $id);
    public function set(string $id, $data) : void;
    public function remove(string $id) : void;
}

$storage->set('foo', 'Hello World!');

echo $storage->get('foo'); // Hello World!

foreach ($storage as $key => $item) {
    echo $key; // foo
    echo $item; // Hello World!
}

您可以在接口文档中了解有关这些接口的所有信息。

特性

BlackBox 可以在以下位置存储数据:

  • 文件
  • 数据库(MySQL、PostgreSQL、SQLite、Oracle 等)使用 Doctrine DBAL
  • PHP 数组(即在内存中)

数据可以选择以以下方式存储:

  • 存储在 JSON
  • 存储在 YAML
  • 使用 AES 加密

此外,可以将一个存储缓存到另一个存储中(例如,将 DB 存储缓存到 Redis 或数组存储)。

后端

后端是实现 Storage 接口的类

  • FileStorage
  • DirectoryStorage
  • DatabaseTable
  • ArrayStorage

您可以在后端文档中了解有关后端的更多信息。

转换器

转换器在存储之前和检索之后转换数据

  • JsonEncoder
  • YamlEncoder
  • ObjectArrayMapper
  • AesEncrypter

您可以在转换器文档中了解有关转换器的更多信息。

// Encode the data in JSON
$storage = new JsonEncoder(
    // Store data in files
    new DirectoryStorage('some/directory')
);

$storage->set('foo', [
    'name' => 'Lebowski',
]);
// will encode it in JSON
// then will store it into a file

$data = $storage->get('foo');
// will read from the file
// then will decode the JSON

echo $data['name']; // Lebowski

许可证

BlackBox 在 MIT 许可证下发布。