eyroot/lx-simple-storage

使用 JSON 和 PHP 的存储解决方案

1.2.0 2020-03-28 18:38 UTC

This package is not auto-updated.

Last update: 2024-09-29 06:25:38 UTC


README

使用 JSON 和 PHP 的存储解决方案。

用法

基本

use Lx\Storage\Factory as StorageFactory;
use Lx\Storage\StorageAbstract;

// initialize storage of type json
$storage = StorageFactory::create(
	StorageFactory::TYPE_JSON, 'items', array(
		'path' => '/path/directory/storage/json',
		StorageAbstract::FIELD_ID => 'id' // name of the id field
	)
);

// insert items
$storage->insert(array(
	'id' => 1,
	'title' => 'item 1'
));
$storage->insert(array(
	'id' => 2,
	'title' => 'item 2'
));

// get items by id
$item1 = $storage->getById(1);
$item2 = $storage->getById(2);

// update item with id 2
$storage->update(array('title' => 'item 2 title updated'), 2);

// retrieve items
$list = $storage->getList();

// delete item with id 1
$storage->delete(1);

启用自增 ID 功能

use Lx\Storage\Factory as StorageFactory;
use Lx\Storage\StorageAbstract;

// initialize storage of type json
$storage = StorageFactory::create(
	StorageFactory::TYPE_JSON, 'items', array(
		'path' => '/path/directory/storage/json',
		StorageAbstract::FIELD_ID => 'id' // name of the id field,
		StorageAbstract::AUTOINCREMENT_ID => true
	)
);

// insert items
$storage->insert(array(
	'title' => 'item 1'
));
$storage->insert(array(
	'title' => 'item 2'
));

// get items by id
$item1 = $storage->getById(1);
$item2 = $storage->getById(2);

开发设置

  • 本地克隆项目
git clone https://github.com/eyroot/lx-simple-storage lx-simple-storage
cd lx-simple-storage
  • 设置项目和安装 composer 依赖
composer install
  • 运行单元测试:如果在运行测试套件时遇到 "错误:没有可用的代码覆盖率驱动程序",请记住安装并启用 php-xdebug 扩展!
cd testing/
mkdir data-storage
../vendor/bin/phpunit
  • 通过浏览器打开以检查测试的代码覆盖率
file:///tmp/coverage-lx-simple-storage/index.html