Completed
Push — master ( 9ed0b2...146511 )
by Adam
17:10
created

TemplateLocator::getCacheKey()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
/*
3
 * WellCommerce Open-Source E-Commerce Platform
4
 *
5
 * This file is part of the WellCommerce package.
6
 *
7
 * (c) Adam Piotrowski <[email protected]>
8
 *
9
 * For the full copyright and license information,
10
 * please view the LICENSE file that was distributed with this source code.
11
 */
12
13
namespace WellCommerce\Bundle\AppBundle\Service\Theme\Locator;
14
15
use Symfony\Bundle\FrameworkBundle\Templating\Loader\TemplateLocator as BaseTemplateLocator;
16
use Symfony\Component\Config\FileLocatorInterface;
17
use Symfony\Component\Templating\TemplateReferenceInterface;
18
19
/**
20
 * Class TemplateLocator
21
 *
22
 * @author  Adam Piotrowski <[email protected]>
23
 *
24
 * Created on base of the LiipAppBundle <https://github.com/liip/LiipAppBundle>
25
 *
26
 * Special thanks goes to its authors and contributors
27
 */
28
class TemplateLocator extends BaseTemplateLocator
29
{
30
    /**
31
     * @return FileLocatorInterface
32
     */
33
    public function getLocator()
34
    {
35
        return $this->locator;
36
    }
37
    
38
    protected function getCacheKey($template)
39
    {
40
        return $template->getLogicalName();
41
    }
42
    
43
    /**
44
     * Returns a full path for a given file.
45
     *
46
     * @param string|TemplateReferenceInterface $template
47
     * @param null                              $currentPath
48
     * @param bool                              $first
49
     *
50
     * @return string The full path for the file
51
     */
52
    public function locate($template, $currentPath = null, $first = true)
53
    {
54
        if (!$template instanceof TemplateReferenceInterface) {
55
            throw new \InvalidArgumentException("The template must be an instance of TemplateReferenceInterface.");
56
        }
57
        
58
        $key = $this->getCacheKey($template);
59
        
60
        if (!isset($this->cache[$key])) {
61
            try {
62
                $this->cache[$key] = $this->locator->locate($template->getPath(), $currentPath);
63
            } catch (\InvalidArgumentException $e) {
64
                throw new \InvalidArgumentException(sprintf(
65
                    'Unable to find template "%s" in "%s".',
66
                    $template,
67
                    $e->getMessage()
68
                ));
69
            }
70
        }
71
        
72
        return $this->cache[$key];
73
    }
74
}
75