Commits

Anonymous committed 3d623c9

Introduce ImportCssTask for Phing

Comments (0)

Files changed (1)

phing/tasks/ext/ImportCssTask.php

+<?php
+require_once 'phing/Task.php';
+
+class ImportCssTask extends Task
+{
+    protected $_basedir = '';
+    protected $_filesets = array();
+
+    public function setBasedir($basedir)
+    {
+    	$r = realpath($basedir);
+    	if (false === $r) {
+            $this->_basedir = $basedir;
+    	} else {
+            $this->_basedir = $r;
+    	}
+    	return $this;
+    }
+
+    public function getBasedir()
+    {
+        return $this->_basedir;
+    }
+
+    public function createFileSet()
+    {
+        $num = array_push($this->_filesets, new FileSet());
+        return $this->_filesets[$num-1];
+    }
+
+    public function main()
+    {
+        $project = $this->getProject();
+        foreach ($this->_filesets as $fs) {
+            $dir = $fs->getDir($this->project)->getPath();
+            $files = $fs->getDirectoryScanner($project)->getIncludedFiles();
+
+            foreach ($files as $file) {
+                $this->_importCss($dir . DIRECTORY_SEPARATOR . $file);
+            }
+        }
+    }
+
+    protected function _importCss($file)
+    {
+        $contents = file_get_contents($file);
+        $contents = preg_replace_callback(
+            '#@IMPORT url\(\'(.+?)\'\);#si',
+            array($this, '_processCallback'),
+            $contents
+        );
+        file_put_contents($file, $contents);
+    }
+
+    public function _processCallback($matches)
+    {
+        if ('/' == $matches[1][0]) {
+            $file = $this->getBasedir() . $matches[1];
+    	} else {
+            $file = $matches[1];
+    	}
+        return file_get_contents($file);
+    }
+}