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
|
|
|
public function __construct(Parser $parser, Profile $profile) { |
30
|
|
|
$this->profile = $profile; |
31
|
|
|
$this->parser = $parser; |
32
|
|
|
$this->writer = new Writer([ |
33
|
|
|
'indentation_character' => $profile->getIndentation('character') == 'tab' ? "\t" : ' ', |
34
|
|
|
'indentation_size' => $profile->getIndentation('size') |
35
|
|
|
]); |
36
|
|
|
|
37
|
|
|
// define rules |
38
|
|
|
$this->defaultFormatter = new DefaultFormatter($parser, $profile, $this->writer); |
39
|
|
|
$this->commentsFormatter = new CommentsFormatter($parser, $profile, $this->writer, $this->defaultFormatter); |
40
|
|
|
$this->indentationFormatter = new IndentationFormatter($parser, $profile, $this->writer, $this->defaultFormatter); |
41
|
|
|
$this->newlineFormatter = new NewlineFormatter($parser, $profile, $this->writer, $this->defaultFormatter); |
42
|
|
|
$this->whitespaceFormatter = new WhitespaceFormatter($parser, $profile, $this->writer, $this->defaultFormatter); |
43
|
|
|
$this->blanksFormatter = new BlanksFormatter($parser, $profile, $this->writer, $this->defaultFormatter); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function format() { |
47
|
|
|
foreach ($this->parser->getTokens() as $token) { |
48
|
|
|
$token->accept($this); |
49
|
|
|
} |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public function visitToken(Token $token) { |
53
|
|
|
$this->parser->getTracker()->visitToken($token); |
54
|
|
|
|
55
|
|
|
// visit all rules |
56
|
|
|
$this->commentsFormatter->visitToken($token); |
57
|
|
|
$this->indentationFormatter->visitToken($token); |
58
|
|
|
$this->newlineFormatter->visitToken($token); |
59
|
|
|
$this->whitespaceFormatter->visitToken($token); |
60
|
|
|
$this->blanksFormatter->visitToken($token); |
61
|
|
|
$this->defaultFormatter->visitToken($token); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function getCode() { |
65
|
|
|
return $this->writer->getContent(); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
} |
69
|
|
|
|