Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
154 changes: 145 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,26 @@ After that, publish the configuration file by call the console command:
php artisan vendor:publish --tag=feeds
```

## Basic Usage
## Usage

### Create Feeds and Feed Items

#### Use a Laravel Idea plugin for PHP Storm

You can also easily create the desired classes using the [Laravel Idea](http://laravel-idea.com) plugin
for [PhpStorm](https://www.jetbrains.com/phpstorm/):

![](.github/images/idea.png)

#### Use Artisan

```bash
# This will create a `app/Feeds/UserFeed.php` file
php artisan make:feed User

# This will create a `app/Feeds/Items/UserFeedItem.php` file
php artisan make:feed-item User
```

### Generate Feeds

Expand All @@ -36,9 +55,38 @@ console command:
php artisan feed:generate
```

### Feed
Each feed can be created in a certain folder of a certain storage.

To indicate the storage, reduce the property of `$storage` in the feed class:

```php
class UserFeed extends Feed
{
protected string $storage = 'public';
}
```

By default, `public`.

The path to the file inside the storage is indicated in the `filiname` method:

```php
class UserFeed extends Feed
{
public function filename(): string
{
return 'your/path/may/be/here.xml';
}
}
```

By default, the class name in `kebab-case` is used. For example, `user-feed.xml` for `UserFeed` class.

### Filling

Create a feed class. For example:
#### Feed

For example, we use this content for the Feed class:

```php
namespace App\Feeds;
Expand Down Expand Up @@ -78,9 +126,9 @@ class UserFeed extends Feed
}
```

### Feed Item
#### Feed Item

Create a feed item class. For example:
For example, we use this content for the Feed Item class:

```php
namespace App\Feeds\Items;
Expand Down Expand Up @@ -155,12 +203,100 @@ According to this example, the XML file with the following contents will be gene
</users>
```

### Laravel Idea Support
## Objects, attributes and more

