Completed
Push — master ( af891e...5c98d3 )
by Sam
11:04
created

TemplateLoader::addSet()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 3
rs 10
1
<?php
2
3
namespace SilverStripe\View;
4
5
/**
6
 * Handles finding templates from a stack of template manifest objects.
7
 *
8
 * @package framework
9
 * @subpackage view
10
 */
11
class TemplateLoader {
12
13
	/**
14
	 * @var TemplateLoader
15
	 */
16
	private static $instance;
17
18
	protected $base;
19
20
	protected $sets = [];
21
22
	public static function instance() {
23
		return self::$instance ? self::$instance : self::$instance = new self();
24
	}
25
26
	public static function set_instance(TemplateLoader $instance) {
27
		self::$instance = $instance;
28
	}
29
30
	public function __construct($base = null) {
31
		$this->base = $base ? $base : BASE_PATH;
32
	}
33
34
	public function addSet($set, $manifest) {
35
		$this->sets[$set] = $manifest;
36
	}
37
38
	public function getPath($identifier) {
39
		$slashPos = strpos($identifier, '/');
40
41
		// If identifier starts with "/", it's a path from root
42
		if ($slashPos === 0) {
43
			return substr($identifier, 1);
44
		}
45
		// Otherwise if there is a "/", identifier is a vendor'ed module
46
		elseif ($slashPos !== false) {
47
			$parts = explode(':', $identifier, 2);
48
49
			list($vendor, $module) = explode('/', $parts[0], 2);
0 ignored issues
show
Unused Code introduced by
The assignment to $vendor is unused. Consider omitting it like so list($first,,$third).

This checks looks for assignemnts to variables using the list(...) function, where not all assigned variables are subsequently used.

Consider the following code example.

<?php

function returnThreeValues() {
    return array('a', 'b', 'c');
}

list($a, $b, $c) = returnThreeValues();

print $a . " - " . $c;

Only the variables $a and $c are used. There was no need to assign $b.

Instead, the list call could have been.

list($a,, $c) = returnThreeValues();
Loading history...
50
			$theme = count($parts) > 1 ? $parts[1] : '';
51
52
			$path = $module . ($theme ? '/themes/'.$theme : '');
53
54
			// Right now we require $module to be a silverstripe module (in root) or theme (in themes dir)
55
			// If both exist, we prefer theme
56
			if (is_dir(THEMES_PATH . '/' .$path)) {
57
				return THEMES_DIR . '/' . $path;
58
			}
59
			else {
60
				return $path;
61
			}
62
		}
63
		// Otherwise it's a (deprecated) old-style "theme" identifier
64
		else {
65
			return THEMES_DIR.'/'.$identifier;
66
		}
67
	}
68
69
	/**
70
	 * Attempts to find possible candidate templates from a set of template
71
	 * names from modules, current theme directory and finally the application
72
	 * folder.
73
	 *
74
	 * The template names can be passed in as plain strings, or be in the
75
	 * format "type/name", where type is the type of template to search for
76
	 * (e.g. Includes, Layout).
77
	 *
78
	 * @param  string|array $templates
0 ignored issues
show
Documentation introduced by
There is no parameter named $templates. Did you maybe mean $template?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.

Consider the following example. The parameter $ireland is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $ireland
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was changed, but the annotation was not.

Loading history...
79
	 * @param  string $theme
0 ignored issues
show
Documentation introduced by
There is no parameter named $theme. Did you maybe mean $themes?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.

Consider the following example. The parameter $ireland is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $ireland
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was changed, but the annotation was not.

Loading history...
80
	 *
81
	 * @return array
82
	 */
83
	public function findTemplate($template, $themes = []) {
84
85
		if(is_array($template)) {
86
			$type = array_key_exists('type', $template) ? $template['type'] : '';
87
			$templateList = array_key_exists('templates', $template) ? $template['templates'] : $template;
88
		}
89
		else {
90
			$type = '';
91
			$templateList = array($template);
92
		}
93
94
		if(count($templateList) == 1 && substr($templateList[0], -3) == '.ss') {
95
			return $templateList[0];
96
		}
97
98
		foreach($templateList as $i => $template) {
99
			$template = str_replace('\\', '/', $template);
100
			$parts = explode('/', $template);
101
102
			$tail = array_pop($parts);
103
			$head = implode('/', $parts);
104
105
			foreach($themes as $themename) {
106
				$subthemes = isset($this->sets[$themename]) ? $this->sets[$themename]->getThemes() : [$themename];
107
108
				foreach($subthemes as $theme) {
109
					$themePath = $this->base . '/' . $this->getPath($theme);
110
111
					$path = $themePath . '/templates/' . implode('/', array_filter([$head, $type, $tail])) . '.ss';
112
					if (file_exists($path)) return $path;
113
				}
114
			}
115
		}
116
	}
117
118
}
119