Browse Source

init base routing

main
yggverse 7 months ago
parent
commit
a3c3f0bad0
  1. 144
      src/Controller/Server/Nex.php
  2. 71
      src/Model/Database.php

144
src/Controller/Server/Nex.php

@ -70,16 +70,143 @@ class Nex implements MessageComponentInterface @@ -70,16 +70,143 @@ class Nex implements MessageComponentInterface
\Ratchet\ConnectionInterface $connection,
$request
) {
// Filter request
$request = trim(
(string) $request
// Format request
$request = '/' . ltrim(
trim($request), '/'
);
// Send response
$connection->send(
'test'
// Route request
switch (true)
{
// Item
case (bool) preg_match('/^\/(?<id>\d+)($|\.gmi)$/i', $request, $attribute):
$lines = [];
// Get channel item info
if ($channelItem = $this->_database->getChannelItem($attribute['id']))
{
if ($channelItem->title)
{
$lines[] = sprintf(
'# %s',
\Yggverse\Pulsar\Model\Filter::title(
$channelItem->title
)
);
}
if ($channelItem->pubTime)
{
$lines[] = date(
'c',
$channelItem->pubTime
) . PHP_EOL;
}
if ($channelItem->description)
{
$lines[] = \Yggverse\Pulsar\Model\Filter::description(
$channelItem->description
) . PHP_EOL;
}
if ($channelItem->content)
{
$lines[] = \Yggverse\Pulsar\Model\Filter::description(
$channelItem->content
) . PHP_EOL;
}
if ($channelItem->link)
{
$lines[] = sprintf(
'=> %s',
$channelItem->link
) . PHP_EOL;
}
}
// Build response
$response = implode(
PHP_EOL,
$lines
);
break;
// Chanel
case (bool) preg_match('/^\/(?<id>\d+)\/($|index\.gmi)$/i', $request, $attribute):
$lines = [];
// Get channel info
if ($channel = $this->_database->getChannel($attribute['id']))
{
if ($channel->title)
{
$lines[] = sprintf(
'# %s',
\Yggverse\Pulsar\Model\Filter::title(
$channel->title
)
);
}
if ($channel->description)
{
$lines[] = $channel->description . PHP_EOL;
}
}
// Get channel items
foreach ($this->_database->getChannelItems(0, 20) as $channelItem)
{
$lines[] = sprintf(
'=> /%d.gmi %s %s',
$channelItem->id,
$channelItem->pubTime ?
date(
'Y-m-d',
$channelItem->pubTime
) : '',
\Yggverse\Pulsar\Model\Filter::title(
$channelItem->title
)
);
}
// Build response
$response = implode(
PHP_EOL,
$lines
);
break;
// Main
// Not found
default:
$lines = [];
// Get channels
foreach ($this->_database->getChannels() as $channel)
{
$lines[] = sprintf(
'=> /%d/index.gmi %s',
$channel->id,
$channel->title
);
}
// Build response
$response = implode(
PHP_EOL,
$lines
);
}
// Debug message event on enabled
if ($this->_config->event->message->debug->enabled)
{
@ -103,6 +230,11 @@ class Nex implements MessageComponentInterface @@ -103,6 +230,11 @@ class Nex implements MessageComponentInterface
);
}
// Send response
$connection->send(
$response
);
// Disconnect
$connection->close();
}

71
src/Model/Database.php

@ -70,6 +70,38 @@ class Database @@ -70,6 +70,38 @@ class Database
');
}
public function getChannel(
int $id
): ?object
{
$query = $this->_database->prepare(
'SELECT * FROM `channel` WHERE `id` = ?'
);
$query->execute([$id]);
if ($result = $query->fetch())
{
return $result;
}
return null;
}
public function getChannels(): ?array
{
$query = $this->_database->query(
'SELECT * FROM `channel`'
);
if ($result = $query->fetchAll())
{
return $result;
}
return null;
}
public function getChannelIdBySource(
string $source
): ?int
@ -123,6 +155,45 @@ class Database @@ -123,6 +155,45 @@ class Database
return null;
}
public function getChannelItem(
int $id
): ?object
{
$query = $this->_database->prepare(
'SELECT * FROM `channelItem` WHERE `id` = ? LIMIT 1'
);
$query->execute([$id]);
if ($result = $query->fetch())
{
return $result;
}
return null;
}
public function getChannelItems(
int $start = 0,
int $limit = 20
): ?array
{
$query = $this->_database->query(
sprintf(
'SELECT * FROM `channelItem` ORDER BY `time` DESC LIMIT %d,%d',
$start,
$limit
)
);
if ($result = $query->fetchAll())
{
return $result;
}
return null;
}
public function isChannelItemExist(
int $channelId,
string $guid

Loading…
Cancel
Save