-
-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathPHPEngine.php
More file actions
87 lines (69 loc) · 1.8 KB
/
Copy pathPHPEngine.php
File metadata and controls
87 lines (69 loc) · 1.8 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
<?php
declare(strict_types=1);
namespace Bow\View\Engine;
use Bow\View\EngineAbstract;
use RuntimeException;
class PHPEngine extends EngineAbstract
{
/**
* The engine name
*
* @var string
*/
protected string $name = 'php';
/**
* PHPEngine constructor.
*
* @param array $config
* @return void
*/
public function __construct(array $config)
{
$this->config = $config;
}
/**
* @inheritdoc
*/
public function render(string $filename, array $data = []): string
{
$hash_filename = $filename;
$filename = $this->checkParseFile($filename);
if ($this->config['path'] !== null) {
$filename = $this->config['path'] . '/' . $filename;
}
$cache_hash_filename = '_PHP_' . md5($hash_filename) . '.php';
$cache_hash_filename = $this->config['cache'] . '/' . $cache_hash_filename;
extract($data);
if (file_exists($cache_hash_filename)) {
if (filemtime($cache_hash_filename) >= fileatime($filename)) {
return $this->includeFile($cache_hash_filename);
}
}
$content = file_get_contents($filename);
// Save to cache
file_put_contents(
$cache_hash_filename,
$content
);
return $this->includeFile($cache_hash_filename);
}
/**
* include the execute filename
*
* @param string $filename
* @return string
*/
private function includeFile(string $filename): string
{
ob_start();
include $filename;
return ob_get_clean();
}
/**
* @inheritDoc
*/
public function getEngine(): mixed
{
throw new RuntimeException("This method cannot work for PHP native engine");
}
}