Link.php
1.59 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
<?php
namespace Nextend\Framework\Parser;
use Nextend\Framework\Parser\Link\ParserInterface;
use Nextend\Framework\ResourceTranslator\ResourceTranslator;
class Link {
/**
* @var ParserInterface[]
*/
private static $parsers = array();
public static $registeredNamespaces = array(
'\\Nextend\\Framework\\Parser\\Link\\',
'\\Nextend\\SmartSlider3\\Parser\\Link\\'
);
public static function parse($url, &$attributes, $isEditor = false) {
if ($url == '#' || $isEditor) {
$attributes['onclick'] = "return false;";
} else {
preg_match('/^([a-zA-Z]+)\[(.*)]$/', $url, $matches);
if (!empty($matches)) {
$matches[1] = ucfirst($matches[1]);
$parser = self::getParser($matches[1]);
if ($parser) {
$url = $parser->parse($matches[2], $attributes);
}
} else {
$url = ResourceTranslator::toUrl($url);
}
}
return $url;
}
public static function getParser($className) {
if (!isset(self::$parsers[$className])) {
foreach (self::$registeredNamespaces as $namespace) {
$class = $namespace . $className;
if (class_exists($class)) {
self::$parsers[$className] = new $class();
break;
}
}
if (!isset(self::$parsers[$className])) {
self::$parsers[$className] = false;
}
}
return self::$parsers[$className];
}
}