|
1
|
|
|
<?php |
|
2
|
|
|
namespace gossi\formatter\formatters; |
|
3
|
|
|
|
|
4
|
|
|
use gossi\code\profiles\Profile; |
|
5
|
|
|
use gossi\formatter\parser\Parser; |
|
6
|
|
|
use gossi\formatter\utils\Writer; |
|
7
|
|
|
use phootwork\tokenizer\Token; |
|
8
|
|
|
use phootwork\tokenizer\TokenVisitorInterface; |
|
9
|
|
|
|
|
10
|
|
|
class DelegateFormatter implements TokenVisitorInterface { |
|
11
|
|
|
|
|
12
|
|
|
/** @var Profile */ |
|
13
|
|
|
protected $profile; |
|
14
|
|
|
|
|
15
|
|
|
/** @var Writer */ |
|
16
|
|
|
protected $writer; |
|
17
|
|
|
|
|
18
|
|
|
/** @var Parser */ |
|
19
|
|
|
protected $parser; |
|
20
|
|
|
|
|
21
|
|
|
// formatters |
|
22
|
|
|
private $defaultFormatter; |
|
23
|
|
|
private $commentsFormatter; |
|
24
|
|
|
private $indentationFormatter; |
|
25
|
|
|
private $newlineFormatter; |
|
26
|
|
|
private $whitespaceFormatter; |
|
27
|
|
|
private $blanksFormatter; |
|
28
|
|
|
|
|
29
|
2 |
|
public function __construct(Parser $parser, Profile $profile) { |
|
30
|
2 |
|
$this->profile = $profile; |
|
31
|
2 |
|
$this->parser = $parser; |
|
32
|
2 |
|
$this->writer = new Writer([ |
|
33
|
2 |
|
'indentation_character' => $profile->getIndentation('character') == 'tab' ? "\t" : ' ', |
|
34
|
2 |
|
'indentation_size' => $profile->getIndentation('size') |
|
35
|
|
|
]); |
|
36
|
|
|
|
|
37
|
|
|
// define rules |
|
38
|
2 |
|
$this->defaultFormatter = new DefaultFormatter($parser, $profile, $this->writer); |
|
39
|
2 |
|
$this->commentsFormatter = new CommentsFormatter($parser, $profile, $this->writer, $this->defaultFormatter); |
|
40
|
2 |
|
$this->indentationFormatter = new IndentationFormatter($parser, $profile, $this->writer, $this->defaultFormatter); |
|
41
|
2 |
|
$this->newlineFormatter = new NewlineFormatter($parser, $profile, $this->writer, $this->defaultFormatter); |
|
42
|
2 |
|
$this->whitespaceFormatter = new WhitespaceFormatter($parser, $profile, $this->writer, $this->defaultFormatter); |
|
43
|
2 |
|
$this->blanksFormatter = new BlanksFormatter($parser, $profile, $this->writer, $this->defaultFormatter); |
|
44
|
2 |
|
} |
|
45
|
|
|
|
|
46
|
2 |
|
public function format() { |
|
47
|
2 |
|
foreach ($this->parser->getTokens() as $token) { |
|
48
|
2 |
|
$token->accept($this); |
|
49
|
|
|
} |
|
50
|
2 |
|
} |
|
51
|
|
|
|
|
52
|
2 |
|
public function visitToken(Token $token) { |
|
53
|
2 |
|
$this->parser->getTracker()->visitToken($token); |
|
54
|
|
|
|
|
55
|
|
|
// visit all rules |
|
56
|
2 |
|
$this->commentsFormatter->visitToken($token); |
|
57
|
2 |
|
$this->indentationFormatter->visitToken($token); |
|
58
|
2 |
|
$this->newlineFormatter->visitToken($token); |
|
59
|
2 |
|
$this->whitespaceFormatter->visitToken($token); |
|
60
|
2 |
|
$this->blanksFormatter->visitToken($token); |
|
61
|
2 |
|
$this->defaultFormatter->visitToken($token); |
|
62
|
2 |
|
} |
|
63
|
|
|
|
|
64
|
2 |
|
public function getCode() { |
|
65
|
2 |
|
return $this->writer->getContent(); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
} |
|
69
|
|
|
|