/**
  * 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 fancy path definition understood by parsePath pointing to the source LESS file
  * @param   string  $altPath     A fancy path definition understood by parsePath pointing to a precompiled CSS file, used when we can't write the generated file to the output directory
  * @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
  *
  * @return  mixed  True = successfully included generated CSS, False = the alternate CSS file was used, null = the source file does not exist
  *
  * @see FOFTemplateUtils::parsePath
  *
  * @since 2.0
  */
 public static function addLESS($path, $altPath = null, $returnPath = false)
 {
     // Does the cache directory exists and is writeable
     static $sanityCheck = null;
     // Get the local LESS file
     $localFile = self::parsePath($path, true);
     JLoader::import('joomla.filesystem.folder');
     if (is_null($sanityCheck)) {
         // Make sure the cache directory exists
         if (!is_dir(JPATH_SITE . '/media/lib_fof/compiled/')) {
             $sanityCheck = JFolder::create(JPATH_SITE . '/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)) {
                 self::addCSS($altPath);
             } elseif (is_array($altPath)) {
                 foreach ($altPath as $anAltPath) {
                     self::addCSS($anAltPath);
                 }
             }
         }
         return false;
     }
     // Get the source file's unique ID
     $id = md5(filemtime($localFile) . filectime($localFile) . $localFile);
     // Get the cached file path
     $cachedPath = JPATH_SITE . '/media/lib_fof/compiled/' . $id . '.css';
     // Get the LESS compiler
     $lessCompiler = new FOFLess();
     $lessCompiler->formatterName = 'compressed';
     // Should I add an alternative import path?
     $altFiles = self::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->compileFile($localFile, $cachedPath);
     $lessCompiler->checkedCompile($localFile, $cachedPath);
     // Add the compiled CSS to the page
     $base_url = rtrim(JUri::base(), '/');
     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 {
         JFactory::getDocument()->addStyleSheet($url);
         return true;
     }
 }
Example #2
0
 /**
  * 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 fancy path definition understood by parsePath pointing to the source LESS file
  * @param   string   $altPath     A fancy path definition understood by parsePath pointing to a precompiled CSS file,
  *                                used when we can't write the generated file to the output directory
  * @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
  *
  * @see FOFTemplateUtils::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 static function addLESS($path, $altPath = null, $returnPath = false)
 {
     // Does the cache directory exists and is writeable
     static $sanityCheck = null;
     // Get the local LESS file
     $localFile = self::parsePath($path, true);
     $filesystem = FOFPlatform::getInstance()->getIntegrationObject('filesystem');
     $platformDirs = FOFPlatform::getInstance()->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)) {
                 self::addCSS($altPath);
             } elseif (is_array($altPath)) {
                 foreach ($altPath as $anAltPath) {
                     self::addCSS($anAltPath);
                 }
             }
         }
         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 FOFLess();
     $lessCompiler->formatterName = 'compressed';
     // Should I add an alternative import path?
     $altFiles = self::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(FOFPlatform::getInstance()->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 {
         $document = FOFPlatform::getInstance()->getDocument();
         if ($document instanceof JDocument) {
             if (method_exists($document, 'addStyleSheet')) {
                 $document->addStyleSheet($url);
             }
         }
         return true;
     }
 }