2017 © Pedro Peláez
 

library feedbundle

A Symfony bundle to build RSS feeds from entities

image

eko/feedbundle

A Symfony bundle to build RSS feeds from entities

  • Friday, January 6, 2017
  • by eko
  • Repository
  • 9 Watchers
  • 105 Stars
  • 230,594 Installations
  • PHP
  • 9 Dependents
  • 1 Suggesters
  • 45 Forks
  • 6 Open issues
  • 23 Versions
  • 4 % Grown

The README.md

FeedBundle

A Symfony bundle to build RSS/Atom feeds from entities, (*1)

SensioLabsInsight, (*2)

Build Status Latest Stable Version Total Downloads, (*3)

Features

  • Generate XML feeds (RSS & Atom formats)
  • Easy to configure & use
  • Items based on your entities
  • Add groups of items
  • Add enclosure media tags
  • Translate your feed data
  • Read XML feeds and populate your Symfony entities
  • Dump your feeds into a file via a Symfony console command

Installation

Add the package to your composer.json file, (*4)

"eko/feedbundle": "dev-master",

Add this to to the config/bundles.php file:, (*5)

<?php

return [
    // ...
    Eko\FeedBundle\EkoFeedBundle::class => ['all' => true],
];

Configuration (only 3 quick steps!)

1) Create a file: config/packages/eko_feed.yml

The following configuration lines are required:, (*6)

eko_feed:
    hydrator: your_hydrator.custom.service # Optional, if you use entity hydrating with a custom hydrator
    translation_domain: test # Optional, if you want to use a custom translation domain
    feeds:
        article:
            title:       'My articles/posts'
            description: 'Latests articles'
            link:        'http://vincent.composieux.fr'
            encoding:    'utf-8'
            author:      'Vincent Composieux' # Only required for Atom feeds

You can also set link as a Symfony route:, (*7)

link:
    route_name: acme_blog_main_index
    route_params: {id: 2} # necessary if route contains required parameters

2) Implement the ItemInterface

Each entities you will use to generate an RSS feed needs to implement Eko\FeedBundle\Item\Writer\ItemInterface or Eko\FeedBundle\Item\Writer\RoutedItemInterface as demonstrated in this example for an Article entity of a blog:, (*8)

Option A: Eko\FeedBundle\Item\Writer\ItemInterface

<?php

namespace Bundle\BlogBundle\Entity;

use Eko\FeedBundle\Item\Writer\ItemInterface;

/**
 * Bundle\BlogBundle\Entity\Article
 */
class Article implements ItemInterface
{

In this same entity, just implement those required methods:, (*9)

  • public function getFeedItemTitle() { … } : this method returns entity item title
  • public function getFeedItemDescription() { … } : this method returns entity item description (or content)
  • public function getFeedItemPubDate() { … } : this method returns entity item publication date
  • public function getFeedItemLink() { … } : this method returns entity item link (URL)

Option B: Eko\FeedBundle\Item\Writer\RoutedItemInterface

Alternatively, if you need to make use of the router service to generate the link for your entity you can use the following interface. You don't need to worry about injecting the router to your entity., (*10)

<?php

namespace Bundle\BlogBundle\Entity;

use Eko\FeedBundle\Item\Writer\RoutedItemInterface;

/**
 * Bundle\BlogBundle\Entity\Article
 */
class Article implements RoutedItemInterface
{

In this entity, you'll need to implement the following methods:, (*11)

  • public function getFeedItemTitle() { … } : this method returns entity item title
  • public function getFeedItemDescription() { … } : this method returns entity item description (or content)
  • public function getFeedItemPubDate() { … } : this method returns entity item publication date
  • public function getFeedItemRouteName() { … } : this method returns the name of the route
  • public function getFeedItemRouteParameters() { … } : this method must return an array with the parameters that are required for the route
  • public function getFeedItemUrlAnchor() { … } : this method returns the anchor that will be appended to the router-generated url. Note: can be an empty string.

3) Generate the feed!

The action now takes place in your controller. Just declare a new action with those examples lines:, (*12)

<?php

namespace App\Controller;

use Eko\FeedBundle\Feed\FeedManager;

use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;

class BlogController extends AbstractController
{
    /**
     * @var FeedManager
     */
    protected $feedManager;

