class LessphpFilter

Loads LESS files using the PHP implementation of less, lessphp.

Less files are mostly compatible, but there are slight differences.

@link http://leafo.net/lessphp/

@author David Buchmann <david@liip.ch> @author Kris Wallsmith <kris.wallsmith@gmail.com>

Hierarchy

Expanded class hierarchy of LessphpFilter

File

drupal/core/vendor/kriswallsmith/assetic/src/Assetic/Filter/LessphpFilter.php, line 28

Namespace

Assetic\Filter
View source
class LessphpFilter implements DependencyExtractorInterface {
  private $presets = array();
  private $formatter;
  private $preserveComments;

  /**
   * Lessphp Load Paths
   *
   * @var array
   */
  protected $loadPaths = array();

  /**
   * Adds a load path to the paths used by lessphp
   *
   * @param string $path Load Path
   */
  public function addLoadPath($path) {
    $this->loadPaths[] = $path;
  }

  /**
   * Sets load paths used by lessphp
   *
   * @param array $loadPaths Load paths
   */
  public function setLoadPaths(array $loadPaths) {
    $this->loadPaths = $loadPaths;
  }
  public function setPresets(array $presets) {
    $this->presets = $presets;
  }

  /**
   * @param string $formatter One of "lessjs", "compressed", or "classic".
   */
  public function setFormatter($formatter) {
    $this->formatter = $formatter;
  }

  /**
   * @param boolean $preserveComments
   */
  public function setPreserveComments($preserveComments) {
    $this->preserveComments = $preserveComments;
  }
  public function filterLoad(AssetInterface $asset) {
    $root = $asset
      ->getSourceRoot();
    $path = $asset
      ->getSourcePath();
    $lc = new \lessc();
    if ($root && $path) {
      $lc->importDir = dirname($root . '/' . $path);
    }
    foreach ($this->loadPaths as $loadPath) {
      $lc
        ->addImportDir($loadPath);
    }
    if ($this->formatter) {
      $lc
        ->setFormatter($this->formatter);
    }
    if (null !== $this->preserveComments) {
      $lc
        ->setPreserveComments($this->preserveComments);
    }
    $asset
      ->setContent($lc
      ->parse($asset
      ->getContent(), $this->presets));
  }
  public function filterDump(AssetInterface $asset) {
  }
  public function getChildren(AssetFactory $factory, $content, $loadPath = null) {
    $loadPaths = $this->loadPaths;
    if (null !== $loadPath) {
      $loadPaths[] = $loadPath;
    }
    if (empty($loadPaths)) {
      return array();
    }
    $children = array();
    foreach (LessUtils::extractImports($content) as $reference) {
      if ('.css' === substr($reference, -4)) {

        // skip normal css imports
        // todo: skip imports with media queries
        continue;
      }
      if ('.less' !== substr($reference, -5)) {
        $reference .= '.less';
      }
      foreach ($loadPaths as $loadPath) {
        if (file_exists($file = $loadPath . '/' . $reference)) {
          $coll = $factory
            ->createAsset($file, array(), array(
            'root' => $loadPath,
          ));
          foreach ($coll as $leaf) {
            $leaf
              ->ensureFilter($this);
            $children[] = $leaf;
            goto next_reference;
          }
        }
      }
      next_reference:
    }
    return $children;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
LessphpFilter::$formatter private property
LessphpFilter::$loadPaths protected property Lessphp Load Paths
LessphpFilter::$preserveComments private property
LessphpFilter::$presets private property
LessphpFilter::addLoadPath public function Adds a load path to the paths used by lessphp
LessphpFilter::filterDump public function Filters an asset just before it's dumped. Overrides FilterInterface::filterDump
LessphpFilter::filterLoad public function Filters an asset after it has been loaded. Overrides FilterInterface::filterLoad
LessphpFilter::getChildren public function Returns child assets. Overrides DependencyExtractorInterface::getChildren
LessphpFilter::setFormatter public function
LessphpFilter::setLoadPaths public function Sets load paths used by lessphp
LessphpFilter::setPreserveComments public function
LessphpFilter::setPresets public function