Skip to content

Commit d766fe6

Browse files
Add make:feed and make:feed-item commands
1 parent 0484cc7 commit d766fe6

File tree

19 files changed

+429
-22
lines changed

19 files changed

+429
-22
lines changed

README.md

Lines changed: 145 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,26 @@ After that, publish the configuration file by call the console command:
2525
php artisan vendor:publish --tag=feeds
2626
```
2727

28-
## Basic Usage
28+
## Usage
29+
30+
### Create Feeds and Feed Items
31+
32+
#### Use a Laravel Idea plugin for PHP Storm
33+
34+
You can also easily create the desired classes using the [Laravel Idea](http://laravel-idea.com) plugin
35+
for [PhpStorm](https://www.jetbrains.com/phpstorm/):
36+
37+
![](.github/images/idea.png)
38+
39+
#### Use Artisan
40+
41+
```bash
42+
# This will create a `app/Feeds/UserFeed.php` file
43+
php artisan make:feed User
44+
45+
# This will create a `app/Feeds/Items/UserFeedItem.php` file
46+
php artisan make:feed-item User
47+
```
2948

3049
### Generate Feeds
3150

@@ -36,9 +55,38 @@ console command:
3655
php artisan feed:generate
3756
```
3857

39-
### Feed
58+
Each feed can be created in a certain folder of a certain storage.
59+
60+
To indicate the storage, reduce the property of `$storage` in the feed class:
61+
62+
```php
63+
class UserFeed extends Feed
64+
{
65+
protected string $storage = 'public';
66+
}
67+
```
68+
69+
By default, `public`.
70+
71+
The path to the file inside the storage is indicated in the `filiname` method:
72+
73+
```php
74+
class UserFeed extends Feed
75+
{
76+
public function filename(): string
77+
{
78+
return 'your/path/may/be/here.xml';
79+
}
80+
}
81+
```
82+
83+
By default, the class name in `kebab-case` is used. For example, `user-feed.xml` for `UserFeed` class.
84+
85+
### Filling
4086

41-
Create a feed class. For example:
87+
#### Feed
88+
89+
For example, we use this content for the Feed class:
4290

4391
```php
4492
namespace App\Feeds;
@@ -78,9 +126,9 @@ class UserFeed extends Feed
78126
}
79127
```
80128

81-
### Feed Item
129+
#### Feed Item
82130

83-
Create a feed item class. For example:
131+
For example, we use this content for the Feed Item class:
84132

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

158-
### Laravel Idea Support
206+
## Objects, attributes and more
159207

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

163-
![](.github/images/idea.png)
210+
```php
211+
class UserFeed extends Feed
212+
{
213+
public function rootItem(): ?string
214+
{
215+
return 'users';
216+
}
217+
}
218+
```
219+
220+
### Adding attributes for the main section
221+
222+
```php
223+
class UserFeedItem extends FeedItem
224+
{
225+
public function attributes(): array
226+
{
227+
return [
228+
'id' => $this->model->id,
229+
'created_at' => $this->model->created_at->format('Y-m-d'),
230+
];
231+
}
232+
233+
public function toArray(): array
234+
{
235+
// ...
236+
}
237+
}
238+
```
239+
240+
### Adding attributes for nested elements
241+
242+
> [!NOTE]
243+
>
244+
> Reserved names are:
245+
>
246+
> - `@attributes`
247+
> - `@cdata`
248+
> - `@mixed`
249+
250+
```php
251+
class UserFeedItem extends FeedItem
252+
{
253+
public function toArray(): array
254+
{
255+
return [
256+
'name' => $this->model->name,
257+
'email' => $this->model->email,
258+
259+
'header' => [
260+
'@cdata' => '<h1>' . $this->model->name . '</h1>',
261+
],
262+
263+
'names' => [
264+
'Good guy' => [
265+
'@attributes' => [
266+
'my-key-1' => 'my value 1',
267+
'my-key-2' => 'my value 2',
268+
],
269+
270+
'name' => 'Luke Skywalker',
271+
'weapon' => 'Lightsaber',
272+
],
273+
274+
'Bad guy' => [
275+
'name' => [
276+
'@cdata' => '<h1>Sauron</h1>',
277+
],
278+
279+
'weapon' => 'Evil Eye',
280+
],
281+
],
282+
];
283+
}
284+
}
285+
```
286+
287+
### Header information
288+
289+
If it is necessary to change the file cap, override the `header` method in the feed class:
290+
291+
```php
292+
class UserFeed extends Feed
293+
{
294+
public function header(): string
295+
{
296+
return '<?xml version="1.0" encoding="UTF-8"?>';
297+
}
298+
}
299+
```
164300

165301
## License
166302

ide.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@
1515
"path": "/stubs/feed.stub",
1616
"parameters": {
1717
"DummyNamespace": "${INPUT_FQN|namespace}",
18-
"DummyClass": "${INPUT_CLASS|replace: ,_|className|upperCamelCase}"
18+
"DummyClass": "${INPUT_CLASS|replace: ,_|className|upperCamelCase}",
19+
"NamespacedDummyUserModel": "App\\Models\\User",
20+
"DummyUser": "User"
1921
}
2022
}
2123
}
@@ -35,7 +37,8 @@
3537
"path": "/stubs/feed_item.stub",
3638
"parameters": {
3739
"DummyNamespace": "${INPUT_FQN|namespace}",
38-
"DummyClass": "${INPUT_CLASS|replace: ,_|className|upperCamelCase}"
40+
"DummyClass": "${INPUT_CLASS|replace: ,_|className|upperCamelCase}",
41+
"NamespacedDummyUserModel": "App\\Models\\User"
3942
}
4043
}
4144
}

