Skip to content

Commit 9678f33

Browse files
author
Alistair Kearney
committed
Numerous cleanup and improvements. Updated CHANGELOG, README.
1 parent 9315134 commit 9678f33

10 files changed

Lines changed: 232 additions & 42 deletions

CHANGELOG.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Change Log
2+
All notable changes to this project will be documented in this file.
3+
This project adheres to [Semantic Versioning](http://semver.org/).
4+
5+
## 0.1.1 - 2016-04-25
6+
#### Added
7+
- Added CONTRIBUTING.md and CHANGELOG.md
8+
9+
#### Changed
10+
- Code cleanup
11+
- Improved README.md
12+
13+
## 0.1.0 - 2015-09-13
14+
#### Added
15+
- Initial release
16+
- Added Symphony PDO as requirement
17+
18+
[0.1.1]: https://github.com/pointybeard/api_framework/compare/v0.1.0...v0.1.1

CONTRIBUTING.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Contributing to this project
2+
3+
We encourage contribution to this project and only ask you follow some simple rules to make everyone's job a little easier.
4+
5+
## Found a bug?
6+
7+
Please lodge an issue at the GitHub issue tracker for this project -- [https://github.com/pointybeard/api_framework/issues](https://github.com/pointybeard/api_framework/issues)
8+
9+
Include details on the behaviour you are seeing, and steps needed to reproduce the problem.
10+
11+
## Want to contribute code?
12+
13+
* Fork the project
14+
* Make your feature addition or bug fix
15+
* Ensure your code is nicely formatted
16+
* Commit just the modifications, do not alter CHANGELOG.md. If relevant, link to GitHub issue (see [https://help.github.com/articles/closing-issues-via-commit-messages/](https://help.github.com/articles/closing-issues-via-commit-messages/))
17+
* Send the pull request
18+
19+
We will review the code and either merge it in, or leave some feedback.

LICENCE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2016 Alistair Kearney
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 150 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,151 @@
1-
# Symphony Api Framework
1+
# RESTful API Framework for Symphony CMS
22

3-
This framework adds a Json renderer to Symphony and helps to quickly build an API with a, event driven, controller interface.
3+
JSON renderer and event driven controller interface for Symphony CMS designed to quickly build a RESTful APIs.
4+
5+
## Installation
6+
7+
This is an extension for Symphony CMS. Add it to your `/extensions` folder in your Symphony CMS installation, then enable it though the interface.
8+
9+
### Requirements
10+
11+
This extension requires the **[Symfony HTTP Foundation](https://github.com/symfony/http-foundation)** (`symfony/http-foundation`) and **[Symphony PDO](https://github.com/pointybeard/symphony-pdo)** (`pointybeard/symphony-pdo`) to be installed via Composer. Either require both of these in your main composer.json file, or run `composer install` on the `extension/api_framework` directory.
12+
13+
"require": {
14+
"php": ">=5.6.6",
15+
"symfony/http-foundation": "^3.0@dev",
16+
"pointybeard/symphony-pdo": "~0.1"
17+
}
18+
19+
## Usage
20+
21+
This extension has two parts: The JSON renderer, and the Controller event.
22+
23+
### JSON Renderer
24+
25+
Any page with a type `JSON` will trigger the new JSON renderer. This automatically converts your XML output into a JSON document. This includes output from any events.
26+
27+
### Controller Event
28+
29+
Use the `API Framework: Controller` event to listen for PUT, POST, PATCH and DELETE requests. To create your own controller, make a folder called `controllers` in your `/workspace` directory.
30+
31+
A controller will respond to the 4 methods (PUT, POST, PATCH and DELETE) via a same named public method. E.g. to respond to a PUT request, create a method called 'put' in your controller like so
32+
33+
public function put(Request $request, Response $response)
34+
{
35+
...
36+
}
37+
38+
Modify `$response` to set return values and status code. E.g.:
39+
40+
$response->setStatusCode(Response::HTTP_OK);
41+
42+
Lastly, call the render method (which is inherited) to generate the output. E.g.
43+
44+
return $this->render($response, ['data' => 'some output']);
45+
46+
To actually use a controller, name it the same as a top level page. E.g. if you had a page called "entry", and you wanted to provide PUT, POST, PATCH and DELETE functionality, name your controller class "ControllerEntry" and the file `ControllerEntry.php`
47+
48+
Here is an example of a completed controller:
49+
50+
<?php
51+
52+
use Symfony\Component\HttpFoundation\Request;
53+
use Symfony\Component\HttpFoundation\Response;
54+
use Symphony\ApiFramework\Lib\AbstractController;
55+
use Symphony\ApiFramework\Lib\Traits;
56+
57+
final class ControllerExample extends AbstractController{
58+
59+
use Traits\hasEndpointSchemaTrait;
60+
61+
public function execute(){
62+
// Optional. Add any code here, such
63+
// as including extension or autoloaders.
64+
// This method is automatically invoked
65+
// anytime this controller is going to be used.
66+
}
67+
68+
public function post(Request $request, Response $response)
69+
{
70+
$data = $request->request->all();
71+
72+
try{
73+
// do some work here
74+
$response->setStatusCode(Response::HTTP_CREATED);
75+
$response->headers->set(
76+
'Location', "some/path/to/resource"
77+
);
78+
return $this->render($response, [
79+
'data' => [
80+
'id' => $idOfNewResource,
81+
],
82+
'status' => $response->getStatusCode(),
83+
'message' => "Item was successfully created."
84+
]);
85+
86+
} catch(\Exception $ex) {
87+
// handle errors here.
88+
}
89+
return;
90+
}
91+
92+
public function put(Request $request, Response $response)
93+
{
94+
$someEntryId = (int)Frontend::instance()
95+
->Page()
96+
->Params()['preference-id'];
97+
98+
$data = $request->request->all();
99+
$output = [];
100+
101+
try{
102+
// do some work here with "someEntryId"
103+
$output = [
104+
'message' => "Entry successfully updated.",
105+
'status' => Response::HTTP_OK
106+
];
107+
108+
} catch(Exceptions\ModelEntryNotFoundException $ex) {
109+
$output = [
110+
'message' => "Entry not found.",
111+
'status' => Response::HTTP_NOT_FOUND
112+
];
113+
}
114+
115+
$response->setStatusCode($output['status']);
116+
return $this->render($response, $output);
117+
}
118+
119+
public function patch(Request $request, Response $response)
120+
{
121+
// Sometimes it is okay just to use the
122+
// PUT code to handle PATCH requests also
123+
return $this->put($request, $response);
124+
}
125+
126+
public function delete(Request $request, Response $response)
127+
{
128+
$someEntryId = (int)Frontend::instance()
129+
->Page()
130+
->Params()['some-id'];
131+
132+
$output = [];
133+
134+
try{
135+
// do some work here using "someEntryId"
136+
$output = [
137+
'message' => "Entry successfully deleted.",
138+
'status' => Response::HTTP_OK
139+
];
140+
141+
} catch(Exceptions\ModelEntryNotFoundException $ex) {
142+
$output = [
143+
'message' => "Entry not found.",
144+
'status' => Response::HTTP_NOT_FOUND
145+
];
146+
}
147+
148+
$response->setStatusCode($output['status']);
149+
return $this->render($response, $output);
150+
}
151+
}

composer.json

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "pointybeard/symphony-api-framework",
33
"version": "1.0.0",
4-
"description": "",
4+
"description": "JSON renderer and event driven controller interface for Symphony CMS designed to quickly build a RESTful APIs.",
55
"homepage": "http://alistairkearney.com",
66
"license": "MIT",
77
"authors": [
@@ -11,24 +11,18 @@
1111
}
1212
],
1313
"support": {
14-
"issues": "https://github.com/pointybeard/symphony-api-framework/issues",
15-
"wiki": "https://github.com/pointybeard/symphony-api-framework/wiki"
14+
"issues": "https://github.com/pointybeard/symphony-api/issues",
15+
"wiki": "https://github.com/pointybeard/symphony-api/wiki"
1616
},
1717
"minimum-stability": "dev",
1818
"require": {
1919
"php": ">=5.6.6",
20-
"monolog/monolog": "^1.16@dev",
2120
"symfony/http-foundation": "^3.0@dev",
2221
"pointybeard/symphony-pdo": "~0.1"
2322
},
2423
"autoload": {
2524
"psr-4": {
2625
"Symphony\\ApiFramework\\": "src"
2726
}
28-
},
29-
"autoload-dev": {
30-
"psr-4": {
31-
"Symphony\\ApiFramework\\Tests\\": "tests"
32-
}
3327
}
3428
}

events/event.controller.php

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,14 @@ class eventController extends SectionEvent
88
public static function about()
99
{
1010
return [
11-
'name' => 'Symphony API Framework: Event Controller',
12-
'author' => array(
11+
'name' => 'API Framework: Controller',
12+
'author' => [
1313
'name' => 'Alistair Kearney',
14-
'website' => 'http://cp.kickd',
15-
'email' => 'alistair@ruleandmake.com'),
16-
'version' => 'Symphony 2.6.3',
17-
'release-date' => '2015-08-28T02:10:22+00:00',
14+
'website' => 'http://alistairkearney.com',
15+
'email' => 'hi@alistairkearney.com'
16+
],
17+
'version' => 'Symphony 2.6.7',
18+
'release-date' => '2016-04-25T02:10:22+00:00',
1819
'trigger-condition' => 'POST|PUT|PATCH|DELETE'
1920
];
2021
}

extension.driver.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<?php
22

3-
include __DIR__.'/vendor/autoload.php';
3+
include __DIR__ . '/vendor/autoload.php';
44

5-
Class extension_api_framework extends Extension {
5+
Class extension_symphony_api extends Extension {
66
public function getSubscribedDelegates(){
77
return[
88
[
@@ -30,7 +30,7 @@ public function setJSONLauncher($context)
3030

3131
public function setBoilerplateXSL($context)
3232
{
33-
// @todo: This should only be available if logged in
33+
// @todo: This should only be available if logged in
3434
if(!isset($_GET['boilerplate-xsl']) && (!in_array('JSON', Frontend::Page()->pageData()['type'])
3535
|| !in_array('boilerplate-xsl', Frontend::Page()->pageData()['type']))) {
3636
return;

src/Includes/JsonRendererLauncher.php

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,32 +7,22 @@ function renderer_json($mode){
77
throw new Lib\Exceptions\InvalidModeException('JSON Renderer launcher is only availalbe on the frontend');
88
}
99

10-
try{
11-
$renderer = Frontend::instance();
10+
$renderer = Frontend::instance();
1211

13-
// Switch the error handlers over
14-
Lib\ExceptionHandler::initialise(true);
15-
Lib\ErrorHandler::initialise();
16-
17-
// #1808
18-
if (isset($_SERVER['HTTP_MOD_REWRITE']))
19-
{
20-
throw new Exception("mod_rewrite is required, however is not enabled.");
21-
}
22-
23-
$output = $renderer->display(getCurrentPage());
24-
25-
cleanup_session_cookies();
26-
} catch (\SymphonyErrorPage $e) {
27-
// We dont want the SymphonyErrorPage handler to kickd in, so lets re-throw this
28-
throw new Exception($e->getMessage, $e->getCode, $e);
12+
// #1808
13+
if (isset($_SERVER['HTTP_MOD_REWRITE']))
14+
{
15+
throw new Exception("mod_rewrite is required, however is not enabled.");
2916
}
3017

18+
$output = $renderer->display(getCurrentPage());
19+
20+
cleanup_session_cookies();
3121

3222
if(in_array('JSON', Frontend::Page()->pageData()['type'])) {
3323
// Load the output into a SimpleXML Container and convert to JSON
34-
$xml = new SimpleXMLElement($output);
35-
$output = json_encode($xml, JSON_PRETTY_PRINT);
24+
$xml = new SimpleXMLElement($output, LIBXML_NOCDATA);
25+
$output = json_encode(xml, JSON_PRETTY_PRINT);
3626
}
3727

3828
echo $output;

src/Lib/AbstractController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
abstract class AbstractController implements Interfaces\ControllerInterface
1313
{
1414
abstract public function execute();
15-
15+
1616
public function render(Response $response, array $data)
1717
{
1818
$response->setData($data);

src/Lib/Traits/hasEndpointSchemaTrait.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
namespace Symphony\ApiFramework\Lib\Traits;
44

55
use Symfony\Component\HttpFoundation\Request;
6-
use Monolog;
76

87
/**
98
* This trait will help resolve schema for a particular endpoint.

0 commit comments

Comments
 (0)