Completed
Push — master ( 166e62...0b3b0f )
by Alexis
01:58
created

AssetExtension::getName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace App\TwigExtension;
4
5
use Psr\Http\Message\ServerRequestInterface;
6
use Twig\Extension\AbstractExtension;
7
use Twig\TwigFunction;
8
9
class AssetExtension extends AbstractExtension
10
{
11
    /**
12
     * @var ServerRequestInterface
13
     */
14
    protected $request;
15
16
    /**
17
     * @var string
18
     */
19
    protected $basePath;
20
21
    /**
22
     * Constructor.
23
     *
24
     * @param ServerRequestInterface $request
25
     * @param string                 $basePath
26
     */
27
    public function __construct(ServerRequestInterface $request, $basePath = '')
28
    {
29
        $this->request = $request;
30
        $this->basePath = $basePath;
31
    }
32
33
    /**
34
     * {@inheritdoc}
35
     */
36
    public function getFunctions()
37
    {
38
        return [
39
            new TwigFunction('asset', [$this, 'asset'])
40
        ];
41
    }
42
43
    /**
44
     * Gets the path to the asset.
45
     *
46
     * @param string $path
47
     *
48
     * @return string
49
     */
50
    public function asset($path)
51
    {
52
        return $this->request->getUri()->getBaseUrl() . '/' . $this->basePath . $path;
53
    }
54
}
55