/** * Compile a LESS file into CSS and add it to the page generated by the CMS. * This method has integrated cache support. The compiled LESS files will be * written to the media/lib_fof/compiled directory of your site. If the file * cannot be written we will use the $altPath, if specified * * @param string $path A path definition understood by parsePath pointing to the source LESS file, * e.g. media://com_example/less/foo.less * @param string $altPath A path definition understood by parsePath pointing to a precompiled CSS file, * used when we can't write the generated file to the output directory, * e.g. media://com_example/css/foo.css * @param boolean $returnPath Return the URL of the generated CSS file but do not include it. If it can't be * generated, false is returned and the alt files are not included * @param string $version (optional) Version string to be added to the URL * @param string $type MIME type of the stylesheeet * @param string $media Media target definition of the style sheet, e.g. "screen" * @param array $attribs Array of attributes * * @see self::parsePath * * @since 2.0 * * @return mixed True = successfully included generated CSS, False = the alternate CSS file was used, null = the source file does not exist */ public function addLESS($path, $altPath = null, $returnPath = false, $version = null, $type = 'text/css', $media = null, $attribs = array()) { // Does the cache directory exists and is writeable static $sanityCheck = null; // Get the local LESS file $localFile = $this->parsePath($path, true); $filesystem = $this->container->filesystem; $platformDirs = $this->container->platform->getPlatformBaseDirs(); if (is_null($sanityCheck)) { // Make sure the cache directory exists if (!is_dir($platformDirs['public'] . '/media/lib_fof/compiled/')) { $sanityCheck = $filesystem->folderCreate($platformDirs['public'] . '/media/lib_fof/compiled/'); } else { $sanityCheck = true; } } // No point continuing if the source file is not there or we can't write to the cache if (!$sanityCheck || !is_file($localFile)) { if (!$returnPath) { if (is_string($altPath)) { $this->addCSS($altPath, $version, $type, $media, $attribs); } elseif (is_array($altPath)) { foreach ($altPath as $anAltPath) { $this->addCSS($anAltPath, $version, $type, $media, $attribs); } } } return false; } // Get the source file's unique ID $id = md5(filemtime($localFile) . filectime($localFile) . $localFile); // Get the cached file path $cachedPath = $platformDirs['public'] . '/media/lib_fof/compiled/' . $id . '.css'; // Get the LESS compiler $lessCompiler = new Less(); $lessCompiler->formatterName = 'compressed'; // Should I add an alternative import path? $altFiles = $this->getAltPaths($path); if (isset($altFiles['alternate'])) { $currentLocation = realpath(dirname($localFile)); $normalLocation = realpath(dirname($altFiles['normal'])); $alternateLocation = realpath(dirname($altFiles['alternate'])); if ($currentLocation == $normalLocation) { $lessCompiler->importDir = array($alternateLocation, $currentLocation); } else { $lessCompiler->importDir = array($currentLocation, $normalLocation); } } // Compile the LESS file $lessCompiler->checkedCompile($localFile, $cachedPath); // Add the compiled CSS to the page $base_url = rtrim($this->container->platform->URIbase(), '/'); if (substr($base_url, -14) == '/administrator') { $base_url = substr($base_url, 0, -14); } $url = $base_url . '/media/lib_fof/compiled/' . $id . '.css'; if ($returnPath) { return $url; } else { $this->addCSS($url, $version, $type, $media, $attribs); return true; } }
/** * Advance counter to next occurrence of $what * $until - don't include $what in advance * $allowNewline, if string, will be used as valid char set * * @param [type] $what [description] * @param [type] &$out [description] * @param boolean $until [description] * @param boolean $allowNewline [description] * * @return boolean */ protected function to($what, &$out, $until = false, $allowNewline = false) { if (is_string($allowNewline)) { $validChars = $allowNewline; } else { $validChars = $allowNewline ? "." : "[^\n]"; } if (!$this->match('(' . $validChars . '*?)' . Less::preg_quote($what), $m, !$until)) { return false; } if ($until) { // Give back $what $this->count -= strlen($what); } $out = $m[1]; return true; }