forked from mikestowe/php-ramlMerge
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathramlMerge.php
73 lines (62 loc) · 1.81 KB
/
ramlMerge.php
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
<?php
function doInclude ($folder, $file, $tabIndex = '') {
$contents = @file_get_contents($file);
if (!$contents) {
return "\n\n# Unable to include " . $file . "\n\n";
}
if ($tabIndex) {
$contents = $tabIndex . str_replace("\n", "\n" . $tabIndex, $contents);
}
$lines = explode("\n", $contents);
$result = "";
foreach($lines as $line) {
$line = preg_replace_callback('/^(([ \t]*)([^#\n]+)[ \t]*[ \t]*)\!include ((.+)\/([^\s]+))$/i',
function($matches) use ($folder) {
$pre = $matches[1];
$spacing = $matches[2];
$resource = $matches[4];
$containerFolder = $matches[5];
$file = $matches[6];
$cap = " \n";
// Resolve where the included file is
if (!preg_match("/^((https?:\/\/)|\/)/i", $file)) {
// File resource
$containerFolder = realpath($folder . "/" . $containerFolder);
$file = realpath($containerFolder . "/" . $file);
} else {
// URL resource
$containerFolder = $folder;
$file = $resource;
}
// Load
$subContent = doInclude($containerFolder, $file, $spacing . " ");
// Add proper connector to included file
if (requiresPipe($pre, $subContent)) {
$cap = "| \n";
}
// Join
return $pre . $cap . $subContent;
},
$line);
$result .= $line . "\n";
}
return $result;
}
function requiresPipe($pre, $subContent) {
if (strpos($pre, "example") || strpos($pre, "scheme") || strpos($pre, "content")) {
return true;
} else {
$i = 0;
$subLines = explode("\n", $subContent);
while (isset($subLines[$i]) && !preg_match("/[^\s]/i", $subLines[$i])) {
$i++;
}
if (strpos($subLines[$i], '{')) {
return true;
}
}
return false;
}
$file = $argv[1];
echo doInclude(dirname($file), $file) . "\n\n\n\n# -----------\n# Merged with ramlMerge.php\n# http://www.mikestowe.com\n\n";
?>