From 17865dab2bc3ea9950fa530b45c8d7bec52916a4 Mon Sep 17 00:00:00 2001 From: Yassine Doghri Date: Tue, 7 May 2024 16:03:07 +0000 Subject: [PATCH] feat: abstract generating rss feed using RSSFeed class --- app/Config/Autoload.php | 3 +- app/Helpers/rss_helper.php | 10 --- app/Libraries/RSSFeed/RSSFeed.php | 64 +++++++++++++++++++ .../{ => RSSFeed}/SimpleRSSElement.php | 2 +- app/Libraries/RSSFeed/Tag.php | 26 ++++++++ 5 files changed, 93 insertions(+), 12 deletions(-) create mode 100644 app/Libraries/RSSFeed/RSSFeed.php rename app/Libraries/{ => RSSFeed}/SimpleRSSElement.php (98%) create mode 100644 app/Libraries/RSSFeed/Tag.php diff --git a/app/Config/Autoload.php b/app/Config/Autoload.php index e4aad938..4a930c6e 100644 --- a/app/Config/Autoload.php +++ b/app/Config/Autoload.php @@ -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/', diff --git a/app/Helpers/rss_helper.php b/app/Helpers/rss_helper.php index 2abafbe4..dfcc3021 100644 --- a/app/Helpers/rss_helper.php +++ b/app/Helpers/rss_helper.php @@ -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( - "" - ); - $channel = $rss->addChild('channel'); $atomLink = $channel->addChild('link', null, $atomNamespace); diff --git a/app/Libraries/RSSFeed/RSSFeed.php b/app/Libraries/RSSFeed/RSSFeed.php new file mode 100644 index 00000000..30df49c4 --- /dev/null +++ b/app/Libraries/RSSFeed/RSSFeed.php @@ -0,0 +1,64 @@ + '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( + "" + ); + + 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 $namespace + */ + private function addTag(string $name, string $value, $attributes, $namespace) + { + $this->tags[] = new Tag($name, $value, $attributes, $namespace); + } +} diff --git a/app/Libraries/SimpleRSSElement.php b/app/Libraries/RSSFeed/SimpleRSSElement.php similarity index 98% rename from app/Libraries/SimpleRSSElement.php rename to app/Libraries/RSSFeed/SimpleRSSElement.php index 1dd2e431..e2634fee 100644 --- a/app/Libraries/SimpleRSSElement.php +++ b/app/Libraries/RSSFeed/SimpleRSSElement.php @@ -8,7 +8,7 @@ declare(strict_types=1); * @link https://castopod.org/ */ -namespace App\Libraries; +namespace App\Libraries\RSSFeed; use DOMDocument; use SimpleXMLElement; diff --git a/app/Libraries/RSSFeed/Tag.php b/app/Libraries/RSSFeed/Tag.php new file mode 100644 index 00000000..db7d6c01 --- /dev/null +++ b/app/Libraries/RSSFeed/Tag.php @@ -0,0 +1,26 @@ + $namespace + */ + public function __construct( + private string $name, + private string $value, + private array $attributes, + private string $namespace + ) { + } +}