@@ -35,196 +35,196 @@ |
||
| 35 | 35 | |
| 36 | 36 | class JSCombiner { |
| 37 | 37 | |
| 38 | - /** @var IAppData */ |
|
| 39 | - protected $appData; |
|
| 40 | - |
|
| 41 | - /** @var IURLGenerator */ |
|
| 42 | - protected $urlGenerator; |
|
| 43 | - |
|
| 44 | - /** @var ICache */ |
|
| 45 | - protected $depsCache; |
|
| 46 | - |
|
| 47 | - /** @var SystemConfig */ |
|
| 48 | - protected $config; |
|
| 49 | - |
|
| 50 | - /** @var ILogger */ |
|
| 51 | - protected $logger; |
|
| 52 | - |
|
| 53 | - /** |
|
| 54 | - * @param IAppData $appData |
|
| 55 | - * @param IURLGenerator $urlGenerator |
|
| 56 | - * @param ICache $depsCache |
|
| 57 | - * @param SystemConfig $config |
|
| 58 | - * @param ILogger $logger |
|
| 59 | - */ |
|
| 60 | - public function __construct(IAppData $appData, |
|
| 61 | - IURLGenerator $urlGenerator, |
|
| 62 | - ICache $depsCache, |
|
| 63 | - SystemConfig $config, |
|
| 64 | - ILogger $logger) { |
|
| 65 | - $this->appData = $appData; |
|
| 66 | - $this->urlGenerator = $urlGenerator; |
|
| 67 | - $this->depsCache = $depsCache; |
|
| 68 | - $this->config = $config; |
|
| 69 | - $this->logger = $logger; |
|
| 70 | - } |
|
| 71 | - |
|
| 72 | - /** |
|
| 73 | - * @param string $root |
|
| 74 | - * @param string $file |
|
| 75 | - * @param string $app |
|
| 76 | - * @return bool |
|
| 77 | - */ |
|
| 78 | - public function process($root, $file, $app) { |
|
| 79 | - if ($this->config->getValue('debug') || !$this->config->getValue('installed')) { |
|
| 80 | - return false; |
|
| 81 | - } |
|
| 82 | - |
|
| 83 | - $path = explode('/', $root . '/' . $file); |
|
| 84 | - |
|
| 85 | - $fileName = array_pop($path); |
|
| 86 | - $path = implode('/', $path); |
|
| 87 | - |
|
| 88 | - try { |
|
| 89 | - $folder = $this->appData->getFolder($app); |
|
| 90 | - } catch(NotFoundException $e) { |
|
| 91 | - // creating css appdata folder |
|
| 92 | - $folder = $this->appData->newFolder($app); |
|
| 93 | - } |
|
| 94 | - |
|
| 95 | - if($this->isCached($fileName, $folder)) { |
|
| 96 | - return true; |
|
| 97 | - } |
|
| 98 | - return $this->cache($path, $fileName, $folder); |
|
| 99 | - } |
|
| 100 | - |
|
| 101 | - /** |
|
| 102 | - * @param string $fileName |
|
| 103 | - * @param ISimpleFolder $folder |
|
| 104 | - * @return bool |
|
| 105 | - */ |
|
| 106 | - protected function isCached($fileName, ISimpleFolder $folder) { |
|
| 107 | - $fileName = str_replace('.json', '.js', $fileName) . '.deps'; |
|
| 108 | - try { |
|
| 109 | - $deps = $this->depsCache->get($folder->getName() . '-' . $fileName); |
|
| 110 | - if ($deps === null || $deps === '') { |
|
| 111 | - $depFile = $folder->getFile($fileName); |
|
| 112 | - $deps = $depFile->getContent(); |
|
| 113 | - } |
|
| 114 | - // check again |
|
| 115 | - if ($deps === null || $deps === '') { |
|
| 116 | - $this->logger->info('JSCombiner: deps file empty: ' . $fileName); |
|
| 117 | - return false; |
|
| 118 | - } |
|
| 119 | - |
|
| 120 | - $deps = json_decode($deps, true); |
|
| 121 | - |
|
| 122 | - if ($deps === NULL) { |
|
| 123 | - return false; |
|
| 124 | - } |
|
| 125 | - |
|
| 126 | - foreach ($deps as $file=>$mtime) { |
|
| 127 | - if (!file_exists($file) || filemtime($file) > $mtime) { |
|
| 128 | - return false; |
|
| 129 | - } |
|
| 130 | - } |
|
| 131 | - |
|
| 132 | - return true; |
|
| 133 | - } catch(NotFoundException $e) { |
|
| 134 | - return false; |
|
| 135 | - } |
|
| 136 | - } |
|
| 137 | - |
|
| 138 | - /** |
|
| 139 | - * @param string $path |
|
| 140 | - * @param string $fileName |
|
| 141 | - * @param ISimpleFolder $folder |
|
| 142 | - * @return bool |
|
| 143 | - */ |
|
| 144 | - protected function cache($path, $fileName, ISimpleFolder $folder) { |
|
| 145 | - $deps = []; |
|
| 146 | - $fullPath = $path . '/' . $fileName; |
|
| 147 | - $data = json_decode(file_get_contents($fullPath)); |
|
| 148 | - $deps[$fullPath] = filemtime($fullPath); |
|
| 149 | - |
|
| 150 | - $res = ''; |
|
| 151 | - foreach ($data as $file) { |
|
| 152 | - $filePath = $path . '/' . $file; |
|
| 153 | - |
|
| 154 | - if (is_file($filePath)) { |
|
| 155 | - $res .= file_get_contents($filePath); |
|
| 156 | - $res .= PHP_EOL . PHP_EOL; |
|
| 157 | - $deps[$filePath] = filemtime($filePath); |
|
| 158 | - } |
|
| 159 | - } |
|
| 160 | - |
|
| 161 | - $fileName = str_replace('.json', '.js', $fileName); |
|
| 162 | - try { |
|
| 163 | - $cachedfile = $folder->getFile($fileName); |
|
| 164 | - } catch(NotFoundException $e) { |
|
| 165 | - $cachedfile = $folder->newFile($fileName); |
|
| 166 | - } |
|
| 167 | - |
|
| 168 | - $depFileName = $fileName . '.deps'; |
|
| 169 | - try { |
|
| 170 | - $depFile = $folder->getFile($depFileName); |
|
| 171 | - } catch (NotFoundException $e) { |
|
| 172 | - $depFile = $folder->newFile($depFileName); |
|
| 173 | - } |
|
| 174 | - |
|
| 175 | - try { |
|
| 176 | - $gzipFile = $folder->getFile($fileName . '.gzip'); # Safari doesn't like .gz |
|
| 177 | - } catch (NotFoundException $e) { |
|
| 178 | - $gzipFile = $folder->newFile($fileName . '.gzip'); # Safari doesn't like .gz |
|
| 179 | - } |
|
| 180 | - |
|
| 181 | - try { |
|
| 182 | - $cachedfile->putContent($res); |
|
| 183 | - $deps = json_encode($deps); |
|
| 184 | - $depFile->putContent($deps); |
|
| 185 | - $this->depsCache->set($folder->getName() . '-' . $depFileName, $deps); |
|
| 186 | - $gzipFile->putContent(gzencode($res, 9)); |
|
| 187 | - |
|
| 188 | - return true; |
|
| 189 | - } catch (NotPermittedException $e) { |
|
| 190 | - return false; |
|
| 191 | - } |
|
| 192 | - } |
|
| 193 | - |
|
| 194 | - /** |
|
| 195 | - * @param string $appName |
|
| 196 | - * @param string $fileName |
|
| 197 | - * @return string |
|
| 198 | - */ |
|
| 199 | - public function getCachedJS($appName, $fileName) { |
|
| 200 | - $tmpfileLoc = explode('/', $fileName); |
|
| 201 | - $fileName = array_pop($tmpfileLoc); |
|
| 202 | - $fileName = str_replace('.json', '.js', $fileName); |
|
| 203 | - |
|
| 204 | - return substr($this->urlGenerator->linkToRoute('core.Js.getJs', array('fileName' => $fileName, 'appName' => $appName)), strlen(\OC::$WEBROOT) + 1); |
|
| 205 | - } |
|
| 206 | - |
|
| 207 | - /** |
|
| 208 | - * @param string $root |
|
| 209 | - * @param string $file |
|
| 210 | - * @return string[] |
|
| 211 | - */ |
|
| 212 | - public function getContent($root, $file) { |
|
| 213 | - /** @var array $data */ |
|
| 214 | - $data = json_decode(file_get_contents($root . '/' . $file)); |
|
| 215 | - if(!is_array($data)) { |
|
| 216 | - return []; |
|
| 217 | - } |
|
| 218 | - |
|
| 219 | - $path = explode('/', $file); |
|
| 220 | - array_pop($path); |
|
| 221 | - $path = implode('/', $path); |
|
| 222 | - |
|
| 223 | - $result = []; |
|
| 224 | - foreach ($data as $f) { |
|
| 225 | - $result[] = $path . '/' . $f; |
|
| 226 | - } |
|
| 227 | - |
|
| 228 | - return $result; |
|
| 229 | - } |
|
| 38 | + /** @var IAppData */ |
|
| 39 | + protected $appData; |
|
| 40 | + |
|
| 41 | + /** @var IURLGenerator */ |
|
| 42 | + protected $urlGenerator; |
|
| 43 | + |
|
| 44 | + /** @var ICache */ |
|
| 45 | + protected $depsCache; |
|
| 46 | + |
|
| 47 | + /** @var SystemConfig */ |
|
| 48 | + protected $config; |
|
| 49 | + |
|
| 50 | + /** @var ILogger */ |
|
| 51 | + protected $logger; |
|
| 52 | + |
|
| 53 | + /** |
|
| 54 | + * @param IAppData $appData |
|
| 55 | + * @param IURLGenerator $urlGenerator |
|
| 56 | + * @param ICache $depsCache |
|
| 57 | + * @param SystemConfig $config |
|
| 58 | + * @param ILogger $logger |
|
| 59 | + */ |
|
| 60 | + public function __construct(IAppData $appData, |
|
| 61 | + IURLGenerator $urlGenerator, |
|
| 62 | + ICache $depsCache, |
|
| 63 | + SystemConfig $config, |
|
| 64 | + ILogger $logger) { |
|
| 65 | + $this->appData = $appData; |
|
| 66 | + $this->urlGenerator = $urlGenerator; |
|
| 67 | + $this->depsCache = $depsCache; |
|
| 68 | + $this->config = $config; |
|
| 69 | + $this->logger = $logger; |
|
| 70 | + } |
|
| 71 | + |
|
| 72 | + /** |
|
| 73 | + * @param string $root |
|
| 74 | + * @param string $file |
|
| 75 | + * @param string $app |
|
| 76 | + * @return bool |
|
| 77 | + */ |
|
| 78 | + public function process($root, $file, $app) { |
|
| 79 | + if ($this->config->getValue('debug') || !$this->config->getValue('installed')) { |
|
| 80 | + return false; |
|
| 81 | + } |
|
| 82 | + |
|
| 83 | + $path = explode('/', $root . '/' . $file); |
|
| 84 | + |
|
| 85 | + $fileName = array_pop($path); |
|
| 86 | + $path = implode('/', $path); |
|
| 87 | + |
|
| 88 | + try { |
|
| 89 | + $folder = $this->appData->getFolder($app); |
|
| 90 | + } catch(NotFoundException $e) { |
|
| 91 | + // creating css appdata folder |
|
| 92 | + $folder = $this->appData->newFolder($app); |
|
| 93 | + } |
|
| 94 | + |
|
| 95 | + if($this->isCached($fileName, $folder)) { |
|
| 96 | + return true; |
|
| 97 | + } |
|
| 98 | + return $this->cache($path, $fileName, $folder); |
|
| 99 | + } |
|
| 100 | + |
|
| 101 | + /** |
|
| 102 | + * @param string $fileName |
|
| 103 | + * @param ISimpleFolder $folder |
|
| 104 | + * @return bool |
|
| 105 | + */ |
|
| 106 | + protected function isCached($fileName, ISimpleFolder $folder) { |
|
| 107 | + $fileName = str_replace('.json', '.js', $fileName) . '.deps'; |
|
| 108 | + try { |
|
| 109 | + $deps = $this->depsCache->get($folder->getName() . '-' . $fileName); |
|
| 110 | + if ($deps === null || $deps === '') { |
|
| 111 | + $depFile = $folder->getFile($fileName); |
|
| 112 | + $deps = $depFile->getContent(); |
|
| 113 | + } |
|
| 114 | + // check again |
|
| 115 | + if ($deps === null || $deps === '') { |
|
| 116 | + $this->logger->info('JSCombiner: deps file empty: ' . $fileName); |
|
| 117 | + return false; |
|
| 118 | + } |
|
| 119 | + |
|
| 120 | + $deps = json_decode($deps, true); |
|
| 121 | + |
|
| 122 | + if ($deps === NULL) { |
|
| 123 | + return false; |
|
| 124 | + } |
|
| 125 | + |
|
| 126 | + foreach ($deps as $file=>$mtime) { |
|
| 127 | + if (!file_exists($file) || filemtime($file) > $mtime) { |
|
| 128 | + return false; |
|
| 129 | + } |
|
| 130 | + } |
|
| 131 | + |
|
| 132 | + return true; |
|
| 133 | + } catch(NotFoundException $e) { |
|
| 134 | + return false; |
|
| 135 | + } |
|
| 136 | + } |
|
| 137 | + |
|
| 138 | + /** |
|
| 139 | + * @param string $path |
|
| 140 | + * @param string $fileName |
|
| 141 | + * @param ISimpleFolder $folder |
|
| 142 | + * @return bool |
|
| 143 | + */ |
|
| 144 | + protected function cache($path, $fileName, ISimpleFolder $folder) { |
|
| 145 | + $deps = []; |
|
| 146 | + $fullPath = $path . '/' . $fileName; |
|
| 147 | + $data = json_decode(file_get_contents($fullPath)); |
|
| 148 | + $deps[$fullPath] = filemtime($fullPath); |
|
| 149 | + |
|
| 150 | + $res = ''; |
|
| 151 | + foreach ($data as $file) { |
|
| 152 | + $filePath = $path . '/' . $file; |
|
| 153 | + |
|
| 154 | + if (is_file($filePath)) { |
|
| 155 | + $res .= file_get_contents($filePath); |
|
| 156 | + $res .= PHP_EOL . PHP_EOL; |
|
| 157 | + $deps[$filePath] = filemtime($filePath); |
|
| 158 | + } |
|
| 159 | + } |
|
| 160 | + |
|
| 161 | + $fileName = str_replace('.json', '.js', $fileName); |
|
| 162 | + try { |
|
| 163 | + $cachedfile = $folder->getFile($fileName); |
|
| 164 | + } catch(NotFoundException $e) { |
|
| 165 | + $cachedfile = $folder->newFile($fileName); |
|
| 166 | + } |
|
| 167 | + |
|
| 168 | + $depFileName = $fileName . '.deps'; |
|
| 169 | + try { |
|
| 170 | + $depFile = $folder->getFile($depFileName); |
|
| 171 | + } catch (NotFoundException $e) { |
|
| 172 | + $depFile = $folder->newFile($depFileName); |
|
| 173 | + } |
|
| 174 | + |
|
| 175 | + try { |
|
| 176 | + $gzipFile = $folder->getFile($fileName . '.gzip'); # Safari doesn't like .gz |
|
| 177 | + } catch (NotFoundException $e) { |
|
| 178 | + $gzipFile = $folder->newFile($fileName . '.gzip'); # Safari doesn't like .gz |
|
| 179 | + } |
|
| 180 | + |
|
| 181 | + try { |
|
| 182 | + $cachedfile->putContent($res); |
|
| 183 | + $deps = json_encode($deps); |
|
| 184 | + $depFile->putContent($deps); |
|
| 185 | + $this->depsCache->set($folder->getName() . '-' . $depFileName, $deps); |
|
| 186 | + $gzipFile->putContent(gzencode($res, 9)); |
|
| 187 | + |
|
| 188 | + return true; |
|
| 189 | + } catch (NotPermittedException $e) { |
|
| 190 | + return false; |
|
| 191 | + } |
|
| 192 | + } |
|
| 193 | + |
|
| 194 | + /** |
|
| 195 | + * @param string $appName |
|
| 196 | + * @param string $fileName |
|
| 197 | + * @return string |
|
| 198 | + */ |
|
| 199 | + public function getCachedJS($appName, $fileName) { |
|
| 200 | + $tmpfileLoc = explode('/', $fileName); |
|
| 201 | + $fileName = array_pop($tmpfileLoc); |
|
| 202 | + $fileName = str_replace('.json', '.js', $fileName); |
|
| 203 | + |
|
| 204 | + return substr($this->urlGenerator->linkToRoute('core.Js.getJs', array('fileName' => $fileName, 'appName' => $appName)), strlen(\OC::$WEBROOT) + 1); |
|
| 205 | + } |
|
| 206 | + |
|
| 207 | + /** |
|
| 208 | + * @param string $root |
|
| 209 | + * @param string $file |
|
| 210 | + * @return string[] |
|
| 211 | + */ |
|
| 212 | + public function getContent($root, $file) { |
|
| 213 | + /** @var array $data */ |
|
| 214 | + $data = json_decode(file_get_contents($root . '/' . $file)); |
|
| 215 | + if(!is_array($data)) { |
|
| 216 | + return []; |
|
| 217 | + } |
|
| 218 | + |
|
| 219 | + $path = explode('/', $file); |
|
| 220 | + array_pop($path); |
|
| 221 | + $path = implode('/', $path); |
|
| 222 | + |
|
| 223 | + $result = []; |
|
| 224 | + foreach ($data as $f) { |
|
| 225 | + $result[] = $path . '/' . $f; |
|
| 226 | + } |
|
| 227 | + |
|
| 228 | + return $result; |
|
| 229 | + } |
|
| 230 | 230 | } |