Skip to content

Commit f9431a4

Browse files
committed
internals: add debug formatter
1 parent 18773cc commit f9431a4

File tree

8 files changed

+130
-23
lines changed

8 files changed

+130
-23
lines changed

scss.inc.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
include_once __DIR__ . '/src/Formatter/Compact.php';
1212
include_once __DIR__ . '/src/Formatter/Compressed.php';
1313
include_once __DIR__ . '/src/Formatter/Crunched.php';
14+
include_once __DIR__ . '/src/Formatter/Debug.php';
1415
include_once __DIR__ . '/src/Formatter/Expanded.php';
1516
include_once __DIR__ . '/src/Formatter/Nested.php';
1617
include_once __DIR__ . '/src/Parser.php';

src/Compiler.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1303,11 +1303,13 @@ protected function compileChild($child, $out)
13031303
}
13041304

13051305
$this->storeEnv = $content->scope;
1306+
//$this->pushEnv();
13061307

13071308
foreach ($content->children as $child) {
13081309
$this->compileChild($child, $out);
13091310
}
13101311

1312+
//$this->popEnv();
13111313
$this->storeEnv = null;
13121314

13131315
break;
@@ -2734,9 +2736,9 @@ public function findImport($url)
27342736
if (is_string($dir)) {
27352737
// check urls for normal import paths
27362738
foreach ($urls as $full) {
2737-
$full = $dir .
2738-
(! empty($dir) && substr($dir, -1) !== '/' ? '/' : '') .
2739-
$full;
2739+
$full = $dir
2740+
. (! empty($dir) && substr($dir, -1) !== '/' ? '/' : '')
2741+
. $full;
27402742

27412743
if ($this->fileExists($file = $full . '.scss') ||
27422744
$this->fileExists($file = $full)

src/Formatter.php

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -92,19 +92,35 @@ public function stripSemicolon(&$lines)
9292
/**
9393
* Output lines inside a block
9494
*
95-
* @param string $inner
9695
* @param \stdClass $block
9796
*/
98-
protected function blockLines($inner, $block)
97+
protected function blockLines($block)
9998
{
99+
$inner = $this->indentStr();
100+
100101
$glue = $this->break . $inner;
102+
101103
echo $inner . implode($glue, $block->lines);
102104

103105
if (! empty($block->children)) {
104106
echo $this->break;
105107
}
106108
}
107109

110+
/**
111+
* Output block selectors
112+
*
113+
* @param \stdClass $block
114+
*/
115+
protected function blockSelectors($block)
116+
{
117+
$inner = $this->indentStr();
118+
119+
echo $inner
120+
. implode($this->tagSeparator, $block->selectors)
121+
. $this->open . $this->break;
122+
}
123+
108124
/**
109125
* Output non-empty block
110126
*
@@ -116,20 +132,16 @@ protected function block($block)
116132
return;
117133
}
118134

119-
$inner = $pre = $this->indentStr();
135+
$pre = $this->indentStr();
120136

121137
if (! empty($block->selectors)) {
122-
echo $pre
123-
. implode($this->tagSeparator, $block->selectors)
124-
. $this->open . $this->break;
138+
$this->blockSelectors($block);
125139

126140
$this->indentLevel++;
127-
128-
$inner = $this->indentStr();
129141
}
130142

131143
if (! empty($block->lines)) {
132-
$this->blockLines($inner, $block);
144+
$this->blockLines($block);
133145
}
134146

135147
foreach ($block->children as $child) {

src/Formatter/Compressed.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,10 @@ public function stripSemicolon(&$lines)
5757
/**
5858
* {@inheritdoc}
5959
*/
60-
public function blockLines($inner, $block)
60+
public function blockLines($block)
6161
{
62+
$inner = $this->indentStr();
63+
6264
$glue = $this->break . $inner;
6365

6466
foreach ($block->lines as $index => $line) {

src/Formatter/Crunched.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,10 @@ public function stripSemicolon(&$lines)
5757
/**
5858
* {@inheritdoc}
5959
*/
60-
public function blockLines($inner, $block)
60+
public function blockLines($block)
6161
{
62+
$inner = $this->indentStr();
63+
6264
$glue = $this->break . $inner;
6365

6466
foreach ($block->lines as $index => $line) {

src/Formatter/Debug.php

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
<?php
2+
/**
3+
* SCSSPHP
4+
*
5+
* @copyright 2012-2015 Leaf Corcoran
6+
*
7+
* @license http://opensource.org/licenses/MIT MIT
8+
*
9+
* @link http://leafo.github.io/scssphp
10+
*/
11+
12+
namespace Leafo\ScssPhp\Formatter;
13+
14+
use Leafo\ScssPhp\Formatter;
15+
16+
/**
17+
* SCSS debug formatter
18+
*
19+
* @author Anthon Pang <anthon.pang@gmail.com>
20+
*/
21+
class Debug extends Formatter
22+
{
23+
/**
24+
* {@inheritdoc}
25+
*/
26+
public function __construct()
27+
{
28+
$this->indentLevel = 0;
29+
$this->indentChar = '';
30+
$this->break = "\n";
31+
$this->open = ' {';
32+
$this->close = ' }';
33+
$this->tagSeparator = ', ';
34+
$this->assignSeparator = ': ';
35+
}
36+
37+
/**
38+
* {@inheritdoc}
39+
*/
40+
protected function blockLines($block)
41+
{
42+
foreach ($block->lines as $index => $line) {
43+
echo "block->lines[{$index}]: $line\n";
44+
}
45+
}
46+
47+
/**
48+
* {@inheritdoc}
49+
*/
50+
protected function blockSelectors($block)
51+
{
52+
foreach ($block->selectors as $index => $selector) {
53+
echo "block->selectors[{$index}]: $selector\n";
54+
}
55+
}
56+
57+
/**
58+
* {@inheritdoc}
59+
*/
60+
protected function block($block)
61+
{
62+
echo "block->type: {$block->type}\n" .
63+
"block->depth: {$block->depth}\n";
64+
65+
if (! empty($block->selectors)) {
66+
$this->blockSelectors($block);
67+
}
68+
69+
if (! empty($block->lines)) {
70+
$this->blockLines($block);
71+
}
72+
73+
foreach ($block->children as $i => $child) {
74+
$this->block($child);
75+
}
76+
}
77+
}

src/Formatter/Expanded.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,10 @@ public function __construct()
3737
/**
3838
* {@inheritdoc}
3939
*/
40-
protected function blockLines($inner, $block)
40+
protected function blockLines($block)
4141
{
42+
$inner = $this->indentStr();
43+
4244
$glue = $this->break . $inner;
4345

4446
foreach ($block->lines as $index => $line) {

src/Formatter/Nested.php

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,10 @@ public function __construct()
3737
/**
3838
* {@inheritdoc}
3939
*/
40-
protected function blockLines($inner, $block)
40+
protected function blockLines($block)
4141
{
42+
$inner = $this->indentStr($block->depth - 1);
43+
4244
$glue = $this->break . $inner;
4345

4446
foreach ($block->lines as $index => $line) {
@@ -54,6 +56,18 @@ protected function blockLines($inner, $block)
5456
}
5557
}
5658

59+
/**
60+
* {@inheritdoc}
61+
*/
62+
protected function blockSelectors($block)
63+
{
64+
$inner = $this->indentStr($block->depth - 1);
65+
66+
echo $inner
67+
. implode($this->tagSeparator, $block->selectors)
68+
. $this->open . $this->break;
69+
}
70+
5771
/**
5872
* {@inheritdoc}
5973
*/
@@ -63,19 +77,14 @@ protected function block($block)
6377
$this->adjustAllChildren($block);
6478
}
6579

66-
$inner = $pre = $this->indentStr($block->depth - 1);
6780
if (! empty($block->selectors)) {
68-
echo $pre .
69-
implode($this->tagSeparator, $block->selectors) .
70-
$this->open . $this->break;
81+
$this->blockSelectors($block);
7182

7283
$this->indentLevel++;
73-
74-
$inner = $this->indentStr($block->depth - 1);
7584
}
7685

7786
if (! empty($block->lines)) {
78-
$this->blockLines($inner, $block);
87+
$this->blockLines($block);
7988
}
8089

8190
foreach ($block->children as $i => $child) {

0 commit comments

Comments
 (0)