-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathCompiler.php
More file actions
155 lines (134 loc) · 3.67 KB
/
Compiler.php
File metadata and controls
155 lines (134 loc) · 3.67 KB
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
<?php
/*
* This file is part of Spoon Library.
*
* (c) Davy Hellemans <davy@spoon-library.com>
*
* For the full copyright and license information, please view the license
* file that was distributed with this source code.
*/
namespace Spoon\Template;
use Spoon\Template\Parser\TextNode;
use Spoon\Template\Parser\VariableNode;
use Spoon\Template\Writer;
/**
* Class that compiles template files to cached php files.
*
* @author Davy Hellemans <davy@spoon-library.com>
*/
class Compiler
{
/**
* Source file to compile.
*
* @var string
*/
protected $filename;
/**
* @var \Spoon\Template\Template
*/
protected $template;
/**
* @param \Spoon\Template\Template $template The actual template object.
* @param string $filename The location of the template you wish to compile.
*/
public function __construct(Template $template, $filename)
{
$this->template = $template;
$this->filename = (string) $filename;
}
/**
* Compiles the template based on the tokens found in it.
*
* @todo write some inline docs to clarify the code
*
* @return string
*/
protected function compile()
{
$lexer = new Lexer($this->template->getEnvironment());
$stream = $lexer->tokenize(file_get_contents($this->filename), basename($this->filename));
// unique class based on the filename
// @todo fix problem with '-' in classes
$class = $this->template->getEnvironment()->getCacheFilename($this->filename);
$class = 'S' . substr($class, 0, -8) . '_Template';
// writer object which contains the parsed PHP code
$writer = new Writer();
$writer->write("<?php\n");
$writer->write("\n");
$writer->write('namespace Spoon\Template;' . "\n");
$writer->write("\n");
$writer->write('/* ' . $this->filename . ' */' . "\n");
$writer->write("class $class extends Renderer\n");
$writer->write("{\n");
$writer->indent();
$writer->write('protected function display(array $context)' . "\n");
$writer->write("{\n");
$writer->indent();
$tags = $this->template->getEnvironment()->getTags();
$token = $stream->getCurrent();
while(!$stream->isEof())
{
switch($token->getType())
{
case Token::TEXT:
$text = new TextNode($stream, $this->template->getEnvironment());
$text->compile($writer);
break;
case Token::VAR_START:
$stream->next();
$variable = new VariableNode($stream, $this->template->getEnvironment());
$variable->compile($writer);
break;
case Token::BLOCK_START:
$token = $stream->next();
// validate tag existence
if(!isset($tags[$token->getValue()]))
{
throw new SyntaxError(
sprintf('There is no such template tag "%s"', $token->getValue()),
$token->getLine(),
$this->filename
);
}
$node = new $tags[$token->getValue()]($stream, $this->template->getEnvironment());
$node->compile($writer);
break;
}
if($token->getType() !== Token::EOF)
{
$token = $stream->next();
}
else break;
}
$writer->outdent();
$writer->write("}\n");
$writer->outdent();
$writer->write("}\n");
return $writer->getSource();
}
/**
* Writes the parsed template to its cache file.
*/
public function write()
{
$source = $this->compile();
// file location
$file = $this->template->getEnvironment()->getCache() . '/';
$file .= $this->template->getEnvironment()->getCacheFilename($this->filename);
// attempt to create the directory if needed
if(!is_dir(dirname($file)))
{
mkdir(dirname($file), 0777, true);
}
// write to tempfile and rename
$tmpFile = tempnam(dirname($file), basename($file));
if(@file_put_contents($tmpFile, $source) !== false)
{
if(@rename($tmpFile, $file))
{
chmod($file, 0644);
}
}
}
}