|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
|
|
4
|
|
|
namespace Eccube\Twig\Extension; |
|
5
|
|
|
|
|
6
|
|
|
|
|
7
|
|
|
use Symfony\Bridge\Twig\Extension\RoutingExtension; |
|
8
|
|
|
use Symfony\Component\DependencyInjection\ParameterBag\ContainerBagInterface; |
|
9
|
|
|
use Symfony\Component\Routing\Exception\RouteNotFoundException; |
|
10
|
|
|
use Twig\Extension\AbstractExtension; |
|
11
|
|
|
use Twig\Node\Node; |
|
12
|
|
|
use Twig\TwigFunction; |
|
13
|
|
|
|
|
14
|
|
|
class IgnoreRoutingNotFoundExtension extends AbstractExtension |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* @var RoutingExtension |
|
18
|
|
|
*/ |
|
19
|
|
|
private $routingExtension; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @var ContainerBagInterface |
|
23
|
|
|
*/ |
|
24
|
|
|
private $containerBag; |
|
25
|
|
|
|
|
26
|
|
|
public function __construct(RoutingExtension $extension, ContainerBagInterface $containerBag) |
|
27
|
|
|
{ |
|
28
|
|
|
$this->routingExtension = $extension; |
|
29
|
|
|
$this->containerBag = $containerBag; |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
public function getFunctions(): array |
|
33
|
|
|
{ |
|
34
|
|
|
return [ |
|
35
|
|
|
new TwigFunction('url', [$this, 'getUrl'], ['is_safe_callback' => [$this, 'isUrlGenerationSafe']]), |
|
36
|
|
|
new TwigFunction('path', [$this, 'getPath'], ['is_safe_callback' => [$this, 'isUrlGenerationSafe']]), |
|
37
|
|
|
]; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
View Code Duplication |
public function getPath(string $name, array $parameters = [], bool $relative = false): string |
|
|
|
|
|
|
41
|
|
|
{ |
|
42
|
|
|
if ($this->containerBag->get('kernel.debug')) { |
|
43
|
|
|
return $this->routingExtension->getPath($name, $parameters, $relative); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
try { |
|
47
|
|
|
return $this->routingExtension->getPath($name, $parameters, $relative); |
|
48
|
|
|
} catch (RouteNotFoundException $e) { |
|
49
|
|
|
// FIXME log関数でエラーになるので, 修正されたらコメントアウトをはずす |
|
50
|
|
|
// log_warning($e->getMessage(), ['exception' => $e]); |
|
51
|
|
|
|
|
52
|
|
|
return $this->routingExtension->getPath('homepage') . '404?bind=' . $name; |
|
53
|
|
|
} |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
View Code Duplication |
public function getUrl(string $name, array $parameters = [], bool $schemeRelative = false): string |
|
|
|
|
|
|
57
|
|
|
{ |
|
58
|
|
|
if ($this->containerBag->get('kernel.debug')) { |
|
59
|
|
|
return $this->routingExtension->getUrl($name, $parameters, $schemeRelative); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
try { |
|
63
|
|
|
return $this->routingExtension->getUrl($name, $parameters, $schemeRelative); |
|
64
|
|
|
} catch (RouteNotFoundException $e) { |
|
65
|
|
|
// FIXME log関数でエラーになるので, 修正されたらコメントアウトをはずす |
|
66
|
|
|
// log_warning($e->getMessage(), ['exception' => $e]); |
|
67
|
|
|
|
|
68
|
|
|
return $this->routingExtension->getUrl('homepage', $parameters, $schemeRelative) . '404?bind=' . $name; |
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
public function isUrlGenerationSafe(Node $argsNode): array |
|
73
|
|
|
{ |
|
74
|
|
|
return $this->routingExtension->isUrlGenerationSafe($argsNode); |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.