Example #1
0
 /**
  * @param string $in    Filename without path or extension.
  * @return bool         True if the output file was saved.
  */
 public function compileFile($in)
 {
     $gantry = Gantry::instance();
     /** @var UniformResourceLocator $locator */
     $locator = $gantry['locator'];
     $out = $this->getCssUrl($in);
     $path = $locator->findResource($out, true, true);
     $paths = $locator->mergeResources($this->paths);
     // Set the lookup paths.
     $this->compiler->setBasePath($path);
     $this->compiler->setImportDir($paths);
     $this->compiler->setFormatter('lessjs');
     // Run the compiler.
     $this->compiler->setVariables($this->getVariables());
     $css = $this->compiler->compileFile($in . '.less"');
     $file = File::instance($path);
     // Attempt to lock the file for writing.
     $file->lock(false);
     //TODO: Better way to handle double writing files at same time.
     if ($file->locked() === false) {
         // File was already locked by another process.
         return false;
     }
     $file->save($css);
     $file->unlock();
     $this->createMeta($out, md5($css));
     return true;
 }
Example #2
0
    /**
     * @param string $in    Filename without path or extension.
     * @return bool         True if the output file was saved.
     */
    public function compileFile($in)
    {
        // Buy some extra time as compilation may take a lot of time in shared environments.
        @set_time_limit(30);
        ob_start();
        $gantry = Gantry::instance();
        /** @var UniformResourceLocator $locator */
        $locator = $gantry['locator'];
        $out = $this->getCssUrl($in);
        $path = $locator->findResource($out, true, true);
        $file = File::instance($path);
        // Attempt to lock the file for writing.
        try {
            $file->lock(false);
        } catch (\Exception $e) {
            // Another process has locked the file; we will check this in a bit.
        }
        if ($file->locked() === false) {
            // File was already locked by another process, lets avoid compiling the same file twice.
            return false;
        }
        $paths = $locator->mergeResources($this->paths);
        // Set the lookup paths.
        $this->compiler->setBasePath($path);
        $this->compiler->setImportDir($paths);
        // Run the compiler.
        $this->compiler->setVariables($this->getVariables());
        $css = $this->compiler->compileFile($in . '.less"');
        $warnings = trim(ob_get_clean());
        if ($warnings) {
            $this->warnings[$in] = explode("\n", $warnings);
        }
        if (!$this->production) {
            $warning = <<<WARN
/* GANTRY5 DEVELOPMENT MODE ENABLED.

   WARNING: This file is automatically generated by Gantry5. Any modifications to this file will be lost!

   For more information on modifying CSS, please read:

   http://docs.gantry.org/gantry5/configure/styles
   http://docs.gantry.org/gantry5/tutorials/adding-a-custom-style-sheet
 */
WARN;
            $css = $warning . "\n\n" . $css;
        }
        $file->save($css);
        $file->unlock();
        $file->free();
        $this->createMeta($out, md5($css));
        return true;
    }