Skip to content
This repository has been archived by the owner on Jan 21, 2020. It is now read-only.

Commit

Permalink
Merge branch 'feature/ag-1.4-support'
Browse files Browse the repository at this point in the history
Provide support for Apigility 1.4, and modernize the module somewhat.
  • Loading branch information
weierophinney committed Sep 30, 2016
2 parents 42dbb13 + a713416 commit 41c50a4
Show file tree
Hide file tree
Showing 17 changed files with 226 additions and 223 deletions.
18 changes: 9 additions & 9 deletions LICENSE.txt → LICENSE.md
@@ -1,19 +1,19 @@
Copyright (c) 2014, Zend Technologies USA, Inc.
Copyright (c) 2014-2016, Zend Technologies USA, Inc.
All rights reserved.

Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
- Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.

* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
- Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.

* Neither the name of Zend Technologies USA, Inc. nor the names of its
contributors may be used to endorse or promote products derived from this
software without specific prior written permission.
- Neither the name of Zend Technologies USA, Inc. nor the names of its
contributors may be used to endorse or promote products derived from this
software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
Expand Down
35 changes: 0 additions & 35 deletions Module.php

This file was deleted.

162 changes: 86 additions & 76 deletions README.md
@@ -1,5 +1,4 @@
StatusLib
=========
# StatusLib

