zfcampus / statuslib-example
Requires
- php: ^5.6 || ^7.0
- ramsey/uuid: ^2.8
- zendframework/zend-config: ^3.1
- zendframework/zend-db: ^2.8.2
- zendframework/zend-hydrator: ^1.1 || ^2.0 || ^3.0
- zendframework/zend-paginator: ^2.7
- zendframework/zend-stdlib: ^2.7.7 || ^3.0.1
- zfcampus/zf-configuration: ^1.0
This package is auto-updated.
Last update: 2020-01-20 17:36:16 UTC
README
仓库于2019-12-31弃用
这是一个旨在展示Apigility "代码连接" REST API的库,并且与Apigility文档并行编写。
它使用了以下组件
- rhumsaa/uuid,一个用于生成和验证UUID的库。
- zfcampus/zf-configuration,用于提供PHP文件作为读取/写入状态消息的可能后端之一。
- zendframework/zend-config,为
zf-configuration
模块使用的实际配置写入器。 - zendframework/zend-db,用于提供数据库表作为读取/写入状态消息的后端。
- zendframework/zend-stdlib,特别是Hydrator子组件,用于将数据从数组转换为对象,以及提供高级数组合并功能的
ArrayUtils
类。 - zendframework/zend-paginator用于提供分页。
它作为Zend Framework模块编写,但可能可以放入其他应用程序中;使用StatusLib\*Factory
类查看如何注入依赖项。
安装
使用Composer在您的应用程序中安装库
$ composer require zfcampus/statuslib-example
如果您将其用作Zend Framework或Apigility应用程序的一部分,可能需要在您的config/application.config.php
文件中启用模块,如果您没有使用zend-component-installer
return [ /* ... */ 'modules' => [ /* ... */ 'StatusLib', ], /* ... */ ];
配置
当用作Zend Framework模块时,您可以定义以下配置值,以便告诉库使用哪个适配器,以及向该适配器传递哪些选项。
[ 'statuslib' => [ 'db' => 'Name of service providing DB adapter', 'table' => 'Name of database table within db to use', 'array_mapper_path' => 'path to PHP file returning an array for use with ArrayMapper', ], 'service_manager' => [ 'aliases' => [ // Set to either StatusLib\ArrayMapper or StatusLib\TableGatewayMapper \StatusLib\Mapper::class => \StatusLib\ArrayMapper::class, ], ], ]
对于Apigility示例的目的,我们建议以下内容
-
在您的应用程序的
data/
目录中创建一个名为statuslib.php
的PHP文件,该文件返回一个数组<?php return [];
-
编辑您的应用程序的
config/autoload/local.php
文件,将array_mapper_path
配置值设置为data/statuslib.php
<?php return [ /* ... */ 'statuslib' => [ 'array_mapper_path' => 'data/statuslib.php', ], ];
上述内容将提供在实验库中测试API所需的最小必要要求。
使用数据库
文件data/statuslib.sqlite.sql
包含一个SQLite模式。您可以使用以下方式创建SQLite数据库
$ sqlite3 statuslib.db < path/to/data/statuslib.sqlite.sql
该模式可以直接用于其他数据库,或者很容易修改以与其他数据库一起使用。
StatusLib 在新的 Zend Framework 项目中
- 从头创建一个新的 Zend Framework 项目;我们将使用
my-project
作为我们的项目文件夹
$ composer create-project zendframework/skeleton-application my-project
- 安装 StatusLib 模块
$ composer require zfcampus/statuslib-example
-
构建一个数据源
-
选项 A:数组数据源
首先,将示例数组复制到应用程序的
data
目录$ cp vendor/zfcampus/statuslib-example/data/sample-data/array-data.php data/status.data.php
然后,通过设置一个
local.php
配置文件来配置这个数据源$ cp config/autoload/local.php.dist config/autoload/local.php
接下来,添加针对基于数组的数组的 StatusLib 特定配置
'statuslib' => [ 'array_mapper_path' => 'data/status.data.php', ], 'service_manager' => [ 'aliases' => [ \StatusLib\Mapper::class => \StatusLib\ArrayMapper::class, ], ],
-
选项 B:SQLite 数据源
首先,创建一个 sqlite3 数据库,并填充示例数据
$ sqlite3 data/status.db < vendor/zfcampus/statuslib-example/data/statuslib.sqlite.sql $ sqlite3 data/status.db < vendor/zfcampus/statuslib-example/data/sample-data/db-sqlite-insert.sql
然后,通过设置一个
local.php
配置文件来配置这个数据源$ cp config/autoload/local.php.dist config/autoload/local.php
接下来,为基于 SQLite 数据库的数据源添加 StatusLib 特定配置
'db' => [ 'adapters' => [ 'MyDb' => [ 'driver' => 'pdo_sqlite', 'database' => __DIR__ . '/../../data/status.db' ], ], ], 'statuslib' => [ 'db' => 'MyDb', 'table' => 'status', ], 'service_manager' => [ 'aliases' => [ \StatusLib\Mapper::class => \StatusLib\TableGatewayMapper::class, ], ],
-
-
创建一个测试脚本来验证数据源是否工作正常
// test.php namespace StatusLib; use Zend\Mvc\Application; use Zend\Stdlib\ArrayUtils; include 'vendor/autoload.php'; $appConfig = include 'config/application.config.php'; if (file_exists('config/development.config.php')) { $appConfig = ArrayUtils::merge( $appConfig, include 'config/development.config.php' ); } $app = Application::init($appConfig); $services = $app->getServiceManager(); $statusMapper = $services->get(Mapper::class); foreach ($statusMapper->fetchAll() as $status) { printf( "[%d] [%s] %s (by %s)\n", $status->timestamp, $status->id, $status->message, $status->user ); }