|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Vectorface\SnappyRouter\Handler; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* A handler for invoking scripts directly. |
|
7
|
|
|
* @copyright Copyright (c) 2014, VectorFace, Inc. |
|
8
|
|
|
* @author Dan Bruce <[email protected]> |
|
9
|
|
|
*/ |
|
10
|
|
|
class DirectScriptHandler extends AbstractRequestHandler |
|
11
|
|
|
{ |
|
12
|
|
|
/** Options key for the path mapping array */ |
|
13
|
|
|
const KEY_PATH_MAP = 'pathMap'; |
|
14
|
|
|
|
|
15
|
|
|
private $scriptPath; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Returns true if the handler determines it should handle this request and false otherwise. |
|
19
|
|
|
* @param string $path The URL path for the request. |
|
20
|
|
|
* @param array $query The query parameters. |
|
21
|
|
|
* @param array $post The post data. |
|
22
|
|
|
* @param string $verb The HTTP verb used in the request. |
|
23
|
|
|
* @return boolean Returns true if this handler will handle the request and false otherwise. |
|
24
|
|
|
*/ |
|
25
|
2 |
|
public function isAppropriate($path, $query, $post, $verb) |
|
26
|
|
|
{ |
|
27
|
2 |
|
$options = $this->getOptions(); |
|
28
|
2 |
|
$pathMaps = array(); |
|
29
|
2 |
|
if (isset($options[self::KEY_PATH_MAP])) { |
|
30
|
2 |
|
$pathMaps = (array)$options[self::KEY_PATH_MAP]; |
|
31
|
|
|
} |
|
32
|
2 |
|
foreach ($pathMaps as $pathPrefix => $folder) { |
|
33
|
2 |
|
if (false !== ($pos = strpos($path, $pathPrefix))) { |
|
34
|
2 |
|
$scriptPath = $folder.DIRECTORY_SEPARATOR.substr($path, $pos+strlen($pathPrefix)); |
|
35
|
2 |
|
if (file_exists($scriptPath) && is_readable($scriptPath)) { |
|
36
|
2 |
|
$this->scriptPath = realpath($scriptPath); |
|
37
|
2 |
|
return true; |
|
38
|
|
|
} |
|
39
|
|
|
} |
|
40
|
|
|
} |
|
41
|
1 |
|
return false; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Returns a request object extracted from the request details (path, query, etc). The method |
|
46
|
|
|
* isAppropriate() must have returned true, otherwise this method should return null. |
|
47
|
|
|
* @return \Vectorface\SnappyRouter\Request\HttpRequest|null Returns a |
|
48
|
|
|
* Request object or null if this handler is not appropriate. |
|
49
|
|
|
*/ |
|
50
|
1 |
|
public function getRequest() |
|
51
|
|
|
{ |
|
52
|
1 |
|
return null; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Performs the actual routing. |
|
57
|
|
|
* @return mixed Returns the result of the route. |
|
58
|
|
|
*/ |
|
59
|
1 |
|
public function performRoute() |
|
60
|
|
|
{ |
|
61
|
1 |
|
$buffer = ob_start(); |
|
62
|
1 |
|
require $this->scriptPath; |
|
63
|
1 |
|
return ob_get_clean(); |
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
|