SCSSCacher.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, John Molakvoæ (skjnldsv@protonmail.com)
  4. *
  5. * @license GNU AGPL version 3 or any later version
  6. *
  7. * This program is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU Affero General Public License as
  9. * published by the Free Software Foundation, either version 3 of the
  10. * License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU Affero General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Affero General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. *
  20. */
  21. namespace OC\Template;
  22. use Leafo\ScssPhp\Compiler;
  23. use Leafo\ScssPhp\Exception\ParserException;
  24. use Leafo\ScssPhp\Formatter\Crunched;
  25. use Leafo\ScssPhp\Formatter\Expanded;
  26. use OC\SystemConfig;
  27. use OCP\Files\IAppData;
  28. use OCP\Files\NotFoundException;
  29. use OCP\Files\NotPermittedException;
  30. use OCP\Files\SimpleFS\ISimpleFolder;
  31. use OCP\IConfig;
  32. use OCP\ILogger;
  33. use OCP\IURLGenerator;
  34. class SCSSCacher {
  35. /** @var ILogger */
  36. protected $logger;
  37. /** @var IAppData */
  38. protected $appData;
  39. /** @var IURLGenerator */
  40. protected $urlGenerator;
  41. /** @var IConfig */
  42. protected $config;
  43. /** @var string */
  44. protected $serverRoot;
  45. /**
  46. * @param ILogger $logger
  47. * @param IAppData $appData
  48. * @param IURLGenerator $urlGenerator
  49. * @param SystemConfig $systemConfig
  50. * @param string $serverRoot
  51. */
  52. public function __construct(ILogger $logger,
  53. IAppData $appData,
  54. IURLGenerator $urlGenerator,
  55. IConfig $config,
  56. $serverRoot) {
  57. $this->logger = $logger;
  58. $this->appData = $appData;
  59. $this->urlGenerator = $urlGenerator;
  60. $this->config = $config;
  61. $this->serverRoot = $serverRoot;
  62. }
  63. /**
  64. * Process the caching process if needed
  65. * @param string $root Root path to the nextcloud installation
  66. * @param string $file
  67. * @param string $app The app name
  68. * @return boolean
  69. */
  70. public function process($root, $file, $app) {
  71. $path = explode('/', $root . '/' . $file);
  72. $fileNameSCSS = array_pop($path);
  73. $fileNameCSS = str_replace('.scss', '.css', $fileNameSCSS);
  74. $path = implode('/', $path);
  75. $webDir = substr($path, strlen($this->serverRoot)+1);
  76. try {
  77. $folder = $this->appData->getFolder($app);
  78. } catch(NotFoundException $e) {
  79. // creating css appdata folder
  80. $folder = $this->appData->newFolder($app);
  81. }
  82. if($this->isCached($fileNameCSS, $fileNameSCSS, $folder, $path)) {
  83. return true;
  84. }
  85. return $this->cache($path, $fileNameCSS, $fileNameSCSS, $folder, $webDir);
  86. }
  87. /**
  88. * Check if the file is cached or not
  89. * @param string $fileNameCSS
  90. * @param string $fileNameSCSS
  91. * @param ISimpleFolder $folder
  92. * @param string $path
  93. * @return boolean
  94. */
  95. private function isCached($fileNameCSS, $fileNameSCSS, ISimpleFolder $folder, $path) {
  96. try {
  97. $cachedFile = $folder->getFile($fileNameCSS);
  98. if ($cachedFile->getSize() > 0) {
  99. $depFile = $folder->getFile($fileNameCSS . '.deps');
  100. $deps = json_decode($depFile->getContent(), true);
  101. foreach ($deps as $file=>$mtime) {
  102. if (!file_exists($file) || filemtime($file) > $mtime) {
  103. return false;
  104. }
  105. }
  106. }
  107. return true;
  108. } catch(NotFoundException $e) {
  109. return false;
  110. }
  111. }
  112. /**
  113. * Cache the file with AppData
  114. * @param string $path
  115. * @param string $fileNameCSS
  116. * @param string $fileNameSCSS
  117. * @param ISimpleFolder $folder
  118. * @param string $webDir
  119. * @return boolean
  120. */
  121. private function cache($path, $fileNameCSS, $fileNameSCSS, ISimpleFolder $folder, $webDir) {
  122. $scss = new Compiler();
  123. $scss->setImportPaths([
  124. $path,
  125. \OC::$SERVERROOT . '/core/css/',
  126. ]);
  127. if($this->config->getSystemValue('debug')) {
  128. // Debug mode
  129. $scss->setFormatter(Expanded::class);
  130. $scss->setLineNumberStyle(Compiler::LINE_COMMENTS);
  131. } else {
  132. // Compression
  133. $scss->setFormatter(Crunched::class);
  134. }
  135. try {
  136. $cachedfile = $folder->getFile($fileNameCSS);
  137. } catch(NotFoundException $e) {
  138. $cachedfile = $folder->newFile($fileNameCSS);
  139. }
  140. $depFileName = $fileNameCSS . '.deps';
  141. try {
  142. $depFile = $folder->getFile($depFileName);
  143. } catch (NotFoundException $e) {
  144. $depFile = $folder->newFile($depFileName);
  145. }
  146. // Compile
  147. try {
  148. $compiledScss = $scss->compile(
  149. '@import "variables.scss";' .
  150. '@import "'.$fileNameSCSS.'";');
  151. } catch(ParserException $e) {
  152. $this->logger->error($e, ['app' => 'core']);
  153. return false;
  154. }
  155. try {
  156. $cachedfile->putContent($this->rebaseUrls($compiledScss, $webDir));
  157. $depFile->putContent(json_encode($scss->getParsedFiles()));
  158. $this->logger->debug($webDir.'/'.$fileNameSCSS.' compiled and successfully cached', ['app' => 'core']);
  159. return true;
  160. } catch(NotPermittedException $e) {
  161. return false;
  162. }
  163. }
  164. /**
  165. * Add the correct uri prefix to make uri valid again
  166. * @param string $css
  167. * @param string $webDir
  168. * @return string
  169. */
  170. private function rebaseUrls($css, $webDir) {
  171. $re = '/url\([\'"]([\.\w?=\/-]*)[\'"]\)/x';
  172. // OC\Route\Router:75
  173. if(($this->config->getSystemValue('htaccess.IgnoreFrontController', false) === true || getenv('front_controller_active') === 'true')) {
  174. $subst = 'url(\'../../'.$webDir.'/$1\')';
  175. } else {
  176. $subst = 'url(\'../../../'.$webDir.'/$1\')';
  177. }
  178. return preg_replace($re, $subst, $css);
  179. }
  180. /**
  181. * Return the cached css file uri
  182. * @param string $appName the app name
  183. * @param string $fileName
  184. * @return string
  185. */
  186. public function getCachedSCSS($appName, $fileName) {
  187. $tmpfileLoc = explode('/', $fileName);
  188. $fileName = array_pop($tmpfileLoc);
  189. $fileName = str_replace('.scss', '.css', $fileName);
  190. return substr($this->urlGenerator->linkToRoute('core.Css.getCss', array('fileName' => $fileName, 'appName' => $appName)), strlen(\OC::$WEBROOT) + 1);
  191. }
  192. }