This is a library designed to demonstrate an [Apigility](http://apigility.org/) "Code-Connected"
REST API, and has been written in parallel with the [Apigility documentation](https://github.com/zfcampus/apigility-documentation).
Expand All @@ -18,52 +17,51 @@ It uses the following components:
array merging capabilities.
- [zendframework/zend-paginator](https://framework.zend.com/) for providing pagination.

It is written as a Zend Framework 2 module, but could potentially be dropped into other
It is written as a Zend Framework module, but could potentially be dropped into other
applications; use the `StatusLib\*Factory` classes to see how dependencies might be injected.

Installation
------------
## Installation

Use [Composer](https://getcomposer.org/) to install the library in your application:

```console
$ composer require zfcampus/statuslib-example:dev-master
$ composer require zfcampus/statuslib-example
```

If you are using this as part of a Zend Framework 2 or Apigility application, you will also need to
enable the module in your `config/application.config.php` file:
If you are using this as part of a Zend Framework or Apigility application, you
may need to enable the module in your `config/application.config.php` file, if
you are not using the [zend-component-installer](https://docs.zendframework.com/zend-component-installer/):

```php
return array(
return [
/* ... */
'modules' => array(
'modules' => [
/* ... */
'StatusLib',
),
],
/* ... */
);
];
```

Configuration
-------------
## Configuration

When used as a Zend Framework 2 module, you may define the following configuration values in order
When used as a Zend Framework module, you may define the following configuration values in order
to tell the library which adapter to use, and what options to pass to that adapter.

```php
array(
'statuslib' => array(
[
'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' => array(
'aliases' => array(
// Set to either 'StatusLib\ArrayMapper' or 'StatusLib\TableGatewayMapper'
'StatusLib\Mapper' => 'StatusLib\ArrayMapper',
),
),
)
],
'service_manager' => [
'aliases' => [
// Set to either StatusLib\ArrayMapper or StatusLib\TableGatewayMapper
\StatusLib\Mapper::class => \StatusLib\ArrayMapper::class,
],
],
]
```

For purposes of the Apigility examples, we suggest the following:
Expand All @@ -73,27 +71,26 @@ For purposes of the Apigility examples, we suggest the following:

```php
<?php
return array();
return [];
```

- Edit your application's `config/autoload/local.php` file to set the `array_mapper_path`
configuration value to `data/statuslib.php`:

```php
<?php
return array(
return [
/* ... */
'statuslib' => array(
'statuslib' => [
'array_mapper_path' => 'data/statuslib.php',
),
);
],
];
```

The above will provide the minimum necessary requirements for experimenting with the library in
order to test an API.

Using a database
----------------
## Using a database

The file `data/statuslib.sqlite.sql` contains a [SQLite](https://www.sqlite.org/) schema. You can
create a SQLite database using:
Expand All @@ -106,19 +103,18 @@ The schema can be either used directly by other databases, or easily modified to
databases.


StatusLib in a New ZF2 Project
------------------------------
## StatusLib in a New Zend Framework Project

1. Create a new ZF2 project from scratch, we'll use `my-project` as our project folder:
1. Create a new Zend Framework project from scratch; we'll use `my-project` as our project folder:

```console
$ composer create-project -sdev --repository-url="https://packages.zendframework.com" zendframework/skeleton-application my-project
$ composer create-project zendframework/skeleton-application my-project
```

2. Install the StatusLib module:

```console
$ composer require zfcampus/statuslib-example:dev-master
$ composer require zfcampus/statuslib-example
```

3. Build a DataSource
Expand All @@ -140,23 +136,23 @@ StatusLib in a New ZF2 Project
Next, add the StatusLib specific configuration for an array based data source:

```php
'statuslib' => array(
'statuslib' => [
'array_mapper_path' => 'data/status.data.php',
),
'service_manager' => array(
'aliases' => array(
'StatusLib\Mapper' => 'StatusLib\ArrayMapper',
),
),
],
'service_manager' => [
'aliases' => [
\StatusLib\Mapper::class => \StatusLib\ArrayMapper::class,
],
],
```

- Option B: Sqlite data source:

First, create a sqlite3 database, and fill it with the sample data:

```console
$ sqlite3 status.db < vendor/zfcampus/statuslib-example/data/statuslib.sqlite.sql
$ sqlite3 status.db < vendor/zfcampus/statuslib-example/data/sample-data/db-sqlite-insert.sql
$ 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
```
Then, configure this datasource by setting up a `local.php` configuration file:
Expand All @@ -168,42 +164,56 @@ StatusLib in a New ZF2 Project
Next, add the StatusLib specific configuration for a sqlite database based data source:

```php
'db' => array(
'adapters' => array(
'MyDb' => array(
'db' => [
'adapters' => [
'MyDb' => [
'driver' => 'pdo_sqlite',
'database' => __DIR__ . '/../../data/statuslib.db'
)
)
),
'statuslib' => array(
'database' => __DIR__ . '/../../data/status.db'
],
],
],
'statuslib' => [
'db' => 'MyDb',
'table' => 'status',
),
'service_manager' => array(
'aliases' => array(
'StatusLib\Mapper' => 'StatusLib\TableGatewayMapper',
),
'abstract_factories' => array(
'Zend\Db\Adapter\Adapter' => 'Zend\Db\Adapter\AdapterAbstractServiceFactory',
)
),
],
'service_manager' => [
'aliases' => [
\StatusLib\Mapper::class => \StatusLib\TableGatewayMapper::class,
],
],
```

4. Alter the stock controller and view to prove the data source is working:
4. Create a test script to prove the data source is working:

- Alter the index view `module/Application/view/application/index/index.phtml`, replacing it with:
```php
// test.php
namespace StatusLib;

```php
<?php foreach ($this->statuses as $status): ?>
<?php echo $status->message . ' by ' . $status->user; ?><br>
<?php endforeach; ?>
```
use Zend\Mvc\Application;
use Zend\Stdlib\ArrayUtils;

- Alter the `module/Application/src/Application/Controller/IndexController.php`'s `indexAction` method:
include 'vendor/autoload.php';

```php
$statusMapper = $this->serviceLocator->get('StatusLib\Mapper');
$statuses = $statusMapper->fetchAll();
return new ViewModel(array('statuses' => $statuses));
```
$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
);
}
```
25 changes: 13 additions & 12 deletions composer.json
Expand Up @@ -17,24 +17,25 @@
},
"extra": {
"branch-alias": {
"dev-master": "1.0-dev"
"dev-master": "1.1-dev"
},
"zf": {
"module": "StatusLib"
}
},
"require": {
"php": ">=5.3.23",
"php": "^5.6 || ^7.0",
"ramsey/uuid": "^2.8",
"zendframework/zend-config": "~2.3",
"zendframework/zend-db": "~2.3",
"zendframework/zend-paginator": "~2.3",
"zendframework/zend-stdlib": "~2.3",
"zfcampus/zf-configuration": "~1.0-dev"
"zendframework/zend-config": "^2.6",
"zendframework/zend-db": "^2.8.2",
"zendframework/zend-paginator": "^2.7",
"zendframework/zend-hydrator": "^1.1 || ^2.0",
"zendframework/zend-stdlib": "^2.7.7 || ^3.0.1",
"zfcampus/zf-configuration": "^1.0"
},
"autoload": {
"psr-0": {
"psr-4": {
"StatusLib\\": "src/"
},
"classmap": [
"Module.php"
]
}
}
}

0 comments on commit 41c50a4

Please sign in to comment.