Passed
Pull Request — development (#3452)
by Emanuele
06:23
created

Directories   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 136
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 28
dl 0
loc 136
rs 10
c 1
b 0
f 0
wmc 15

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getDirectories() 0 3 1
A hasDirectories() 0 3 1
B fileInclude() 0 37 7
A addDirectory() 0 5 1
A reloadDirectories() 0 19 4
A __construct() 0 3 1
1
<?php
2
3
/**
4
 * This class has the only scope to handle themes directories to
5
 * tell Templates and loadLanguageFiles where to go check for files.
6
 *
7
 * @package   ElkArte Forum
8
 * @copyright ElkArte Forum contributors
9
 * @license   BSD http://opensource.org/licenses/BSD-3-Clause (see accompanying LICENSE.txt file)
10
 *
11
 * @version 2.0 dev
12
 *
13
 */
14
15
namespace ElkArte\Themes;
16
17
class Directories
18
{
19
	/**
20
	 * Template directory's that we will be searching for the sheets
21
	 *
22
	 * @var array
23
	 */
24
	protected $dirs = [];
25
26
	/**
27
	 * Holds the file that are in the include list
28
	 *
29
	 * @var array
30
	 */
31
	protected $templates = [];
32
33
	/**
34
	 *
35
	 * @param array $settings
36
	 */
37
	public function __construct(array $settings)
38
	{
39
		$this->reloadDirectories($settings);
40
	}
41
42
	/**
43
	 * Add a template directory to the search stack
44
	 *
45
	 * @param string $dir
46
	 *
47
	 * @return $this
48
	 */
49
	public function addDirectory($dir)
50
	{
51
		$this->dirs[] = (string) $dir;
52
53
		return $this;
54
	}
55
56
	/**
57
	 * Returns if theme directory's have been loaded
58
	 *
59
	 * @return bool
60
	 */
61
	public function hasDirectories()
62
	{
63
		return !empty($this->dirs);
64
	}
65
66
	/**
67
	 * Returns the loaded directories
68
	 *
69
	 * @return string[]
70
	 */
71
	public function getDirectories()
72
	{
73
		return $this->dirs;
74
	}
75
76
	/**
77
	 * Reloads the directory stack/queue to ensure they are searched in the proper order
78
	 *
79
	 * @param array $settings
80
	 */
81
	public function reloadDirectories(array $settings)
82
	{
83
		$this->dirs = [];
84
85
		if (!empty($settings['theme_dir']))
86
		{
87
			$this->addDirectory($settings['theme_dir']);
88
		}
89
90
		// Based on theme (if there is one).
91
		if (!empty($settings['base_theme_dir']))
92
		{
93
			$this->addDirectory($settings['base_theme_dir']);
94
		}
95
96
		// Lastly the default theme.
97
		if ($settings['theme_dir'] !== $settings['default_theme_dir'])
98
		{
99
			$this->addDirectory($settings['default_theme_dir']);
100
		}
101
	}
102
103
	/**
104
	 * Load the template/language file using eval or require? (with eval we can show an
105
	 * error message!)
106
	 *
107
	 * What it does:
108
	 * - Loads the template or language file specified by filename.
109
	 * - Uses eval unless disableTemplateEval is enabled.
110
	 * - Outputs a parse error if the file did not exist or contained errors.
111
	 * - Attempts to detect the error and line, and show detailed information.
112
	 *
113
	 * @param string $filename
114
	 * @param bool $once = false, if true only includes the file once (like include_once)
115
	 */
116
	public function fileInclude($filename, $once = false)
117
	{
118
		/*
119
		 * I know this looks weird but this is used to include $txt files.
120
		 * If the parent doesn't declare them global, the scope will be
121
		 * local to this function. IOW, don't remove this line!
122
		 */
123
		global $txt;
124
125
		// Don't include the file more than once, if $once is true.
126
		if ($once && in_array($filename, $this->templates))
127
		{
128
			return;
129
		}
130
		// Add this file to the include list, whether $once is true or not.
131
		else
132
		{
133
			$this->templates[] = $filename;
134
		}
135
136
		// Load it if we find it
137
		$file_found = file_exists($filename);
138
139
		try
140
		{
141
			if ($once && $file_found)
142
			{
143
				require_once($filename);
144
			}
145
			elseif ($file_found)
146
			{
147
				require($filename);
148
			}
149
		}
150
		catch (Error $e)
0 ignored issues
show
Bug introduced by
The type ElkArte\Themes\Error was not found. Did you mean Error? If so, make sure to prefix the type with \.
Loading history...
151
		{
152
			$this->templateNotFound($e);
0 ignored issues
show
Bug introduced by
The method templateNotFound() does not exist on ElkArte\Themes\Directories. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

152
			$this->/** @scrutinizer ignore-call */ 
153
          templateNotFound($e);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
153
		}
154
	}
155
}