Completed
Push — master ( 18d09b...7f99bd )
by David
15s
created

ThemeExtension::getResourceUrl()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
4
namespace TheCodingMachine\CMS\Theme\Extensions;
5
6
7
class ThemeExtension extends \Twig_Extension
8
{
9
    /**
10
     * A list of theme URLs (the active one is the last one)
11
     *
12
     * @var string[]
13
     */
14
    private $themeUrls = [];
15
16
    public function pushThemeUrl(?string $themeUrl): void
17
    {
18
        $this->themeUrls[] = rtrim($themeUrl, '/').'/';
19
    }
20
21
    public function popThemeUrl(): ?string
22
    {
23
        return array_pop($this->themeUrls);
24
    }
25
26
    private function getThemeUrl(): ?string
27
    {
28
        return $this->themeUrls[count($this->themeUrls)-1];
29
    }
30
31
32
    /**
33
     * Returns a list of functions to add to the existing list.
34
     *
35
     * @return \Twig_Function[]
36
     */
37
    public function getFunctions()
38
    {
39
        return [
40
            new \Twig_Function('theme', [$this, 'getResourceUrl']),
41
        ];
42
    }
43
44
    public function getResourceUrl(string $resource): string
45
    {
46
        return $this->getThemeUrl().$resource;
47
    }
48
}
49