forked from pwarelis/ScriptCompiler
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathResourceManager.php
More file actions
145 lines (115 loc) · 3.31 KB
/
ResourceManager.php
File metadata and controls
145 lines (115 loc) · 3.31 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
<?php
namespace ScriptCompiler;
use ScriptCompiler\Cache\DiscCache;
use ScriptCompiler\Compiler\LanguageCompiler;
class ResourceManager {
/** @var DiscCache */
protected $cache;
/** @var PathManager */
protected $paths;
protected $types;
protected $cacheRemoteScripts;
protected $resources = array();
protected $compilers = array();
const SCRIPT_APPEND = 0;
const SCRIPT_PREPEND = 1;
private $now;
private $debug;
public function __construct($config) {
$this->now = time();
$this->types = $config["types"];
$this->cacheRemoteScripts = $config["cache_remote"];
$this->debug = $config["debug"];
}
public function setCache(DiscCache $cache) {
$this->cache = $cache;
}
public function setPathManager(PathManager $pm) {
$this->paths = $pm;
}
public function addResources($resources, $direction) {
if (is_string($resources)) $resources = array($resources);
foreach ($resources as $item) {
$this->addResource($item, $direction);
}
}
protected function getFilePath($resource) {
if ($resource->isRemote) {
return $this->cache->buildFilename($resource->url);
}
return $this->paths->getPath($resource->url);
}
protected function addResource($data, $direction) {
if (is_string($data)) {
$data = array("url" => $data);
}
$resource = new Resource($data);
$resource->path = $this->getFilePath($resource);
if ($direction == self::SCRIPT_APPEND) {
$this->resources[] = $resource;
} else {
array_unshift($this->resources, $resource);
}
}
/**
* @param string $language
* @return LanguageCompiler
* @throws \Exception
*/
protected function getCompiler($language) {
if (!isset($this->compilers[$language])) {
if (!isset($this->types[$language])) {
throw new \Exception("No compiler found for this language: {$language}");
}
$this->compilers[$language] = new $this->types[$language];
}
return $this->compilers[$language];
}
public function compileResources($requestKey)
{
foreach ($this->resources as $resource) {
$compiler = $this->getCompiler($resource->language);
$compiler->compileResource($resource, $this->cache);
}
$lastModifyTime = 0;
$baseList = array();
$sets = array();
foreach ($this->resources as $resource) {
if (!isset($sets[$resource->base])) {
$sets[$resource->base] = array();
}
if ($resource->hasChanged) {
$baseList[$resource->base] = true;
}
$sets[$resource->base][] = array('hash'=>$resource->hash
, 'path'=>$resource->path
);
if ($resource->modified > $lastModifyTime) {
$lastModifyTime = $resource->modified;
}
}
$urls = array();
$masterCacheFile = $this->cache->buildFilename($requestKey.$lastModifyTime);
foreach ($sets as $base => $fileList)
{
$script = "{$masterCacheFile}.{$base}";
if (!file_exists($script))
{
// Copy all the files over to the master script file
$fd = fopen($script, "w");
foreach ($fileList as $file)
{
$content = ( $this->debug
? "/**\n * compiled from:".str_replace($_SERVER['DOCUMENT_ROOT'], '', $file['path'])."\n */\n"
: '' );
$content .= file_get_contents($file['hash']);
fwrite($fd, $content);
}
fclose($fd);
}
// $urls[$base] = "/cache/" . basename($script);
$urls[$base] = str_replace($_SERVER['DOCUMENT_ROOT'] . '/', '', $script);
}
return $urls;
}
}