Example #1
0
 /**
  * @param string $in    Filename without path or extension.
  * @param string $out   Full path to the file to be written.
  * @return bool         True if the output file was saved.
  */
 public function compileFile($in, $out = null)
 {
     $gantry = Gantry::instance();
     /** @var UniformResourceLocator $locator */
     $locator = $gantry['locator'];
     if (!$out) {
         $out = $locator->findResource($this->getCssUrl($in), true, true);
     }
     $paths = $locator->mergeResources($this->paths);
     // Set the lookup paths.
     $this->compiler->setBasePath($out);
     $this->compiler->setImportPaths($paths);
     $this->compiler->setFormatter('scss_formatter_nested');
     // Run the compiler.
     $this->compiler->setVariables($this->getVariables());
     $scss = '@import "' . $in . '.scss"';
     $css = $this->compiler->compile($scss);
     if (strpos($css, $scss) === 0) {
         $css = '/* ' . $scss . ' */';
     }
     $file = File::instance($out);
     // 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();
     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;
        }
        // Set the lookup paths.
        $this->compiler->setBasePath($path);
        $this->compiler->setImportPaths([[$this, 'findImport']]);
        // Run the compiler.
        $this->compiler->setVariables($this->getVariables());
        $scss = '@import "' . $in . '.scss"';
        $css = $this->compiler->compile($scss);
        if (strpos($css, $scss) === 0) {
            $css = '/* ' . $scss . ' */';
        }
        $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;
    }