Completed
Branch master (928280)
by Alexis
02:27
created

AssetExtension::asset()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 1
1
<?php
2
3
namespace App\Twig;
4
5
use Psr\Http\Message\ServerRequestInterface as Request;
6
use Twig\Extension\AbstractExtension;
7
use Twig\TwigFunction;
8
9
class AssetExtension extends AbstractExtension
10
{
11
    /**
12
     * @var Request
13
     */
14
    protected $request;
15
16
    /**
17
     * @var string
18
     */
19
    protected $basePath;
20
21
    /**
22
     * Constructor.
23
     *
24
     * @param Request $request
25
     * @param string  $basePath
26
     */
27
    public function __construct(Request $request, $basePath = null)
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
        if (null !== $this->basePath) {
53
            return $this->request->getUri()->getBaseUrl() . '/' . trim($this->basePath, '/') . '/' . $path;
54
        }
55
56
        return $this->request->getUri()->getBaseUrl() . '/' . $path;
57
    }
58
}
59