This repository has been archived by the owner on Nov 8, 2023. It is now read-only.
forked from Mac2/libiwparser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathParserFactoryConfigC.php
168 lines (142 loc) · 5.46 KB
/
ParserFactoryConfigC.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
<?php
/*
* ----------------------------------------------------------------------------
* "THE BEER-WARE LICENSE" (Revision 42):
* <benjamin.woester@googlemail.com> wrote this file. As long as you retain
* this notice you can do whatever you want with this stuff. If we meet some
* day, and you think this stuff is worth it, you can buy me a beer in return.
* Benjamin Wöster
* ----------------------------------------------------------------------------
*/
/**
* @author Benjamin Wöster <benjamin.woester@googlemail.com>
* @package libIwParsers
* @subpackage helpers
*/
/////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////
class ParserFactoryConfigC implements ParserFactoryI
{
static private $_instance = null;
private $_aRegisteredParsers = array();
///////////////////////////////////////////////////////////////////////
/**
* constructor of the factory.
*
* Use getInstance() to get an instance of the factory.
*/
public function __construct()
{
$aConfiguredParsers = ConfigC::get('lib.aRegisteredParsers');
$rcParser = null;
foreach ($aConfiguredParsers as $parserInfo) {
require_once($parserInfo['filename']);
$rcParser = new ReflectionClass($parserInfo['classname']);
if ($rcParser->implementsInterface('ParserI')
&& $rcParser->isInstantiable()
) {
$parser = $rcParser->newInstance();
$this->_aRegisteredParsers[$parser->getIdentifier()] = $parser;
}
}
}
///////////////////////////////////////////////////////////////////////
/**
* Returns an instance of the factory.
*
* @return object instance of the factory.
*/
static public function getInstance()
{
if (self::$_instance === null) {
self::$_instance = new ParserFactoryConfigC();
}
return self::$_instance;
}
///////////////////////////////////////////////////////////////////////
/**
* Checks for all known parsers if they can parse the provided text.
*
* @param $text string, the text to be parsed
*
* @return array of strings, ids of the parsers, that can parse the
* provided text.
*/
public function getParserIdsFor($text)
{
$retVal = array();
$this->cleanupText($text);
foreach ($this->_aRegisteredParsers as $parserId => $parser) {
if ($parser->canParseText($text)) {
$retVal[] = $parserId;
}
}
return $retVal;
}
///////////////////////////////////////////////////////////////////////
/**
* Gets an instance of the first parser the factory knows, that can
* parse the provided text. You can define the parser that is to be
* used to parse the text, by providing its id as second parameter.
*
* @param $text string, the text to be parsed
* @param $parserId string, the id of a known parser that shall be used
* to parse the provided text
*
* @return mixed
* ParserI - instance of the parser that can be used to parse
* the provided text.
* boolean - false, if
* 1) no parser was found that could parse the provided
* text
* 2) you provided a parser id that does not exist
* 3) you provided a parser id of an existent parser, but
* that parser isn't able to parse the provided text.
*/
public function getParser($text, $parserId = '')
{
$retVal = false;
if ($parserId === '') {
foreach ($this->_aRegisteredParsers as $parserId => $parser) {
if ($parser->canParseText($text)) {
$retVal = $this->_aRegisteredParsers[$parserId];
break;
}
}
} elseif ($parserId !== ''
&& array_key_exists($parserId, $this->_aRegisteredParsers)
&& $this->_aRegisteredParsers[$parserId]->canParseText($text)
) {
$retVal = $this->_aRegisteredParsers[$parserId];
}
return $retVal;
}
///////////////////////////////////////////////////////////////////////
public function getParserList()
{
$list = array();
foreach ($this->_aRegisteredParsers as $parserId => $parser) {
$list[$parserId] = $parser->getName();
}
return $list;
}
///////////////////////////////////////////////////////////////////////
protected function cleanupText(&$text)
{
//replace different line endings by \n (linux)
$replacements = array(
chr(13) . chr(10) => chr(10), //windows
chr(13) => chr(10), //mac
);
$text = str_replace(
array_keys($replacements),
array_values($replacements),
$text
);
//undo magic quotes
if (ini_get("magic_quotes_gpc") == 1) {
$text = stripslashes($text);
}
}
}