    /**
     * Constructor.
     * 
     * @param FeedManager $feedManager
     */
    public function __construct(FeedManager $feedManager)
    {
        $this->feedManager = $feedManager;
    }

    /**
     * Generate the article feed
     * 
     * @Route("/feed.rss", name="app_feed")
     *
     * @return Response XML Feed
     */
    public function feed()
    {
        $articles = $this->getDoctrine()->getRepository('BundleBlogBundle:Article')->findAll();

        $feed = $this->feedManager->get('article');
        $feed->addFromArray($articles);

        return new Response($feed->render('rss')); // or 'atom'
    }
}

Please note that for better performances you can add a cache control., (*13)

Moreover, entities objects can be added separately with add method:, (*14)

<?php
$feed = $this->get('eko_feed.feed.manager')->get('article');
$feed->add($article);

Go further with your feeds

Add some custom channel fields

You can add custom fields to main channel by adding them this way:, (*15)

<?php
$feed = $this->get('eko_feed.feed.manager')->get('article');
$feed->add(new FakeEntity());
$feed->addChannelField(new ChannelField('custom_name', 'custom_value'));

Add some custom items fields

Add custom item fields

You can add custom items fields for your entities nodes by adding them this way:, (*16)

<?php
$feed = $this->get('eko_feed.feed.manager')->get('article');
$feed->add(new FakeEntity());
$feed->addItemField(new ItemField('fake_custom', 'getFeedItemCustom'));

Of course, getFeedItemCustom() method needs to be declared in your entity., (*17)

Add a group of custom item fields (optionally, with attributes)

You can also add group item fields using this way, if your method returns an array:, (*18)

<?php
$feed = $this->get('eko_feed.feed.manager')->get('article');
$feed->add(new FakeEntity());
$feed->addItemField(
    new GroupItemField(
        'categories',
        new ItemField('category', 'getFeedCategoriesCustom', array(), array('category-attribute', 'test'),
        array('categories-attribute', 'getAttributeValue')
    )
);

or even, multiple item fields in a group, like this:, (*19)

$feed->addItemField(
    new GroupItemField('author', array(
        new ItemField('name', 'getFeedItemAuthorName', array('cdata' => true)),
        new ItemField('email', 'getFeedItemAuthorEmail')
    )
);

or even, nested group item field in a group, like this:, (*20)

$feed->addItemField(
    new GroupItemField('authors', array(
        new GroupItemField('author', array(
            new ItemField('name', 'Vincent', array('cdata' => true)),
            new ItemField('email', 'vincent@test.com')
        )),
        new GroupItemField('author', array(
            new ItemField('name', 'Audrey', array('cdata' => true)),
            new ItemField('email', 'audrey@test.com')
        ))
    )
);
Add a group of custom channel fields

As you can do for item fields, you can also add a custom group of channel fields like this:, (*21)

$feed->addChannelField(
    new GroupChannelField('author', array(
        new ChannelField('name', 'My author name'),
        new ChannelField('email', 'myauthor@email.org')
    )
);
Add custom media item fields

Media enclosure can be added using the MediaItemField field type as below:, (*22)

<?php
$feed = $this->get('eko_feed.feed.manager')->get('article');
$feed->add(new FakeEntity());
$feed->addItemField(new MediaItemField('getFeedMediaItem'));

The getFeedMediaItem() method must return an array with the following keys: type, length & value:, (*23)

/**
 * Returns a custom media field
 *
 * @return string
 */
public function getFeedMediaItem()
{
    return array(
        'type'   => 'image/jpeg',
        'length' => 500,
        'value'  => 'http://website.com/image.jpg'
    );
}

This media items can also be grouped using GroupItemField., (*24)

Dump your feeds by using the Symfony console command

You can dump your feeds into a .xml file if you don't want to generate it on the fly by using the php app/console eko:feed:dump Symfony command., (*25)

Here are the options :, (*26)

Option Description
--name Feed name defined in eko_feed configuration
--entity Entity to use to generate the feed
--filename Defines feed filename
--orderBy Order field to sort by using findBy() method
--direction Direction to give to sort field with findBy() method
--format Formatter to use to generate, "rss" is default
--limit Defines a limit of entity items to retrieve
Host Defines the host base to generate absolute Url

An example with all the options:, (*27)

php app/console eko:feed:dump --name=article --entity=AcmeDemoBundle:Fake --filename=test.xml --format=atom --orderBy=id --direction=DESC www.myhost.com, (*28)

This will result:, (*29)

Start dumping "article" feed from "AcmeDemoBundle:Fake" entity...
done!
Feed has been dumped and located in "/Users/vincent/dev/perso/symfony/web/test.xml"

Dump your feeds by using the Eko\FeedBundle\Service\FeedDumpService

You can dump your feeds by simply using the "Eko\FeedBundle\Service\FeedDumpService" service. Used by the dump command, you have the same value to set. If you already have you items feed ready, you can dump it using the setItems()., (*30)

<?php

use Eko\FeedBundle\Service\FeedDumpService;

$feedDumpService = $this->get(FeedDumpService::class);
$feedDumpService
        ->setName($name)
        //You can set an entity
        //->setEntity($entity)
        // Or set you Items
        ->setItems($MyOwnItemList)
        ->setFilename($filename)
        ->setFormat($format)
        ->setLimit($limit)
        ->setDirection($direction)
        ->setOrderBy($orderBy)
    ;

$feedDumpService->dump();

For any question, do not hesitate to contact me and/or participate., (*31)

Read an XML feed and populate an entity

If you only want to read an XML feed, here is the way:, (*32)

<?php
$reader = $this->get('eko_feed.feed.reader');
$reader->setHydrator(new DefaultHydrator());
$feed = $reader->load('http://php.net/feed.atom')->get();

$feed will be a \Zend\Feed\Reader\Feed\FeedInterface that you can manipulate., (*33)


You can also populate an entity from an XML feed. This is very easy., (*34)

Just load the feed and call the populate method with your entity name which needs to implement Eko\FeedBundle\Item\Reader\ItemInterface, take a look on this example:, (*35)

<?php
$reader = $this->get('eko_feed.feed.reader');
$reader->setHydrator(new DefaultHydrator());
$items = $reader->load('http://php.net/feed.atom')->populate('MyNamespace\Entity\Name');

In this example, $items will be an array that will contains an array with your entities populated with the given feed content., (*36)

Use a custom hydrator to populate your entity

You can also write your own hydrator and use it this way:, (*37)

$reader = $this->get('eko_feed.feed.reader');
$reader->setHydrator(new MyCustomHydrator());

$items = $reader->load('http://php.net/feed.atom')->populate('MyNamespace\Entity\Name');

This way, your custom hydrator will be used instead of the Eko\FeedBundle\Hydrator\DefaultHydrator, (*38)

Define a custom feed formatter

You can define your own feed formatter by using the following tag:, (*39)

<service id="acme.my_bundle.formatter.custom" class="Acme\MyBundle\Feed\Formatter\CustomFormatter">
    <tag name="eko_feed.formatter" format="custom"></tag>

    <argument type="service" id="translator" />
</service>

Then, use it by simply calling $feed->render('custom')., (*40)

Contributors

The Versions

06/01 2017

dev-master

9999999-dev http://github.com/eko/FeedBundle

A Symfony bundle to build RSS feeds from entities

  Sources   Download

MIT

The Requires

 

The Development Requires

by Vincent Composieux

bundle atom feed rss

29/07 2016

dev-analysis-zRP5yD

dev-analysis-zRP5yD http://github.com/eko/FeedBundle

A Symfony bundle to build RSS feeds from entities

  Sources   Download

MIT

The Requires

 

The Development Requires

by Vincent Composieux

bundle atom feed rss

20/04 2016

dev-analysis-zYjeyo

dev-analysis-zYjeyo http://github.com/eko/FeedBundle

A Symfony bundle to build RSS feeds from entities

  Sources   Download

MIT

The Requires

 

The Development Requires

by Vincent Composieux

bundle atom feed rss

20/04 2016

1.2.8

1.2.8.0 http://github.com/eko/FeedBundle

A Symfony bundle to build RSS feeds from entities

  Sources   Download

MIT

The Requires

 

The Development Requires

by Vincent Composieux

bundle atom feed rss

10/12 2015

1.2.7

1.2.7.0 http://github.com/eko/FeedBundle

A Symfony bundle to build RSS feeds from entities

  Sources   Download

MIT

The Requires

 

The Development Requires

by Vincent Composieux

bundle atom feed rss

10/09 2015

1.2.6

1.2.6.0 http://github.com/eko/FeedBundle

A Symfony bundle to build RSS feeds from entities

  Sources   Download

MIT

The Requires

 

The Development Requires

by Vincent Composieux

bundle atom feed rss

06/06 2015

1.2.5

1.2.5.0 http://github.com/eko/FeedBundle

A Symfony bundle to build RSS feeds from entities

  Sources   Download

MIT

The Requires

 

The Development Requires

by Vincent Composieux

bundle atom feed rss

15/04 2015

1.2.4

1.2.4.0 http://github.com/eko/FeedBundle

A Symfony bundle to build RSS feeds from entities

  Sources   Download

MIT

The Requires

 

The Development Requires

by Vincent Composieux

bundle atom feed rss

31/03 2015

1.2.3

1.2.3.0 http://github.com/eko/FeedBundle

A Symfony bundle to build RSS feeds from entities

  Sources   Download

MIT

The Requires

 

The Development Requires

by Vincent Composieux

bundle atom feed rss

07/02 2015

1.2.2

1.2.2.0 http://github.com/eko/FeedBundle

A Symfony bundle to build RSS feeds from entities

  Sources   Download

MIT

The Requires

 

The Development Requires

by Vincent Composieux

bundle atom feed rss

24/11 2014

1.2.0

1.2.0.0 http://github.com/eko/FeedBundle

A Symfony bundle to build RSS feeds from entities

  Sources   Download

MIT

The Requires

 

The Development Requires

by Vincent Composieux

bundle atom feed rss

24/11 2014

1.2.1

1.2.1.0 http://github.com/eko/FeedBundle

A Symfony bundle to build RSS feeds from entities

  Sources   Download

MIT

The Requires

 

The Development Requires

by Vincent Composieux

bundle atom feed rss

17/08 2014

1.1.8

1.1.8.0 http://github.com/eko/FeedBundle

A Symfony bundle to build RSS feeds from entities

  Sources   Download

MIT

The Requires

 

by Vincent Composieux

bundle atom feed rss

09/08 2014

1.1.7

1.1.7.0 http://github.com/eko/FeedBundle

A Symfony bundle to build RSS feeds from entities

  Sources   Download

MIT

The Requires

 

by Vincent Composieux

bundle atom feed rss

09/04 2014

1.1.6

1.1.6.0 http://github.com/eko/FeedBundle

A Symfony bundle to build RSS feeds from entities

  Sources   Download

MIT

The Requires

 

by Vincent Composieux

bundle atom feed rss

07/03 2014

1.1.5

1.1.5.0 http://github.com/eko/FeedBundle

A Symfony bundle to build RSS feeds from entities

  Sources   Download

MIT

The Requires

 

by Vincent Composieux

bundle atom feed rss

26/02 2014

1.1.4

1.1.4.0 http://github.com/eko/FeedBundle

A Symfony bundle to build RSS feeds from entities

  Sources   Download

MIT

The Requires

 

by Vincent Composieux

bundle atom feed rss

25/01 2014

1.1.3

1.1.3.0 http://github.com/eko/FeedBundle

A Symfony bundle to build RSS feeds from entities

  Sources   Download

MIT

The Requires

 

by Vincent Composieux

bundle atom feed rss

08/09 2013

1.1.2

1.1.2.0 http://github.com/eko/FeedBundle

A Symfony bundle to build RSS feeds from entities

  Sources   Download

MIT

The Requires

 

by Vincent Composieux

bundle atom feed rss

05/09 2013

1.1.1

1.1.1.0 http://github.com/eko/FeedBundle

A Symfony bundle to build RSS feeds from entities

  Sources   Download

MIT

The Requires

 

by Vincent Composieux

bundle atom feed rss

13/07 2013

1.1.0

1.1.0.0 http://github.com/eko/FeedBundle

A Symfony bundle to build RSS feeds from entities

  Sources   Download

MIT

The Requires

 

by Vincent Composieux

bundle atom feed rss

08/06 2013

1.0.1

1.0.1.0 http://github.com/eko/FeedBundle

A Symfony bundle to build RSS feeds from entities

  Sources   Download

MIT

The Requires

 

by Vincent Composieux

bundle atom feed rss

07/04 2013

1.0.0

1.0.0.0 http://github.com/eko/FeedBundle

A Symfony bundle to build RSS feeds from entities

  Sources   Download

MIT

The Requires

 

by Vincent Composieux

bundle atom feed rss