markocupic/rss-feed-generator-bundle
Composer 安装命令:
composer require markocupic/rss-feed-generator-bundle
包简介
Simple RSS feed generator. Generate RSS feeds inside a symfony controller.
README 文档
README
RSS Feed Generator Bundle
Use this bundle to generate rss feeds inside your Symfony application.
❤ Many thanks to @eko (Vincent Composieux) for giving me the inspiration to program this bundle. https://github.com/eko/FeedBundle.
Installation
composer require markocupic/rss-feed-generator-bundle
Option A: Add this to your config/bundles.php.
<?php return [ // ... Markocupic\RssFeedGeneratorBundle\MarkocupicRssFeedGeneratorBundle::class => ['all' => true], ];
Option B: In a Contao ❤ environment register the rss feed generator bundle in the Contao Manager Plugin class of your bundle.
<?php declare(strict_types=1); namespace Contao\CoreBundle\ContaoManager; use Contao\CoreBundle\ContaoCoreBundle; use Contao\ManagerPlugin\Bundle\BundlePluginInterface; use Contao\ManagerPlugin\Bundle\Config\BundleConfig; use Contao\ManagerPlugin\Bundle\Parser\ParserInterface; use Contao\ManagerPlugin\Config\ConfigPluginInterface; use Contao\ManagerPlugin\Routing\RoutingPluginInterface; use Symfony\Component\Config\Loader\LoaderInterface; use Symfony\Component\Config\Loader\LoaderResolverInterface; use Symfony\Component\HttpKernel\KernelInterface; use Markocupic\RssFeedGeneratorBundle\MarkocupicRssFeedGeneratorBundle; use Acme\MyBundle\AcmeMyBundleBundle; class Plugin implements BundlePluginInterface, RoutingPluginInterface, ConfigPluginInterface { /** * {@inheritdoc} */ public function getBundles(ParserInterface $parser) { return [ // Register RSS feed generator bundle BundleConfig::create(MarkocupicRssFeedGeneratorBundle::class), // register other bundles BundleConfig::create(AcmeMyBundle::class) ->setLoadAfter(MarkocupicRssFeedGeneratorBundle::class) ->setLoadAfter(ContaoCoreBundle::class) ]; }
Use dependency injection to require the feed factory in your controller.
# config/services.yml
services:
Markocupic\DemoBundle\Controller\Feed\FeedController:
arguments:
- '@Markocupic\RssFeedGeneratorBundle\Feed\FeedFactory'
- '@database_connection'
- '%kernel.project_dir%'
public: true
Create the feed
// Use the feed factory to generate the feed object $rss = $this->feedFactory->createFeed(\Markocupic\RssFeedGeneratorBundle\Feed\Feed::ENCODING_UTF8); // Add one or more attributes to the root element $rss->setRootAttributes([ 'xmlns:tourdb' => 'https://acme.com/schema/tourdbrss/1.0', 'xmlns:atom'=>'http://www.w3.org/2005/Atom', ]); // Add one or more attributes to the channel element $rss->setChannelAttributes([ 'foo' => 'bar', ]);
Add feed Channel elements
Use the Item class inside the feed factory method FeedFactory::addChannelField().
The Item::__constructor($elementName, $strValue, $arrOptions, $arrAttributes) takes four arguments:
- (string) element name
- (string) content
- optional: (array) options (at the moment cdata, and filters)
- optional: (array) with attributes
$rss->addChannelField( new Item('title', 'Demo feed') ); $rss->addChannelField( new Item('link', 'https://foobar.ch') );
Make cdata elements and insert attributes:
// Add CDATA element and an attribute $rss->addChannelField( new Item('description', 'Check our news feed and have fun!', ['cdata' => true], ['attrName' => 'Here comes the attribute value']) );
Filter od replace content
// filter or replace values $arrFilter = ['Ferrari' => 'Italia', 'car' => 'country'] ; $rss->addChannelField( new Item('description', 'Ferrari is my favourite car!', ['filter' => $arrFilter]) ); // Will result in: // <description>Italia is my favourite country!</description>
Add channel items
Use FeedFactory::addChannelItemField(), ItemGroup() and Item() to generate channel items.
The ItemGroup::__constructor($elementName, $arrItemObjects, $arrAttributes) takes three arguments:
- (string) element name
- (array) with Item objects
- optional: (array) with attributes
// Retrieve data from database and add items $results = $this->getEvents($section); if (null !== $results) { while (false !== ($arrEvent = $results->fetch())) { // Use a new instance of ItemGroup to add a collection of items all of the same level. $rss->addChannelItemField( new ItemGroup('item', [ new Item('title', $arrEvent['title']), new Item('link', $arrEvent['link']), new Item('description', $arrEvent['description'], ['cdata' => true]), new Item('pubDate', date('r',(int) $arrEvent['tstamp'])), new Item('author', $arrEvent['author']), new Item('guid', $arrEvent['uuid']), new Item('tourdb:startdate', date('Y-m-d', (int) $arrEvent['startDate'])), new Item('tourdb:enddate', date('Y-m-d', (int) $arrEvent['endDate'])), ]) ); } }
Nested items
// Append nested items with ItemGroup. $rss->addChannelItemField( new ItemGroup('item', [ new Item('title', 'Title'), new Item('link', 'https://foo.bar'), new ItemGroup('nestedItems', [ new Item('subitem', 'Some content'), new Item('subitem', 'Some content'), ], ['foo'=> 'bar']), ]) );
Result:
<item> <title>Title</title> <link>https://foo.bar</link> <nestedItem foo="bar"> <subitem>Some content</subitem> <subitem>Some content</subitem> </nestedItem> </item>
Render and send content to the browser.
return $rss->render();
Render and save content to the filesystem.
return $rss->render('public/share/myfeed.xml);
Generate RSS 2.0 feed inside a controller in a Symfony bundle
<?php declare(strict_types=1); namespace Acme\DemoBundle\Controller\Feed; use Doctrine\DBAL\Connection; use Doctrine\DBAL\Query\QueryBuilder; use Markocupic\RssFeedGeneratorBundle\Feed\FeedFactory; use Markocupic\RssFeedGeneratorBundle\Item\Item; use Markocupic\RssFeedGeneratorBundle\Feed\ItemGroup; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\Routing\Annotation\Route; class FeedController extends AbstractController { /** * @var FeedFactory */ private $feedFactory; /** * @var Connection */ private $connection; /** * @var string */ private $projectDir; /** * @Route("/_rssfeed", name="rss_feed") */ public function printLatestEvents(): Response { $rss = $this->feedFactory->createFeed('utf-8'); $rss->addChannelField( new Item('title', 'Acme news') ); $rss->addChannelField( new Item('description', 'Enjoj our news.') ); $rss->addChannelField( new Item('link', 'https://acme.com') ); $rss->addChannelField( new Item('language', 'de') ); $rss->addChannelField( new Item('pubDate', date('r', (time() - 3600))) ); // Retrieve data from db $results = $this->getEvents($section); // Add some channel items if (null !== $results) { while (false !== ($arrEvent = $results->fetch())) { $eventsModel = $calendarEventsModelAdapter->findByPk($arrEvent['id']); $rss->addChannelItemField( new ItemGroup('item', [ new Item('title', $arrEvent['title']), new Item('link', $arrEvent['link']), new Item('description', $arrEvent['description'], ['cdata' => true]), new Item('pubDate', date('r', (int) $eventsModel->tstamp)), ]) ); } } return $rss->render($this->projectDir.'/public/share/rss.xml'); } }
Filter & search and replace strings
The extension will filter by default some characters. Linebreaks will be replaced with a whitespace, etc. Please have a look at the Plugin Configuration.
Overriding these defaults is pretty easy and can be done in config/parameters.yml. Please use regular expressions for the search patterns.
# config/parameters.yml
markocupic_rss_feed_generator:
filter:
'/</': '<'
'/[\n\r]+/': ' '
'/(/': '('
'/)/': ')'
'/\[-\]/': ''
'/\­/': ''
'/\[nbsp\]/': ' '
'/ /': ' '
'/&/': '&'
markocupic/rss-feed-generator-bundle 适用场景与选型建议
markocupic/rss-feed-generator-bundle 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 8.88k 次下载、GitHub Stars 达 1, 最近一次更新时间为 2021 年 03 月 20 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「symfony」 「feed」 「rss」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 markocupic/rss-feed-generator-bundle 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 markocupic/rss-feed-generator-bundle 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 markocupic/rss-feed-generator-bundle 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
The bundle for easy using json-rpc api on your project
Simple RSS generator library for PHP 5.5 or later. clone from Bhaktaraz Bhatta ttps://github.com/bhaktaraz/php-rss-generator
Bundle Symfony DaplosBundle
Import an RSS-feed into blog
Google Shopping Feed API (with ns problem fixed)
This module is designed to provide a special alerts feed block for Howard University
统计信息
- 总下载量: 8.88k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 18
- 依赖项目数: 2
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2021-03-20
