1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Copyright (c) 2016-2016} Andreas Heigl<[email protected]> |
4
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
5
|
|
|
* of this software and associated documentation files (the "Software"), to deal |
6
|
|
|
* in the Software without restriction, including without limitation the rights |
7
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
8
|
|
|
* copies of the Software, and to permit persons to whom the Software is |
9
|
|
|
* furnished to do so, subject to the following conditions: |
10
|
|
|
* The above copyright notice and this permission notice shall be included in |
11
|
|
|
* all copies or substantial portions of the Software. |
12
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
13
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
14
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
15
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
16
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
17
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
18
|
|
|
* THE SOFTWARE. |
19
|
|
|
* |
20
|
|
|
* @author Andreas Heigl<[email protected]> |
21
|
|
|
* @copyright 2016-2016 Andreas Heigl |
22
|
|
|
* @license http://www.opensource.org/licenses/mit-license.php MIT-License |
23
|
|
|
* @version 0.0 |
24
|
|
|
* @since 16.01.2016 |
25
|
|
|
* @link http://github.com/heiglandreas/callingallpapers |
26
|
|
|
*/ |
27
|
|
|
|
28
|
|
|
namespace Callingallpapers\Api\Middleware; |
29
|
|
|
|
30
|
|
|
use Callingallpapers\Api\Renderer\IcalendarRenderer; |
31
|
|
|
use Callingallpapers\Api\Renderer\RssRenderer; |
32
|
|
|
use Callingallpapers\Api\Renderer\TwigRenderer; |
33
|
|
|
use JsonHelpers\JsonHelpers; |
34
|
|
|
use Psr\Http\Message\ResponseInterface; |
35
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
36
|
|
|
|
37
|
|
|
class Renderer |
38
|
|
|
{ |
39
|
|
|
protected $app; |
40
|
|
|
|
41
|
|
|
public function __construct($app) |
42
|
|
|
{ |
43
|
|
|
$this->app = $app; |
44
|
|
|
} |
45
|
|
|
/** |
46
|
|
|
* Example middleware invokable class |
47
|
|
|
* |
48
|
|
|
* @param \Psr\Http\Message\ServerRequestInterface $request PSR7 request |
49
|
|
|
* @param \Psr\Http\Message\ResponseInterface $response PSR7 response |
50
|
|
|
* @param callable $next Next middleware |
51
|
|
|
* |
52
|
|
|
* @return \Psr\Http\Message\ResponseInterface |
53
|
|
|
*/ |
54
|
|
|
public function __invoke( |
55
|
|
|
ServerRequestInterface $request, |
56
|
|
|
ResponseInterface $response, |
57
|
|
|
callable $next |
58
|
|
|
) { |
59
|
|
|
$accept = $request->getHeader('Accept'); |
60
|
|
|
if ($accept === []) { |
61
|
|
|
$jsonHelpers = new JsonHelpers($this->app->getContainer()); |
62
|
|
|
$jsonHelpers->registerResponseView(); |
63
|
|
|
$jsonHelpers->registerErrorHandlers(); |
64
|
|
|
return $next($request, $response); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
if (strpos($accept[0], 'text/calendar') !== false) { |
68
|
|
|
$container = $this->app->getContainer(); |
69
|
|
|
$container['view'] = new IcalendarRenderer(); |
70
|
|
|
} elseif (strpos($accept[0], 'application/rss+xml') !== false) { |
71
|
|
|
$container = $this->app->getContainer(); |
72
|
|
|
$container['view'] = new RssRenderer(); |
73
|
|
|
} elseif (strpos($accept[0], 'text/html') !== false) { |
74
|
|
|
$container = $this->app->getContainer(); |
75
|
|
|
$container['view'] = function ($container) { |
76
|
|
|
$config = $container['settings']; |
77
|
|
|
$tView = new \Slim\Views\Twig( |
78
|
|
|
$config['renderer']['template_path'], |
79
|
|
|
[ |
80
|
|
|
'cache' => $config['renderer']['cache_path'], |
81
|
|
|
] |
82
|
|
|
); |
83
|
|
|
$tView->addExtension(new \Slim\Views\TwigExtension( |
84
|
|
|
$container['router'], |
85
|
|
|
$container['request']->getUri() |
86
|
|
|
)); |
87
|
|
|
|
88
|
|
|
$view = new TwigRenderer($tView); |
89
|
|
|
|
90
|
|
|
return $view; |
91
|
|
|
}; |
92
|
|
|
} else { |
93
|
|
|
$jsonHelpers = new JsonHelpers($this->app->getContainer()); |
94
|
|
|
$jsonHelpers->registerResponseView(); |
95
|
|
|
$jsonHelpers->registerErrorHandlers(); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
return $next($request, $response); |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|