Completed
Push — master ( 94ab2e...2cd79a )
by Lukas
12:30
created

JSCombiner::getContent()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 9
c 0
b 0
f 0
nc 2
nop 2
dl 0
loc 14
rs 9.4285
1
<?php
2
/**
3
 * @copyright 2017, Roeland Jago Douma <[email protected]>
4
 *
5
 * @author Roeland Jago Douma <[email protected]>
6
 *
7
 * @license GNU AGPL version 3 or any later version
8
 *
9
 * This program is free software: you can redistribute it and/or modify
10
 * it under the terms of the GNU Affero General Public License as
11
 * published by the Free Software Foundation, either version 3 of the
12
 * License, or (at your option) any later version.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 * GNU Affero General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU Affero General Public License
20
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21
 *
22
 */
23
namespace OC\Template;
24
25
use OC\SystemConfig;
26
use OCP\ICache;
27
use OCP\Files\IAppData;
28
use OCP\Files\NotFoundException;
29
use OCP\Files\NotPermittedException;
30
use OCP\Files\SimpleFS\ISimpleFolder;
31
use OCP\IURLGenerator;
32
33
class JSCombiner {
34
35
	/** @var IAppData */
36
	protected $appData;
37
38
	/** @var IURLGenerator */
39
	protected $urlGenerator;
40
41
	/** @var ICache */
42
	protected $depsCache;
43
44
	/** @var SystemConfig */
45
	protected $config;
46
47
	/**
48
	 * JSCombiner constructor.
49
	 *
50
	 * @param IAppData $appData
51
	 * @param IURLGenerator $urlGenerator
52
	 * @param ICache $depsCache
53
	 */
54
	public function __construct(IAppData $appData,
55
								IURLGenerator $urlGenerator,
56
								ICache $depsCache,
57
								SystemConfig $config) {
58
		$this->appData = $appData;
59
		$this->urlGenerator = $urlGenerator;
60
		$this->depsCache = $depsCache;
61
		$this->config = $config;
62
	}
63
64
	/**
65
	 * @param string $root
66
	 * @param string $file
67
	 * @param string $app
68
	 * @return bool
69
	 */
70
	public function process($root, $file, $app) {
71
		if ($this->config->getValue('debug')) {
72
			return false;
73
		}
74
75
		$path = explode('/', $root . '/' . $file);
76
77
		$fileName = array_pop($path);
78
		$path = implode('/', $path);
79
80
		try {
81
			$folder = $this->appData->getFolder($app);
82
		} catch(NotFoundException $e) {
83
			// creating css appdata folder
84
			$folder = $this->appData->newFolder($app);
85
		}
86
87
		if($this->isCached($fileName, $folder)) {
88
			return true;
89
		}
90
		return $this->cache($path, $fileName, $folder);
91
	}
92
93
	/**
94
	 * @param string $fileName
95
	 * @param ISimpleFolder $folder
96
	 * @return bool
97
	 */
98
	protected function isCached($fileName, ISimpleFolder $folder) {
99
		$fileName = str_replace('.json', '.js', $fileName) . '.deps';
100
		try {
101
			$deps = $this->depsCache->get($folder->getName() . '-' . $fileName);
102 View Code Duplication
			if ($deps === null) {
103
				$depFile = $folder->getFile($fileName);
104
				$deps = $depFile->getContent();
105
				$this->depsCache->set($folder->getName() . '-' . $fileName, $deps);
106
			}
107
			$deps = json_decode($deps, true);
108
109 View Code Duplication
			foreach ($deps as $file=>$mtime) {
110
				if (!file_exists($file) || filemtime($file) > $mtime) {
111
					return false;
112
				}
113
			}
114
115
			return true;
116
		} catch(NotFoundException $e) {
117
			return false;
118
		}
119
	}
120
121
	/**
122
	 * @param string $path
123
	 * @param string $fileName
124
	 * @param ISimpleFolder $folder
125
	 * @return bool
126
	 */
127
	protected function cache($path, $fileName, ISimpleFolder $folder) {
128
		$deps = [];
129
		$fullPath = $path . '/' . $fileName;
130
		$data = json_decode(file_get_contents($fullPath));
131
		$deps[$fullPath] = filemtime($fullPath);
132
133
		$res = '';
134
		foreach ($data as $file) {
135
			$filePath = $path . '/' . $file;
136
137
			if (is_file($filePath)) {
138
				$res .= file_get_contents($filePath);
139
				$res .= PHP_EOL . PHP_EOL;
140
				$deps[$filePath] = filemtime($filePath);
141
			}
142
		}
143
144
		$fileName = str_replace('.json', '.js', $fileName);
145
		try {
146
			$cachedfile = $folder->getFile($fileName);
147
		} catch(NotFoundException $e) {
148
			$cachedfile = $folder->newFile($fileName);
149
		}
150
151
		$depFileName = $fileName . '.deps';
152
		try {
153
			$depFile = $folder->getFile($depFileName);
154
		} catch (NotFoundException $e) {
155
			$depFile = $folder->newFile($depFileName);
156
		}
157
158
		try {
159
			$cachedfile->putContent($res);
160
			$depFile->putContent(json_encode($deps));
161
			return true;
162
		} catch (NotPermittedException $e) {
163
			return false;
164
		}
165
	}
166
167
	/**
168
	 * @param string $appName
169
	 * @param string $fileName
170
	 * @return string
171
	 */
172 View Code Duplication
	public function getCachedJS($appName, $fileName) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
173
		$tmpfileLoc = explode('/', $fileName);
174
		$fileName = array_pop($tmpfileLoc);
175
		$fileName = str_replace('.json', '.js', $fileName);
176
177
		return substr($this->urlGenerator->linkToRoute('core.Js.getJs', array('fileName' => $fileName, 'appName' => $appName)), strlen(\OC::$WEBROOT) + 1);
178
	}
179
180
	/**
181
	 * @param string $root
182
	 * @param string $file
183
	 * @return string[]
184
	 */
185
	public function getContent($root, $file) {
186
		$data = json_decode(file_get_contents($root . '/' . $file));
187
188
		$path = explode('/', $file);
189
		array_pop($path);
190
		$path = implode('/', $path);
191
192
		$result = [];
193
		foreach ($data as $f) {
194
			$result[] = $path . '/' . $f;
195
		}
196
197
		return $result;
198
	}
199
}
200