mirror of
https://code.castopod.org/adaures/castopod
synced 2025-04-19 04:51:17 +00:00
feat: abstract generating rss feed using RSSFeed class
This commit is contained in:
parent
bb03798aff
commit
17865dab2b
@ -40,6 +40,7 @@ class Autoload extends AutoloadConfig
|
||||
*/
|
||||
public $psr4 = [
|
||||
APP_NAMESPACE => APPPATH,
|
||||
'Themes' => ROOTPATH . 'themes',
|
||||
'Modules' => ROOTPATH . 'modules/',
|
||||
'Modules\Admin' => ROOTPATH . 'modules/Admin/',
|
||||
'Modules\Analytics' => ROOTPATH . 'modules/Analytics/',
|
||||
@ -55,7 +56,7 @@ class Autoload extends AutoloadConfig
|
||||
'Modules\PremiumPodcasts' => ROOTPATH . 'modules/PremiumPodcasts/',
|
||||
'Modules\Update' => ROOTPATH . 'modules/Update/',
|
||||
'Modules\WebSub' => ROOTPATH . 'modules/WebSub/',
|
||||
'Themes' => ROOTPATH . 'themes',
|
||||
'RSSFeed' => APPPATH . 'Libraries/RSSFeed',
|
||||
'ViewComponents' => APPPATH . 'Libraries/ViewComponents/',
|
||||
'ViewThemes' => APPPATH . 'Libraries/ViewThemes/',
|
||||
'Vite' => APPPATH . 'Libraries/Vite/',
|
||||
|
@ -37,16 +37,6 @@ if (! function_exists('get_rss_feed')) {
|
||||
|
||||
$episodes = $podcast->episodes;
|
||||
|
||||
$itunesNamespace = 'http://www.itunes.com/dtds/podcast-1.0.dtd';
|
||||
|
||||
$podcastNamespace = 'https://podcastindex.org/namespace/1.0';
|
||||
|
||||
$atomNamespace = 'http://www.w3.org/2005/Atom';
|
||||
|
||||
$rss = new SimpleRSSElement(
|
||||
"<?xml version='1.0' encoding='utf-8'?><rss version='2.0' xmlns:itunes='{$itunesNamespace}' xmlns:podcast='{$podcastNamespace}' xmlns:atom='{$atomNamespace}' xmlns:content='http://purl.org/rss/1.0/modules/content/'></rss>"
|
||||
);
|
||||
|
||||
$channel = $rss->addChild('channel');
|
||||
|
||||
$atomLink = $channel->addChild('link', null, $atomNamespace);
|
||||
|
64
app/Libraries/RSSFeed/RSSFeed.php
Normal file
64
app/Libraries/RSSFeed/RSSFeed.php
Normal file
@ -0,0 +1,64 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* @copyright 2024 Ad Aures
|
||||
* @license https://www.gnu.org/licenses/agpl-3.0.en.html AGPL3
|
||||
* @link https://castopod.org/
|
||||
*/
|
||||
|
||||
namespace App\Libraries\RSSFeed;
|
||||
|
||||
class RSSFeed
|
||||
{
|
||||
public const NAMESPACES = [
|
||||
'itunes' => 'http://www.itunes.com/dtds/podcast-1.0.dtd',
|
||||
'podcast' => 'https://podcastindex.org/namespace/1.0',
|
||||
'atom' => 'http://www.w3.org/2005/Atom',
|
||||
];
|
||||
|
||||
/**
|
||||
* @var Tag[]
|
||||
*/
|
||||
private array $tags = [];
|
||||
|
||||
private SimpleRSSElement $root;
|
||||
|
||||
private SimpleRSSElement $channel;
|
||||
|
||||
public function findTag(string $name): Tag
|
||||
{
|
||||
}
|
||||
|
||||
public function render(): string
|
||||
{
|
||||
$this->root = new SimpleRSSElement(
|
||||
"<rss version='2.0' xmlns:content='http://purl.org/rss/1.0/modules/content/' />"
|
||||
);
|
||||
|
||||
foreach ($this::NAMESPACES as $key => $namespace) {
|
||||
$this->root->addAttribute('xmlns:' . $key, $namespace);
|
||||
}
|
||||
|
||||
$this->channel = $this->root->addChild('channel');
|
||||
|
||||
/** @var string|false $xml */
|
||||
$xml = $this->root->asXML();
|
||||
|
||||
if (! $xml) {
|
||||
return ''; // TODO: show exception?
|
||||
}
|
||||
|
||||
return $xml;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array{string,string}[] $attributes
|
||||
* @param key-of<self::NAMESPACES> $namespace
|
||||
*/
|
||||
private function addTag(string $name, string $value, $attributes, $namespace)
|
||||
{
|
||||
$this->tags[] = new Tag($name, $value, $attributes, $namespace);
|
||||
}
|
||||
}
|
@ -8,7 +8,7 @@ declare(strict_types=1);
|
||||
* @link https://castopod.org/
|
||||
*/
|
||||
|
||||
namespace App\Libraries;
|
||||
namespace App\Libraries\RSSFeed;
|
||||
|
||||
use DOMDocument;
|
||||
use SimpleXMLElement;
|
26
app/Libraries/RSSFeed/Tag.php
Normal file
26
app/Libraries/RSSFeed/Tag.php
Normal file
@ -0,0 +1,26 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* @copyright 2024 Ad Aures
|
||||
* @license https://www.gnu.org/licenses/agpl-3.0.en.html AGPL3
|
||||
* @link https://castopod.org/
|
||||
*/
|
||||
|
||||
namespace App\Libraries\RSSFeed;
|
||||
|
||||
class Tag
|
||||
{
|
||||
/**
|
||||
* @param array{string,string}[] $attributes
|
||||
* @param key-of<RSSFeed::NAMESPACES> $namespace
|
||||
*/
|
||||
public function __construct(
|
||||
private string $name,
|
||||
private string $value,
|
||||
private array $attributes,
|
||||
private string $namespace
|
||||
) {
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user