Passed
Push — master ( dcbacd...31b117 )
by Pauli
02:46
created

HtmlUtil::getSvgPath()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 4
rs 10
1
<?php
2
3
/**
4
 * ownCloud - Music app
5
 *
6
 * This file is licensed under the Affero General Public License version 3 or
7
 * later. See the COPYING file.
8
 *
9
 * @author Pauli Järvinen <[email protected]>
10
 * @copyright Pauli Järvinen 2020
11
 */
12
13
namespace OCA\Music\Utility;
14
15
/**
16
 * Utility functions to be used from the front-end templates
17
 */
18
class HtmlUtil {
19
20
	/**
21
	 * Sanitized printing
22
	 * @param string $string
23
	 */
24
	public static function p($string) {
25
		print(/** @scrutinizer ignore-type */ \OCP\Util::sanitizeHTML($string));
26
	}
27
28
	/**
29
	 * Print path to a icon of the Music app
30
	 * @param string $iconName Name of the icon without path or the '.svg' suffix
31
	 */
32
	public static function printSvgPath($iconName) {
33
		print(self::getSvgPath($iconName));
34
	}
35
36
	/**
37
	 * Get path to a icon of the Music app
38
	 * @param string $iconName Name of the icon without path or the '.svg' suffix
39
	 */
40
	public static function getSvgPath($iconName) {
41
		$manifest = self::getManifest();
42
		$hashedName = $manifest["img/$iconName.svg"];
43
		return \OCP\Template::image_path('music', '../dist/' . $hashedName);
44
	}
45
46
	/**
47
	 * Print AngularJS template whose contents can be found under templates/partials
48
	 * @param string $templateName
49
	 */
50
	public static function printNgTemplate($templateName) {
51
		print(
52
			'<script type="text/ng-template" id="'.$templateName.'.html">' .
53
				self::partialContent($templateName) .
54
			'</script>'
55
		);
56
	}
57
58
	/**
59
	 * Print a partial template
60
	 * @param string $partialName Name of the file under templates/partials without the '.php' suffix
61
	 */
62
	public static function printPartial($partialName) {
63
		print(self::partialContent($partialName));
64
	}
65
66
	/**
67
	 * @param string $partialName
68
	 */
69
	private static function partialContent($partialName) {
70
		$fileName = \join(DIRECTORY_SEPARATOR, [\dirname(__DIR__), 'templates', 'partials', $partialName.'.php']);
71
72
		\ob_start();
73
		try {
74
			include $fileName;
75
			$data = \ob_get_contents();
76
		} catch (\Exception $e) {
77
			\ob_end_clean();
78
			throw $e;
79
		}
80
		\ob_end_clean();
81
82
		return $data;
83
	}
84
85
	/**
86
	 * @param string $name
87
	 */
88
	public static function addWebpackScript($name) {
89
		$manifest = self::getManifest();
90
		$hashedName = \substr($manifest["$name.js"], 0, -3); // the extension is cropped from the name in $manifest
91
		\OCP\Util::addScript('music', '../dist/' . $hashedName);
92
	}
93
94
	/**
95
	 * @param string $name
96
	 */
97
	public static function addWebpackStyle($name) {
98
		$manifest = self::getManifest();
99
		$hashedName = \substr($manifest["$name.css"], 0, -4); // the extension is cropped from the name in $manifest
100
		\OCP\Util::addStyle('music', '../dist/' . $hashedName);
101
	}
102
103
	private static $manifest = null;
104
	private static function getManifest() {
105
		if (self::$manifest === null) {
106
			$manifestPath = \join(DIRECTORY_SEPARATOR, [\dirname(__DIR__), 'dist', 'manifest.json']);
107
			$manifestText = \file_get_contents($manifestPath);
108
			self::$manifest = \json_decode($manifestText, true);
109
		}
110
		return self::$manifest;
111
	}
112
}
113