phpunit.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
<testsuite name="Feature">
1111
<directory>./tests/Feature</directory>
1212
</testsuite>
13+
<testsuite name="Unit">
14+
<directory>./tests/Unit</directory>
15+
</testsuite>
1316
</testsuites>
1417

1518
<source>

src/Concerns/InteractsWithName.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace DragonCode\LaravelFeed\Concerns;
6+
7+
use Illuminate\Support\Str;
8+
9+
use function class_basename;
10+
use function str_replace;
11+
12+
/** @mixin \Illuminate\Console\GeneratorCommand */
13+
trait InteractsWithName
14+
{
15+
protected function qualifyClass($name): string
16+
{
17+
return Str::finish(parent::qualifyClass($name), $this->type);
18+
}
19+
20+
protected function buildClass($name): string
21+
{
22+
return str_replace(
23+
['DummyUser'],
24+
class_basename($this->userProviderModel()),
25+
parent::buildClass($name)
26+
);
27+
}
28+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace DragonCode\LaravelFeed\Console\Commands;
6+
7+
use DragonCode\LaravelFeed\Concerns\InteractsWithName;
8+
use Illuminate\Console\GeneratorCommand;
9+
use Symfony\Component\Console\Attribute\AsCommand;
10+
use Symfony\Component\Console\Input\InputOption;
11+
12+
use function str_replace;
13+
14+
#[AsCommand('make:feed-item', 'Create a new feed item')]
15+
class FeedItemMakeCommand extends GeneratorCommand
16+
{
17+
use InteractsWithName;
18+
19+
protected $type = 'FeedItem';
20+
21+
protected function buildClass($name): string
22+
{
23+
return str_replace(
24+
['DummyUser'],
25+
$this->userProviderModel(),
26+
parent::buildClass($name)
27+
);
28+
}
29+
30+
protected function getStub(): string
31+
{
32+
return __DIR__ . '/../../../stubs/feed_item.stub';
33+
}
34+
35+
protected function getDefaultNamespace($rootNamespace): string
36+
{
37+
return $rootNamespace . '\Feeds\Items';
38+
}
39+
40+
protected function getOptions(): array
41+
{
42+
return [
43+
['force', 'f', InputOption::VALUE_NONE, 'Create the class even if the feed already exists'],
44+
];
45+
}
46+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace DragonCode\LaravelFeed\Console\Commands;
6+
7+
use DragonCode\LaravelFeed\Concerns\InteractsWithName;
8+
use Illuminate\Console\GeneratorCommand;
9+
use Symfony\Component\Console\Attribute\AsCommand;
10+
use Symfony\Component\Console\Input\InputOption;
11+
12+
#[AsCommand('make:feed', 'Create a new feed')]
13+
class FeedMakeCommand extends GeneratorCommand
14+
{
15+
use InteractsWithName;
16+
17+
protected $type = 'Feed';
18+
19+
protected function getStub(): string
20+
{
21+
return __DIR__ . '/../../../stubs/feed.stub';
22+
}
23+
24+
protected function getDefaultNamespace($rootNamespace): string
25+
{
26+
return $rootNamespace . '\Feeds';
27+
}
28+
29+
protected function getOptions(): array
30+
{
31+
return [
32+
['force', 'f', InputOption::VALUE_NONE, 'Create the class even if the feed already exists'],
33+
];
34+
}
35+
}

src/LaravelFeedServiceProvider.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
namespace DragonCode\LaravelFeed;
66

77
use DragonCode\LaravelFeed\Console\Commands\FeedGenerateCommand;
8+
use DragonCode\LaravelFeed\Console\Commands\FeedItemMakeCommand;
9+
use DragonCode\LaravelFeed\Console\Commands\FeedMakeCommand;
810
use Illuminate\Support\ServiceProvider;
911

1012
class LaravelFeedServiceProvider extends ServiceProvider
@@ -35,6 +37,8 @@ protected function registerCommands(): void
3537
{
3638
$this->commands([
3739
FeedGenerateCommand::class,
40+
FeedMakeCommand::class,
41+
FeedItemMakeCommand::class,
3842
]);
3943
}
4044
}

stubs/feed.stub

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,13 @@ use DragonCode\LaravelFeed\Feed;
88
use DragonCode\LaravelFeed\FeedItem;
99
use Illuminate\Database\Eloquent\Builder;
1010
use Illuminate\Database\Eloquent\Model;
11+
use NamespacedDummyUserModel;
1112

1213
class DummyClass extends Feed
1314
{
1415
public function builder(): Builder
1516
{
16-
return \App\Models\User::query();
17+
return DummyUser::query();
1718
}
1819

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

2930
public function item(Model $model): FeedItem
3031
{
31-
return new \App\Feeds\Items\UserFeedItem($model);
32+
return new Items\DummyUserFeedItem($model);
3233
}
3334
}

stubs/feed_item.stub

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@ namespace DummyNamespace;
66

77
use DragonCode\LaravelFeed\FeedItem;
88

9-
/** @property-read \App\Models\User $model */
9+
/** @property-read NamespacedDummyUserModel $model */
1010
class DummyClass extends FeedItem
1111
{
1212
public function toArray(): array
1313
{
1414
return [
15-
'_attributes' => [
15+
'@attributes' => [
1616
'id' => $this->model->id,
1717

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

2323
'name' => [
24-
'_cdata' => '<h1>' . $this->model->name . '</h1>',
24+
'@cdata' => '<h1>' . $this->model->name . '</h1>',
2525
],
2626

2727
'email' => $this->model->email,

0 commit comments

Comments
 (0)