FilePath::setBaseDir()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 1
1
<?php
2
/* @description     Transformation Style Sheets - Revolutionising PHP templating    *
3
 * @author          Tom Butler [email protected]                                             *
4
 * @copyright       2017 Tom Butler <[email protected]> | https://r.je/                      *
5
 * @license         http://www.opensource.org/licenses/bsd-license.php  BSD License *
6
 * @version         1.2                                                             */
7
namespace Transphporm;
8
class FilePath {
9
	private $paths = ['.'];
10
	private $baseDir;
11
12
	public function addPath($path) {
13
		$this->paths[] = rtrim($path, DIRECTORY_SEPARATOR);
14
	}
15
16
	public function setBaseDir($baseDir) {
17
		$this->baseDir = $baseDir;
18
	}
19
20
	public function getFilePath($filePath) {
21
		if (is_file($filePath)) return $filePath;
22
		else if (is_file($this->baseDir . DIRECTORY_SEPARATOR . $filePath)) return $this->baseDir . DIRECTORY_SEPARATOR . $filePath;
23
		else return $this->loadFromPaths($filePath);
24
	}
25
26
	private function loadFromPaths($filePath) {
27
		foreach ($this->paths as $path) {
28
			if (is_file($path . DIRECTORY_SEPARATOR . $filePath)) return $path . DIRECTORY_SEPARATOR . $filePath;
29
		}
30
31
		throw new \Exception('File ' . $filePath . ' not found in paths (' . implode(', ', $this->paths) . ')');
32
	}
33
}
34