|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* micrometa |
|
5
|
|
|
* |
|
6
|
|
|
* @category Jkphl |
|
7
|
|
|
* @package Jkphl\Micrometa |
|
8
|
|
|
* @subpackage Jkphl\Micrometa\Ports |
|
9
|
|
|
* @author Joschi Kuphal <[email protected]> / @jkphl |
|
10
|
|
|
* @copyright Copyright © 2018 Joschi Kuphal <[email protected]> / @jkphl |
|
11
|
|
|
* @license http://opensource.org/licenses/MIT The MIT License (MIT) |
|
12
|
|
|
*/ |
|
13
|
|
|
|
|
14
|
|
|
/*********************************************************************************** |
|
15
|
|
|
* The MIT License (MIT) |
|
16
|
|
|
* |
|
17
|
|
|
* Copyright © 2018 Joschi Kuphal <[email protected]> / @jkphl |
|
18
|
|
|
* |
|
19
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy of |
|
20
|
|
|
* this software and associated documentation files (the "Software"), to deal in |
|
21
|
|
|
* the Software without restriction, including without limitation the rights to |
|
22
|
|
|
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of |
|
23
|
|
|
* the Software, and to permit persons to whom the Software is furnished to do so, |
|
24
|
|
|
* subject to the following conditions: |
|
25
|
|
|
* |
|
26
|
|
|
* The above copyright notice and this permission notice shall be included in all |
|
27
|
|
|
* copies or substantial portions of the Software. |
|
28
|
|
|
* |
|
29
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|
30
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS |
|
31
|
|
|
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR |
|
32
|
|
|
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER |
|
33
|
|
|
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
|
34
|
|
|
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
|
35
|
|
|
***********************************************************************************/ |
|
36
|
|
|
|
|
37
|
|
|
namespace Jkphl\Micrometa\Ports; |
|
38
|
|
|
|
|
39
|
|
|
use Jkphl\Domfactory\Ports\Dom; |
|
40
|
|
|
use Jkphl\Micrometa\Application\Service\ExtractorService; |
|
41
|
|
|
use Jkphl\Micrometa\Infrastructure\Factory\ItemFactory; |
|
42
|
|
|
use Jkphl\Micrometa\Infrastructure\Factory\ParserFactory; |
|
43
|
|
|
use Jkphl\Micrometa\Infrastructure\Logger\ExceptionLogger; |
|
44
|
|
|
use Jkphl\Micrometa\Ports\Item\ItemInterface; |
|
45
|
|
|
use Jkphl\Micrometa\Ports\Item\ItemObjectModel; |
|
46
|
|
|
use Jkphl\Micrometa\Ports\Item\ItemObjectModelInterface; |
|
47
|
|
|
use League\Uri\Http; |
|
48
|
|
|
use Psr\Log\LoggerInterface; |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Parser |
|
52
|
|
|
* |
|
53
|
|
|
* @package Jkphl\Micrometa |
|
54
|
|
|
* @subpackage Jkphl\Micrometa\Ports |
|
55
|
|
|
*/ |
|
56
|
|
|
class Parser |
|
57
|
|
|
{ |
|
58
|
|
|
/** |
|
59
|
|
|
* Micro information formats |
|
60
|
|
|
* |
|
61
|
|
|
* @var int |
|
62
|
|
|
*/ |
|
63
|
|
|
protected $formats; |
|
64
|
|
|
/** |
|
65
|
|
|
* Logger |
|
66
|
|
|
* |
|
67
|
|
|
* @var LoggerInterface |
|
68
|
|
|
*/ |
|
69
|
|
|
protected $logger; |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Parser constructor |
|
73
|
|
|
* |
|
74
|
|
|
* @param int $formats Micro information formats to extract |
|
75
|
|
|
* @param LoggerInterface|null $logger PSR-3 compatible logger |
|
76
|
|
|
* |
|
77
|
|
|
* @api |
|
78
|
|
|
*/ |
|
79
|
2 |
|
public function __construct($formats = Format::ALL, LoggerInterface $logger = null) |
|
80
|
|
|
{ |
|
81
|
2 |
|
$this->formats = $formats; |
|
82
|
2 |
|
$this->logger = $logger ?: new ExceptionLogger(); |
|
83
|
2 |
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Extract micro information items out of a URI or piece of source |
|
87
|
|
|
* |
|
88
|
|
|
* @param string $uri URI |
|
89
|
|
|
* @param string|null $source Source code |
|
90
|
|
|
* @param int $formats Micro information formats to extract |
|
91
|
|
|
* @param array $httpOptions HTTP request options |
|
92
|
|
|
* |
|
93
|
|
|
* @return ItemObjectModelInterface Item object model |
|
94
|
|
|
* @api |
|
95
|
|
|
*/ |
|
96
|
2 |
|
public function __invoke($uri, $source = null, $formats = null, array $httpOptions = []) |
|
97
|
|
|
{ |
|
98
|
2 |
|
$dom = new \DOMDocument(); |
|
99
|
2 |
|
$items = []; |
|
100
|
|
|
|
|
101
|
|
|
try { |
|
102
|
2 |
|
$parsers = ParserFactory::createParsersFromFormats( |
|
103
|
2 |
|
intval($formats ?: $this->formats), |
|
104
|
2 |
|
Http::createFromString($uri), |
|
105
|
2 |
|
$this->logger |
|
106
|
|
|
); |
|
107
|
2 |
|
$dom = $this->createDom($uri, $source, $httpOptions); |
|
108
|
2 |
|
$items = $this->extractItems($dom, $parsers); |
|
109
|
|
|
|
|
110
|
|
|
// In case of exceptions: Log if possible |
|
111
|
1 |
|
} catch (\Exception $exception) { |
|
112
|
1 |
|
$this->logger->critical($exception->getMessage(), ['exception' => $exception]); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
1 |
|
return new ItemObjectModel($dom, $items); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* Create the DOM document to parse |
|
120
|
|
|
* |
|
121
|
|
|
* @param string $uri URI |
|
122
|
|
|
* @param string|null $source Source code |
|
123
|
|
|
* @param array $httpOptions HTTP request options |
|
124
|
|
|
* |
|
125
|
|
|
* @return \DOMDocument DOM document |
|
126
|
|
|
*/ |
|
127
|
2 |
|
protected function createDom($uri, $source = null, array $httpOptions = []) |
|
128
|
|
|
{ |
|
129
|
2 |
|
return (($source !== null) && strlen(trim($source))) ? |
|
130
|
2 |
|
Dom::createFromString($source) : Dom::createFromUri($uri, $httpOptions); |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* Extract all items from a DOM using particular parsers |
|
135
|
|
|
* |
|
136
|
|
|
* @param \DOMDocument $dom DOM document |
|
137
|
|
|
* @param \Generator $parsers Parsers |
|
138
|
|
|
* |
|
139
|
|
|
* @return ItemInterface[] Items |
|
140
|
|
|
*/ |
|
141
|
2 |
|
protected function extractItems(\DOMDocument $dom, \Generator $parsers) |
|
142
|
|
|
{ |
|
143
|
2 |
|
$items = []; |
|
144
|
2 |
|
$extractor = new ExtractorService(); |
|
145
|
2 |
|
foreach ($parsers as $parser) { |
|
146
|
2 |
|
$results = $extractor->extract($dom, $parser); |
|
147
|
1 |
|
$items = array_merge($items, ItemFactory::createFromApplicationItems($results->getItems())); |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
1 |
|
return $items; |
|
151
|
|
|
} |
|
152
|
|
|
} |
|
153
|
|
|
|