1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the takeit/AmpHtmlBundle package. |
5
|
|
|
* |
6
|
|
|
* (c) Rafał Muszyński <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Takeit\Bundle\AmpHtmlBundle\Routing\Loader; |
13
|
|
|
|
14
|
|
|
use Symfony\Component\Config\Loader\Loader; |
15
|
|
|
use Symfony\Component\Routing\Route; |
16
|
|
|
use Symfony\Component\Routing\RouteCollection; |
17
|
|
|
use Takeit\Bundle\AmpHtmlBundle\Checker\AmpSupportCheckerInterface; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* AMP HTML Route Loader. |
21
|
|
|
* |
22
|
|
|
* @author Rafał Muszyński <[email protected]> |
23
|
|
|
*/ |
24
|
|
|
class AmpLoader extends Loader |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* @var bool |
28
|
|
|
*/ |
29
|
|
|
private $loaded = false; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var AmpSupportCheckerInterface |
33
|
|
|
*/ |
34
|
|
|
private $checker; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var array |
38
|
|
|
*/ |
39
|
|
|
private $parameters; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var string |
43
|
|
|
*/ |
44
|
|
|
private $controller; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @param AmpSupportCheckerInterface $checker |
48
|
|
|
* @param array $parameters |
49
|
|
|
* @param string $controller |
50
|
|
|
*/ |
51
|
|
|
public function __construct(AmpSupportCheckerInterface $checker, array $parameters, $controller) |
52
|
|
|
{ |
53
|
|
|
$this->checker = $checker; |
54
|
|
|
$this->parameters = $parameters; |
55
|
|
|
$this->controller = $controller; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* {@inheritdoc} |
60
|
|
|
*/ |
61
|
|
|
public function load($resource, $type = null) |
62
|
|
|
{ |
63
|
|
|
if (true === $this->loaded) { |
64
|
|
|
throw new \RuntimeException('Do not add the "amp" loader twice'); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
$routes = new RouteCollection(); |
68
|
|
|
|
69
|
|
|
if (!$this->checker->isEnabled()) { |
70
|
|
|
return $routes; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
$path = sprintf('%s/{%s}', $this->parameters['prefix'], $this->parameters['parameter']); |
74
|
|
|
if (isset($this->parameters['pattern']) && null !== $this->parameters['pattern']) { |
75
|
|
|
$path = sprintf( |
76
|
|
|
'/%s/%s/{%s}', |
77
|
|
|
ltrim($this->parameters['prefix'], '/'), |
78
|
|
|
$this->parameters['pattern'], |
79
|
|
|
$this->parameters['parameter'] |
80
|
|
|
); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
$defaults = array( |
84
|
|
|
'_controller' => $this->controller, |
85
|
|
|
'_amp_route' => true, |
86
|
|
|
); |
87
|
|
|
|
88
|
|
|
$requirements = array( |
89
|
|
|
$this->parameters['parameter'] => $this->parameters['parameterRegex'], |
90
|
|
|
); |
91
|
|
|
|
92
|
|
|
$routes->add('takeit_amp_html_view', new Route($path, $defaults, $requirements)); |
93
|
|
|
$this->loaded = true; |
94
|
|
|
|
95
|
|
|
return $routes; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* {@inheritdoc} |
100
|
|
|
*/ |
101
|
|
|
public function supports($resource, $type = null) |
102
|
|
|
{ |
103
|
|
|
return is_string($resource) && 'amp' === $type; |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|