Completed
Push — master ( 64a4da...21cb6b )
by Thomas
07:39
created

ResourceLocator::append()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 7
nc 4
nop 4
dl 0
loc 12
rs 9.2
c 0
b 0
f 0
1
<?php
2
/**
3
 * @author Bart Visscher <[email protected]>
4
 * @author Joas Schilling <[email protected]>
5
 * @author Jörn Friedrich Dreyer <[email protected]>
6
 * @author Morris Jobke <[email protected]>
7
 * @author Robin McCorkell <[email protected]>
8
 *
9
 * @copyright Copyright (c) 2016, ownCloud GmbH.
10
 * @license AGPL-3.0
11
 *
12
 * This code is free software: you can redistribute it and/or modify
13
 * it under the terms of the GNU Affero General Public License, version 3,
14
 * as published by the Free Software Foundation.
15
 *
16
 * This program is distributed in the hope that it will be useful,
17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19
 * GNU Affero General Public License for more details.
20
 *
21
 * You should have received a copy of the GNU Affero General Public License, version 3,
22
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
23
 *
24
 */
25
26
namespace OC\Template;
27
28
use OC\Theme\Theme;
29
30
abstract class ResourceLocator {
31
	/**
32
	 * @var Theme
33
	 */
34
	protected $theme;
35
36
	protected $mapping;
37
	protected $serverroot;
38
	protected $thirdpartyroot;
39
	protected $webroot;
40
41
	protected $resources = [];
42
43
	/** @var \OCP\ILogger */
44
	protected $logger;
45
46
	/**
47
	 * @param \OCP\ILogger $logger
48
	 * @param Theme $theme
49
	 * @param array $core_map
50
	 * @param array $party_map
51
	 */
52
	public function __construct(\OCP\ILogger $logger, $theme, $core_map, $party_map) {
53
		$this->logger = $logger;
54
		$this->theme = $theme;
55
		$this->mapping = $core_map + $party_map;
56
		$this->serverroot = key($core_map);
57
		$this->thirdpartyroot = key($party_map);
58
		$this->webroot = $this->mapping[$this->serverroot];
59
	}
60
61
	/**
62
	 * @param string $resource
63
	 */
64
	abstract public function doFind($resource);
65
66
	/**
67
	 * @param string $resource
68
	 */
69
	abstract public function doFindTheme($resource);
70
71
	/**
72
	 * Finds the resources and adds them to the list
73
	 *
74
	 * @param array $resources
75
	 */
76
	public function find($resources) {
77 View Code Duplication
		foreach ($resources as $resource) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
78
			try {
79
				$this->doFind($resource);
80
			} catch (ResourceNotFoundException $e) {
81
				$resourceApp = substr($resource, 0, strpos($resource, '/'));
82
				$this->logger->error('Could not find resource file "' . $e->getResourcePath() . '"', ['app' => $resourceApp]);
83
			}
84
		}
85
		if (!empty($this->theme)) {
86 View Code Duplication
			foreach ($resources as $resource) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
87
				try {
88
					$this->doFindTheme($resource);
89
				} catch (ResourceNotFoundException $e) {
90
					$resourceApp = substr($resource, 0, strpos($resource, '/'));
91
					$this->logger->error('Could not find resource file "' . $e->getResourcePath() . '"', ['app' => $resourceApp]);
92
				}
93
			}
94
		}
95
	}
96
97
	/**
98
	 * append the $file resource once if exist at $root
99
	 *
100
	 * @param string $root path to check
101
	 * @param string $file the filename
102
	 * @param string|null $webRoot base for path, default map $root to $webRoot
103
	 * @return bool True if the resource was found, false otherwise
104
	 */
105
	protected function appendOnceIfExist($root, $file, $webRoot = null) {
106
107
		$path = $this->buildPath([$root, $file]);
108
		
109
		if (!isset( $this->resources[$path] ) && is_file($path)) {
110
			$this->append($root, $file, $webRoot, false);
111
			return true;
112
		}
113
		return false;
114
	}
115
116
	/**
117
	 * append the $file resource at $root
118
	 *
119
	 * @param string $root path to check
120
	 * @param string $file the filename
121
	 * @param string|null $webRoot base for path, default map $root to $webRoot
122
	 * @param bool $throw Throw an exception, when the route does not exist
123
	 * @throws ResourceNotFoundException Only thrown when $throw is true and the resource is missing
124
	 */
125
	protected function append($root, $file, $webRoot = null, $throw = true) {
126
		if (!$webRoot) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $webRoot of type string|null is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
127
			$webRoot = $this->mapping[$root];
128
		}
129
		
130
		$path = $this->buildPath([$root, $file]);
131
		$this->resources[$path] = [$root, $webRoot, $file];
132
133
		if ($throw && !is_file($path) ) {
134
			throw new ResourceNotFoundException($file, $webRoot);
135
		}
136
	}
137
	
138
	/**
139
	 * build a path by given parts concatenated with a '/' (DIRECTORY_SEPARATOR)
140
	 *
141
	 * @param string[] $parts path parts to concatenate
142
	 * @return string $parts concatenated
143
	 */
144
	private function buildPath($parts){
145
		return join(DIRECTORY_SEPARATOR, $parts);
146
	}
147
148
	/**
149
	 * Returns the list of all resources that should be loaded
150
	 * @return array
151
	 */
152
	public function getResources() {
153
		return $this->resources;
154
	}
155
}
156