-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJLessCompiler.php
More file actions
99 lines (84 loc) · 3.67 KB
/
JLessCompiler.php
File metadata and controls
99 lines (84 loc) · 3.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
<?php
namespace JLess;
/**
* JLessCompiler class file.
* @author João Parreira <joaofrparreira@gmail.com>
* @copyright Copyright © 2012, João Parreira
* @license http://www.opensource.org/licenses/bsd-license.php New BSD License
* This extension is inspired in LessCompiler made by Christoffer Niska <ChristofferNiska@gmail.com>
*/
/**
* JLessCompiler Parent class for compilers
*/
class JLessCompiler {
protected $originalPath = ''; //Original path defined by user
protected $originalFullPath = ''; //Full path for original file
protected $destinationPath; //Destination path to resultant file
protected $destinationFullPath; //Full path to resultant file
protected $cssString; //String with css code parsed
protected $compiler; //Compiler Handler
private $_options = array( //Default options to all compilers
'forceCompile' => true,
);
protected $options;
public function __construct($options) {
$this->options = \CMap::mergeArray($this->_options, $options);
//Detects if is loaded a less file and define the path's necessary
if ($this->options['type'] == 'file') {
$this->originalPath = $this->options['url'];
$this->originalFullPath = $this->options['subfolder'] ? dirname(\Yii::getPathOfAlias('webroot')) . $this->originalPath : \Yii::getPathOfAlias('webroot') . $this->originalPath;
$nomeFicheiro = pathinfo($this->originalPath, PATHINFO_FILENAME);
if ($this->options['path'] != "")
$this->destinationPath = $this->options["path"];
else {
if (!isset($this->options["destination"]))
throw new \CException("Destination folder not chosen");
else
$this->destinationPath = \Yii::app()->getRequest()->getBaseUrl() . $this->options["destination"] . '/' . $nomeFicheiro . '.css';
}
$this->destinationFullPath = $this->options['subfolder'] ? dirname(\Yii::getPathOfAlias('webroot')) . $this->destinationPath : \Yii::getPathOfAlias('webroot') . $this->destinationPath;
}
}
/*
* Parent function for file parser
*/
protected function compileFile() {
return ($this->options['forceCompile'] || $this->hasChanges());
}
/*
* Parent function for string parser
*/
protected function compileString() {
}
/**
* Returns if the file has changes or not by the modified time
* @return boolean the result
*/
private function hasChanges() {
if (!file_exists($this->originalFullPath)) //If original file don't exist it should do nothing in here
throw new \CException(__CLASS__ . ': Failed to compile less file. Source path does not exist.');
if (!file_exists($this->destinationFullPath)) //If file don't exist it should be force compiled
return true;
else {
$statOriginal = stat($this->originalFullPath);
$statDestination = stat($this->destinationFullPath);
if ($statOriginal['mtime'] > $statDestination['mtime']) //If Original file is more recent than Destination file then compile
return true;
}
}
/*
* Returns the path to the resultant css file or the css string
*/
public function getCss() {
switch ($this->options['type']) {
case 'file':
return $this->destinationPath;
case 'string':
return $this->cssString;
default:
throw new \CException(__CLASS__ . ': Invalid type option.');
break;
}
}
}
?>