BasicTemplateResolver   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 65
rs 10
c 0
b 0
f 0
wmc 6
lcom 1
cbo 1

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 2
A resolve() 0 4 1
A add_path() 0 14 2
A get_paths() 0 4 1
1
<?php
2
3
/*
4
 * This file is part of the ICanBoogie package.
5
 *
6
 * (c) Olivier Laviale <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace ICanBoogie\Render;
13
14
/**
15
 * Resolves templates pathname.
16
 */
17
class BasicTemplateResolver implements TemplateResolver
18
{
19
	use TemplateResolverTrait;
20
21
	/**
22
	 * An array of key/value pairs, where _key_ if a pathname and _value_ its weight.
23
	 *
24
	 * @var array
25
	 */
26
	protected $paths = [];
27
28
	/**
29
	 * @param array $paths
30
	 */
31
	public function __construct(array $paths = [])
32
	{
33
		foreach ($paths as $path)
34
		{
35
			$this->add_path($path);
36
		}
37
	}
38
39
	/**
40
	 * @inheritdoc
41
	 */
42
	public function resolve($name, array $extensions, &$tried = [])
43
	{
44
		return $this->resolve_path($this->resolve_tries($this->get_paths(), $name, $extensions), $tried);
45
	}
46
47
	/**
48
	 * Adds a path to search templates in.
49
	 *
50
	 * Note: The path is discarded if it cannot be resolved with `realpath()`.
51
	 *
52
	 * @param string $path
53
	 * @param int $weight
54
	 *
55
	 * @return string|false The real path, or `false` if the path was not added.
56
	 */
57
	public function add_path($path, $weight = 0)
58
	{
59
		$path = realpath($path);
60
61
		if (!$path)
62
		{
63
			return false;
64
		}
65
66
		$path = $path . DIRECTORY_SEPARATOR;
67
		$this->paths[$path] = $weight;
68
69
		return $path;
70
	}
71
72
	/**
73
	 * Returns the paths used to search templates.
74
	 *
75
	 * @return array
76
	 */
77
	public function get_paths()
78
	{
79
		return array_keys(array_reverse($this->paths));
80
	}
81
}
82