-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEntityCssGenerator.php
More file actions
88 lines (65 loc) · 2.06 KB
/
EntityCssGenerator.php
File metadata and controls
88 lines (65 loc) · 2.06 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
<?php
namespace CustomEntity;
use MapasCulturais\App;
class EntityCssGenerator
{
const FLAGS_PATH = PRIVATE_FILES_PATH . "CustomEntity/";
const CSS_PATH = __DIR__ . "/assets/css/";
public readonly string $filename;
function __construct(
public readonly EntityDefinition $entityDefinition
) {
$this->filename = "custom-entity.{$this->entityDefinition->entity}.css";
}
function log(string $message)
{
$app = App::i();
$app->log->debug($message);
}
function renderTemplate(): string
{
$color = $this->entityDefinition->color;
return $this->entityDefinition->renderTemplate('style.css', replacements: [
'color-500' => $color,
'color-300' => Color::darken($color, 25),
'color-700' => Color::lighten($color, 25),
]);
}
function generateHash(): string|false
{
return md5($this->renderTemplate());
}
function getUpdatedFlagFilename(): string
{
return self::FLAGS_PATH . "css.{$this->entityDefinition->slug}.hash";
}
function flagAsUpdated()
{
if (!is_dir(self::FLAGS_PATH)) {
mkdir(self::FLAGS_PATH);
};
$hash_filename = $this->getUpdatedFlagFilename();
$hash = $this->generateHash();
file_put_contents($hash_filename, $hash);
}
function isUpdated()
{
$hash_filename = $this->getUpdatedFlagFilename();
if (!file_exists($hash_filename)) {
return false;
}
$saved_hash = file_get_contents($hash_filename);
$actual_hash = $this->generateHash();
return $saved_hash == $actual_hash;
}
function create(): string
{
if ($this->isUpdated()) {
return self::CSS_PATH . $this->filename;
}
Plugin::log("Atualizando arquivo css da entidade {$this->entityDefinition->entity}");
$content = $this->renderTemplate();;
file_put_contents(self::CSS_PATH . $this->filename, $content);
return self::CSS_PATH . $this->filename;
}
}