Skip to content

Commit 46fa6de

Browse files
committed
Merge pull request #6 from DocnetUK/2.0
Merging 2.0 branch into master
2 parents 8a022b3 + 7f10e91 commit 46fa6de

34 files changed

+2344
-905
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,5 @@ Thumbs.db
1111
# IDE #
1212
#######
1313
.idea/
14+
15+
build/

.travis.yml

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
language: php
2+
3+
php:
4+
- 5.4
5+
- 5.5
6+
- 5.6
7+
8+
before_script:
9+
- composer self-update
10+
- composer install --prefer-source --dev
11+
12+
script:
13+
- mkdir -p build/logs
14+
- php vendor/bin/phpunit
15+
16+
after_script:
17+
- php vendor/bin/coveralls

LICENSE

+1-1
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ Apache License
186186
same "printed page" as the copyright notice for easier
187187
identification within third-party archives.
188188

189-
Copyright 2014 Docnet
189+
Copyright 2015 Docnet
190190

191191
Licensed under the Apache License, Version 2.0 (the "License");
192192
you may not use this file except in compliance with the License.

README.md

+69-80
Original file line numberDiff line numberDiff line change
@@ -1,88 +1,65 @@
1-
# PHP JSON API Library #
1+
[![Build Status](https://api.travis-ci.org/DocnetUK/php-japi.svg?branch=2.0)](https://travis-ci.org/DocnetUK/php-japi)
2+
[![Coverage Status](https://coveralls.io/repos/DocnetUK/php-japi/badge.svg?branch=2.0)](https://coveralls.io/r/DocnetUK/php-japi)
23

3-
Simple library for building HTTP JSON APIs in PHP.
4+
# PHP JSON API Library #
45

5-
Sure, I know, there are loads of MVC frameworks out there - and a few very popular ones - that can do this for
6-
you and a lot more besides.
6+
Version 2 of our library for building HTTP JSON APIs in PHP.
77

8-
BUT, `php-japi` is designed to ONLY do HTTP JSON APIs, so it's small and fast.
8+
Some major changes in version 2
9+
- Adopt better code practices, allowing for Dependency Injection
10+
- Adopt our new "Single Responsibility Controller" approach
11+
- Decouple Router from JAPI container
12+
- Use PSR logging
13+
- Adopt PHP 5.4 minimum version
914

1015
As we expand our Service Orientated Architecture (SOA) at Docnet, we're using this more and more - so I hope it's useful
1116
to someone else ;)
1217

1318
Intended to use HTTP status codes wherever possible for passing success/failure etc. back to the client.
1419

15-
Data/payload is your responsibility!
20+
## Single Responsibility Controller ##
1621

17-
## Hello, World! ##
22+
We've adopted a new (for us) take on routing and controller complexity in 2.0. As such, where previously, you might have
23+
had multiple actions (methods) on the same class like this:
1824

19-
Let's assume we want our API to respond on the following URL: `api.example.com/hello/world`
25+
- `BasketController::fetchDetailAction()`
26+
- `BasketController::addAction()`
27+
- `BasketController::removeAction()`
28+
- `BasketController::emptyAction()`
2029

21-
So, here's the JAPI controller we'll need:
22-
23-
```php
24-
<?php
25-
class Hello extends \Docnet\JAPI\Controller
26-
{
27-
public function worldAction()
28-
{
29-
$this->setResponse(array(
30-
'message' =>'Hello, World!'
31-
));
32-
}
33-
}
34-
```
30+
Now this would be 4 name-spaced classes, like this
3531

36-
See the examples folder for a working demo.
32+
- `Basket\FetchDetail`
33+
- `Basket\Add`
34+
- `Basket\Remove`
35+
- `Basket\Empty`
3736

38-
## Getting Started ##
37+
This allows for
38+
- Greater code modularity
39+
- Smaller classes
40+
- Much easier Dependency Injection via `__construct()` as each "action" is it's own class.
3941

40-
### Install with Composer ###
42+
You can still share common code via extension/composition - whatever takes your fancy!
4143

42-
Here's the require line for Composer users...
44+
JAPI will call the `dispatch()` method on your controller.
4345

44-
`"docnet/php-japi": "v1.1.1"`
46+
### SOLID Routing ###
4547

46-
...or just download and use the src folder.
48+
The bundled router will accept any depth of controller namespace, like this
4749

48-
### Entry Point (index.php) ###
50+
- `/one` => `One`
51+
- `/one/two` => `One\Two`
52+
- `/one/two/three` => `One\Two\Three`
4953

50-
Assuming:
51-
52-
- You've got Apache/whatever set up to route all requests to this file
53-
- An auto-loader is present (like the Composer example here) or you've included all files necessary
54-
- Your controllers are not name spaced and you're happy with our default configuration
55-
56-
then something like this is all the code you need
54+
When you construct the Router, you can give it a "root" namespace, like this:
5755

5856
```php
59-
<?php
60-
require_once('vendor/autoload.php');
61-
$api = new \Docnet\JAPI();
62-
$api->run();
57+
$router = new \Docnet\JAPI\SolidRouter('\\Docnet\\App\\Controller\\');
6358
```
6459

65-
See the examples folder for a working demo (api.php).
66-
67-
## Routing ##
68-
69-
The standard routing is quite strict, and (at the time ot writing) expects a controller + action pair for all requests.
70-
71-
e.g. `api.example.com/hello/world`
72-
73-
URLs without a 2-part controller + action pair will result in a 404, such as
60+
Which results in this routing:
7461

75-
- `api.example.com`
76-
- `api.example.com/`
77-
- `api.example.com/controller`
78-
79-
We do conversion to `StudlyCaps` classes and `camelCase` methods, splitting on hyphens and suffix 'Action' for the
80-
method. e.g.
81-
82-
- `api.example.com/hello/world` becomes `Hello::worldAction()`
83-
- `api.example.com/hello-world/long-name` becomes `HelloWorld::longNameAction()`
84-
85-
I seem to recall this is similar to ZF1.
62+
- `/one/two` => `\Docnet\App\Controller\One\Two`
8663

8764
### Static Routes ###
8865

@@ -92,39 +69,51 @@ and so make calls very slightly faster.
9269
Add a single custom route
9370

9471
```php
95-
<?php
96-
$api = new \Docnet\JAPI();
97-
$api->getRouter()->addRoute('/goodbye', 'Hello', 'worldAction');
98-
$api->run();
72+
$router = new \Docnet\JAPI\SolidRouter();
73+
$router->addRoute('/hello', '\\Some\\Controller');
9974
```
10075

10176
Or set a load of them
10277

10378
```php
104-
<?php
105-
$api = new \Docnet\JAPI();
106-
$api->getRouter()->setRoutes(array(
107-
'/goodbye' => array('Hello', 'worldAction'),
108-
'/testing' => array('SomeController', 'testAction'),
109-
));
110-
$api->run();
79+
$router = new \Docnet\JAPI\SolidRouter();
80+
$router->setRoutes([
81+
'/hello' => '\\Some\\Controller',
82+
'/world' => '\\Other\\Controller'
83+
]);
11184
```
11285

113-
### Custom Router ###
86+
## Installation ##
87+
88+
Here's the require line for Composer users (during 2-series development)...
11489

115-
If you want to write your own Router class? no problem!
90+
`"docnet/php-japi": "2.0.*@dev"`
11691

117-
Perhaps you want to route based on HTTP request methods (GET/POST/PUT/DELETE).
92+
...or just download and use the src folder.
93+
94+
## Bootstrapping ##
11895

119-
There's a Router interface and you can follow and you can change the router through the JAPI object like this:
96+
Assuming...
97+
98+
- You've got Apache/whatever set up to route all requests to this file
99+
- An auto-loader is present (like the Composer example here) or you've included all files necessary
100+
101+
...then something like this is all the code you need in your `index.php`
120102

121103
```php
122-
<?php
123-
$api = new \Docnet\JAPI();
124-
$api->setRouter(new MyAwesomeRouter());
125-
$api->run();
104+
(new \Docnet\JAPI())->bootstrap(function(){
105+
106+
$obj_router = new \Docnet\JAPI\SolidRouter();
107+
$obj_router->route();
108+
109+
$str_controller = $obj_router->getController();
110+
return new $str_controller();
111+
112+
});
126113
```
127114

115+
See the examples folder for a working demo (api.php).
116+
128117
## Coding Standards ##
129118

130-
Desired adherence to [PSR-2](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-2-coding-style-guide.md).
119+
Desired adherence to [PSR-2](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-2-coding-style-guide.md). Uses [PSR-3](https://github.com/php-fig/log) logging.

composer.json

+15-3
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,29 @@
11
{
22
"name": "docnet/php-japi",
33
"type": "library",
4-
"description": "Simple framework for building HTTP JSON APIs in PHP",
4+
"description": "Simple framework for building HTTP JSON APIs in PHP. 2.",
5+
"authors": [
6+
{
7+
"name": "Tom Walder",
8+
"email": "[email protected]"
9+
}
10+
],
511
"keywords": ["docnet", "json", "http", "api"],
612
"homepage": "https://github.com/DocnetUK/php-japi",
713
"license": "Apache-2.0",
814
"require": {
9-
"php": ">=5.3.0"
15+
"php": ">=5.4.0",
16+
"psr/log": "~1.0"
17+
},
18+
"require-dev": {
19+
"phpunit/phpunit": "~4.0",
20+
"satooshi/php-coveralls": "dev-master"
1021
},
1122
"autoload": {
1223
"classmap": [
1324
"src/"
14-
]
25+
],
26+
"psr-4": {"Docnet\\": "src/Docnet/"}
1527
},
1628
"include-path": ["src/"]
1729
}

0 commit comments

Comments
 (0)