1 <?php
2 /**
3 * @package FrameworkOnFramework
4 * @subpackage less
5 * @copyright Copyright (C) 2010-2016 Nicholas K. Dionysopoulos / Akeeba Ltd. All rights reserved.
6 * @license GNU General Public License version 2 or later; see LICENSE.txt
7 */
8 // Protect from unauthorized access
9 defined('FOF_INCLUDED') or die;
10
11 /**
12 * This class is taken verbatim from:
13 *
14 * lessphp v0.3.9
15 * http://leafo.net/lessphp
16 *
17 * LESS css compiler, adapted from http://lesscss.org
18 *
19 * Copyright 2012, Leaf Corcoran <leafot@gmail.com>
20 * Licensed under MIT or GPLv3, see LICENSE
21 *
22 * @package FrameworkOnFramework
23 * @since 2.0
24 */
25 class FOFLessFormatterClassic
26 {
27 public $indentChar = " ";
28
29 public $break = "\n";
30
31 public $open = " {";
32
33 public $close = "}";
34
35 public $selectorSeparator = ", ";
36
37 public $assignSeparator = ":";
38
39 public $openSingle = " { ";
40
41 public $closeSingle = " }";
42
43 public $disableSingle = false;
44
45 public $breakSelectors = false;
46
47 public $compressColors = false;
48
49 /**
50 * Public constructor
51 */
52 public function __construct()
53 {
54 $this->indentLevel = 0;
55 }
56
57 /**
58 * Indent a string by $n positions
59 *
60 * @param integer $n How many positions to indent
61 *
62 * @return string The indented string
63 */
64 public function indentStr($n = 0)
65 {
66 return str_repeat($this->indentChar, max($this->indentLevel + $n, 0));
67 }
68
69 /**
70 * Return the code for a property
71 *
72 * @param string $name The name of the porperty
73 * @param string $value The value of the porperty
74 *
75 * @return string The CSS code
76 */
77 public function property($name, $value)
78 {
79 return $name . $this->assignSeparator . $value . ";";
80 }
81
82 /**
83 * Is a block empty?
84 *
85 * @param stdClass $block The block to check
86 *
87 * @return boolean True if the block has no lines or children
88 */
89 protected function isEmpty($block)
90 {
91 if (empty($block->lines))
92 {
93 foreach ($block->children as $child)
94 {
95 if (!$this->isEmpty($child))
96 {
97 return false;
98 }
99 }
100
101 return true;
102 }
103
104 return false;
105 }
106
107 /**
108 * Output a CSS block
109 *
110 * @param stdClass $block The block definition to output
111 *
112 * @return void
113 */
114 public function block($block)
115 {
116 if ($this->isEmpty($block))
117 {
118 return;
119 }
120
121 $inner = $pre = $this->indentStr();
122
123 $isSingle = !$this->disableSingle &&
124 is_null($block->type) && count($block->lines) == 1;
125
126 if (!empty($block->selectors))
127 {
128 $this->indentLevel++;
129
130 if ($this->breakSelectors)
131 {
132 $selectorSeparator = $this->selectorSeparator . $this->break . $pre;
133 }
134 else
135 {
136 $selectorSeparator = $this->selectorSeparator;
137 }
138
139 echo $pre .
140 implode($selectorSeparator, $block->selectors);
141
142 if ($isSingle)
143 {
144 echo $this->openSingle;
145 $inner = "";
146 }
147 else
148 {
149 echo $this->open . $this->break;
150 $inner = $this->indentStr();
151 }
152 }
153
154 if (!empty($block->lines))
155 {
156 $glue = $this->break . $inner;
157 echo $inner . implode($glue, $block->lines);
158
159 if (!$isSingle && !empty($block->children))
160 {
161 echo $this->break;
162 }
163 }
164
165 foreach ($block->children as $child)
166 {
167 $this->block($child);
168 }
169
170 if (!empty($block->selectors))
171 {
172 if (!$isSingle && empty($block->children))
173 {
174 echo $this->break;
175 }
176
177 if ($isSingle)
178 {
179 echo $this->closeSingle . $this->break;
180 }
181 else
182 {
183 echo $pre . $this->close . $this->break;
184 }
185
186 $this->indentLevel--;
187 }
188 }
189 }
190