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
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ for [PhpStorm](https://www.jetbrains.com/phpstorm/):
# This will create a `app/Feeds/UserFeed.php` file
php artisan make:feed User

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

# This will create a `app/Feeds/Items/UserFeedItem.php` file
php artisan make:feed-item User
```
Expand Down
11 changes: 0 additions & 11 deletions src/Console/Commands/FeedItemMakeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,13 @@
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';
Expand Down
21 changes: 21 additions & 0 deletions src/Console/Commands/FeedMakeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,26 @@ class FeedMakeCommand extends GeneratorCommand

protected $type = 'Feed';

public function handle(): void
{
parent::handle();

if ($this->option('with-item')) {
$this->makeFeedItem(
$this->argument('name'),
(bool) $this->option('force')
);
}
}

protected function makeFeedItem(string $name, bool $force): void
{
$this->call(FeedItemMakeCommand::class, [
'name' => $name,
'--force' => $force,
]);
}

protected function getStub(): string
{
return __DIR__ . '/../../../stubs/feed.stub';
Expand All @@ -29,6 +49,7 @@ protected function getDefaultNamespace($rootNamespace): string
protected function getOptions(): array
{
return [
['with-item', 'wi', InputOption::VALUE_NONE, 'Create the class with feed item'],
['force', 'f', InputOption::VALUE_NONE, 'Create the class even if the feed already exists'],
];
}
Expand Down
34 changes: 34 additions & 0 deletions tests/.pest/snapshots/Unit/Console/MakeTest/make_with_item.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

declare(strict_types=1);

namespace App\Feeds;

use DragonCode\LaravelFeed\Feed;
use DragonCode\LaravelFeed\FeedItem;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Foundation\Auth\User;

class QweRtyFeed extends Feed
{
public function builder(): Builder
{
return User::query();
}

public function rootItem(): ?string
{
return 'users';
}

public function filename(): string
{
return 'my-feed.xml';
}

public function item(Model $model): FeedItem
{
return new Items\UserFeedItem($model);
}
}
30 changes: 30 additions & 0 deletions tests/.pest/snapshots/Unit/Console/MakeTest/make_with_item__2.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

declare(strict_types=1);

namespace App\Feeds\Items;

use DragonCode\LaravelFeed\FeedItem;

/** @property-read Illuminate\Foundation\Auth\User $model */
class QweRtyFeedItem extends FeedItem
{
public function toArray(): array
{
return [
'@attributes' => [
'id' => $this->model->id,

'updated_at' => $this->model->updated_at->toDateTimeString(),

'verified' => ! empty($this->model->email_verified_at),
],

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

'email' => $this->model->email,
];
}
}
15 changes: 15 additions & 0 deletions tests/Unit/Console/MakeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,18 @@

expect('FooBar')->toMatchFeedSnapshot();
});

test('make with item', function () {
deleteFeed('QweRty');
deleteFeed('Items/QweRty');

artisan(FeedMakeCommand::class, [
'name' => 'QweRty',
'--with-item' => true,
'--force' => true,
])->assertSuccessful()->run();

expect('QweRty')
->toMatchFeedSnapshot()
->toMatchFeedItemSnapshot();
});