mirror of
https://github.com/YGGverse/Yoda.git
synced 2026-04-01 00:55:28 +00:00
refactor response model to multi-protocol connection interface
This commit is contained in:
parent
3316a149a6
commit
f0024a0855
8 changed files with 652 additions and 350 deletions
100
src/Model/Connection.php
Normal file
100
src/Model/Connection.php
Normal file
|
|
@ -0,0 +1,100 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Yggverse\Yoda\Model;
|
||||
|
||||
use \Yggverse\Net\Address;
|
||||
|
||||
use \Yggverse\Yoda\Model\Connection\File;
|
||||
use \Yggverse\Yoda\Model\Connection\Gemini;
|
||||
use \Yggverse\Yoda\Model\Connection\Nex;
|
||||
|
||||
class Connection extends \Yggverse\Yoda\Abstract\Model\Connection
|
||||
{
|
||||
public function request(
|
||||
string $request,
|
||||
int $timeout = 5
|
||||
): void
|
||||
{
|
||||
// Build address instance
|
||||
$address = new Address(
|
||||
$request
|
||||
);
|
||||
|
||||
// Detect protocol
|
||||
switch ($address->getScheme())
|
||||
{
|
||||
case 'file':
|
||||
|
||||
(new File($this))->sync(
|
||||
$address
|
||||
);
|
||||
|
||||
break;
|
||||
|
||||
case 'gemini':
|
||||
|
||||
(new Gemini($this))->sync(
|
||||
$address,
|
||||
$timeout
|
||||
);
|
||||
|
||||
break;
|
||||
|
||||
case 'nex':
|
||||
|
||||
(new Nex($this))->sync(
|
||||
$address,
|
||||
$timeout
|
||||
);
|
||||
|
||||
break;
|
||||
|
||||
case null: // no scheme provided
|
||||
|
||||
// Use gemini protocol by default
|
||||
$redirect = new Address(
|
||||
sprintf(
|
||||
'gemini://%s',
|
||||
$address->get()
|
||||
)
|
||||
);
|
||||
|
||||
// Hostname valid
|
||||
if (filter_var(
|
||||
$redirect->getHost(),
|
||||
FILTER_VALIDATE_DOMAIN,
|
||||
FILTER_FLAG_HOSTNAME
|
||||
)
|
||||
) {
|
||||
// Redirect
|
||||
$this->setRedirect(
|
||||
$redirect->get()
|
||||
);
|
||||
}
|
||||
|
||||
// Redirect to default search provider
|
||||
else
|
||||
{
|
||||
// @TODO custom providers
|
||||
$this->setRedirect(
|
||||
sprintf(
|
||||
'gemini://tlgs.one/search?%s',
|
||||
urlencode(
|
||||
$request
|
||||
)
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
return;
|
||||
|
||||
default:
|
||||
|
||||
throw new \Exception(
|
||||
_('Protocol not supported')
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue