Pre-1.0. Not intended for public use yet. The API is subject to undocumented change, and these pages may already be wrong about it.

Skip to content

Feed Sources ​

Introduction ​

A source is where a feed reads its items: the database by default, or an array, a config entry, or an API. A source's items read into the same payload as recorded activities, ready for the same kits.

To build a feed by hand in PHP, from data your app already has, see Composing Feeds.

Reading an Array ​

The ArraySource reads items you pass in as arrays:

A controller, or wherever the feed is read
php
use Storyfeed\Facades\Storyfeed;
use Storyfeed\Sources\ArraySource;

$json = file_get_contents(resource_path('releases.json'));

$releases = collect(json_decode($json, true))
    ->map(fn (array $release) => [
        'verb' => 'ship',
        'actor' => 'Storyfeed',
        'object' => [
            'type' => 'release',
            'label' => $release['version'],
            'url' => $release['url'],
        ],
        'published_at' => $release['published_at'],
        'body' => $release['summary'],
    ]);

Storyfeed::feed()->source(new ArraySource($releases))->get();

Give the verb a headline in your feed file, as you would for a model:

routes/feed.php
php
use Storyfeed\Facades\Story;

Story::for('release')
    ->verb('ship')
    ->headline(':actor shipped :object')
    ->icon('rocket');
S
Storyfeed shipped v0.18.0

Feeds can read from sources other than the database.

S
Storyfeed shipped v0.17.0

The feed applies its headlines, bodies, Live and Log modes, only() and except(), role filters, limits and cursors to these items, as it does to stored activities. Unlike a composed feed, a source hides items dated in the future, as the database hides scheduled activities.

To keep the items in the order given, pass inOrder: true:

A controller, or wherever the feed is read
php
Storyfeed::feed()
    ->source(new ArraySource($projects, inOrder: true))
    ->get();

Defining Items ​

Each item is an array with these keys:

KeyValue
verbthe verb, as a string or a verb enum
headlinethe item's own headline, with role tokens filled from its roles
published_ata date or date string; an item without one is dateless
actor, object, target, context, origin, result, instrument, location, generatora role (see below)
starts_at, ends_atthe time range the activity describes
dataan array, carried as the activity's data
bodythe object's body: a string, a body, or a list of bodies; an item with a body needs an object
idthe activity's payload id; derived from the item when absent

Any other key throws an InvalidArgumentException.

A role may be any of these:

Role ValueExampleIn the Payload
a Feedable model$projectthe model's entity, from its toFeed()
a party name'Storyfeed'a party entity, type: "storyfeed.entity"
an entity with no model['type' => 'release', 'label' => 'v0.18.0', 'url' => '…']an entity with that type and label, linked to the url

An entity array may also carry an id, which defaults to its label, plus data and a body.

It may also carry media, the pictures a model would give from its feedMedia(): an icon, an image or a preview, each a URL or an array with src, alt, width and height. An entity with an icon draws it as its avatar:

A controller, or wherever the feed is read
php
$entry->by([
    'type' => 'product',
    'label' => 'InvoiceJam',
    'media' => ['icon' => 'https://teylabs.com/marks/invoicejam.svg'],
])->headline(':actor sends invoices');

An Image or MediaObject body that names a slot the entity doesn't declare, such as preview, throws an InvalidArgumentException rather than drawing nothing. Declare the picture in media, or give the body its own image.

To build items in PHP rather than as arrays, use Entry::make(), which takes the same values as named arguments:

A controller, or wherever the feed is read
php
use Storyfeed\Sources\Entry;

Entry::make(
    verb: 'ship',
    publishedAt: $release['published_at'],
    actor: 'Storyfeed',
    object: [
        'type' => 'release',
        'label' => $release['version'],
        'url' => $release['url'],
    ],
    body: $release['summary'],
);

Naming Sources ​

Name a source in config/storyfeed.php, as you name a filesystem disk. The array driver reads the items listed under items, and in_order keeps their order:

config/storyfeed.php
php
'sources' => [
    'database' => ['driver' => 'database'],

    'releases' => [
        'driver' => 'array',
        'items' => json_decode(
            file_get_contents(resource_path('releases.json')),
            true,
        ),
    ],
],

Pass the name to source:

A controller, or wherever the feed is read
php
use Storyfeed\Facades\Storyfeed;

Storyfeed::feed()->source('releases')->live()->get();

The database source is the default. A feed that never calls source reads the database.

Writing a Custom Driver ​

A driver is a class that implements Storyfeed\Contracts\FeedSource. Its items method returns the source's items, as arrays, Entry instances or Storyfeed\Models\Activity models. This driver reads a repository's releases from GitHub:

app/Sources/GitHubSource.php
php
<?php

namespace App\Sources;

use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Http;
use Storyfeed\Contracts\FeedSource;
use Storyfeed\Sources\Entry;

class GitHubSource implements FeedSource
{
    public function __construct(protected array $config) {}

    public function items(): iterable
    {
        $repo = $this->config['repo'];

        $releases = Cache::remember(
            "github.releases.{$repo}",
            3600,
            fn () => Http::withToken($this->config['token'])
                ->get("https://api.github.com/repos/{$repo}/releases")
                ->throw()
                ->json(),
        );

        foreach ($releases as $release) {
            yield Entry::make(
                verb: 'ship',
                publishedAt: $release['published_at'],
                actor: $release['author']['login'],
                object: [
                    'type' => 'release',
                    'label' => $release['name'] ?: $release['tag_name'],
                    'url' => $release['html_url'],
                ],
                body: $release['body'],
            );
        }
    }
}

Register the driver with Storyfeed::extend in a service provider's boot method. The closure receives the application and the source's configuration:

app/Providers/AppServiceProvider.phpboot()
php
use App\Sources\GitHubSource;
use Storyfeed\Facades\Storyfeed;

Storyfeed::extend('github', fn ($app, array $config) => (
    new GitHubSource($config)
));

Then name a source that uses it:

config/storyfeed.php
php
'sources' => [
    'releases' => [
        'driver' => 'github',
        'repo' => 'storyfeed/storyfeed',
        'token' => env('GITHUB_TOKEN'),
    ],
],

Storyfeed::source('releases') returns the source. It is built the first time it is used and reused after that.

Source Limitations ​

A composed feed or a source other than the database reads its items in memory. These need stored history, so they behave differently:

Call or KeyOn a Composed Feed or Another Source
involving(), involvingDirectly(), involvingType()throws FeedMisconfigured
query()throws FeedMisconfigured
members()throws FeedMisconfigured
sync_tokenalways null on a paginated page

Filter by role instead: actor(), object(), target(), context(), origin(), result(), instrument(), location(), generator() and their *Type() forms all work, and actor('Storyfeed') matches the items whose actor is that party name.

Live groups are worked out from all of the items each time the feed is read, with the same axes, thresholds and burst windows as stored activities.

A source's page renders like any other: hand it to the kit, as in Rendering a Composed Feed.

Released under the MIT License.