1: <?php
2:
3: namespace PHPixie\Template;
4:
5: class Compiler
6: {
7: protected $filesystemRoot;
8: protected $formats;
9: protected $configData;
10:
11: protected $cacheDirectory;
12:
13: public function __construct($filesystemRoot, $formats, $configData)
14: {
15: $this->filesystemRoot = $filesystemRoot;
16: $this->formats = $formats;
17: $this->configData = $configData;
18: }
19:
20: public function compile($path)
21: {
22: $format = $this->formats->getByFilename($path);
23:
24: if($format === null) {
25: return $path;
26: }
27:
28: $hash = crc32($path);
29: $cachePath = $this->cacheDirectory().'/'.$hash.'.php';
30:
31: if(!file_exists($cachePath) || filemtime($cachePath) < filemtime($path)) {
32: $compiled = $format->compile($path);
33: file_put_contents($cachePath, $compiled);
34: }
35:
36: return $cachePath;
37: }
38:
39: protected function cacheDirectory()
40: {
41: if($this->cacheDirectory === null) {
42: $path = $this->configData->getRequired('cacheDirectory');
43: $this->cacheDirectory = $this->filesystemRoot->path($path);
44: }
45:
46: return $this->cacheDirectory;
47: }
48: }