-
Notifications
You must be signed in to change notification settings - Fork 0
/
ProjectRouter.php
134 lines (114 loc) · 3.62 KB
/
ProjectRouter.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
<?php
namespace Router;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Router;
use Symfony\Bundle\FrameworkBundle\Routing\DelegatingLoader;
use Symfony\Component\Routing\Loader\YamlFileLoader;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\Routing\Exception\ResourceNotFoundException;
use Symfony\Component\Routing\RequestContext;
/**
*
*/
class ProjectRouter
{
/**
* @var string
*/
protected $routingDir;
/**
* @var mixed
*/
protected $resource;
/**
* @var boolean
*/
protected $debug;
/**
* @var Router
*/
protected $router;
/**
* @var string
*/
protected $cacheDir;
/**
* @param string $routingDir The directory to look for the routing resource
* @param mixed $resource The routing resource to source from
* @param boolean $debug Whether to load the router in debug mode
*/
public function __construct($routingDir, $resource, $debug = false, $cacheDir)
{
$this->routingDir = $routingDir;
$this->resource = $resource;
$this->debug = $debug;
$this->cacheDir = $cacheDir;
}
/**
* @return Router
*/
public function getRouter()
{
if ($this->router === null) {
$loader = new YamlFileLoader(new FileLocator($this->routingDir));
$cacheDir = $this->cacheDir.'/'.($this->debug ? 'debug' : 'prod');
if (!file_exists($cacheDir)) {
if (!file_exists(dirname($cacheDir))) {
throw new \Exception(sprintf('Cache directory "%s" does not exist.', dirname($cacheDir)));
}
if (!is_writable(dirname($cacheDir))) {
throw new \Exception(sprintf('Cache directory "%s" is not writable.', dirname($cacheDir)));
}
mkdir($cacheDir, 0777);
}
$this->router = new Router($loader, $this->resource, array(
'cache_dir' => $cacheDir,
'debug' => $this->debug,
));
}
return $this->router;
}
/**
* Manually set the router
*
* @param string Router $router The router to use.
*/
public function setRouter(Router $router)
{
$this->router = $router;
}
/**
* Returns the application that should be used for the current request.
*
* This matches against the routing and looks for an _app parameter.
*
* @param Request $request The current request object to match on
* @param string $default The default to return if no application is matched
* @return string
*/
public function matchApplication(Request $request, $default = null)
{
// allows a ?app_force=XXX to be added to any url
if ($this->debug && $app = $request->get('app_force')) {
return $app;
}
// set the context information on the router
$context = new RequestContext(
$request->getBaseUrl(),
$request->getMethod(),
$request->getHost(),
$request->getScheme()
);
$this->getRouter()->setContext($context);
try {
$parameters = $this->getRouter()->match($request->getPathInfo());
if (!isset($parameters['_app'])) {
$route = $parameters['_route'];
throw new \InvalidArgumentException(sprintf('No "_app" parameter specified for route "%s"', $route));
}
return $parameters['_app'];
} catch (ResourceNotFoundException $e) {
return $default;
}
}
}