-
Notifications
You must be signed in to change notification settings - Fork 3
/
Slot.php
171 lines (143 loc) · 4.36 KB
/
Slot.php
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
<?php
/*
* This file is a part of Solve framework.
*
* @author Alexandr Viniychuk <alexandr.viniychuk@icloud.com>
* @copyright 2009-2014, Alexandr Viniychuk
* created: 05.01.14 01:34
*/
namespace Solve\Slot;
use Solve\Storage\ArrayStorage;
use Solve\Utils\FSService;
use Solve\Utils\Inflector;
/**
* Class Slot
* @package Solve\Slot
*
* Class Slot is used to ...
*
* @version 1.0
* @author Alexandr Viniychuk <alexandr.viniychuk@icloud.com>
*/
class Slot {
/**
* @var array config
*/
private $_config = array(
'forceCompile' => true,
);
/**
* @var Compiler
*/
private $_compiler;
/**
* @var string path to compiled templates
*/
private $_compileDir;
/**
* @var string
*/
private $_templateDir;
private $_tplVars;
public function __construct() {
$this->_config = new ArrayStorage($this->_config);
$this->_compiler = new Compiler($this);
$this->registerInternalBlocks();
}
/**
* @return Compiler
*/
public function getCompiler() {
return $this->_compiler;
}
/**
* @param string $compileDir
*/
public function setCompileDir($compileDir) {
$this->_compileDir = $compileDir;
$this->_tplVars = array();
FSService::makeWritable($this->_compileDir);
}
/**
* @return string
*/
public function getCompileDir() {
return $this->_compileDir;
}
/**
* @param string $templateDir
*/
public function setTemplateDir($templateDir) {
$this->_templateDir = $templateDir;
}
/**
* @return string
*/
public function getTemplateDir() {
return $this->_templateDir;
}
protected function registerInternalBlocks() {
$blocks = GLOB(__DIR__ . '/Blocks/*.php');
foreach($blocks as $block) {
$blockName = lcfirst(substr($block, strrpos($block, '/')+1, -9));
if ($blockName !== 'base') {
$this->registerBlock($blockName);
}
}
}
public function registerBlock($block, $namespace = '\Solve\Slot\Blocks\\') {
$className = Inflector::camelize($block . 'Block');
$r = new \ReflectionClass($namespace . $className);
$doc = $r->getDocComment();
$config = array(
'paired' => strpos($doc, '@paired') !== false,
'runtime' => strpos($doc, '@runtime') !== false,
'namespace' => $namespace
);
$this->_compiler->registerBlock($block, $config);
}
public function processTemplate($templatePath) {
$source = file_get_contents($templatePath);
return $this->_compiler->compileSource($source);
}
/**
* @param $templatePath
* @param array $variables
* @param array|ArrayStorage $params
* @return string
* @throws \Exception
*/
public function fetchTemplate($templatePath, $variables = array(), $params = array()) {
if (!empty($variables)) {
$this->_tplVars = $variables;
}
if (empty($params['absolute'])) {
$templateFile = $templatePath;
$templatePath = $this->getTemplateDir() . $templatePath;
} else {
$templateFile = substr($templatePath, strrpos($templatePath, '/')+1);
}
if (is_file($templatePath)) {
$compilePath = $this->getCompiledPath($templateFile);
if ($this->_config['forceCompile'] || !is_file($compilePath)) {
$old_files = GLOB($this->getCompileDir() . str_replace('/', '_', $templateFile) . '.*');
foreach($old_files as $file) if (is_file($file)) @unlink($file);
$content = '<?php $__lv = &$this->_tplVars; ?>';
$content .= $this->processTemplate($templatePath);
file_put_contents($compilePath, $content);
}
} else {
throw new \Exception('File not found:' . $templatePath);
}
ob_start();
if (is_file($compilePath)) {
include $compilePath;
}
$output = ob_get_clean();
return $output;
}
public function getCompiledPath($templateFile) {
$templatePath = $this->_templateDir . $templateFile;
return $this->getCompileDir() . str_replace('/', '_', $templateFile) . '.' . filectime($templatePath) . '.php';
}
}