Passed
Push — master ( 9c7d71...8b4d49 )
by Julius
15:19 queued 11s
created

JSResourceLocator::appendScriptIfExist()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 2
eloc 3
c 1
b 1
f 0
nc 2
nop 3
dl 0
loc 5
rs 10
1
<?php
2
/**
3
 * @copyright Copyright (c) 2016, ownCloud, Inc.
4
 *
5
 * @author Bart Visscher <[email protected]>
6
 * @author Christoph Wurst <[email protected]>
7
 * @author Joas Schilling <[email protected]>
8
 * @author Kyle Fazzari <[email protected]>
9
 * @author Morris Jobke <[email protected]>
10
 * @author Roeland Jago Douma <[email protected]>
11
 * @author Thomas Müller <[email protected]>
12
 *
13
 * @license AGPL-3.0
14
 *
15
 * This code is free software: you can redistribute it and/or modify
16
 * it under the terms of the GNU Affero General Public License, version 3,
17
 * as published by the Free Software Foundation.
18
 *
19
 * This program is distributed in the hope that it will be useful,
20
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22
 * GNU Affero General Public License for more details.
23
 *
24
 * You should have received a copy of the GNU Affero General Public License, version 3,
25
 * along with this program. If not, see <http://www.gnu.org/licenses/>
26
 *
27
 */
28
namespace OC\Template;
29
30
use OCP\App\AppPathNotFoundException;
31
use OCP\App\IAppManager;
32
use Psr\Log\LoggerInterface;
33
34
class JSResourceLocator extends ResourceLocator {
35
	protected JSCombiner $jsCombiner;
36
	protected IAppManager $appManager;
37
38
	public function __construct(LoggerInterface $logger, JSCombiner $JSCombiner, IAppManager $appManager) {
39
		parent::__construct($logger);
40
41
		$this->jsCombiner = $JSCombiner;
42
		$this->appManager = $appManager;
43
	}
44
45
	/**
46
	 * @param string $script
47
	 */
48
	public function doFind($script) {
49
		$theme_dir = 'themes/'.$this->theme.'/';
50
51
		// Extracting the appId and the script file name
52
		$app = substr($script, 0, strpos($script, '/'));
53
		$scriptName = basename($script);
54
55
		if (strpos($script, '/l10n/') !== false) {
56
			// For language files we try to load them all, so themes can overwrite
57
			// single l10n strings without having to translate all of them.
58
			$found = 0;
59
			$found += $this->appendScriptIfExist($this->serverroot, 'core/'.$script);
60
			$found += $this->appendScriptIfExist($this->serverroot, $theme_dir.'core/'.$script);
61
			$found += $this->appendScriptIfExist($this->serverroot, $script);
62
			$found += $this->appendScriptIfExist($this->serverroot, $theme_dir.$script);
63
			$found += $this->appendScriptIfExist($this->serverroot, 'apps/'.$script);
64
			$found += $this->appendScriptIfExist($this->serverroot, $theme_dir.'apps/'.$script);
65
66
			if ($found) {
67
				return;
68
			}
69
		} elseif ($this->appendScriptIfExist($this->serverroot, $theme_dir.'apps/'.$script)
70
			|| $this->appendScriptIfExist($this->serverroot, $theme_dir.$script)
71
			|| $this->appendScriptIfExist($this->serverroot, $script)
72
			|| $this->appendScriptIfExist($this->serverroot, $theme_dir."dist/$app-$scriptName")
73
			|| $this->appendScriptIfExist($this->serverroot, "dist/$app-$scriptName")
74
			|| $this->appendScriptIfExist($this->serverroot, 'apps/'.$script)
75
			|| $this->cacheAndAppendCombineJsonIfExist($this->serverroot, $script.'.json')
76
			|| $this->appendScriptIfExist($this->serverroot, $theme_dir.'core/'.$script)
77
			|| $this->appendScriptIfExist($this->serverroot, 'core/'.$script)
78
			|| (strpos($scriptName, '/') === -1 && ($this->appendScriptIfExist($this->serverroot, $theme_dir."dist/core-$scriptName")
79
				|| $this->appendScriptIfExist($this->serverroot, "dist/core-$scriptName")))
80
			|| $this->cacheAndAppendCombineJsonIfExist($this->serverroot, 'core/'.$script.'.json')
81
		) {
82
			return;
83
		}
84
85
		$script = substr($script, strpos($script, '/') + 1);
86
		$app_url = null;
0 ignored issues
show
Unused Code introduced by
The assignment to $app_url is dead and can be removed.
Loading history...
87
88
		try {
89
			$app_url = $this->appManager->getAppWebPath($app);
90
		} catch (AppPathNotFoundException) {
91
			// pass
92
		}
93
94
		try {
95
			$app_path = $this->appManager->getAppPath($app);
96
97
			// Account for the possibility of having symlinks in app path. Only
98
			// do this if $app_path is set, because an empty argument to realpath
99
			// gets turned into cwd.
100
			$app_path = realpath($app_path);
101
102
			// missing translations files will be ignored
103
			if (strpos($script, 'l10n/') === 0) {
104
				$this->appendScriptIfExist($app_path, $script, $app_url);
105
				return;
106
			}
107
108
			if (!$this->cacheAndAppendCombineJsonIfExist($app_path, $script.'.json', $app)) {
109
				$this->appendScriptIfExist($app_path, $script, $app_url);
110
			}
111
		} catch (AppPathNotFoundException) {
112
			$this->logger->error('Could not find resource {resource} to load', [
113
				'resource' => $app . '/' . $script . '.js',
114
				'app' => 'jsresourceloader',
115
			]);
116
		}
117
	}
118
119
	/**
120
	 * @param string $script
121
	 */
122
	public function doFindTheme($script) {
123
	}
124
125
	/**
126
	 * Try to find ES6 script file (`.mjs`) with fallback to plain javascript (`.js`)
127
	 * @see appendIfExist()
128
	 */
129
	protected function appendScriptIfExist(string $root, string $file, string $webRoot = null) {
130
		if (!$this->appendIfExist($root, $file . '.mjs', $webRoot)) {
131
			return $this->appendIfExist($root, $file . '.js', $webRoot);
132
		}
133
		return true;
134
	}
135
136
	protected function cacheAndAppendCombineJsonIfExist($root, $file, $app = 'core') {
137
		if (is_file($root.'/'.$file)) {
138
			if ($this->jsCombiner->process($root, $file, $app)) {
139
				$this->append($this->serverroot, $this->jsCombiner->getCachedJS($app, $file), false, false);
0 ignored issues
show
Bug introduced by
false of type false is incompatible with the type null|string expected by parameter $webRoot of OC\Template\ResourceLocator::append(). ( Ignorable by Annotation )

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

139
				$this->append($this->serverroot, $this->jsCombiner->getCachedJS($app, $file), /** @scrutinizer ignore-type */ false, false);
Loading history...
140
			} else {
141
				// Add all the files from the json
142
				$files = $this->jsCombiner->getContent($root, $file);
143
				$app_url = null;
0 ignored issues
show
Unused Code introduced by
The assignment to $app_url is dead and can be removed.
Loading history...
144
				try {
145
					$app_url = $this->appManager->getAppWebPath($app);
146
				} catch (AppPathNotFoundException) {
147
					// pass
148
				}
149
150
				foreach ($files as $jsFile) {
151
					$this->append($root, $jsFile, $app_url);
152
				}
153
			}
154
			return true;
155
		}
156
157
		return false;
158
	}
159
}
160