1: <?php
2:
3: 4: 5: 6: 7: 8:
9:
10: namespace XSLTBenchmarking\TestsGenerator;
11:
12: require_once LIBS . '/Smarty/Smarty.class.php';
13: require_once LIBS . '/PhpPath/PhpPath.min.php';
14: require_once __DIR__ . '/ITemplatingDriver.php';
15: require_once ROOT . '/Exceptions.php';
16:
17:
18: use PhpPath\P;
19:
20: 21: 22: 23: 24:
25: class SmartyTemplatingDriver extends \Smarty implements ITemplatingDriver
26: {
27:
28:
29: 30: 31: 32: 33:
34: public function __construct($tmpDirectory)
35: {
36: parent::__construct();
37: $this->debugging = FALSE;
38: $this->caching = FALSE;
39: $this->compile_dir = P::mcd($tmpDirectory, '/');
40:
41: $this->registerPlugin('modifier', 'pathEnd', array($this, 'pathEnd'));
42: $this->registerPlugin('modifier', 'timeNicer', array($this, 'timeNicer'));
43: $this->registerPlugin('modifier', 'memoryNicer', array($this, 'memoryNicer'));
44: }
45:
46:
47: 48: 49: 50: 51: 52: 53: 54: 55: 56:
57: public function generate($templatePath, $outputPath, array $settings)
58: {
59: P::cf($templatePath);
60:
61:
62: $this->assign('templateDir', pathinfo($templatePath, PATHINFO_DIRNAME) . DIRECTORY_SEPARATOR);
63: foreach ($settings as $name => $value)
64: {
65: $this->assign($name, $value);
66: }
67:
68:
69: ob_start();
70: try {
71: $this->display($templatePath);
72: } catch (\Exception $e) {
73: throw new \XSLTBenchmarking\GenerateTemplateException('Cannot generate template by Smarty Driver (' . $e->getMessage() . ')', 0, $e);
74: }
75: $content = ob_get_clean();
76:
77:
78:
79:
80:
81: $indent = TRUE;
82: $linearize = TRUE;
83: if (isset($settings['outputFiler']))
84: {
85: switch ($settings['outputFiler'])
86: {
87: case 'linearize':
88: $indent = FALSE;
89: $linearize = TRUE;
90: break;
91: case 'nothing':
92: $indent = FALSE;
93: $linearize = FALSE;
94: break;
95: }
96: }
97: if ($linearize)
98: {
99: $content = $this->repareIndent($content, $indent);
100: }
101:
102: if (!file_put_contents($outputPath, $content))
103: {
104: throw new \XSLTBenchmarking\GenerateTemplateException('Cannot create file "' . $outputFile . '".');
105: }
106: }
107:
108:
109: 110: 111: 112: 113: 114: 115: 116:
117: public function repareIndent($content, $indent)
118: {
119:
120: $content = preg_replace('/>[\s]*</', '><', $content);
121:
122: if (!$indent)
123: {
124: return $content;
125: }
126:
127:
128: $contentSimpleXml = new \SimpleXMLElement($content);
129: $contentDomXml = dom_import_simplexml($contentSimpleXml)->ownerDocument;
130: $contentDomXml->formatOutput = TRUE;
131: $content = $contentDomXml->saveXml();
132:
133:
134: $content = str_replace("\r\n", "\n", $content);
135: $lines = explode("\n", $content);
136: $linesNew = array();
137: foreach ($lines as $line)
138: {
139: $indentEndPos = strpos($line, '<');
140: $linesNew[] = str_repeat("\t", $indentEndPos / 2) . substr($line, $indentEndPos);
141: }
142: $content = implode(PHP_EOL, $linesNew);
143:
144:
145: $content = preg_replace('/(\t*<xsl:template)/', PHP_EOL . '$0', $content);
146: $content = str_replace('</xsl:stylesheet>', PHP_EOL . '</xsl:stylesheet>', $content);
147:
148: return $content;
149: }
150:
151:
152:
153:
154:
155: 156: 157: 158: 159: 160: 161: 162:
163: public function pathEnd($path, $partsNumber = 1)
164: {
165: $parts = explode('/', $path);
166: $separator = '/';
167: if (count($parts) == 1)
168: {
169: $parts = explode('\\', $path);
170: $separator = '\\';
171: }
172: $parts = array_slice($parts, -$partsNumber);
173: return implode($separator, $parts);
174: }
175:
176:
177: 178: 179: 180: 181: 182: 183:
184: public function timeNicer($time)
185: {
186: if ($time === '')
187: {
188: return '';
189: }
190: return substr($time, 0, -3) . ' sec';
191: }
192:
193:
194: 195: 196: 197: 198: 199: 200:
201: public function memoryNicer($memory)
202: {
203: if ($memory === '')
204: {
205: return '';
206: }
207:
208: $units = array(
209: 'GB' => 1000000000,
210: 'MB' => 1000000,
211: 'KB' => 1000,
212: 'B' => 1,
213: );
214:
215: $number = (int)$memory;
216: foreach ($units as $unit => $limit)
217: {
218: if ($number > $limit)
219: {
220: $rounded = number_format($number / $limit, 1) . ' ' . $unit;
221: break;
222: }
223: }
224:
225: return $rounded;
226: }
227:
228:
229: }
230: