1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* apparat-object |
5
|
|
|
* |
6
|
|
|
* @category Apparat |
7
|
|
|
* @package Apparat\Object |
8
|
|
|
* @subpackage Apparat\Object\Infrastructure |
9
|
|
|
* @author Joschi Kuphal <[email protected]> / @jkphl |
10
|
|
|
* @copyright Copyright © 2016 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 © 2016 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 Apparat\Object\Infrastructure\Utilities; |
38
|
|
|
|
39
|
|
|
use Apparat\Kernel\Ports\Kernel; |
40
|
|
|
use Apparat\Object\Application\Contract\CommonMarkPayloadProcessorInterface; |
41
|
|
|
use Apparat\Object\Application\Model\Object\AbstractCommonMarkObject; |
42
|
|
|
use Apparat\Object\Ports\Types\Relation; |
43
|
|
|
use League\CommonMark\Block\Element\Document; |
44
|
|
|
use League\CommonMark\DocParser; |
45
|
|
|
use League\CommonMark\DocumentProcessorInterface; |
46
|
|
|
use League\CommonMark\Environment; |
47
|
|
|
use League\CommonMark\Inline\Element\Image; |
48
|
|
|
use League\CommonMark\Inline\Element\Link; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* CommonMark payload processor |
52
|
|
|
* |
53
|
|
|
* @package Apparat\Object |
54
|
|
|
* @subpackage Apparat\Object\Infrastructure |
55
|
|
|
*/ |
56
|
|
|
class CommonMarkPayloadProcessor extends AbstractPayloadProcessor implements |
57
|
|
|
CommonMarkPayloadProcessorInterface, |
58
|
|
|
DocumentProcessorInterface |
59
|
|
|
{ |
60
|
|
|
/** |
61
|
|
|
* Mailto URL scheme |
62
|
|
|
* |
63
|
|
|
* @var string |
64
|
|
|
*/ |
65
|
|
|
const SCHEME_MAILTO = 'mailto'; |
66
|
|
|
/** |
67
|
|
|
* List of refers-to relation URLs |
68
|
|
|
* |
69
|
|
|
* @var array |
70
|
|
|
*/ |
71
|
|
|
protected $refersTo; |
72
|
|
|
/** |
73
|
|
|
* List of embeds relation URLs |
74
|
|
|
* |
75
|
|
|
* @var array |
76
|
|
|
*/ |
77
|
|
|
protected $embeds; |
78
|
|
|
/** |
79
|
|
|
* Owning CommonMark object |
80
|
|
|
* |
81
|
|
|
* @var AbstractCommonMarkObject |
82
|
|
|
*/ |
83
|
|
|
protected $object; |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Process the payload of an object |
87
|
|
|
* |
88
|
|
|
* @param string $payload Payload |
89
|
|
|
* @return string Processed payload |
90
|
|
|
*/ |
91
|
5 |
|
public function processPayload($payload) |
92
|
|
|
{ |
93
|
|
|
// Reset all relevant relations |
94
|
5 |
|
$this->resetRefersToRelations(); |
95
|
5 |
|
$this->resetEmbedsRelations(); |
96
|
|
|
|
97
|
5 |
|
$env = Environment::createCommonMarkEnvironment(); |
98
|
5 |
|
$env->addDocumentProcessor($this); |
99
|
|
|
|
100
|
|
|
// Parse and process the object payload |
101
|
|
|
/** @var DocParser $docParser */ |
102
|
5 |
|
$docParser = Kernel::create(DocParser::class, [$env]); |
103
|
5 |
|
$docParser->parse($payload); |
104
|
|
|
|
105
|
5 |
|
return $payload; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Reset the refers-to relations |
110
|
|
|
*/ |
111
|
5 |
|
protected function resetRefersToRelations() |
112
|
|
|
{ |
113
|
5 |
|
$this->refersTo = []; |
114
|
|
|
|
115
|
|
|
// Run through all refers-to relations and delete them |
116
|
5 |
|
foreach ($this->object->findRelations([Relation::TYPE => Relation::REFERS_TO]) as $refersToRelation) { |
117
|
1 |
|
$this->object->deleteRelation($refersToRelation); |
118
|
|
|
} |
119
|
5 |
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Reset the embeds relations |
123
|
|
|
*/ |
124
|
5 |
|
protected function resetEmbedsRelations() |
125
|
|
|
{ |
126
|
5 |
|
$this->embeds = []; |
127
|
|
|
|
128
|
|
|
// Run through all refers-to relations and delete them |
129
|
5 |
|
foreach ($this->object->findRelations([Relation::TYPE => Relation::EMBEDS]) as $embedsRelation) { |
130
|
1 |
|
$this->object->deleteRelation($embedsRelation); |
131
|
|
|
} |
132
|
5 |
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Process the CommonMark AST |
136
|
|
|
* |
137
|
|
|
* @param Document $document CommonMark AST |
138
|
|
|
* @return void |
139
|
|
|
*/ |
140
|
5 |
|
public function processDocument(Document $document) |
141
|
|
|
{ |
142
|
5 |
|
$walker = $document->walker(); |
143
|
5 |
|
while ($event = $walker->next()) { |
144
|
5 |
|
$node = $event->getNode(); |
145
|
|
|
|
146
|
|
|
// Process link starts as refers-to relations |
147
|
5 |
|
if (($node instanceof Link) && $event->isEntering()) { |
148
|
1 |
|
$this->addRefersToRelation( |
149
|
1 |
|
$this->stripFragment($node->getUrl()), |
150
|
1 |
|
empty($node->data['title']) ? null : $node->data['title'] |
151
|
|
|
); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
// Process image starts as embeds relations |
155
|
5 |
|
if (($node instanceof Image) && $event->isEntering()) { |
156
|
1 |
|
$this->addEmbedsRelation( |
157
|
1 |
|
$this->stripFragment($node->getUrl()), |
158
|
1 |
|
empty($node->data['title']) ? null : $node->data['title'] |
159
|
|
|
); |
160
|
|
|
} |
161
|
|
|
} |
162
|
5 |
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* Add a refers-to relation |
166
|
|
|
* |
167
|
|
|
* @param string $url Referred URL |
168
|
|
|
* @param string $label Label |
169
|
|
|
*/ |
170
|
1 |
|
protected function addRefersToRelation($url, $label = null) |
171
|
|
|
{ |
172
|
1 |
|
if (strlen($url) && !array_key_exists($url, $this->refersTo)) { |
173
|
1 |
|
$this->refersTo[$url] = true; |
174
|
1 |
|
$this->object->addRelation($this->getRelationString($url, $label), Relation::REFERS_TO); |
175
|
|
|
} |
176
|
1 |
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* Create a relation string |
180
|
|
|
* |
181
|
|
|
* @param string $url URL |
182
|
|
|
* @param string $label Label |
183
|
|
|
* @return string relation string |
184
|
|
|
*/ |
185
|
1 |
|
protected function getRelationString($url, $label) |
186
|
|
|
{ |
187
|
1 |
|
$relationString = (strtolower(parse_url($url, PHP_URL_SCHEME)) == self::SCHEME_MAILTO) |
188
|
1 |
|
? '<'.substr($url, strlen(self::SCHEME_MAILTO) + 1).'>' |
189
|
1 |
|
: $url; |
190
|
1 |
|
if (!empty($label)) { |
191
|
1 |
|
$relationString .= ' '.$label; |
192
|
|
|
} |
193
|
1 |
|
return $relationString; |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* Strip off the fragment of an URL |
198
|
|
|
* |
199
|
|
|
* @param string $url URL |
200
|
|
|
* @return string URL with fragment stripped |
201
|
|
|
*/ |
202
|
1 |
|
protected function stripFragment($url) |
203
|
|
|
{ |
204
|
1 |
|
$fragment = parse_url($url, PHP_URL_FRAGMENT); |
205
|
1 |
|
if (!empty($fragment) && (substr($url, -strlen($fragment) - 1) == '#'.$fragment)) { |
206
|
1 |
|
$url = substr($url, 0, -strlen($fragment) - 1); |
207
|
|
|
} |
208
|
1 |
|
return $url; |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
/** |
212
|
|
|
* Add an embeds relation |
213
|
|
|
* |
214
|
|
|
* @param string $url Embedded URL |
215
|
|
|
* @param string $label Label |
216
|
|
|
*/ |
217
|
1 |
|
protected function addEmbedsRelation($url, $label = null) |
218
|
|
|
{ |
219
|
1 |
|
if (strlen($url) && !array_key_exists($url, $this->embeds)) { |
220
|
1 |
|
$this->embeds[$url] = true; |
221
|
1 |
|
$this->object->addRelation($this->getRelationString($url, $label), Relation::EMBEDS); |
222
|
|
|
} |
223
|
1 |
|
} |
224
|
|
|
} |
225
|
|
|
|