Completed
Pull Request — master (#6573)
by Robin
18:10
created
lib/private/Template/ResourceLocator.php 1 patch
Indentation   +169 added lines, -169 removed lines patch added patch discarded remove patch
@@ -27,173 +27,173 @@
 block discarded – undo
27 27
 namespace OC\Template;
28 28
 
29 29
 abstract class ResourceLocator {
30
-	protected $theme;
31
-
32
-	protected $mapping;
33
-	protected $serverroot;
34
-	protected $thirdpartyroot;
35
-	protected $webroot;
36
-
37
-	protected $resources = array();
38
-
39
-	/** @var \OCP\ILogger */
40
-	protected $logger;
41
-
42
-	/**
43
-	 * @param \OCP\ILogger $logger
44
-	 * @param string $theme
45
-	 * @param array $core_map
46
-	 * @param array $party_map
47
-	 */
48
-	public function __construct(\OCP\ILogger $logger, $theme, $core_map, $party_map) {
49
-		$this->logger = $logger;
50
-		$this->theme = $theme;
51
-		$this->mapping = $core_map + $party_map;
52
-		$this->serverroot = key($core_map);
53
-		$this->thirdpartyroot = key($party_map);
54
-		$this->webroot = $this->mapping[$this->serverroot];
55
-	}
56
-
57
-	/**
58
-	 * @param string $resource
59
-	 */
60
-	abstract public function doFind($resource);
61
-
62
-	/**
63
-	 * @param string $resource
64
-	 */
65
-	abstract public function doFindTheme($resource);
66
-
67
-	/**
68
-	 * Finds the resources and adds them to the list
69
-	 *
70
-	 * @param array $resources
71
-	 */
72
-	public function find($resources) {
73
-		foreach ($resources as $resource) {
74
-			try {
75
-				$this->doFind($resource);
76
-			} catch (ResourceNotFoundException $e) {
77
-				$resourceApp = substr($resource, 0, strpos($resource, '/'));
78
-				$this->logger->debug('Could not find resource file "' . $e->getResourcePath() . '"', ['app' => $resourceApp]);
79
-			}
80
-		}
81
-		if (!empty($this->theme)) {
82
-			foreach ($resources as $resource) {
83
-				try {
84
-					$this->doFindTheme($resource);
85
-				} catch (ResourceNotFoundException $e) {
86
-					$resourceApp = substr($resource, 0, strpos($resource, '/'));
87
-					$this->logger->debug('Could not find resource file in theme "' . $e->getResourcePath() . '"', ['app' => $resourceApp]);
88
-				}
89
-			}
90
-		}
91
-	}
92
-
93
-	/**
94
-	 * append the $file resource if exist at $root
95
-	 *
96
-	 * @param string $root path to check
97
-	 * @param string $file the filename
98
-	 * @param string|null $webRoot base for path, default map $root to $webRoot
99
-	 * @return bool True if the resource was found, false otherwise
100
-	 */
101
-	protected function appendIfExist($root, $file, $webRoot = null) {
102
-		if (is_file($root.'/'.$file)) {
103
-			$this->append($root, $file, $webRoot, false);
104
-			return true;
105
-		}
106
-		return false;
107
-	}
108
-
109
-	/**
110
-	 * Attempt to find the webRoot
111
-	 *
112
-	 * traverse the potential web roots upwards in the path
113
-	 *
114
-	 * example:
115
-	 *   - root: /srv/www/apps/myapp
116
-	 *   - available mappings: ['/srv/www']
117
-	 *
118
-	 * First we check if a mapping for /srv/www/apps/myapp is available,
119
-	 * then /srv/www/apps, /srv/www/apps, /srv/www, ... until we find a
120
-	 * valid web root
121
-	 *
122
-	 * @param string $root
123
-	 * @return string|null The web root or null on failure
124
-	 */
125
-	protected function findWebRoot($root) {
126
-		$webRoot = null;
127
-		$tmpRoot = $root;
128
-
129
-		while ($webRoot === null) {
130
-			if (isset($this->mapping[$tmpRoot])) {
131
-				$webRoot = $this->mapping[$tmpRoot];
132
-				break;
133
-			}
134
-
135
-			if ($tmpRoot === '/') {
136
-				break;
137
-			}
138
-
139
-			$tmpRoot = dirname($tmpRoot);
140
-		}
141
-
142
-		if ($webRoot === null) {
143
-			$realpath = realpath($root);
144
-
145
-			if ($realpath && ($realpath !== $root)) {
146
-				return $this->findWebRoot($realpath);
147
-			}
148
-		}
149
-
150
-		return $webRoot;
151
-	}
152
-
153
-	/**
154
-	 * append the $file resource at $root
155
-	 *
156
-	 * @param string $root path to check
157
-	 * @param string $file the filename
158
-	 * @param string|null $webRoot base for path, default map $root to $webRoot
159
-	 * @param bool $throw Throw an exception, when the route does not exist
160
-	 * @throws ResourceNotFoundException Only thrown when $throw is true and the resource is missing
161
-	 */
162
-	protected function append($root, $file, $webRoot = null, $throw = true) {
163
-
164
-		if (!is_string($root)) {
165
-			if ($throw) {
166
-				throw new ResourceNotFoundException($file, $webRoot);
167
-			}
168
-			return;
169
-		}
170
-
171
-		if (!$webRoot) {
172
-			$webRoot = $this->findWebRoot($root);
173
-
174
-			if ($webRoot === null) {
175
-				$webRoot = '';
176
-				$this->logger->error('ResourceLocator can not find a web root (root: {root}, file: {file}, webRoot: {webRoot}, throw: {throw})', [
177
-					'app' => 'lib',
178
-					'root' => $root,
179
-					'file' => $file,
180
-					'webRoot' => $webRoot,
181
-					'throw' => $throw ? 'true' : 'false'
182
-				]);
183
-			}
184
-		}
185
-		$this->resources[] = array($root, $webRoot, $file);
186
-
187
-		if ($throw && !is_file($root . '/' . $file)) {
188
-			throw new ResourceNotFoundException($file, $webRoot);
189
-		}
190
-	}
191
-
192
-	/**
193
-	 * Returns the list of all resources that should be loaded
194
-	 * @return array
195
-	 */
196
-	public function getResources() {
197
-		return $this->resources;
198
-	}
30
+    protected $theme;
31
+
32
+    protected $mapping;
33
+    protected $serverroot;
34
+    protected $thirdpartyroot;
35
+    protected $webroot;
36
+
37
+    protected $resources = array();
38
+
39
+    /** @var \OCP\ILogger */
40
+    protected $logger;
41
+
42
+    /**
43
+     * @param \OCP\ILogger $logger
44
+     * @param string $theme
45
+     * @param array $core_map
46
+     * @param array $party_map
47
+     */
48
+    public function __construct(\OCP\ILogger $logger, $theme, $core_map, $party_map) {
49
+        $this->logger = $logger;
50
+        $this->theme = $theme;
51
+        $this->mapping = $core_map + $party_map;
52
+        $this->serverroot = key($core_map);
53
+        $this->thirdpartyroot = key($party_map);
54
+        $this->webroot = $this->mapping[$this->serverroot];
55
+    }
56
+
57
+    /**
58
+     * @param string $resource
59
+     */
60
+    abstract public function doFind($resource);
61
+
62
+    /**
63
+     * @param string $resource
64
+     */
65
+    abstract public function doFindTheme($resource);
66
+
67
+    /**
68
+     * Finds the resources and adds them to the list
69
+     *
70
+     * @param array $resources
71
+     */
72
+    public function find($resources) {
73
+        foreach ($resources as $resource) {
74
+            try {
75
+                $this->doFind($resource);
76
+            } catch (ResourceNotFoundException $e) {
77
+                $resourceApp = substr($resource, 0, strpos($resource, '/'));
78
+                $this->logger->debug('Could not find resource file "' . $e->getResourcePath() . '"', ['app' => $resourceApp]);
79
+            }
80
+        }
81
+        if (!empty($this->theme)) {
82
+            foreach ($resources as $resource) {
83
+                try {
84
+                    $this->doFindTheme($resource);
85
+                } catch (ResourceNotFoundException $e) {
86
+                    $resourceApp = substr($resource, 0, strpos($resource, '/'));
87
+                    $this->logger->debug('Could not find resource file in theme "' . $e->getResourcePath() . '"', ['app' => $resourceApp]);
88
+                }
89
+            }
90
+        }
91
+    }
92
+
93
+    /**
94
+     * append the $file resource if exist at $root
95
+     *
96
+     * @param string $root path to check
97
+     * @param string $file the filename
98
+     * @param string|null $webRoot base for path, default map $root to $webRoot
99
+     * @return bool True if the resource was found, false otherwise
100
+     */
101
+    protected function appendIfExist($root, $file, $webRoot = null) {
102
+        if (is_file($root.'/'.$file)) {
103
+            $this->append($root, $file, $webRoot, false);
104
+            return true;
105
+        }
106
+        return false;
107
+    }
108
+
109
+    /**
110
+     * Attempt to find the webRoot
111
+     *
112
+     * traverse the potential web roots upwards in the path
113
+     *
114
+     * example:
115
+     *   - root: /srv/www/apps/myapp
116
+     *   - available mappings: ['/srv/www']
117
+     *
118
+     * First we check if a mapping for /srv/www/apps/myapp is available,
119
+     * then /srv/www/apps, /srv/www/apps, /srv/www, ... until we find a
120
+     * valid web root
121
+     *
122
+     * @param string $root
123
+     * @return string|null The web root or null on failure
124
+     */
125
+    protected function findWebRoot($root) {
126
+        $webRoot = null;
127
+        $tmpRoot = $root;
128
+
129
+        while ($webRoot === null) {
130
+            if (isset($this->mapping[$tmpRoot])) {
131
+                $webRoot = $this->mapping[$tmpRoot];
132
+                break;
133
+            }
134
+
135
+            if ($tmpRoot === '/') {
136
+                break;
137
+            }
138
+
139
+            $tmpRoot = dirname($tmpRoot);
140
+        }
141
+
142
+        if ($webRoot === null) {
143
+            $realpath = realpath($root);
144
+
145
+            if ($realpath && ($realpath !== $root)) {
146
+                return $this->findWebRoot($realpath);
147
+            }
148
+        }
149
+
150
+        return $webRoot;
151
+    }
152
+
153
+    /**
154
+     * append the $file resource at $root
155
+     *
156
+     * @param string $root path to check
157
+     * @param string $file the filename
158
+     * @param string|null $webRoot base for path, default map $root to $webRoot
159
+     * @param bool $throw Throw an exception, when the route does not exist
160
+     * @throws ResourceNotFoundException Only thrown when $throw is true and the resource is missing
161
+     */
162
+    protected function append($root, $file, $webRoot = null, $throw = true) {
163
+
164
+        if (!is_string($root)) {
165
+            if ($throw) {
166
+                throw new ResourceNotFoundException($file, $webRoot);
167
+            }
168
+            return;
169
+        }
170
+
171
+        if (!$webRoot) {
172
+            $webRoot = $this->findWebRoot($root);
173
+
174
+            if ($webRoot === null) {
175
+                $webRoot = '';
176
+                $this->logger->error('ResourceLocator can not find a web root (root: {root}, file: {file}, webRoot: {webRoot}, throw: {throw})', [
177
+                    'app' => 'lib',
178
+                    'root' => $root,
179
+                    'file' => $file,
180
+                    'webRoot' => $webRoot,
181
+                    'throw' => $throw ? 'true' : 'false'
182
+                ]);
183
+            }
184
+        }
185
+        $this->resources[] = array($root, $webRoot, $file);
186
+
187
+        if ($throw && !is_file($root . '/' . $file)) {
188
+            throw new ResourceNotFoundException($file, $webRoot);
189
+        }
190
+    }
191
+
192
+    /**
193
+     * Returns the list of all resources that should be loaded
194
+     * @return array
195
+     */
196
+    public function getResources() {
197
+        return $this->resources;
198
+    }
199 199
 }
Please login to merge, or discard this patch.
lib/private/Template/CSSResourceLocator.php 1 patch
Indentation   +109 added lines, -109 removed lines patch added patch discarded remove patch
@@ -29,113 +29,113 @@
 block discarded – undo
29 29
 
30 30
 class CSSResourceLocator extends ResourceLocator {
31 31
 
32
-	/** @var SCSSCacher */
33
-	protected $scssCacher;
34
-
35
-	/**
36
-	 * @param ILogger $logger
37
-	 * @param string $theme
38
-	 * @param array $core_map
39
-	 * @param array $party_map
40
-	 * @param SCSSCacher $scssCacher
41
-	 */
42
-	public function __construct(ILogger $logger, $theme, $core_map, $party_map, $scssCacher) {
43
-		$this->scssCacher = $scssCacher;
44
-
45
-		parent::__construct($logger, $theme, $core_map, $party_map);
46
-	}
47
-
48
-	/**
49
-	 * @param string $style
50
-	 */
51
-	public function doFind($style) {
52
-		$app = substr($style, 0, strpos($style, '/'));
53
-		if (strpos($style, '3rdparty') === 0
54
-			&& $this->appendIfExist($this->thirdpartyroot, $style.'.css')
55
-			|| $this->cacheAndAppendScssIfExist($this->serverroot, $style.'.scss', $app)
56
-			|| $this->cacheAndAppendScssIfExist($this->serverroot, 'core/'.$style.'.scss')
57
-			|| $this->appendIfExist($this->serverroot, $style.'.css')
58
-			|| $this->appendIfExist($this->serverroot, 'core/'.$style.'.css')
59
-		) {
60
-			return;
61
-		}
62
-		$style = substr($style, strpos($style, '/')+1);
63
-		$app_path = \OC_App::getAppPath($app);
64
-		$app_url = \OC_App::getAppWebPath($app);
65
-
66
-		if ($app_path === false && $app_url === false) {
67
-			$this->logger->error('Could not find resource {resource} to load', [
68
-				'resource' => $app . '/' . $style . '.css',
69
-				'app' => 'cssresourceloader',
70
-			]);
71
-			return;
72
-		}
73
-
74
-		if(!$this->cacheAndAppendScssIfExist($app_path, $style.'.scss', $app)) {
75
-			$this->append($app_path, $style.'.css', $app_url);
76
-		}
77
-	}
78
-
79
-	/**
80
-	 * @param string $style
81
-	 */
82
-	public function doFindTheme($style) {
83
-		$theme_dir = 'themes/'.$this->theme.'/';
84
-		$this->appendIfExist($this->serverroot, $theme_dir.'apps/'.$style.'.css')
85
-			|| $this->appendIfExist($this->serverroot, $theme_dir.$style.'.css')
86
-			|| $this->appendIfExist($this->serverroot, $theme_dir.'core/'.$style.'.css');
87
-	}
88
-
89
-	/**
90
-	 * cache and append the scss $file if exist at $root
91
-	 *
92
-	 * @param string $root path to check
93
-	 * @param string $file the filename
94
-	 * @return bool True if the resource was found and cached, false otherwise
95
-	 */
96
-	protected function cacheAndAppendScssIfExist($root, $file, $app = 'core') {
97
-		if (is_file($root.'/'.$file)) {
98
-			if($this->scssCacher !== null) {
99
-				if($this->scssCacher->process($root, $file, $app)) {
100
-
101
-					$this->append($root, $this->scssCacher->getCachedSCSS($app, $file), false, true, true);
102
-					return true;
103
-				} else {
104
-					$this->logger->warning('Failed to compile and/or save '.$root.'/'.$file, ['app' => 'core']);
105
-					return false;
106
-				}
107
-			} else {
108
-				$this->logger->debug('Scss is disabled for '.$root.'/'.$file.', ignoring', ['app' => 'core']);
109
-				return true;
110
-			}
111
-		}
112
-		return false;
113
-	}
114
-
115
-	public function append($root, $file, $webRoot = null, $throw = true, $scss = false) {
116
-		if (!$scss) {
117
-			parent::append($root, $file, $webRoot, $throw);
118
-		} else {
119
-			if (!$webRoot) {
120
-				$webRoot = $this->findWebRoot($root);
121
-
122
-				if ($webRoot === null) {
123
-					$webRoot = '';
124
-					$this->logger->error('ResourceLocator can not find a web root (root: {root}, file: {file}, webRoot: {webRoot}, throw: {throw})', [
125
-						'app' => 'lib',
126
-						'root' => $root,
127
-						'file' => $file,
128
-						'webRoot' => $webRoot,
129
-						'throw' => $throw ? 'true' : 'false'
130
-					]);
131
-
132
-					if ($throw && $root === '/') {
133
-						throw new ResourceNotFoundException($file, $webRoot);
134
-					}
135
-				}
136
-			}
137
-
138
-			$this->resources[] = array($webRoot? : '/', $webRoot, $file);
139
-		}
140
-	}
32
+    /** @var SCSSCacher */
33
+    protected $scssCacher;
34
+
35
+    /**
36
+     * @param ILogger $logger
37
+     * @param string $theme
38
+     * @param array $core_map
39
+     * @param array $party_map
40
+     * @param SCSSCacher $scssCacher
41
+     */
42
+    public function __construct(ILogger $logger, $theme, $core_map, $party_map, $scssCacher) {
43
+        $this->scssCacher = $scssCacher;
44
+
45
+        parent::__construct($logger, $theme, $core_map, $party_map);
46
+    }
47
+
48
+    /**
49
+     * @param string $style
50
+     */
51
+    public function doFind($style) {
52
+        $app = substr($style, 0, strpos($style, '/'));
53
+        if (strpos($style, '3rdparty') === 0
54
+            && $this->appendIfExist($this->thirdpartyroot, $style.'.css')
55
+            || $this->cacheAndAppendScssIfExist($this->serverroot, $style.'.scss', $app)
56
+            || $this->cacheAndAppendScssIfExist($this->serverroot, 'core/'.$style.'.scss')
57
+            || $this->appendIfExist($this->serverroot, $style.'.css')
58
+            || $this->appendIfExist($this->serverroot, 'core/'.$style.'.css')
59
+        ) {
60
+            return;
61
+        }
62
+        $style = substr($style, strpos($style, '/')+1);
63
+        $app_path = \OC_App::getAppPath($app);
64
+        $app_url = \OC_App::getAppWebPath($app);
65
+
66
+        if ($app_path === false && $app_url === false) {
67
+            $this->logger->error('Could not find resource {resource} to load', [
68
+                'resource' => $app . '/' . $style . '.css',
69
+                'app' => 'cssresourceloader',
70
+            ]);
71
+            return;
72
+        }
73
+
74
+        if(!$this->cacheAndAppendScssIfExist($app_path, $style.'.scss', $app)) {
75
+            $this->append($app_path, $style.'.css', $app_url);
76
+        }
77
+    }
78
+
79
+    /**
80
+     * @param string $style
81
+     */
82
+    public function doFindTheme($style) {
83
+        $theme_dir = 'themes/'.$this->theme.'/';
84
+        $this->appendIfExist($this->serverroot, $theme_dir.'apps/'.$style.'.css')
85
+            || $this->appendIfExist($this->serverroot, $theme_dir.$style.'.css')
86
+            || $this->appendIfExist($this->serverroot, $theme_dir.'core/'.$style.'.css');
87
+    }
88
+
89
+    /**
90
+     * cache and append the scss $file if exist at $root
91
+     *
92
+     * @param string $root path to check
93
+     * @param string $file the filename
94
+     * @return bool True if the resource was found and cached, false otherwise
95
+     */
96
+    protected function cacheAndAppendScssIfExist($root, $file, $app = 'core') {
97
+        if (is_file($root.'/'.$file)) {
98
+            if($this->scssCacher !== null) {
99
+                if($this->scssCacher->process($root, $file, $app)) {
100
+
101
+                    $this->append($root, $this->scssCacher->getCachedSCSS($app, $file), false, true, true);
102
+                    return true;
103
+                } else {
104
+                    $this->logger->warning('Failed to compile and/or save '.$root.'/'.$file, ['app' => 'core']);
105
+                    return false;
106
+                }
107
+            } else {
108
+                $this->logger->debug('Scss is disabled for '.$root.'/'.$file.', ignoring', ['app' => 'core']);
109
+                return true;
110
+            }
111
+        }
112
+        return false;
113
+    }
114
+
115
+    public function append($root, $file, $webRoot = null, $throw = true, $scss = false) {
116
+        if (!$scss) {
117
+            parent::append($root, $file, $webRoot, $throw);
118
+        } else {
119
+            if (!$webRoot) {
120
+                $webRoot = $this->findWebRoot($root);
121
+
122
+                if ($webRoot === null) {
123
+                    $webRoot = '';
124
+                    $this->logger->error('ResourceLocator can not find a web root (root: {root}, file: {file}, webRoot: {webRoot}, throw: {throw})', [
125
+                        'app' => 'lib',
126
+                        'root' => $root,
127
+                        'file' => $file,
128
+                        'webRoot' => $webRoot,
129
+                        'throw' => $throw ? 'true' : 'false'
130
+                    ]);
131
+
132
+                    if ($throw && $root === '/') {
133
+                        throw new ResourceNotFoundException($file, $webRoot);
134
+                    }
135
+                }
136
+            }
137
+
138
+            $this->resources[] = array($webRoot? : '/', $webRoot, $file);
139
+        }
140
+    }
141 141
 }
Please login to merge, or discard this patch.