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
5667Setup 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
6172use \Hawkbit\Application;
6273use \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+
138274You are now able to [ render a different view] ( http://platesphp.com/engine/folders/ ) e.g. ` $presentationService->render('acme::index') `
139275and use a [ view helper function] ( http://platesphp.com/engine/functions/ ) within an view (template).
140276
0 commit comments