Skip to content

Commit a20a16e

Browse files
committed
Close #82
1 parent 716ad52 commit a20a16e

File tree

207 files changed

+11287
-12359
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

207 files changed

+11287
-12359
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ vendor/
22
build/
33
.idea/
44
composer.lock
5+
perf/blackfire.io.env

NOTICE

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
Framework agnostic JSON API implementation
22

3-
Copyright 2015 [email protected]
3+
Copyright 2015-2019 [email protected]
44

5-
This product includes software developed at Neomerx (www.neomerx.com).
5+
This product includes software developed at Neomerx (www.neomerx.com).

README.md

Lines changed: 46 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
[![Project Management](https://img.shields.io/badge/project-management-blue.svg)](https://waffle.io/neomerx/json-api)
1+
[![Build Status](https://travis-ci.org/neomerx/json-api.svg?branch=master)](https://travis-ci.org/neomerx/json-api)
22
[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/neomerx/json-api/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/neomerx/json-api/?branch=master)
33
[![Code Coverage](https://scrutinizer-ci.com/g/neomerx/json-api/badges/coverage.png?b=master)](https://scrutinizer-ci.com/g/neomerx/json-api/?branch=master)
4-
[![Build Status](https://travis-ci.org/neomerx/json-api.svg?branch=master)](https://travis-ci.org/neomerx/json-api)
54
[![License](https://img.shields.io/packagist/l/neomerx/json-api.svg)](https://packagist.org/packages/neomerx/json-api)
65

76
## Description
@@ -10,18 +9,19 @@
109

1110
A good API is one of most effective ways to improve the experience for your clients. Standardized approaches for data formats and communication protocols increase productivity and make integration between applications smooth.
1211

13-
This framework agnostic package implements [JSON API](http://jsonapi.org/) specification **version v1.0** and helps focusing on core application functionality rather than on protocol implementation. It supports document structure, errors, data fetching as described in [JSON API Format](http://jsonapi.org/format/) and covers parsing and checking HTTP request parameters and headers. For instance it helps to correctly respond with ```Unsupported Media Type``` (HTTP code 415) and ```Not Acceptable``` (HTTP code 406) to invalid requests. You don't need to manually validate all input parameters on every request. You can configure what parameters are supported by your services and this package will check incoming requests automatically. It greatly simplifies API development and fully support specification. In particular
12+
This framework agnostic package implements [JSON API](http://jsonapi.org/) specification **version v1.1** and helps focusing on core application functionality rather than on protocol implementation. It supports document structure, errors, data fetching as described in [JSON API Format](http://jsonapi.org/format/) and covers parsing and checking HTTP request parameters and headers. For instance it helps to correctly respond with ```Unsupported Media Type``` (HTTP code 415) and ```Not Acceptable``` (HTTP code 406) to invalid requests. You don't need to manually validate all input parameters on every request. You can configure what parameters are supported by your services and this package will check incoming requests automatically. It greatly simplifies API development and fully support specification. In particular
1413

1514
* Resource attributes and relationships
1615
* Polymorphic resource data and relationships
1716
* Compound documents with inclusion of related resources (circular resource references supported)
1817
* Meta information for document, resources, errors, relationship and link objects
18+
* Profiles
1919
* Parsing HTTP `Accept` and `Content-Type` headers in accordance with [RFC 7231](https://tools.ietf.org/html/rfc7231)
2020
* Parsing HTTP query parameters (e.g. pagination, sorting and etc)
2121
* Sparse fieldsets and customized included paths
2222
* Errors
2323

24-
High code quality and **100% test coverage** with **200+ tests**. Production ready.
24+
High code quality and **100% test coverage** with **150+ tests**. Production ready.
2525

2626
**To find out more, please check out the [Wiki](https://github.com/neomerx/json-api/wiki) and [Sample App](/sample)**.
2727

@@ -50,8 +50,10 @@ Assuming you've got an ```$author``` of type ```\Author``` you can encode it to
5050

5151
```php
5252
$encoder = Encoder::instance([
53-
'\Author' => '\AuthorSchema',
54-
], new EncoderOptions(JSON_PRETTY_PRINT, 'http://example.com/api/v1'));
53+
Author::class => AuthorSchema::class,
54+
])
55+
->withUrlPrefix('http://example.com/api/v1')
56+
->withEncodeOptions(JSON_PRETTY_PRINT);
5557

5658
echo $encoder->encodeData($author) . PHP_EOL;
5759
```
@@ -60,15 +62,22 @@ will output
6062

6163
```json
6264
{
63-
"data": {
64-
"type": "people",
65-
"id": "123",
66-
"attributes": {
67-
"first_name": "John",
68-
"last_name": "Dow"
65+
"data" : {
66+
"type" : "people",
67+
"id" : "123",
68+
"attributes" : {
69+
"first-name": "John",
70+
"last-name": "Doe"
71+
},
72+
"relationships" : {
73+
"comments" : {
74+
"links": {
75+
"related" : "http://example.com/api/v1/people/123/comments"
76+
}
77+
}
6978
},
70-
"links": {
71-
"self": "http://example.com/api/v1/people/123"
79+
"links" : {
80+
"self" : "http://example.com/api/v1/people/123"
7281
}
7382
}
7483
}
@@ -79,36 +88,50 @@ The ```AuthorSchema``` provides information about resource's attributes and migh
7988
```php
8089
class AuthorSchema extends BaseSchema
8190
{
82-
protected $resourceType = 'people';
91+
public function getType(): string
92+
{
93+
return 'people';
94+
}
8395

8496
public function getId($author): ?string
8597
{
86-
/** @var Author $author */
8798
return $author->authorId;
8899
}
89100

90-
public function getAttributes($author, array $fieldKeysFilter = null): ?array
101+
public function getAttributes($author): iterable
91102
{
92-
/** @var Author $author */
93103
return [
94-
'first_name' => $author->firstName,
95-
'last_name' => $author->lastName,
104+
'first-name' => $author->firstName,
105+
'last-name' => $author->lastName,
106+
];
107+
}
108+
109+
public function getRelationships($author): iterable
110+
{
111+
return [
112+
'comments' => [
113+
self::RELATIONSHIP_LINKS_SELF => false,
114+
self::RELATIONSHIP_LINKS_RELATED => true,
115+
116+
// Data include supported as other cool features
117+
// self::RELATIONSHIP_DATA => $author->comments,
118+
],
96119
];
97120
}
98121
}
99122
```
100123

101-
The first ```EncoderOptions``` parameter ```JSON_PRETTY_PRINT``` is a PHP predefined [JSON constant](http://php.net/manual/en/json.constants.php).
124+
Parameter ```http://example.com/api/v1``` is a URL prefix that will be applied to all encoded links unless they have a flag set telling not to add any prefixes.
102125

103-
The second ```EncoderOptions``` parameter ```http://example.com/api/v1``` is a URL prefix that will be applied to all encoded links unless they have ```$treatAsHref``` flag set to ```true```.
126+
Parameter ```JSON_PRETTY_PRINT``` is a PHP predefined [JSON constant](http://php.net/manual/en/json.constants.php).
104127

105128
A sample program with encoding of multiple, nested, filtered objects and more is [here](sample).
106129

107130
**For more advanced usage please check out the [Wiki](https://github.com/neomerx/json-api/wiki)**.
108131

109132
## Versions
110133

111-
Current version is 2.x (PHP 7.1+) for older PHP (PHP 5.5 - 7.0, HHVM) please use version 1.x.
134+
Current version is 3.x (PHP 7.1+) for older PHP (PHP 5.5 - 7.0, HHVM) please use version 1.x.
112135

113136
## Questions?
114137

composer.json

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,23 +21,21 @@
2121
}
2222
],
2323
"require": {
24-
"php": ">=7.1.0",
25-
"psr/log": "^1.0"
24+
"php": ">=7.1.0"
2625
},
2726
"require-dev": {
2827
"phpunit/phpunit": "^7.0",
2928
"mockery/mockery": "^1.0",
3029
"scrutinizer/ocular": "^1.4",
3130
"squizlabs/php_codesniffer": "^2.9",
32-
"monolog/monolog": "^1.23",
3331
"phpmd/phpmd": "^2.6"
3432
},
3533
"minimum-stability": "stable",
3634
"autoload": {
3735
"psr-4": {
3836
"Neomerx\\JsonApi\\": "src/"
3937
},
40-
"files": ["src/I18n/translate.php"]
38+
"files": ["src/I18n/format.php"]
4139
},
4240
"autoload-dev": {
4341
"psr-4": {
@@ -48,10 +46,8 @@
4846
"scripts": {
4947
"test": ["@test-unit", "@test-cs", "@test-md"],
5048
"test-unit": "./vendor/phpunit/phpunit/phpunit --coverage-text",
51-
"test-unit-with-coverage": "phpdbg -qrr ./vendor/bin/phpunit --coverage-text",
49+
"test-unit-phpdbg": "phpdbg -qrr ./vendor/bin/phpunit --coverage-text",
5250
"test-cs": "./vendor/bin/phpcs -p -s --standard=PSR2 ./src ./tests",
53-
"test-md": "./vendor/bin/phpmd ./src text codesize,controversial,cleancode,design,unusedcode,naming",
54-
55-
"perf-test": "docker-compose run --rm cli_7_1_php php /app/sample/sample.php -t=10000"
51+
"test-md": "./vendor/bin/phpmd ./src text codesize,controversial,cleancode,design,unusedcode,naming"
5652
}
5753
}

docker-compose.yml

Lines changed: 0 additions & 46 deletions
This file was deleted.

perf/Dockerfile

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
FROM php:7.3-cli
2+
3+
ARG DEBIAN_FRONTEND=noninteractive
4+
5+
RUN version=$(php -r "echo PHP_MAJOR_VERSION.PHP_MINOR_VERSION;") \
6+
&& mkdir -p /tmp/blackfire \
7+
&& curl -A "Docker" -L -s https://blackfire.io/api/v1/releases/probe/php/linux/amd64/$version | tar zxp -C /tmp/blackfire \
8+
&& curl -A "Docker" -L -s https://blackfire.io/api/v1/releases/client/linux_static/amd64 | tar zxp -C /tmp/blackfire \
9+
&& mv /tmp/blackfire/blackfire /usr/bin/blackfire \
10+
&& mv /tmp/blackfire/blackfire-*.so $(php -r "echo ini_get('extension_dir');")/blackfire.so \
11+
&& printf "extension=blackfire.so\nblackfire.agent_socket=tcp://blackfire:8707\n" > /usr/local/etc/php/conf.d/blackfire.ini \
12+
&& rm -Rf /tmp/blackfire \
13+
&& apt-get clean; rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* /usr/share/doc/*

perf/blackfire.io.env.sample

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#
2+
# Credential should be taken from https://blackfire.io/my/settings/credentials
3+
#
4+
BLACKFIRE_CLIENT_ID=<put Client ID here>
5+
BLACKFIRE_CLIENT_TOKEN=<put Client Token here>
6+
BLACKFIRE_SERVER_ID=<put Server ID here>
7+
BLACKFIRE_SERVER_TOKEN=<put Server Token here>

perf/docker-compose.yml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
version: '3.7'
2+
3+
services:
4+
cli_php:
5+
build:
6+
context: ./
7+
dockerfile: Dockerfile
8+
container_name: cli_php_json_api_blackfire
9+
env_file: blackfire.io.env
10+
volumes:
11+
- type: bind
12+
source: ./../
13+
target: /app
14+
working_dir: /app
15+
tty: true

perf/readme.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Overview
2+
3+
This is performance test suite for the library that uses [Blackfire](https://blackfire.io) _Performance Management Solution_.
4+
5+
## Prerequisites
6+
- Install [docker](https://docs.docker.com/install/#supported-platforms);
7+
- Install [docker-compose](https://docs.docker.com/compose/install/).
8+
9+
## Installation
10+
11+
- Copy `blackfire.io.env.sample` to `blackfire.io.env`;
12+
- Put your Client ID, Client Token, Server ID and Server Token to `blackfire.io.env` from [Blackfire.io credentials page](https://blackfire.io/my/settings/credentials) (registration needed).
13+
14+
## Profile Performance
15+
16+
```bash
17+
$ docker-compose run --rm cli_php blackfire run php -d zend.assertions=-1 /app/sample/sample.php -t=100
18+
```
19+
20+
The output will contain basic performance info and a URL with detailed profiling info [such as this one](https://blackfire.io/profiles/207fb294-d851-48ad-a31c-db29478172e3/graph).
21+
22+
> Note: The **first** run will download necessary docker images which takes some time. The subsequent runs will not require such downloads and be faster.
23+
24+
The created container can be removed from the local machine with
25+
26+
```bash
27+
$ docker rmi perf_cli_php
28+
```

0 commit comments

Comments
 (0)