|
1
|
|
|
<?php |
|
|
|
|
|
|
2
|
|
|
/** |
|
3
|
|
|
* Copyright (c) 2017 Ryan Parman <http://ryanparman.com>. |
|
4
|
|
|
* Copyright (c) 2017 Contributors. |
|
5
|
|
|
* |
|
6
|
|
|
* http://opensource.org/licenses/Apache2.0 |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
declare(strict_types=1); |
|
10
|
|
|
|
|
11
|
|
|
namespace SimplePie; |
|
12
|
|
|
|
|
13
|
|
|
use Psr\Http\Message\StreamInterface; |
|
14
|
|
|
use Psr\Log\NullLogger; |
|
15
|
|
|
use SimplePie\Configuration\SetLoggerInterface; |
|
16
|
|
|
use SimplePie\Middleware\Xml\Atom; |
|
17
|
|
|
use SimplePie\Mixin as T; |
|
18
|
|
|
use SimplePie\Parser\Xml as XmlParser; |
|
19
|
|
|
|
|
20
|
|
|
\define('SIMPLEPIE_ROOT', __DIR__); |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* `SimplePie\SimplePie` is the primary entry point for SimplePie NG. |
|
24
|
|
|
*/ |
|
25
|
|
|
class SimplePie implements SetLoggerInterface |
|
26
|
|
|
{ |
|
27
|
|
|
use T\LoggerTrait; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Bitwise libxml options to use for parsing XML. |
|
31
|
|
|
* |
|
32
|
|
|
* @var int |
|
33
|
|
|
*/ |
|
34
|
|
|
protected $libxml; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* The handler stack which contains registered middleware. |
|
38
|
|
|
* |
|
39
|
|
|
* @var HandlerStackInterface |
|
40
|
|
|
*/ |
|
41
|
|
|
protected $middleware; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Constructs a new instance of this class. |
|
45
|
|
|
*/ |
|
46
|
|
|
public function __construct() |
|
47
|
|
|
{ |
|
48
|
|
|
// Default logger |
|
49
|
|
|
$this->logger = new NullLogger(); |
|
50
|
|
|
|
|
51
|
|
|
// Default middleware stack |
|
52
|
|
|
$this->middleware = new HandlerStack(); |
|
53
|
|
|
$this->middleware->setLogger($this->getLogger()); |
|
54
|
|
|
$this->middleware->append(new Atom(), 'atom'); |
|
55
|
|
|
|
|
56
|
|
|
// Default libxml2 settings |
|
57
|
|
|
$this->libxml = LIBXML_HTML_NOIMPLIED // Required, or things crash. |
|
58
|
|
|
| LIBXML_BIGLINES |
|
59
|
|
|
| LIBXML_COMPACT |
|
60
|
|
|
| LIBXML_HTML_NODEFDTD |
|
61
|
|
|
| LIBXML_NOBLANKS |
|
62
|
|
|
| LIBXML_NOENT |
|
63
|
|
|
| LIBXML_NOXMLDECL |
|
64
|
|
|
| LIBXML_NSCLEAN |
|
65
|
|
|
| LIBXML_PARSEHUGE; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
//-------------------------------------------------------------------------- |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Sets the libxml value to use for parsing XML. |
|
72
|
|
|
* |
|
73
|
|
|
* @param int $libxml |
|
74
|
|
|
* |
|
75
|
|
|
* @return int |
|
|
|
|
|
|
76
|
|
|
*/ |
|
77
|
|
|
public function setLibxml(int $libxml) |
|
78
|
|
|
{ |
|
79
|
|
|
$this->libxml = $libxml; |
|
80
|
|
|
|
|
81
|
|
|
// What are the libxml2 configurations? |
|
82
|
|
|
$this->logger->debug(\sprintf( |
|
83
|
|
|
'Libxml configuration has a bitwise value of `%s`.%s', |
|
84
|
|
|
$this->libxml, |
|
85
|
|
|
(4792582 === $this->libxml) |
|
86
|
|
|
? ' (This is the default configuration.)' |
|
87
|
|
|
: '' |
|
88
|
|
|
)); |
|
89
|
|
|
|
|
90
|
|
|
return $this; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* Gets the libxml value to use for parsing XML. |
|
95
|
|
|
* |
|
96
|
|
|
* @return int |
|
97
|
|
|
*/ |
|
98
|
|
|
public function getLibxml(): int |
|
99
|
|
|
{ |
|
100
|
|
|
return $this->libxml; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* Sets the handler stack which contains registered middleware. |
|
105
|
|
|
* |
|
106
|
|
|
* @param HandlerStackInterface $handlerStack |
|
107
|
|
|
* |
|
108
|
|
|
* @return self |
|
109
|
|
|
*/ |
|
110
|
|
|
public function setMiddlewareStack(HandlerStackInterface $handlerStack) |
|
111
|
|
|
{ |
|
112
|
|
|
$this->middleware = $handlerStack; |
|
113
|
|
|
$this->middleware->setLogger($this->getLogger()); |
|
114
|
|
|
|
|
115
|
|
|
return $this; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* Gets the handler stack which contains registered middleware. |
|
120
|
|
|
* |
|
121
|
|
|
* @return HandlerStackInterface |
|
122
|
|
|
*/ |
|
123
|
|
|
public function getMiddlewareStack(): HandlerStackInterface |
|
124
|
|
|
{ |
|
125
|
|
|
return $this->middleware; |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
//-------------------------------------------------------------------------- |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* Parses content which is known to be valid XML and is encoded as UTF-8. |
|
132
|
|
|
* |
|
133
|
|
|
* @param StreamInterface $stream A PSR-7 `StreamInterface` which is typically returned by the |
|
134
|
|
|
* `getBody()` method of a `ResponseInterface` class. |
|
135
|
|
|
* @param bool $handleHtmlEntitiesInXml Whether or not SimplePie should pre-parse the XML as HTML to |
|
136
|
|
|
* resolve the entities. A value of `true` means that SimplePie |
|
137
|
|
|
* should inject the entity definitions. A value of `false` means |
|
138
|
|
|
* that SimplePie should NOT inject the entity definitions. The |
|
139
|
|
|
* default value is `false`. |
|
140
|
|
|
* |
|
141
|
|
|
* @return XmlParser |
|
142
|
|
|
*/ |
|
143
|
|
|
public function parseXml(StreamInterface $stream, bool $handleHtmlEntitiesInXml = false): XmlParser |
|
144
|
|
|
{ |
|
145
|
|
|
$parser = new XmlParser( |
|
146
|
|
|
$stream, |
|
147
|
|
|
$this->logger, |
|
148
|
|
|
$this->middleware, |
|
149
|
|
|
$this->libxml, |
|
150
|
|
|
$handleHtmlEntitiesInXml |
|
151
|
|
|
); |
|
152
|
|
|
|
|
153
|
|
|
return $parser; |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* Parses content which is known to be valid JSON and is encoded as UTF-8. |
|
158
|
|
|
* |
|
159
|
|
|
* @param StreamInterface $stream A PSR-7 `StreamInterface` which is typically returned by the |
|
160
|
|
|
* `getBody()` method of a `ResponseInterface` class. |
|
161
|
|
|
* |
|
162
|
|
|
* @return JsonParser |
|
|
|
|
|
|
163
|
|
|
*/ |
|
164
|
|
|
public function parseJson(StreamInterface $stream) |
|
165
|
|
|
{ |
|
166
|
|
|
return $stream->getContents(); |
|
167
|
|
|
} |
|
168
|
|
|
} |
|
169
|
|
|
|
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.