|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Created by PhpStorm. |
|
4
|
|
|
* User: simu |
|
5
|
|
|
* Date: 19.12.14 |
|
6
|
|
|
* Time: 16:43 |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace rootLogin\JSRoutingProvider; |
|
10
|
|
|
|
|
11
|
|
|
use Silex\Application; |
|
12
|
|
|
use Symfony\Component\Routing\Route; |
|
13
|
|
|
use Symfony\Component\Routing\RouteCollection; |
|
14
|
|
|
|
|
15
|
|
|
class JSRoutingProvider { |
|
16
|
|
|
/** |
|
17
|
|
|
* @var Application |
|
18
|
|
|
*/ |
|
19
|
|
|
private $app; |
|
20
|
|
|
|
|
21
|
|
|
public function __construct(Application &$app) |
|
22
|
|
|
{ |
|
23
|
|
|
$this->app = $app; |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
public function getJSwithRoutes() { |
|
27
|
|
|
$res = $this->getJavaScript() . $this->getJSRoutes(); |
|
28
|
|
|
if (!empty($this->app['jsrouting.base_url'])) { |
|
29
|
|
|
$res .= "\nrouter.baseurl = '" . $this->app['jsrouting.base_url'] . "';"; |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
return $res; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
public function getJavaScript() |
|
36
|
|
|
{ |
|
37
|
|
|
return file_get_contents(__DIR__ . "/Resources/js/routing.js"); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
public function getJSRoutes() |
|
41
|
|
|
{ |
|
42
|
|
|
$res = ""; |
|
43
|
|
|
foreach($this->getAllRoutes() as $name => $route) { |
|
44
|
|
|
$route = json_encode($route); |
|
45
|
|
|
$res .= "\nrouter.addRoute('$name', $route);"; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
return $res; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
public function getAllRoutes() |
|
52
|
|
|
{ |
|
53
|
|
|
/** @var RouteCollection $routeCollection */ |
|
54
|
|
|
$routeCollection = $this->app['routes']; |
|
55
|
|
|
|
|
56
|
|
|
$routes = array(); |
|
57
|
|
|
/** @var Route $route */ |
|
58
|
|
|
foreach($routeCollection as $key => $route) { |
|
59
|
|
|
if($route->getOption("expose") || in_array($key, $this->app['jsrouting.exposed_routes'])) { |
|
60
|
|
|
$routes[$key] = array( |
|
61
|
|
|
"host" => $route->getHost(), |
|
62
|
|
|
"path" => $route->getPath(), |
|
63
|
|
|
"schemes" => $route->getSchemes(), |
|
64
|
|
|
"requirements" => $route->getRequirements(), |
|
65
|
|
|
"condition" => $route->getCondition(), |
|
66
|
|
|
); |
|
67
|
|
|
} |
|
68
|
|
|
} |
|
69
|
|
|
return $routes; |
|
70
|
|
|
} |
|
71
|
|
|
} |
|
72
|
|
|
|