-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Link.php
62 lines (49 loc) · 1.66 KB
/
Link.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
<?php
/*
* This file is part of Flarum.
*
* For detailed copyright and license information, please view the
* LICENSE file that was distributed with this source code.
*/
namespace Flarum\Extend;
use Flarum\Extension\Extension;
use Flarum\Foundation\Config;
use Illuminate\Contracts\Container\Container;
use Laminas\Diactoros\Uri;
use s9e\TextFormatter\Renderer;
use s9e\TextFormatter\Utils;
class Link implements ExtenderInterface
{
protected $setRel = null;
protected $setTarget = null;
public function setRel(callable $callable)
{
$this->setRel = $callable;
return $this;
}
public function setTarget(callable $callable)
{
$this->setTarget = $callable;
return $this;
}
public function extend(Container $container, Extension $extension = null)
{
$siteUrl = $container->make(Config::class)->url();
(new Formatter)->render(function (Renderer $renderer, $context, string $xml) use ($siteUrl) {
return Utils::replaceAttributes($xml, 'URL', function ($attributes) use ($siteUrl) {
$uri = isset($attributes['url'])
? new Uri($attributes['url'])
: null;
$setRel = $this->setRel;
if ($setRel && $rel = $setRel($uri, $siteUrl, $attributes)) {
$attributes['rel'] = $rel;
}
$setTarget = $this->setTarget;
if ($setTarget && $target = $setTarget($uri, $siteUrl, $attributes)) {
$attributes['target'] = $target;
}
return $attributes;
});
})->extend($container);
}
}