/
cli.php
159 lines (127 loc) · 4.26 KB
/
cli.php
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
<?
/**
* tool for command line
*/
class tool {
var $argc = '',
$argv = '',
$sort = null,
$in = null,
$out = null;
/**
* print man
*/
function man(){
?>
CSSComb 2.11 (build c6abf31-1211171628) Command line tool for resort CSS code.
SYNOPSIS
$ php <?php echo $this->argv[0]; ?> -s <file with JSON array> -i <path to input css file> -o <path to result css file>
DESCRIPTION
options:
-s, --sort-order specify file with custom sort order. File must contain JSON data. For detail information run '<?php echo $this->argv[0]; ?> --help-sort-order'
-i, --input must be directory name or file that needs to be sorted
-o, --output sort result file. Use output when input is not a directory name, otherwise output param ignored. If output filename does not exist, the file is created. Otherwise, the existing file is overwritten.
--help, -help, -h, -? or no options will print this man.
EXAMPLE
<?php echo $this->argv[0]; ?> -s my-custom-sort-order.json -i css/style.css -o css/style-resorted.css
EXAMPLE 2
<?php echo $this->argv[0]; ?> -i some_directory_name
SEE ALSO
http://csscomb.com/
<?php
}
/**
* init
*/
function init($argc, $argv){
$this->argc = $argc;
$this->argv = $argv;
//echo "argc: \n".$this->argc."\n\n";
//echo "argv: \n"; var_dump($this->argv); echo "\n\n";
for ($i = 0; $i < $this->argc; $i++) {
switch ($this->argv[$i]) {
case '-s':
$this->sort = $this->argv[$i+1];
//echo "sort order from: ".$this->sort."\n";
break;
case '-i':
$this->in = $this->argv[$i+1];
//echo "input file: ".$this->in."\n";
break;
case '-o':
$this->out = $this->argv[$i+1];
//echo "output file: ".$this->out."\n";
break;
}
}
}
function tool($argc, $argv){
$this->init($argc, $argv);
if($this->argc == 1 || in_array($this->argv[1], array('--help', '-help', '-h', '-?'))) {
$this->man();
exit(1);
}
else {
if($this->in != null) {
$c = new csscomb('', false, 'yandex');
if(is_dir($this->in)) {
$files = $this->getArrayOfCssFilenames($this->in);
foreach($files as $file) {
echo "Sorting ".$file."...\n";
$result = $c->csscomb(file_get_contents($file));
file_put_contents($file, $result);
}
}
elseif($this->out != null){
echo "Sorting ".$this->in."...\n";
if (preg_match('/^text/', mime_content_type($this->in))) {
$result = $c->csscomb(file_get_contents($this->in));
file_put_contents($this->out, $result);
} else {
echo("Wrong input file mime type.");
exit(1);
}
}
echo "Done.\n";
exit(0);
} else {
echo "No input file\n";
$this->man();
exit(1);
}
}
}
/**
* Возвращает массив с путями до CSS-файлов
* @param: $dir директория, где искать
* @param: $extensions массив с расширениями файлов, в которых CSS-код.
*/
function getArrayOfCssFilenames($dir, $extensions = Array('.css','.sass','.less','.scss')) {
$files = Array();
if($handle = opendir($dir)) {
while(false !== ($filename = readdir($handle))) {
if($filename != '.' && $filename != '..') {
if(is_dir($dir.'/'.$filename)) {
$files = array_merge($files, $this->getArrayOfCssFilenames($dir.'/'.$filename, $extensions));
}
else {
$pos = strrpos($filename, '.');
$ext = substr($filename, $pos, strlen($filename) - $pos);
if($extensions) {
if(in_array($ext, $extensions)) {
$files[] = $dir.'/'.$filename;
}
}
else {
$files[] = $dir.'/'.$filename;
}
}
}
}
closedir($handle);
}
return $files;
}
}
$tool = new tool($argc, $argv);
?>