forked from scandio/lmvc-modules
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSnippetHandler.php
95 lines (86 loc) · 3.1 KB
/
SnippetHandler.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
<?php
namespace Scandio\lmvc\modules\snippets;
use Scandio\lmvc\LVC;
use Scandio\lmvc\modules\security\Security;
use Scandio\lmvc\utils\string\StringUtils;
abstract class SnippetHandler
{
protected static $snippetFile;
protected static $prefix = '';
protected static $snippetPath = array();
/**
* used to include the snippets - behaviour may be overridden by private or public static functions
*
* @static
* @param string $name camelCasedName of the snippet
* @param array $params parameters passed to the snippet
* @return mixed|string the result of your private static function, an ErrorMessage or just an empty string
*/
public static function __callStatic($name, $params)
{
$result = "";
if (method_exists(get_called_class(), $name)) {
$result = call_user_func_array('static::' . $name, $params);
} else {
self::$snippetFile = self::searchSnippet(static::$prefix . StringUtils::camelCaseTo($name) . '.html');
if (self::$snippetFile) {
$app = LVC::get(); // should be available in the snippet's scope as in views for convenience
include(self::$snippetFile);
} elseif (preg_match('/^get[A-Z]/', $name)) {
ob_start();
echo static::__callStatic(lcfirst(substr($name, 3)), $params);
$result = ob_get_contents();
ob_clean();
} else {
$result = PHP_EOL . "<!-- No snippet file for " . get_called_class() . "::" . $name . "() exists. -->" . PHP_EOL;
}
}
return $result;
}
/**
* registers a new snippet directory to search for the snippets
*
* @static
* @param array|string $path specifies the directory to register
*/
public static function registerSnippetDirectory($path)
{
if (is_array($path)) {
$snippetPath = implode(DIRECTORY_SEPARATOR, $path);
} elseif (is_string($path)) {
$snippetPath = $path;
} else {
echo PHP_EOL . "<!-- Couldn't register SnippetDirectory:" . PHP_EOL;
var_dump($path);
echo "-->" . PHP_EOL;
return;
}
$class = get_called_class();
if (isset(self::$snippetPath[$class])) {
array_unshift(self::$snippetPath[$class], $snippetPath);
} else {
self::$snippetPath[$class] = array($snippetPath);
}
}
/**
* searches for the snippet in the registered directories
*
* @static
* @param string $snippet the snippet to search for
* @return string|bool either the snippet's full path or false
*/
private static function searchSnippet($snippet)
{
$class = get_called_class();
if (!isset(self::$snippetPath[$class])) {
return false;
}
foreach (self::$snippetPath[$class] as $path) {
$snippetPath = $path . DIRECTORY_SEPARATOR . $snippet;
if (file_exists($snippetPath)) {
return $snippetPath;
}
}
return false;
}
}