/** * Less process file. * * @param string $source ("Example: MyPlugin.path/to/file.less") * @param bool|false $force * @throws \JBZoo\Less\Exception * @return string */ public function process($source, $force = false) { $file = $this->_path->isVirtual($source) ? $this->_path->get($source) : $this->_path->get('root:' . $source); try { if ($force) { $this->config('force', true); } $less = new Less($this->_config); if (!FS::isFile($file)) { return $source; } list($source, $isExpired) = $less->compile($file); if ($isExpired) { $cacheId = FS::firstLine($source); $comment = '/* resource: ' . $this->_path->url($file) . ' */' . PHP_EOL; $fileHead = implode('', [$cacheId, Str::low($comment)]); $css = $this->_normalizeContent($source, $fileHead); $this->_write($source, $css); } $source = $this->_path->url(FS::clean($source, '/')); return $source; } catch (InternalErrorException $e) { throw new InternalErrorException($e->getMessage()); } }
/** * Check is current cache is expired */ public function isExpired() { if (!FS::isFile($this->_resultFile)) { return true; } $fileAge = abs(time() - filemtime($this->_resultFile)); if ($fileAge >= $this->_cache_ttl) { return true; } $firstLine = trim(FS::firstLine($this->_resultFile)); $expected = trim($this->_getHeader()); if ($expected === $firstLine) { return false; } return true; }
public function testFirstLine() { isContain('<?php', FS::firstLine(__FILE__)); isNull(FS::firstLine(__FILE__ . '_noexists')); }