|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
* This file is part of the Respect\Rest package. |
|
4
|
|
|
* |
|
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
6
|
|
|
* file that was distributed with this source code. |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace Respect\Rest\Routines; |
|
10
|
|
|
|
|
11
|
|
|
use SplObjectStorage; |
|
12
|
|
|
use Respect\Rest\Request; |
|
13
|
|
|
|
|
14
|
|
|
/** Handles User Agent filters */ |
|
15
|
|
|
class UserAgent extends AbstractCallbackMediator implements ProxyableThrough, Unique |
|
16
|
|
|
{ |
|
17
|
|
|
const ACCEPT_HEADER = 'HTTP_USER_AGENT'; |
|
18
|
|
|
private $negotiated = false; |
|
19
|
|
|
|
|
20
|
4 |
|
protected function identifyRequested(Request $request, $params) |
|
|
|
|
|
|
21
|
|
|
{ |
|
22
|
4 |
|
return array($_SERVER[self::ACCEPT_HEADER]); |
|
23
|
|
|
} |
|
24
|
4 |
|
protected function considerProvisions($requested) |
|
25
|
|
|
{ |
|
26
|
4 |
|
return $this->getKeys(); |
|
27
|
|
|
} |
|
28
|
3 |
|
protected function notifyApproved($requested, $provided, Request $request, $params) |
|
29
|
|
|
{ |
|
30
|
3 |
|
$this->negotiated = new SplObjectStorage(); |
|
|
|
|
|
|
31
|
3 |
|
$this->negotiated[$request] = $this->getCallback($provided); |
|
32
|
3 |
|
} |
|
33
|
1 |
|
protected function notifyDeclined($requested, $provided, Request $request, $params) |
|
34
|
|
|
{ |
|
35
|
1 |
|
$this->negotiated = false; |
|
36
|
1 |
|
} |
|
37
|
|
|
|
|
38
|
5 |
|
protected function authorize($requested, $provided) |
|
39
|
|
|
{ |
|
40
|
5 |
|
if ($provided === '*' || preg_match("#$provided#", $requested)) { |
|
41
|
4 |
|
return true; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
3 |
|
return false; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
4 |
|
public function through(Request $request, $params) |
|
48
|
|
|
{ |
|
49
|
4 |
|
if (false !== $this->negotiated) { |
|
50
|
3 |
|
return $this->negotiated[$request]; |
|
51
|
|
|
} |
|
52
|
1 |
|
} |
|
53
|
|
|
} |
|
54
|
|
|
|
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: