-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathScriptLoader.php
More file actions
62 lines (53 loc) · 1.88 KB
/
ScriptLoader.php
File metadata and controls
62 lines (53 loc) · 1.88 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
<?php
require_once dirname(__FILE__) . "/ContentDatabase.php";
require_once dirname(__FILE__) . "/OutlineText.php";
require_once dirname(__FILE__) . "/CacheStore.php";
require_once dirname(__FILE__) . "/Logger.php";
class ScriptLoader
{
public $macros = [[], []];
/**
* @var ContentDatabase
*/
private $database;
public function __construct(?ContentDatabase $database = null)
{
$this->database = $database ?? new ContentDatabase;
}
public function load(string $scriptPath)
{
$scriptContent = $this->database->get($scriptPath);
if (!$scriptContent) {
return [];
}
$cache = new Cache();
try {
$cache->connect('script-' . $scriptContent->path)->lock(LOCK_EX)->fetch();
} catch (Exception $error) {
\logger()->warning($error);
}
if (
($cache->data['updatedTime'] ?? 0) < $scriptContent->modifiedTime ||
!array_key_exists('scripts', $cache->data)
) {
$cache->data['scripts'] = [];
OutlineText\Parser::Init();
OutlineText\Parser::Parse($scriptContent->body, $context);
foreach ($context->morphSequence->morphs as $morph) {
if ($morph['isCodeBlock']) {
$scriptName = $morph['codeBlockAttribute'];
$script = str_replace($this->macros[0], $this->macros[1], $morph['content']);
$cache->data['scripts'][$scriptName] = ($cache->data['scripts'][$scriptName] ?? '') . $script . "\n";
}
}
$cache->data['updatedTime'] = time();
try {
$cache->apply();
} catch (Exception $error) {
\logger()->warning($error);
}
}
$cache->unlock()->disconnect();
return $cache->data['scripts'];
}
}