You can also easily create the desired classes using the [Laravel Idea](http://laravel-idea.com) plugin
for [PhpStorm](https://www.jetbrains.com/phpstorm/):
### Setting the name of the root element

![](.github/images/idea.png)
```php
class UserFeed extends Feed
{
public function rootItem(): ?string
{
return 'users';
}
}
```

### Adding attributes for the main section

```php
class UserFeedItem extends FeedItem
{
public function attributes(): array
{
return [
'id' => $this->model->id,
'created_at' => $this->model->created_at->format('Y-m-d'),
];
}

public function toArray(): array
{
// ...
}
}
```

### Adding attributes for nested elements

> [!NOTE]
>
> Reserved names are:
>
> - `@attributes`
> - `@cdata`
> - `@mixed`

```php
class UserFeedItem extends FeedItem
{
public function toArray(): array
{
return [
'name' => $this->model->name,
'email' => $this->model->email,

'header' => [
'@cdata' => '<h1>' . $this->model->name . '</h1>',
],

'names' => [
'Good guy' => [
'@attributes' => [
'my-key-1' => 'my value 1',
'my-key-2' => 'my value 2',
],

'name' => 'Luke Skywalker',
'weapon' => 'Lightsaber',
],

'Bad guy' => [
'name' => [
'@cdata' => '<h1>Sauron</h1>',
],

'weapon' => 'Evil Eye',
],
],
];
}
}
```

### Header information

If it is necessary to change the file cap, override the `header` method in the feed class:

```php
class UserFeed extends Feed
{
public function header(): string
{
return '<?xml version="1.0" encoding="UTF-8"?>';
}
}
```

## License

Expand Down
7 changes: 5 additions & 2 deletions ide.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
"path": "/stubs/feed.stub",
"parameters": {
"DummyNamespace": "${INPUT_FQN|namespace}",
"DummyClass": "${INPUT_CLASS|replace: ,_|className|upperCamelCase}"
"DummyClass": "${INPUT_CLASS|replace: ,_|className|upperCamelCase}",
"NamespacedDummyUserModel": "App\\Models\\User",
"DummyUser": "User"
}
}
}
Expand All @@ -35,7 +37,8 @@
"path": "/stubs/feed_item.stub",
"parameters": {
"DummyNamespace": "${INPUT_FQN|namespace}",
"DummyClass": "${INPUT_CLASS|replace: ,_|className|upperCamelCase}"
"DummyClass": "${INPUT_CLASS|replace: ,_|className|upperCamelCase}",
"NamespacedDummyUserModel": "App\\Models\\User"
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
<testsuite name="Feature">
<directory>./tests/Feature</directory>
</testsuite>
<testsuite name="Unit">
<directory>./tests/Unit</directory>
</testsuite>
</testsuites>

<source>
Expand Down
28 changes: 28 additions & 0 deletions src/Concerns/InteractsWithName.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace DragonCode\LaravelFeed\Concerns;

use Illuminate\Support\Str;

use function class_basename;
use function str_replace;

/** @mixin \Illuminate\Console\GeneratorCommand */
trait InteractsWithName
{
protected function qualifyClass($name): string
{
return Str::finish(parent::qualifyClass($name), $this->type);
}

protected function buildClass($name): string
{
return str_replace(
['DummyUser'],
class_basename($this->userProviderModel()),
parent::buildClass($name)
);
}
}
46 changes: 46 additions & 0 deletions src/Console/Commands/FeedItemMakeCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

declare(strict_types=1);

namespace DragonCode\LaravelFeed\Console\Commands;

use DragonCode\LaravelFeed\Concerns\InteractsWithName;
use Illuminate\Console\GeneratorCommand;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Input\InputOption;

use function str_replace;

#[AsCommand('make:feed-item', 'Create a new feed item')]
class FeedItemMakeCommand extends GeneratorCommand
{
use InteractsWithName;

protected $type = 'FeedItem';

protected function buildClass($name): string
{
return str_replace(
['DummyUser'],
$this->userProviderModel(),
parent::buildClass($name)
);
}

protected function getStub(): string
{
return __DIR__ . '/../../../stubs/feed_item.stub';
}

protected function getDefaultNamespace($rootNamespace): string
{
return $rootNamespace . '\Feeds\Items';
}

protected function getOptions(): array
{
return [
['force', 'f', InputOption::VALUE_NONE, 'Create the class even if the feed already exists'],
];
}
}
35 changes: 35 additions & 0 deletions src/Console/Commands/FeedMakeCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

declare(strict_types=1);

namespace DragonCode\LaravelFeed\Console\Commands;

use DragonCode\LaravelFeed\Concerns\InteractsWithName;
use Illuminate\Console\GeneratorCommand;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Input\InputOption;

#[AsCommand('make:feed', 'Create a new feed')]
class FeedMakeCommand extends GeneratorCommand
{
use InteractsWithName;

protected $type = 'Feed';

protected function getStub(): string
{
return __DIR__ . '/../../../stubs/feed.stub';
}

protected function getDefaultNamespace($rootNamespace): string
{
return $rootNamespace . '\Feeds';
}

protected function getOptions(): array
{
return [
['force', 'f', InputOption::VALUE_NONE, 'Create the class even if the feed already exists'],
];
}
}
4 changes: 4 additions & 0 deletions src/LaravelFeedServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
namespace DragonCode\LaravelFeed;

use DragonCode\LaravelFeed\Console\Commands\FeedGenerateCommand;
use DragonCode\LaravelFeed\Console\Commands\FeedItemMakeCommand;
use DragonCode\LaravelFeed\Console\Commands\FeedMakeCommand;
use Illuminate\Support\ServiceProvider;

class LaravelFeedServiceProvider extends ServiceProvider
Expand Down Expand Up @@ -35,6 +37,8 @@ protected function registerCommands(): void
{
$this->commands([
FeedGenerateCommand::class,
FeedMakeCommand::class,
FeedItemMakeCommand::class,
]);
}
}
5 changes: 3 additions & 2 deletions stubs/feed.stub
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@ use DragonCode\LaravelFeed\Feed;
use DragonCode\LaravelFeed\FeedItem;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use NamespacedDummyUserModel;

class DummyClass extends Feed
{
public function builder(): Builder
{
return \App\Models\User::query();
return DummyUser::query();
}

public function rootItem(): ?string
Expand All @@ -28,6 +29,6 @@ class DummyClass extends Feed

public function item(Model $model): FeedItem
{
return new \App\Feeds\Items\UserFeedItem($model);
return new Items\DummyUserFeedItem($model);
}
}
6 changes: 3 additions & 3 deletions stubs/feed_item.stub
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ namespace DummyNamespace;

use DragonCode\LaravelFeed\FeedItem;

/** @property-read \App\Models\User $model */
/** @property-read NamespacedDummyUserModel $model */
class DummyClass extends FeedItem
{
public function toArray(): array
{
return [
'_attributes' => [
'@attributes' => [
'id' => $this->model->id,

'updated_at' => $this->model->updated_at->toDateTimeString(),
Expand All @@ -21,7 +21,7 @@ class DummyClass extends FeedItem
],

'name' => [
'_cdata' => '<h1>' . $this->model->name . '</h1>',
'@cdata' => '<h1>' . $this->model->name . '</h1>',
],

'email' => $this->model->email,
Expand Down
Loading