Skip to content

Commit 7439d5b

Browse files
author
aganttor
committed
Release hawkbit presentation 2.0
1 parent da89bb6 commit 7439d5b

12 files changed

+634
-164
lines changed

CHANGELOG.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,21 @@
11
# Hawkbit Presentation Changelog
22

3+
## 2.0
4+
5+
### Added
6+
7+
- Add extensible presentation adapters, for support of different view engines
8+
- Convert Plates integration into Plates adapter as default adapter
9+
- Add convenient Psr7 adapter wrapper, which captures render output and inject into psr7 response body
10+
11+
### Altered
12+
13+
- Remove dependencies to hawkbit
14+
15+
### Removed
16+
17+
- Remove service provider since their is no standard. Hawbit presentation provides no service provider for each derivate of a service provider
18+
319
## 1.1.0
420

521
### Added

README.md

Lines changed: 142 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@
66
[![Total Downloads][ico-downloads]][link-downloads]
77
[![Coverage Status][ico-coveralls]][link-coveralls]
88

9-
Presentation layer for Hawkbit PSR-7 Micro PHP framework.
10-
Hawkbit Presentation uses [`league/plates`](http://platesphp.com/) as view engine and wraps it with in a PresentationService
9+
Ployglott and extensible presentation layer for different presentation engines.
10+
The presentation layer uses [`league/plates`](http://platesphp.com/) as default engine and could be extended with Twig,
11+
Smarty, Liquif, blade and further more.
1112

1213
## Install
1314

@@ -51,6 +52,16 @@ The following versions of PHP are supported by this version.
5152
* PHP 7.1
5253
* HHVM
5354

55+
In addition to PHP you also need a valid [PSR-7](https://packagist.org/providers/psr/http-message-implementation)
56+
and [PSR-11](https://packagist.org/providers/psr/container-implementation) integration.
57+
58+
[Hawkbit Micro Framework](https://github.com/HawkBitPhp/hawkbit) is supported by default.
59+
60+
[Silex](https://silex.sensiolabs.org/),
61+
[Lumen](https://lumen.laravel.com/),
62+
[zend-expressive](https://docs.zendframework.com/zend-expressive/)
63+
and [Slim](https://www.slimframework.com/) support is untested but should work as well.
64+
5465
## Setup
5566

5667
Setup with an existing application configuration (we refer to [tests/assets/config.php](tests/assets/config.php))
@@ -60,14 +71,22 @@ Setup with an existing application configuration (we refer to [tests/assets/conf
6071

6172
use \Hawkbit\Application;
6273
use \Hawkbit\Presentation\PresentationService;
63-
use \Hawkbit\Presentation\PresentationServiceProvider;
74+
use \Hawkbit\Presentation\Adapters\PlatesAdapter;
75+
use \Hawkbit\Presentation\Adapters\Adapter;
6476

65-
$app = new Application(require_once __DIR__ . '/config.php');
77+
$app = new Application(require './config.php');
6678

67-
$app->register(new PresentationServiceProvider([
79+
// or configure manually
80+
81+
$app = new Application();
82+
83+
$app[Adapter::class] = new PlatesAdapter([
6884
'default' => __DIR__ . '/path/to/templates',
6985
'another' => __DIR__ . '/path/to/other/templates',
70-
]));
86+
]);
87+
88+
$app[PresentationService::class] = new PresentationService($app->getContainer());
89+
7190
```
7291

7392
### Presentation from Hawbit Application
@@ -135,6 +154,123 @@ $service->getEngine()
135154

136155
```
137156

157+
### Wrap into PSR 7
158+
159+
Hawkbit presentation provides a PSR-7 Wrapper to capture rendered output into psr 7 response.
160+
161+
162+
Please keep in mind to add an additional [PSR-7](https://packagist.org/providers/psr/http-message-implementation)
163+
implementation!
164+
165+
You just need to wrap you favorite presentation adapter into psr 7 adapter
166+
167+
```php
168+
<?php
169+
170+
use \Hawkbit\Application;
171+
use \Hawkbit\Presentation\PresentationService;
172+
use \Hawkbit\Presentation\Adapters\PlatesAdapter;
173+
use \Hawkbit\Presentation\Adapters\Adapter;
174+
use \Hawkbit\Presentation\Adapters\Psr7WrapperAdapter;
175+
176+
$app = new Application(require './config.php');
177+
178+
// or configure manually
179+
180+
$app = new Application();
181+
182+
$app[Adapter::class] = new Psr7WrapperAdapter(new PlatesAdapter([
183+
'default' => __DIR__ . '/path/to/templates',
184+
'another' => __DIR__ . '/path/to/other/templates',
185+
]),
186+
$app[\Psr\Http\Message\ServerRequestInterface::class],
187+
$app[\Psr\Http\Message\ResponseInterface::class]);
188+
189+
$app[PresentationService::class] = new PresentationService($app->getContainer());
190+
191+
```
192+
193+
The integrations works with examples mentioned above
194+
195+
#### Rendering
196+
197+
Please keep in mind, that the render method is now returning an instance of `\Psr\Http\Message\ResponseInterface`
198+
instead of a string!
199+
200+
Your presentation logic e. g. in a controller is now reduced as follows
201+
202+
```php
203+
<?php
204+
205+
use Hawkbit\Presentation\PresentationService;
206+
use Psr\Http\Message\ServerRequestInterface;
207+
use Psr\Http\Message\ResponseInterface;
208+
209+
class MyController
210+
{
211+
/**
212+
* @var PresentationService
213+
*/
214+
private $presentationService;
215+
216+
/**
217+
* TestInjectableController constructor.
218+
* @param PresentationService $presentationService
219+
*/
220+
public function __construct(PresentationService $presentationService)
221+
{
222+
$this->presentationService = $presentationService;
223+
}
224+
225+
public function getIndex(ServerRequestInterface $request, ResponseInterface $response, array $args = [])
226+
{
227+
// configured with PSR-7 adapter
228+
return $this->presentationService->render('index', ['world' => 'World']);
229+
}
230+
}
231+
```
232+
233+
#### Add response on render
234+
235+
Response class is attached while rendering by default. But in some cases you need to
236+
add your own response class just before rendering. The wrappers render method takes
237+
optional response as a third argument.
238+
239+
```php
240+
<?php
241+
242+
use Hawkbit\Presentation\PresentationService;
243+
use Psr\Http\Message\ServerRequestInterface;
244+
use Psr\Http\Message\ResponseInterface;
245+
246+
class MyController
247+
{
248+
/**
249+
* @var PresentationService
250+
*/
251+
private $presentationService;
252+
253+
/**
254+
* TestInjectableController constructor.
255+
* @param PresentationService $presentationService
256+
*/
257+
public function __construct(PresentationService $presentationService)
258+
{
259+
$this->presentationService = $presentationService;
260+
}
261+
262+
public function getIndex(ServerRequestInterface $request, ResponseInterface $response, array $args = [])
263+
{
264+
// manipulate response
265+
// for example we need to add an api key
266+
$response = $response->withHeader('API-KEY', 123);
267+
268+
// configured with PSR-7 adapter
269+
return $this->presentationService->render('index', ['world' => 'World'], $response);
270+
}
271+
}
272+
```
273+
138274
You are now able to [render a different view](http://platesphp.com/engine/folders/) e.g. `$presentationService->render('acme::index')`
139275
and use a [view helper function](http://platesphp.com/engine/functions/) within an view (template).
140276

composer.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,13 @@
1818
],
1919
"require": {
2020
"php": ">=5.5.0",
21-
"league/plates": "~3.0"
21+
"league/plates": "~3.0",
22+
"psr/container": "~1.0",
23+
"psr/http-message": "~1.0"
2224
},
2325
"require-dev": {
2426
"phpunit/phpunit": "~4.8",
25-
"hawkbit/hawkbit": "~2.0"
27+
"hawkbit/hawkbit": "~2.4"
2628
},
2729
"autoload": {
2830
"psr-4": {

src/Adapters/Adaptable.php

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
<?php
2+
/**
3+
* The Hawkbit Micro Framework. An advanced derivate of Proton Micro Framework
4+
*
5+
* @author Marco Bunge <[email protected]>
6+
* @copyright Marco Bunge <[email protected]>
7+
*
8+
* @license MIT
9+
* @since 2.0
10+
*/
11+
12+
namespace Hawkbit\Presentation\Adapters;
13+
14+
15+
abstract class Adaptable implements Adapter
16+
{
17+
18+
/** @var Adapter */
19+
protected $adapter;
20+
21+
/**
22+
* @return Adapter
23+
*/
24+
protected function getAdapter()
25+
{
26+
return $this->adapter;
27+
}
28+
29+
/**
30+
* @param Adapter $adapter
31+
* @return Adaptable
32+
*/
33+
protected function setAdapter($adapter)
34+
{
35+
$this->adapter = $adapter;
36+
return $this;
37+
}
38+
39+
public function getEngine(){
40+
return $this->adapter->getEngine();
41+
}
42+
43+
/**
44+
* Add preassigned template data.
45+
* @param array $data;
46+
* @param null|string|array $templates;
47+
* @return $this
48+
*/
49+
public function addData(array $data, $templates = null){
50+
$this->adapter->addData($data, $templates);
51+
return $this;
52+
}
53+
54+
/**
55+
* Get all preassigned template data.
56+
* @param null|string $template;
57+
* @return array
58+
*/
59+
public function getData($template){
60+
return $this->adapter->getData($template);
61+
}
62+
63+
/**
64+
* Check if a template exists.
65+
* @param string $name
66+
* @return boolean
67+
*/
68+
public function exists($name)
69+
{
70+
return $this->adapter->exists($name);
71+
}
72+
73+
/**
74+
* Create a new template.
75+
* @param string $name
76+
* @return mixed
77+
*/
78+
public function make($name)
79+
{
80+
return $this->adapter->make($name);
81+
}
82+
83+
/**
84+
* Create a new template and render it.
85+
* @param string $name
86+
* @param array $data
87+
* @param null $optional Optional rendering logic or implementations e. g. response for PSR7 Wrapper
88+
* @return string
89+
*/
90+
public function render($name, array $data = array(), $optional = null)
91+
{
92+
return $this->adapter->render($name, $data, $optional);
93+
}
94+
95+
}

src/Adapters/Adapter.php

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
/**
3+
* The Hawkbit Micro Framework. An advanced derivate of Proton Micro Framework
4+
*
5+
* @author Marco Bunge <[email protected]>
6+
* @copyright Marco Bunge <[email protected]>
7+
*
8+
* @license MIT
9+
* @since 2.0
10+
*/
11+
12+
namespace Hawkbit\Presentation\Adapters;
13+
14+
15+
interface Adapter
16+
{
17+
/**
18+
* @return mixed
19+
*/
20+
public function getEngine();
21+
22+
/**
23+
* Add preassigned template data.
24+
* @param array $data;
25+
* @param null|string|array $templates;
26+
* @return $this
27+
*/
28+
public function addData(array $data, $templates = null);
29+
30+
/**
31+
* Get all preassigned template data.
32+
* @param null|string $template;
33+
* @return array
34+
*/
35+
public function getData($template);
36+
37+
/**
38+
* Check if a template exists.
39+
* @param string $name
40+
* @return boolean
41+
*/
42+
public function exists($name);
43+
44+
/**
45+
* Create a new template.
46+
* @param string $name
47+
* @return mixed
48+
*/
49+
public function make($name);
50+
51+
/**
52+
* Create a new template and render it.
53+
* @param string $name
54+
* @param array $data
55+
* @return string
56+
*/
57+
public function render($name, array $data = array());
58+
}

0 commit comments

Comments
 (0)