MagnetExtension for Twig refactored: MagnetGenerator extracted to separate service.
This commit is contained in:
parent
13f974cc59
commit
47a671c493
36
src/Magnet/MagnetGenerator.php
Normal file
36
src/Magnet/MagnetGenerator.php
Normal file
|
@ -0,0 +1,36 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Magnet;
|
||||||
|
|
||||||
|
class MagnetGenerator
|
||||||
|
{
|
||||||
|
private const MAGNET_TEMPLATE = 'magnet:?xt=urn:btih:%s';
|
||||||
|
|
||||||
|
/** @var string[] Array of public trackers which will be added to the magnet link */
|
||||||
|
private $publicTrackers = [];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string[] $publicTrackers
|
||||||
|
*/
|
||||||
|
public function __construct(array $publicTrackers = [])
|
||||||
|
{
|
||||||
|
$this->publicTrackers = $publicTrackers;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function generate(string $infoHash, ?string $name = null, bool $withTrackers = true): string
|
||||||
|
{
|
||||||
|
$url = sprintf(self::MAGNET_TEMPLATE, urlencode($infoHash));
|
||||||
|
|
||||||
|
if (null !== $name) {
|
||||||
|
$url .= '&dn='.urlencode($name);
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($withTrackers) {
|
||||||
|
foreach ($this->publicTrackers as $tracker) {
|
||||||
|
$url .= '&tr='.urlencode($tracker);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return $url;
|
||||||
|
}
|
||||||
|
}
|
|
@ -2,42 +2,31 @@
|
||||||
|
|
||||||
namespace App\Twig;
|
namespace App\Twig;
|
||||||
|
|
||||||
|
use App\Magnet\MagnetGenerator;
|
||||||
use Twig\Extension\AbstractExtension;
|
use Twig\Extension\AbstractExtension;
|
||||||
use Twig\TwigFunction;
|
use Twig\{TwigFilter, TwigFunction};
|
||||||
|
|
||||||
class MagnetExtension extends AbstractExtension
|
class MagnetExtension extends AbstractExtension
|
||||||
{
|
{
|
||||||
private const MAGNET_TEMPLATE = 'magnet:?xt=urn:btih:%s&dn=%s';
|
/** @var MagnetGenerator */
|
||||||
|
private $magnetGenerator;
|
||||||
|
|
||||||
/** @var string[] Array of public trackers which will be added to the magnet link */
|
public function __construct(MagnetGenerator $magnetGenerator)
|
||||||
private $publicTrackers = [];
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param string[] $publicTrackers
|
|
||||||
*/
|
|
||||||
public function __construct(array $publicTrackers = [])
|
|
||||||
{
|
{
|
||||||
$this->publicTrackers = $publicTrackers;
|
$this->magnetGenerator = $magnetGenerator;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public function getFunctions(): array
|
public function getFunctions(): array
|
||||||
{
|
{
|
||||||
return [
|
return [
|
||||||
new TwigFunction('magnet', [$this, 'createMagnet']),
|
new TwigFunction('magnet', [$this->magnetGenerator, 'generate']),
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
public function createMagnet(string $name, string $infoHash, bool $addPublicTrackers = true): string
|
public function getFilters()
|
||||||
{
|
{
|
||||||
$magnetUrl = sprintf(self::MAGNET_TEMPLATE, urlencode($infoHash), urlencode($name));
|
return [
|
||||||
|
new TwigFilter('magnet', [$this->magnetGenerator, 'generate']),
|
||||||
if ($addPublicTrackers) {
|
];
|
||||||
foreach ($this->publicTrackers as $tracker) {
|
|
||||||
$magnetUrl .= '&tr='.urlencode($tracker);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return $magnetUrl;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -19,7 +19,7 @@
|
||||||
{# @var torrent \App\Magnetico\Entity\Torrent #}
|
{# @var torrent \App\Magnetico\Entity\Torrent #}
|
||||||
{% for torrent in torrents %}
|
{% for torrent in torrents %}
|
||||||
<tr>
|
<tr>
|
||||||
<td><a href="{{ magnet(torrent.name, torrent.infoHash) }}">🔗</a></td>
|
<td><a href="{{ magnet(torrent.infoHash, torrent.name) }}">🔗</a></td>
|
||||||
<td><a href="{{ path('torrents_show', {'id': torrent.id}) }}">{{ torrent.name }}</a></td>
|
<td><a href="{{ path('torrents_show', {'id': torrent.id}) }}">{{ torrent.name }}</a></td>
|
||||||
<td>{{ torrent.totalSize | readable_size }}</td>
|
<td>{{ torrent.totalSize | readable_size }}</td>
|
||||||
<td>{{ torrent.discoveredOn | date('Y-m-d H:i:s')}}</td>
|
<td>{{ torrent.discoveredOn | date('Y-m-d H:i:s')}}</td>
|
||||||
|
|
|
@ -10,7 +10,7 @@
|
||||||
<tr>
|
<tr>
|
||||||
<td>Hash</td>
|
<td>Hash</td>
|
||||||
<td>
|
<td>
|
||||||
<a href="{{ magnet(torrent.name, torrent.infoHash) }}">{{ torrent.infoHash }}</a>
|
<a href="{{ magnet(torrent.infoHash, torrent.name) }}">{{ torrent.infoHash }}</a>
|
||||||
</td>
|
</td>
|
||||||
</tr>
|
</tr>
|
||||||
<tr>
|
<tr>
|
||||||
|
|
Loading…
Reference in a new issue