-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgrid.php
74 lines (60 loc) · 1.71 KB
/
grid.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
74
<?php
use Herbie\DI;
use Herbie\Hook;
class GridPlugin
{
private static $templates = [];
public static function install()
{
self::$templates = DI::get('Config')->get('plugins.config.grid.templates', []);
Hook::attach('renderContent', ['GridPlugin', 'renderContent']);
}
public static function renderContent($content)
{
$replaced = preg_replace_callback(
'/-{2}\s+grid\s+(.+?)-{2}(.*?)-{2}\s+grid\s+-{2}/msi',
['GridPlugin', 'replace'],
$content
);
if (!is_null($replaced)) {
return $replaced;
}
return $content;
}
/**
* @param array $matches
* @return string
*/
private static function replace($matches)
{
if (empty($matches) || (count($matches) <> 3)) {
return null;
}
$key = trim($matches[1]);
$content = $matches[2];
// no template defined
if (!array_key_exists($key, self::$templates)) {
// return content as it is
return $content;
}
$template = self::$templates[$key];
// split cols
$cols = preg_split('/--\r?\n/', $content);
$html = '';
// replace cols
foreach ($cols as $i => $col) {
if (isset($template['cols'][$i])) {
$html .= str_replace(['{col}', '{index}'], [$col, $i+1], $template['cols'][$i]);
} else {
$html .= $col;
}
}
// replace row
if (array_key_exists('row', $template)) {
return str_replace('{row}', $html, $template['row']);
}
// give it back
return $html;
}
}
GridPlugin::install();