pxpros 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +7 -0
- package/index.d.ts +26 -0
- package/index.js +1 -0
- package/package.json +24 -0
- package/src/pxpros.js +23 -0
- package/src/pxpros.php +426 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Maxime Larrivée-Roy
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
package/index.d.ts
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
// index.d.ts
|
|
2
|
+
|
|
3
|
+
export interface PXProsSuccess {
|
|
4
|
+
success: true;
|
|
5
|
+
// Le PHP peut retourner d'autres champs JSON; on ne les connaît pas ici.
|
|
6
|
+
[key: string]: unknown;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export interface PXProsFailure {
|
|
10
|
+
success: false;
|
|
11
|
+
error: string;
|
|
12
|
+
// Peut contenir d'autres infos (stack, code, etc.)
|
|
13
|
+
[key: string]: unknown;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export type PXProsResult = PXProsSuccess | PXProsFailure;
|
|
17
|
+
|
|
18
|
+
declare const PXPros: {
|
|
19
|
+
/**
|
|
20
|
+
* Rend un fichier via pxpros.php et retourne l'objet JSON produit.
|
|
21
|
+
* @param file Chemin vers le fichier à traiter.
|
|
22
|
+
*/
|
|
23
|
+
render(file: string): Promise<PXProsResult>;
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
export = PXPros;
|
package/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
module.exports = require("./src/pxpros");
|
package/package.json
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "pxpros",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "PHP Renderer for Static Website",
|
|
5
|
+
"main": "index.js",
|
|
6
|
+
"types": "index.d.ts",
|
|
7
|
+
"files": [
|
|
8
|
+
"index.js",
|
|
9
|
+
"index.d.ts",
|
|
10
|
+
"src/"
|
|
11
|
+
],
|
|
12
|
+
"repository": {
|
|
13
|
+
"type": "git",
|
|
14
|
+
"url": "git+https://github.com/ZmotriN/pxpros.git"
|
|
15
|
+
},
|
|
16
|
+
"keywords": ["PHP", "template", "static"],
|
|
17
|
+
"author": "",
|
|
18
|
+
"license": "MIT",
|
|
19
|
+
"type": "commonjs",
|
|
20
|
+
"bugs": {
|
|
21
|
+
"url": "https://github.com/ZmotriN/pxpros/issues"
|
|
22
|
+
},
|
|
23
|
+
"homepage": "https://github.com/ZmotriN/pxpros#readme"
|
|
24
|
+
}
|
package/src/pxpros.js
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
const path = require('path');
|
|
2
|
+
const { exec } = require('child_process')
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
const render = async (file) => {
|
|
6
|
+
return new Promise(resolve => {
|
|
7
|
+
exec(`php -d display_errors=1 -d log_errors=1 -d error_log=php://stderr -d html_errors=0 -d error_reporting=32767 "${path.join(__dirname, 'pxpros.php')}" "${file}"`, (error, stdout, stderr) => {
|
|
8
|
+
if (error) {
|
|
9
|
+
const errstr = stdout.trim() || stderr.trim() || `${error}`.trim();
|
|
10
|
+
try { errobj = JSON.parse(errstr); }
|
|
11
|
+
catch(e) { errobj = { success: false, error: errstr }; }
|
|
12
|
+
resolve(errobj);
|
|
13
|
+
} else {
|
|
14
|
+
try { retobj = JSON.parse(stdout); }
|
|
15
|
+
catch(e) { retobj = { success: false, error: "Response parsing error." }; }
|
|
16
|
+
resolve(retobj);
|
|
17
|
+
}
|
|
18
|
+
});
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
module.exports.render = render;
|
package/src/pxpros.php
ADDED
|
@@ -0,0 +1,426 @@
|
|
|
1
|
+
<?php
|
|
2
|
+
|
|
3
|
+
const BR = '<br>';
|
|
4
|
+
const RN = "\r\n";
|
|
5
|
+
const S = '/';
|
|
6
|
+
const R = "\r";
|
|
7
|
+
const N = "\n";
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
try {
|
|
11
|
+
$PAGE = null;
|
|
12
|
+
$files = [];
|
|
13
|
+
|
|
14
|
+
if (!isset($argv[1])) error("Invalid argument.");
|
|
15
|
+
if (!$target = realpath($argv[1])) error("Invalid target.");
|
|
16
|
+
|
|
17
|
+
if (is_dir($target)) {
|
|
18
|
+
if (!$seed = PXPros::findSeed($target)) error("No project configuration found.");
|
|
19
|
+
$prj = new PXPros($seed);
|
|
20
|
+
foreach (dig($target . '/*.php') as $file) {
|
|
21
|
+
$parent = pathinfo(pathinfo($file, PATHINFO_DIRNAME), PATHINFO_BASENAME);
|
|
22
|
+
if (strpos($parent, '_') === 0) continue;
|
|
23
|
+
if (strpos(pathinfo($file, PATHINFO_FILENAME), '_') !== 0) continue;
|
|
24
|
+
$files[] = $prj->render($file);
|
|
25
|
+
}
|
|
26
|
+
} elseif (preg_match('#^_(.*)\.php$#i', pathinfo($target, PATHINFO_BASENAME), $m)) {
|
|
27
|
+
if (!$seed = PXPros::findSeed($target)) error("No project configuration found.");
|
|
28
|
+
$pxpros = new PXPros($seed);
|
|
29
|
+
$files[] = $pxpros->render($target);
|
|
30
|
+
} else {
|
|
31
|
+
throw new Exception("Invalid target.");
|
|
32
|
+
}
|
|
33
|
+
} catch(Exception $e) {
|
|
34
|
+
error($e->getMessage());
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
succeed($files);
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
final class PXPros
|
|
45
|
+
{
|
|
46
|
+
|
|
47
|
+
const SEED_FILE = 'pxpros.json';
|
|
48
|
+
|
|
49
|
+
private $root;
|
|
50
|
+
private $file;
|
|
51
|
+
private $page;
|
|
52
|
+
private $config;
|
|
53
|
+
private $vars = [];
|
|
54
|
+
private $tags = [];
|
|
55
|
+
private $hooks = [];
|
|
56
|
+
private $plugins = [];
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* __construct
|
|
61
|
+
*
|
|
62
|
+
* @param mixed $prjfile Project configuration file (_pxprox.json)
|
|
63
|
+
* @return void
|
|
64
|
+
*/
|
|
65
|
+
public function __construct($prjfile)
|
|
66
|
+
{
|
|
67
|
+
if (!is_file($prjfile)) return false; //throw error
|
|
68
|
+
if (!$json = file_get_contents($prjfile)) return false; //throw error
|
|
69
|
+
if (!$this->config = json_decode($json)) return false; //throw error
|
|
70
|
+
$this->root = pathinfo($prjfile, PATHINFO_DIRNAME) . S;
|
|
71
|
+
$GLOBALS['PAGE'] = $this;
|
|
72
|
+
$this->includes();
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Project and page data getter
|
|
78
|
+
*
|
|
79
|
+
* @param mixed $name Variable name
|
|
80
|
+
* @return void
|
|
81
|
+
*/
|
|
82
|
+
public function __get($name)
|
|
83
|
+
{
|
|
84
|
+
switch ($name) {
|
|
85
|
+
case 'root':
|
|
86
|
+
return $this->root;
|
|
87
|
+
case 'plugins':
|
|
88
|
+
return $this->plugins;
|
|
89
|
+
case 'file':
|
|
90
|
+
return $this->file;
|
|
91
|
+
default:
|
|
92
|
+
if (!empty($this->vars[$name])) return $this->vars[$name];
|
|
93
|
+
elseif (!empty($this->page->{$name})) return $this->page->{$name};
|
|
94
|
+
elseif (!empty($this->config->{$name})) return $this->config->{$name};
|
|
95
|
+
elseif (!empty($this->config->data->{$name})) return $this->config->data->{$name};
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* Project and page data setter
|
|
102
|
+
*
|
|
103
|
+
* @param mixed $name
|
|
104
|
+
* @param mixed $val
|
|
105
|
+
* @return void
|
|
106
|
+
*/
|
|
107
|
+
public function __set($name, $val)
|
|
108
|
+
{
|
|
109
|
+
$this->vars[$name] = $val;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* Includes base .php files
|
|
115
|
+
*
|
|
116
|
+
* @return void
|
|
117
|
+
*/
|
|
118
|
+
private function includes()
|
|
119
|
+
{
|
|
120
|
+
if (!empty($this->config->includes)) foreach ($this->config->includes as $path) {
|
|
121
|
+
if (!is_file(realpath($this->root . $path))) continue;
|
|
122
|
+
else include_once(realpath($this->root . $path));
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* Render a page
|
|
129
|
+
*
|
|
130
|
+
* @param mixed $file File to render
|
|
131
|
+
* @return void
|
|
132
|
+
*/
|
|
133
|
+
public function render($file)
|
|
134
|
+
{
|
|
135
|
+
global $PAGE;
|
|
136
|
+
$PAGE = $this;
|
|
137
|
+
$dir = pathinfo($file, PATHINFO_DIRNAME) . S;
|
|
138
|
+
$this->page = php_file_info($file);
|
|
139
|
+
$target = $dir . ltrim(pathinfo($file, PATHINFO_FILENAME), '_') . '.html';
|
|
140
|
+
$this->file = realpath($file);
|
|
141
|
+
$this->plugins = [];
|
|
142
|
+
$this->processHook('pre_render', file_get_contents($file));
|
|
143
|
+
ob_start();
|
|
144
|
+
if ($this->before) include(realpath($this->root . $this->before));
|
|
145
|
+
include($file);
|
|
146
|
+
if ($this->after) include(realpath($this->root . $this->after));
|
|
147
|
+
$contents = ob_get_clean();
|
|
148
|
+
$contents = $this->processTags($contents);
|
|
149
|
+
$contents = $this->processHook('post_render', $contents);
|
|
150
|
+
file_put_contents($target, $contents);
|
|
151
|
+
return realpath($target);
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
|
|
155
|
+
|
|
156
|
+
/**
|
|
157
|
+
* registerTag
|
|
158
|
+
*
|
|
159
|
+
* @param mixed $tag
|
|
160
|
+
* @param mixed $clb
|
|
161
|
+
* @return void
|
|
162
|
+
*/
|
|
163
|
+
public function registerTag($tag, $clb)
|
|
164
|
+
{
|
|
165
|
+
$this->tags[$tag] = $clb;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
|
|
169
|
+
/**
|
|
170
|
+
* processTags
|
|
171
|
+
*
|
|
172
|
+
* @return void
|
|
173
|
+
*/
|
|
174
|
+
public function processTags($contents)
|
|
175
|
+
{
|
|
176
|
+
foreach ($this->tags as $tag => $clb) {
|
|
177
|
+
$contents = replace_tags($tag, $contents, $clb);
|
|
178
|
+
}
|
|
179
|
+
return $contents;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
|
|
183
|
+
/**
|
|
184
|
+
* registerHook
|
|
185
|
+
*
|
|
186
|
+
* @param string $hook Name of the hook
|
|
187
|
+
* @param callable $clb The callback
|
|
188
|
+
* @return void
|
|
189
|
+
*/
|
|
190
|
+
public function registerHook($hook, $clb)
|
|
191
|
+
{
|
|
192
|
+
$this->hooks[$hook][] = $clb;
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
|
|
196
|
+
/**
|
|
197
|
+
* processHook
|
|
198
|
+
*
|
|
199
|
+
* @param string $hook Name of the hook
|
|
200
|
+
* @param mixed $data The data to be returned by the callback
|
|
201
|
+
* @return mixed
|
|
202
|
+
*/
|
|
203
|
+
public function processHook($hook, $data = null)
|
|
204
|
+
{
|
|
205
|
+
if (!empty($this->hooks[$hook])) {
|
|
206
|
+
foreach ($this->hooks[$hook] as $clb) {
|
|
207
|
+
$data = call_user_func($clb, $data);
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
return $data;
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
|
|
214
|
+
/**
|
|
215
|
+
* Find the currect project configuration file
|
|
216
|
+
*
|
|
217
|
+
* @param mixed $path Current path
|
|
218
|
+
* @return mixed Returns the project configuration file if exists, otherwise false.
|
|
219
|
+
*/
|
|
220
|
+
public static function findSeed($path)
|
|
221
|
+
{
|
|
222
|
+
if (is_file($path)) $path = pathinfo(realpath($path), PATHINFO_DIRNAME);
|
|
223
|
+
elseif (!$path = realpath($path)) return false;
|
|
224
|
+
$recPath = $path;
|
|
225
|
+
|
|
226
|
+
do {
|
|
227
|
+
$file = $path . S . self::SEED_FILE;
|
|
228
|
+
if (is_file($file)) return realpath($file);
|
|
229
|
+
$path = pathinfo($path, PATHINFO_DIRNAME);
|
|
230
|
+
} while ($path != pathinfo($path, PATHINFO_DIRNAME));
|
|
231
|
+
|
|
232
|
+
|
|
233
|
+
$ignoreDirs = ['.git', 'node_modules', 'vendor'];
|
|
234
|
+
$dir = new RecursiveDirectoryIterator($recPath, FilesystemIterator::SKIP_DOTS);
|
|
235
|
+
$filter = new RecursiveCallbackFilterIterator($dir, function (SplFileInfo $current) use ($ignoreDirs) {
|
|
236
|
+
if ($current->isDir()) {
|
|
237
|
+
return !in_array($current->getFilename(), $ignoreDirs, true);
|
|
238
|
+
}
|
|
239
|
+
return true;
|
|
240
|
+
});
|
|
241
|
+
$it = new RecursiveIteratorIterator($filter, RecursiveIteratorIterator::SELF_FIRST);
|
|
242
|
+
foreach ($it as $file) {
|
|
243
|
+
/** @var SplFileInfo $file */
|
|
244
|
+
if ($file->isFile() && $file->getFilename() === self::SEED_FILE) {
|
|
245
|
+
return $file->getRealPath();
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
return false;
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
|
|
253
|
+
public static function findRoot(string $path, bool $abs = false)
|
|
254
|
+
{
|
|
255
|
+
if(!$seed = self::findSeed($path)) return false;
|
|
256
|
+
if(!$root = pathinfo($seed, PATHINFO_DIRNAME)) return false;
|
|
257
|
+
return $abs ? $root . S : get_relative_path($path, $root);
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
|
|
261
|
+
public function addPlugin($file) {
|
|
262
|
+
array_push($this->plugins, $file);
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
|
|
268
|
+
|
|
269
|
+
|
|
270
|
+
|
|
271
|
+
|
|
272
|
+
|
|
273
|
+
|
|
274
|
+
|
|
275
|
+
|
|
276
|
+
|
|
277
|
+
|
|
278
|
+
|
|
279
|
+
|
|
280
|
+
function error($str)
|
|
281
|
+
{
|
|
282
|
+
file_put_contents('php://stderr', json_encode([
|
|
283
|
+
'success' => false,
|
|
284
|
+
'error' => trim($str)
|
|
285
|
+
], JSON_PRETTY_PRINT), FILE_APPEND);
|
|
286
|
+
exit(1);
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
|
|
290
|
+
function succeed($files)
|
|
291
|
+
{
|
|
292
|
+
file_put_contents('php://stdout', json_encode([
|
|
293
|
+
'success' => true,
|
|
294
|
+
'files' => $files
|
|
295
|
+
], JSON_PRETTY_PRINT), FILE_APPEND);
|
|
296
|
+
exit(0);
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
|
|
300
|
+
|
|
301
|
+
/**
|
|
302
|
+
* Recursevly walk a folder and yield files corresponding to the pattern
|
|
303
|
+
*
|
|
304
|
+
* @param mixed $path Path and pattern to walk through
|
|
305
|
+
* @return iterable
|
|
306
|
+
*/
|
|
307
|
+
function dig($path): iterable
|
|
308
|
+
{
|
|
309
|
+
$patt = pathinfo($path, PATHINFO_BASENAME);
|
|
310
|
+
$path = pathinfo($path, PATHINFO_DIRNAME);
|
|
311
|
+
if ($path = realpath($path)) {
|
|
312
|
+
$path .= S;
|
|
313
|
+
foreach (glob($path . $patt) as $file) {
|
|
314
|
+
if (!is_dir($file)) yield $file;
|
|
315
|
+
}
|
|
316
|
+
foreach (glob($path . '*', GLOB_ONLYDIR) as $dir) {
|
|
317
|
+
foreach (call_user_func(__FUNCTION__, $dir . S . $patt) as $file) yield $file;
|
|
318
|
+
}
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
|
|
323
|
+
|
|
324
|
+
|
|
325
|
+
/**
|
|
326
|
+
* Parse the first DOCKBLOCK of a file and return attributes as an object
|
|
327
|
+
*
|
|
328
|
+
* @param mixed $file PHP File to be parse
|
|
329
|
+
* @return void
|
|
330
|
+
*/
|
|
331
|
+
function php_file_info($file)
|
|
332
|
+
{
|
|
333
|
+
static $files = [];
|
|
334
|
+
if (!$file = realpath($file)) return false;
|
|
335
|
+
if (!isset($files[$file])) {
|
|
336
|
+
$tokens = token_get_all(file_get_contents($file));
|
|
337
|
+
foreach ($tokens as $tok) {
|
|
338
|
+
if (!is_array($tok)) continue;
|
|
339
|
+
if ($tok[0] == T_DOC_COMMENT) {
|
|
340
|
+
$block = $tok[1];
|
|
341
|
+
break;
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
if (empty($block)) return new stdClass;
|
|
345
|
+
if (!preg_match_all('#@([a-z0-9]+)[\s\t]+([^\n]+)#msi', $block, $m)) $files[$file] = new stdClass;
|
|
346
|
+
else {
|
|
347
|
+
foreach ($m[1] as $k => $v) $info[trim($v)] = trim($m[2][$k]);
|
|
348
|
+
$files[$file] = (object)$info;
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
return $files[$file];
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
|
|
355
|
+
|
|
356
|
+
/**
|
|
357
|
+
* replace_tags
|
|
358
|
+
*
|
|
359
|
+
* @param mixed $tag
|
|
360
|
+
* @param mixed $contents
|
|
361
|
+
* @param mixed $clb
|
|
362
|
+
* @return void
|
|
363
|
+
*/
|
|
364
|
+
function replace_tags($tag, $contents, $clb)
|
|
365
|
+
{
|
|
366
|
+
$contents = preg_replace_callback('#<' . preg_quote($tag, '#') . '([^>]*)>(.*?)</' . preg_quote($tag, '#') . '>#msi', function ($m) use ($clb) {
|
|
367
|
+
return call_user_func($clb, $m[0], parse_html_attributes($m[1]), $m[2]);
|
|
368
|
+
}, $contents);
|
|
369
|
+
return $contents;
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
|
|
373
|
+
|
|
374
|
+
/**
|
|
375
|
+
* parse_html_attributes
|
|
376
|
+
*
|
|
377
|
+
* @param mixed $attributes
|
|
378
|
+
* @return void
|
|
379
|
+
*/
|
|
380
|
+
function parse_html_attributes($attributes)
|
|
381
|
+
{
|
|
382
|
+
if (preg_match_all('#(\\w+)\s*=\\s*("[^"]*"|\'[^\']*\'|[^"\'\\s>]*)#i', $attributes, $m)) {
|
|
383
|
+
foreach ($m[1] as $k => $key) {
|
|
384
|
+
$attrs[strtolower($key)] = stripslashes(substr($m[2][$k], 1, -1));;
|
|
385
|
+
}
|
|
386
|
+
}
|
|
387
|
+
return isset($attrs) ? $attrs : [];
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
|
|
391
|
+
|
|
392
|
+
|
|
393
|
+
/**
|
|
394
|
+
* get_relative_path
|
|
395
|
+
*
|
|
396
|
+
* @param mixed $from
|
|
397
|
+
* @param mixed $to
|
|
398
|
+
* @return string
|
|
399
|
+
*/
|
|
400
|
+
function get_relative_path($from, $to)
|
|
401
|
+
{
|
|
402
|
+
$from = is_dir($from) ? rtrim($from, '\/') . '/' : $from;
|
|
403
|
+
$to = is_dir($to) ? rtrim($to, '\/') . '/' : $to;
|
|
404
|
+
$from = str_replace('\\', '/', $from);
|
|
405
|
+
$to = str_replace('\\', '/', $to);
|
|
406
|
+
$from = explode('/', $from);
|
|
407
|
+
$to = explode('/', $to);
|
|
408
|
+
$relPath = $to;
|
|
409
|
+
|
|
410
|
+
foreach ($from as $depth => $dir) {
|
|
411
|
+
if ($dir === $to[$depth]) {
|
|
412
|
+
array_shift($relPath);
|
|
413
|
+
} else {
|
|
414
|
+
$remaining = count($from) - $depth;
|
|
415
|
+
if ($remaining > 1) {
|
|
416
|
+
$padLength = (count($relPath) + $remaining - 1) * -1;
|
|
417
|
+
$relPath = array_pad($relPath, $padLength, '..');
|
|
418
|
+
break;
|
|
419
|
+
} else {
|
|
420
|
+
$relPath[0] = './' . $relPath[0];
|
|
421
|
+
}
|
|
422
|
+
}
|
|
423
|
+
}
|
|
424
|
+
return $relPath ? implode('/', $relPath) : './';
|
|
425
|
+
}
|
|
426
|
+
|