This repository has been archived by the owner on Oct 20, 2023. It is now read-only.
/
ParserRegistry.php
109 lines (96 loc) · 2.42 KB
/
ParserRegistry.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
<?php
/**
* Copyright 2010-2012, Cake Development Corporation (http://cakedc.com)
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright 2010-2012, Cake Development Corporation (http://cakedc.com)
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
/**
* Parser Registry. A factory to return parser instances from available parsers listed using the Configure class
*
* To setup a new parser use
*
* Configure::write('Parsers.my_parser' => array(
* 'name' => 'MyParser',
* 'className' => 'MyPlugin.MyParser'
* ))
*
* @package markup_parsers
* @subpackage markup_parsers.libs
*/
class ParserRegistry {
/**
* Available parsers
*
* @var array
*/
protected static $_parsers = null;
/**
* Available parsers
*
* @var array
*/
protected static $_availableParsers = array();
/**
* Initializes the registry by loading all the existing parsers
*
* @return void
*/
protected static function _init() {
$defaults = array(
'markdown' => array(
'name' => 'Markdown',
'className' => 'MarkupParsers.Markdown'),
'bbcode' => array(
'name' => 'BBCode',
'className' => 'MarkupParsers.Bbcode'),
'html' => array(
'name' => 'Html',
'className' => 'MarkupParsers.Html'),
'textile' => array(
'name' => 'Textile',
'className' => 'MarkupParsers.Textile'
)
);
if (empty(self::$_availableParsers)) {
self::$_availableParsers = array_merge($defaults, (array)Configure::read('Parsers'));
}
}
/**
* Returns the parsers that implements the passed interfaces
*
* @return array
*/
public static function getParsers() {
self::_init();
$result = array();
foreach (self::$_availableParsers as $key => $parser) {
$result[$key] = $parser['name'];
}
return $result;
}
/**
* Returns a parser instance
*
* @param string parser key
* @return object Parser instance
*
*/
public static function getParser($parser = '') {
self::_init();
if (empty(self::$_parsers[$parser])) {
list($plugin, $class) = pluginSplit(self::$_availableParsers[$parser]['className']);
$location = (!empty($plugin) ? $plugin . '.' : '' ) . 'Parser';
$class .= 'Parser';
App::uses($class, $location);
if (!class_exists($class)) {
throw new Exception(__d('markup_parsers', 'Invalid Parser', true));
}
self::$_parsers[$parser] = new $class();
}
return self::$_parsers[$parser];
}
}