1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Vperyod\AcceptHandler |
4
|
|
|
* |
5
|
|
|
* PHP version 5 |
6
|
|
|
* |
7
|
|
|
* Copyright (C) 2016 Jake Johns |
8
|
|
|
* |
9
|
|
|
* This software may be modified and distributed under the terms |
10
|
|
|
* of the MIT license. See the LICENSE file for details. |
11
|
|
|
* |
12
|
|
|
* @category Middleware |
13
|
|
|
* @package Vperyod\AcceptHandler |
14
|
|
|
* @author Jake Johns <[email protected]> |
15
|
|
|
* @copyright 2016 Jake Johns |
16
|
|
|
* @license http://jnj.mit-license.org/2016 MIT License |
17
|
|
|
* @link https://github.com/vperyod/vperyod.accept-handler |
18
|
|
|
*/ |
19
|
|
|
|
20
|
|
|
namespace Vperyod\AcceptHandler; |
21
|
|
|
|
22
|
|
|
use Psr\Http\Message\ResponseInterface as Response; |
23
|
|
|
use Psr\Http\Message\ServerRequestInterface as Request; |
24
|
|
|
|
25
|
|
|
use Aura\Accept\AcceptFactory; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* AcceptHandler |
29
|
|
|
* |
30
|
|
|
* @category Middleware |
31
|
|
|
* @package Vperyod\AcceptHandler |
32
|
|
|
* @author Jake Johns <[email protected]> |
33
|
|
|
* @license http://jnj.mit-license.org/2016 MIT License |
34
|
|
|
* @link https://github.com/vperyod/vperyod.accept-handler |
35
|
|
|
*/ |
36
|
|
|
class AcceptHandler |
37
|
|
|
{ |
38
|
|
|
use AcceptRequestAwareTrait; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Accept Factory Factory |
42
|
|
|
* |
43
|
|
|
* @var callable |
44
|
|
|
* |
45
|
|
|
* @access protected |
46
|
|
|
*/ |
47
|
|
|
protected $acceptFactoryFactory; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Media types |
51
|
|
|
* A map of file .extensions to media types |
52
|
|
|
* |
53
|
|
|
* @var array |
54
|
|
|
* |
55
|
|
|
* @access protected |
56
|
|
|
*/ |
57
|
|
|
protected $mediaTypes = []; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Create an AcceptHandler |
61
|
|
|
* |
62
|
|
|
* @param callable $acceptFactoryFactory factory to create an AcceptFactory |
63
|
|
|
* |
64
|
|
|
* @access public |
65
|
|
|
*/ |
66
|
2 |
|
public function __construct(callable $acceptFactoryFactory = null) |
67
|
|
|
{ |
68
|
2 |
|
$this->acceptFactoryFactory = $acceptFactoryFactory |
69
|
1 |
|
?: [$this, 'newAcceptFactory']; |
70
|
2 |
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Adds Accept object to request |
74
|
|
|
* |
75
|
|
|
* @param Request $request PSR7 HTTP Request |
76
|
|
|
* @param Response $response PSR7 HTTP Response |
77
|
|
|
* @param callable $next Next callable middleware |
78
|
|
|
* |
79
|
|
|
* @return Response |
80
|
|
|
* |
81
|
|
|
* @access public |
82
|
|
|
*/ |
83
|
2 |
|
public function __invoke(Request $request, Response $response, callable $next) |
84
|
|
|
{ |
85
|
2 |
|
$request = $request->withAttribute( |
86
|
2 |
|
$this->acceptAttribute, |
87
|
2 |
|
$this->newAccept($request) |
88
|
|
|
); |
89
|
2 |
|
return $next($request, $response); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Set media types |
94
|
|
|
* |
95
|
|
|
* @param array $types A map of file .extensions to media types |
96
|
|
|
* |
97
|
|
|
* @return $this |
98
|
|
|
* |
99
|
|
|
* @access public |
100
|
|
|
*/ |
101
|
1 |
|
public function setMediaTypes(array $types) |
102
|
|
|
{ |
103
|
1 |
|
$this->mediaTypes = $types; |
104
|
1 |
|
return $this; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Create a new AcceptFactory |
109
|
|
|
* |
110
|
|
|
* @param array $server representing $_SERVER |
111
|
|
|
* @param array $types Media Types array |
112
|
|
|
* |
113
|
|
|
* @return AcceptFactory |
114
|
|
|
* |
115
|
|
|
* @access protected |
116
|
|
|
*/ |
117
|
1 |
|
protected function newAcceptFactory(array $server, array $types) |
118
|
|
|
{ |
119
|
1 |
|
return new AcceptFactory($server, $types); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Create a new Accept |
124
|
|
|
* |
125
|
|
|
* @param Request $request PSR7 Request |
126
|
|
|
* |
127
|
|
|
* @return Accept |
128
|
|
|
* |
129
|
|
|
* @access protected |
130
|
|
|
*/ |
131
|
2 |
|
protected function newAccept(Request $request) |
132
|
|
|
{ |
133
|
2 |
|
$factoryFactory = $this->acceptFactoryFactory; |
134
|
2 |
|
return $factoryFactory($request->getServerParams(), $this->mediaTypes) |
135
|
2 |
|
->newInstance(); |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|