Skip to content

Commit d29e26a

Browse files
Added --with-item parameter for make:feed console command
1 parent 7ecfbd2 commit d29e26a

File tree

6 files changed

+103
-11
lines changed

6 files changed

+103
-11
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ for [PhpStorm](https://www.jetbrains.com/phpstorm/):
4242
# This will create a `app/Feeds/UserFeed.php` file
4343
php artisan make:feed User
4444

45+
# This will create a `app/Feeds/UserFeed.php` and `app/Feeds/Items/UserFeedItem.php` files
46+
php artisan make:feed User --with-item
47+
4548
# This will create a `app/Feeds/Items/UserFeedItem.php` file
4649
php artisan make:feed-item User
4750
```

src/Console/Commands/FeedItemMakeCommand.php

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,24 +9,13 @@
99
use Symfony\Component\Console\Attribute\AsCommand;
1010
use Symfony\Component\Console\Input\InputOption;
1111

12-
use function str_replace;
13-
1412
#[AsCommand('make:feed-item', 'Create a new feed item')]
1513
class FeedItemMakeCommand extends GeneratorCommand
1614
{
1715
use InteractsWithName;
1816

1917
protected $type = 'FeedItem';
2018

21-
protected function buildClass($name): string
22-
{
23-
return str_replace(
24-
['DummyUser'],
25-
$this->userProviderModel(),
26-
parent::buildClass($name)
27-
);
28-
}
29-
3019
protected function getStub(): string
3120
{
3221
return __DIR__ . '/../../../stubs/feed_item.stub';

src/Console/Commands/FeedMakeCommand.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,26 @@ class FeedMakeCommand extends GeneratorCommand
1616

1717
protected $type = 'Feed';
1818

19+
public function handle(): void
20+
{
21+
parent::handle();
22+
23+
if ($this->option('with-item')) {
24+
$this->makeFeedItem(
25+
$this->argument('name'),
26+
(bool) $this->option('force')
27+
);
28+
}
29+
}
30+
31+
protected function makeFeedItem(string $name, bool $force): void
32+
{
33+
$this->call(FeedItemMakeCommand::class, [
34+
'name' => $name,
35+
'--force' => $force,
36+
]);
37+
}
38+
1939
protected function getStub(): string
2040
{
2141
return __DIR__ . '/../../../stubs/feed.stub';
@@ -29,6 +49,7 @@ protected function getDefaultNamespace($rootNamespace): string
2949
protected function getOptions(): array
3050
{
3151
return [
52+
['with-item', 'wi', InputOption::VALUE_NONE, 'Create the class with feed item'],
3253
['force', 'f', InputOption::VALUE_NONE, 'Create the class even if the feed already exists'],
3354
];
3455
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App\Feeds;
6+
7+
use DragonCode\LaravelFeed\Feed;
8+
use DragonCode\LaravelFeed\FeedItem;
9+
use Illuminate\Database\Eloquent\Builder;
10+
use Illuminate\Database\Eloquent\Model;
11+
use Illuminate\Foundation\Auth\User;
12+
13+
class QweRtyFeed extends Feed
14+
{
15+
public function builder(): Builder
16+
{
17+
return User::query();
18+
}
19+
20+
public function rootItem(): ?string
21+
{
22+
return 'users';
23+
}
24+
25+
public function filename(): string
26+
{
27+
return 'my-feed.xml';
28+
}
29+
30+
public function item(Model $model): FeedItem
31+
{
32+
return new Items\UserFeedItem($model);
33+
}
34+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App\Feeds\Items;
6+
7+
use DragonCode\LaravelFeed\FeedItem;
8+
9+
/** @property-read Illuminate\Foundation\Auth\User $model */
10+
class QweRtyFeedItem extends FeedItem
11+
{
12+
public function toArray(): array
13+
{
14+
return [
15+
'@attributes' => [
16+
'id' => $this->model->id,
17+
18+
'updated_at' => $this->model->updated_at->toDateTimeString(),
19+
20+
'verified' => ! empty($this->model->email_verified_at),
21+
],
22+
23+
'name' => [
24+
'@cdata' => '<h1>' . $this->model->name . '</h1>',
25+
],
26+
27+
'email' => $this->model->email,
28+
];
29+
}
30+
}

tests/Unit/Console/MakeTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,18 @@
1616

1717
expect('FooBar')->toMatchFeedSnapshot();
1818
});
19+
20+
test('make with item', function () {
21+
deleteFeed('QweRty');
22+
deleteFeed('Items/QweRty');
23+
24+
artisan(FeedMakeCommand::class, [
25+
'name' => 'QweRty',
26+
'--with-item' => true,
27+
'--force' => true,
28+
])->assertSuccessful()->run();
29+
30+
expect('QweRty')
31+
->toMatchFeedSnapshot()
32+
->toMatchFeedItemSnapshot();
33+
});

0 commit comments

Comments
 (0)