AgnosticUrlGenerator::getContext()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 2
1
<?php
2
3
namespace Majora\Framework\Routing;
4
5
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
6
use Symfony\Component\Routing\RequestContext;
7
8
/**
9
 * Base routing proxy which strips debug base urls in order
10
 * to handle externals sources even with custom dev front controllers
11
 */
12
class AgnosticUrlGenerator implements UrlGeneratorInterface
13
{
14
    /**
15
     * @var UrlGeneratorInterface
16
     */
17
    protected $urlGenerator;
18
19
    /**
20
     * @var RequestContext
21
     */
22
    protected $context;
23
24
    /**
25
     * Construct
26
     *
27
     * @param UrlGeneratorInterface $urlGenerator
28
     */
29
    public function __construct(UrlGeneratorInterface $urlGenerator)
30
    {
31
        $this->urlGenerator = $urlGenerator;
32
        $this->setContext($urlGenerator->getContext());
33
    }
34
35
    /**
36
     * @see RequestContextAwareInterface::setContext()
37
     */
38
    public function setContext(RequestContext $context)
39
    {
40
        $this->context = clone $context;
41
        $this->context->setBaseUrl(preg_replace(
42
            '/(\\/[\w]+_dev.php)/',
43
            '',
44
            $context->getBaseUrl()
45
        ));
46
    }
47
48
    /**
49
     * @see RequestContextAwareInterface::getContext()
50
     */
51
    public function getContext()
52
    {
53
        return $this->context;
54
    }
55
56
    /**
57
     * @see UrlGeneratorInterface::generate()
58
     */
59
    public function generate($name, $parameters = array(), $referenceType = UrlGeneratorInterface::ABSOLUTE_PATH)
60
    {
61
        $proxiedContext = $this->urlGenerator->getContext();
62
        $this->setContext($proxiedContext);
63
64
        $this->urlGenerator->setContext($this->getContext());
65
        $url = $this->urlGenerator->generate($name, $parameters, $referenceType);
66
67
        $this->urlGenerator->setContext($proxiedContext);
68
69
        return $url;
70
    }
71
}
72