zfcampus/statuslib-example

此包已被弃用且不再维护。作者建议使用laminas-api-tools/statuslib-example包。

用于与Apigility文档示例一起使用的库

1.2.0 2018-12-11 20:34 UTC

This package is auto-updated.

Last update: 2020-01-20 17:36:16 UTC


README

仓库于2019-12-31弃用

此仓库已迁移至laminas-api-tools/statuslib-example

这是一个旨在展示Apigility "代码连接" REST API的库,并且与Apigility文档并行编写。

它使用了以下组件

它作为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 项目中

  1. 从头创建一个新的 Zend Framework 项目;我们将使用 my-project 作为我们的项目文件夹
$ composer create-project zendframework/skeleton-application my-project
  1. 安装 StatusLib 模块
$ composer require zfcampus/statuslib-example
  1. 构建一个数据源

    • 选项 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,
          ],
      ],
  2. 创建一个测试脚本来验证数据源是否工作正常

    // 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
        );
    }