/
RouteMock.php
89 lines (76 loc) · 1.73 KB
/
RouteMock.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
<?php
/**
* Class Mock
*
* @author: Jiří Šifalda <sifalda.jiri@gmail.com>
* @date: 08.09.13
*/
namespace Flame\Modules\Application\Routers;
use Nette\Application\IRouter;
use Nette\Application\Request;
use Nette\InvalidStateException;
use Nette\Object;
use Nette\Reflection\ClassType;
use Nette;
class RouteMock extends Object implements IRouteMock, IRouter
{
/** @var string */
public $class;
/** @var array */
public $args;
/** @var IRouter */
private $router;
/**
* @param string $class
* @param array $args
*/
function __construct($class, array $args = array())
{
$this->args = $args;
$this->class = (string) $class;
}
/**
* @return IRouter
* @throws \Nette\InvalidStateException
*/
public function getRouter()
{
if($this->router === null) {
$this->router = $this->createInstance();
}
if($this->router instanceof IRouter) {
return $this->router;
}
throw new InvalidStateException('Class "' . $this->class . '" is not instance of Nette\Application\IRouter');
}
/**
* @return object
* @throws \Nette\InvalidStateException
*/
protected function createInstance()
{
if(!class_exists($this->class)) {
throw new InvalidStateException('Route class "' . $this->class . '" not found!');
}
$route = new ClassType($this->class);
return $route->newInstanceArgs($this->args);
}
/**
* Maps HTTP request to a Request object.
*
* @return Request|NULL
*/
function match(Nette\Http\IRequest $httpRequest)
{
return $this->getRouter()->match($httpRequest);
}
/**
* Constructs absolute URL from Request object.
*
* @return string|NULL
*/
function constructUrl(Request $appRequest, Nette\Http\Url $refUrl)
{
return $this->getRouter()->constructUrl($appRequest, $refUrl);
}
}