Skip to content

Commit 8a89cd5

Browse files
Merge pull request #30 from TheDragonCode/1.x
Added `make:feed-info` console command
2 parents 942df14 + 65bce68 commit 8a89cd5

File tree

16 files changed

+295
-52
lines changed

16 files changed

+295
-52
lines changed

README.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,39 @@ class UserFeed extends Feed
195195
}
196196
```
197197

198+
#### Setting the head info
199+
200+
In some cases, you need to add various information to the beginning of the file.
201+
To do this, use the `info` method:
202+
203+
```php
204+
use DragonCode\LaravelFeed\Data\ElementData;
205+
use DragonCode\LaravelFeed\Feeds\Feed;
206+
use DragonCode\LaravelFeed\Feeds\Info\FeedInfo;
207+
208+
class UserFeed extends Feed
209+
{
210+
public function info(): FeedInfo
211+
{
212+
return new FeedInfo();
213+
}
214+
}
215+
```
216+
217+
```php
218+
use DragonCode\LaravelFeed\Feeds\Info\FeedInfo;
219+
220+
class UserFeedInfo extends FeedInfo
221+
{
222+
public function toArray(): array
223+
{
224+
return [
225+
// ...
226+
];
227+
}
228+
}
229+
```
230+
198231
#### Adding attributes for the main section
199232

200233
```php

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
"require": {
1414
"php": "^8.2",
1515
"ext-dom": "*",
16+
"ext-libxml": ">=2.6.21",
1617
"illuminate/database": "^11.0 || ^12.0",
1718
"illuminate/filesystem": "^11.0 || ^12.0",
1819
"illuminate/support": "^11.0 || ^12.0",

ide.json

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,27 @@
4545
}
4646
}
4747
]
48+
},
49+
{
50+
"id": "dragon-code.xml-feeds.info",
51+
"name": "Create XML Feed Info",
52+
"classSuffix": "FeedInfo",
53+
"regex": ".+",
54+
"files": [
55+
{
56+
"appNamespace": "Feeds\\Info",
57+
"name": "${INPUT_CLASS|replace: ,_|className|upperCamelCase}.php",
58+
"template": {
59+
"type": "stub",
60+
"path": "/stubs/feed_info.stub",
61+
"fallbackPath": "stubs/feed_info.stub",
62+
"parameters": {
63+
"DummyNamespace": "${INPUT_FQN|namespace}",
64+
"DummyClass": "${INPUT_CLASS|replace: ,_|className|upperCamelCase}"
65+
}
66+
}
67+
}
68+
]
4869
}
4970
]
5071
}
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-info', 'Create a new feed info')]
13+
class FeedInfoMakeCommand extends GeneratorCommand
14+
{
15+
use InteractsWithName;
16+
17+
protected $type = 'FeedInfo';
18+
19+
protected function getStub(): string
20+
{
21+
return __DIR__ . '/../../../stubs/feed_info.php';
22+
}
23+
24+
protected function getDefaultNamespace($rootNamespace): string
25+
{
26+
return $rootNamespace . '\Feeds\Info';
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/Feeds/Feed.php

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

77
use DragonCode\LaravelFeed\Data\ElementData;
8+
use DragonCode\LaravelFeed\Feeds\Info\FeedInfo;
89
use DragonCode\LaravelFeed\Feeds\Items\FeedItem;
910
use Illuminate\Contracts\Filesystem\Filesystem;
1011
use Illuminate\Database\Eloquent\Builder;
@@ -47,6 +48,11 @@ public function root(): ElementData
4748
return new ElementData;
4849
}
4950

51+
public function info(): FeedInfo
52+
{
53+
return new FeedInfo;
54+
}
55+
5056
public function filename(): string
5157
{
5258
return $this->filename ??= Str::kebab(class_basename($this)) . '.xml';

src/Feeds/Info/FeedInfo.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace DragonCode\LaravelFeed\Feeds\Info;
6+
7+
use Illuminate\Contracts\Support\Arrayable;
8+
9+
class FeedInfo implements Arrayable
10+
{
11+
public function toArray(): array
12+
{
13+
return [];
14+
}
15+
}

src/LaravelFeedServiceProvider.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace DragonCode\LaravelFeed;
66

77
use DragonCode\LaravelFeed\Console\Commands\FeedGenerateCommand;
8+
use DragonCode\LaravelFeed\Console\Commands\FeedInfoMakeCommand;
89
use DragonCode\LaravelFeed\Console\Commands\FeedItemMakeCommand;
910
use DragonCode\LaravelFeed\Console\Commands\FeedMakeCommand;
1011
use Illuminate\Support\ServiceProvider;
@@ -37,8 +38,9 @@ protected function registerCommands(): void
3738
{
3839
$this->commands([
3940
FeedGenerateCommand::class,
40-
FeedMakeCommand::class,
41+
FeedInfoMakeCommand::class,
4142
FeedItemMakeCommand::class,
43+
FeedMakeCommand::class,
4244
]);
4345
}
4446
}

src/Services/ConvertToXml.php

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
namespace DragonCode\LaravelFeed\Services;
66

77
use DOMDocument;
8-
use DOMElement;
8+
use DOMNode;
99
use DragonCode\LaravelFeed\Feeds\Items\FeedItem;
1010
use Illuminate\Container\Attributes\Config;
1111
use Illuminate\Support\Str;
@@ -31,7 +31,7 @@ public function __construct(
3131
$this->document->preserveWhiteSpace = ! $pretty;
3232
}
3333

34-
public function convert(FeedItem $item): string
34+
public function convertItem(FeedItem $item): string
3535
{
3636
$box = $this->performBox($item);
3737

@@ -40,7 +40,16 @@ public function convert(FeedItem $item): string
4040
return $this->toXml($box);
4141
}
4242

43-
protected function performBox(FeedItem $item): DOMElement
43+
public function convertInfo(array $info): string
44+
{
45+
$box = $this->document->createDocumentFragment();
46+
47+
$this->performItem($box, $info);
48+
49+
return $this->toXml($box);
50+
}
51+
52+
protected function performBox(FeedItem $item): DOMNode
4453
{
4554
$element = $this->createElement($item->name());
4655

@@ -51,7 +60,7 @@ protected function performBox(FeedItem $item): DOMElement
5160
return $element;
5261
}
5362

54-
protected function performItem(DOMElement $parent, array $items): void
63+
protected function performItem(DOMNode $parent, array $items): void
5564
{
5665
foreach ($items as $key => $value) {
5766
$key = $this->convertKey($key);
@@ -92,34 +101,34 @@ protected function isPrefixed(string $key): bool
92101
return str_starts_with($key, '@');
93102
}
94103

95-
protected function createElement(string $name, bool|float|int|string|null $value = ''): DOMElement
104+
protected function createElement(string $name, bool|float|int|string|null $value = ''): DOMNode
96105
{
97106
return $this->document->createElement($name, $this->convertValue($value));
98107
}
99108

100-
protected function setAttributes(DOMElement $element, array $attributes): void
109+
protected function setAttributes(DOMNode $element, array $attributes): void
101110
{
102111
foreach ($attributes as $key => $value) {
103112
$element->setAttribute($key, $this->convertValue($value));
104113
}
105114
}
106115

107-
protected function setCData(DOMElement $element, string $value): void
116+
protected function setCData(DOMNode $element, string $value): void
108117
{
109118
$element->appendChild(
110119
$this->document->createCDATASection($value)
111120
);
112121
}
113122

114-
protected function setMixed(DOMElement $element, string $value): void
123+
protected function setMixed(DOMNode $element, string $value): void
115124
{
116125
$fragment = $this->document->createDocumentFragment();
117126
$fragment->appendXML($value);
118127

119128
$element->appendChild($fragment);
120129
}
121130

122-
protected function setItemsArray(DOMElement $parent, $value, string $key): void
131+
protected function setItemsArray(DOMNode $parent, $value, string $key): void
123132
{
124133
$key = Str::substr($key, 1);
125134

@@ -128,7 +137,7 @@ protected function setItemsArray(DOMElement $parent, $value, string $key): void
128137
}
129138
}
130139

131-
protected function setItems(DOMElement $parent, string $key, mixed $value): void
140+
protected function setItems(DOMNode $parent, string $key, mixed $value): void
132141
{
133142
$element = $this->createElement($key, is_array($value) ? '' : $this->convertValue($value));
134143

@@ -139,14 +148,14 @@ protected function setItems(DOMElement $parent, string $key, mixed $value): void
139148
$parent->appendChild($element);
140149
}
141150

142-
protected function setRaw(DOMElement $parent, mixed $value): void
151+
protected function setRaw(DOMNode $parent, mixed $value): void
143152
{
144153
$parent->nodeValue = $this->convertValue($value);
145154
}
146155

147-
protected function toXml(DOMElement $item): string
156+
protected function toXml(DOMNode $item): string
148157
{
149-
return $this->document->saveXML($item);
158+
return $this->document->saveXML($item, LIBXML_COMPACT);
150159
}
151160

152161
protected function convertKey(int|string $key): string

src/Services/Generator.php

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use DragonCode\LaravelFeed\Feeds\Feed;
99
use Illuminate\Database\Eloquent\Collection;
1010

11+
use function blank;
1112
use function collect;
1213
use function implode;
1314
use function sprintf;
@@ -26,6 +27,8 @@ public function feed(Feed $feed): void
2627
);
2728

2829
$this->performHeader($file, $feed);
30+
$this->performInfo($file, $feed);
31+
$this->performRoot($file, $feed);
2932
$this->performItem($file, $feed);
3033
$this->performFooter($file, $feed);
3134

@@ -38,7 +41,7 @@ protected function performItem($file, Feed $feed): void
3841
$content = [];
3942

4043
foreach ($models as $model) {
41-
$content[] = $this->converter->convert(
44+
$content[] = $this->converter->convertItem(
4245
$feed->item($model)
4346
);
4447
}
@@ -49,14 +52,30 @@ protected function performItem($file, Feed $feed): void
4952

5053
protected function performHeader($file, Feed $feed): void
5154
{
52-
$value = $feed->header();
55+
$this->append($file, $feed->header());
56+
}
5357

54-
if ($name = $feed->root()->name) {
55-
$value .= ! empty($feed->root()->attributes)
56-
? sprintf("\n<%s %s>\n", $name, $this->makeRootAttributes($feed->root()))
57-
: sprintf("\n<%s>\n", $name);
58+
protected function performInfo($file, Feed $feed): void
59+
{
60+
if (blank($info = $feed->info()->toArray())) {
61+
return;
62+
}
63+
64+
$value = $this->converter->convertInfo($info);
65+
66+
$this->append($file, PHP_EOL . $value);
67+
}
68+
69+
protected function performRoot($file, Feed $feed): void
70+
{
71+
if (! $name = $feed->root()->name) {
72+
return;
5873
}
5974

75+
$value = ! empty($feed->root()->attributes)
76+
? sprintf("\n<%s %s>\n", $name, $this->makeRootAttributes($feed->root()))
77+
: sprintf("\n<%s>\n", $name);
78+
6079
$this->append($file, $value);
6180
}
6281

stubs/feed_info.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace DummyNamespace;
6+
7+
use DragonCode\LaravelFeed\Feeds\Info\FeedInfo;
8+
9+
class DummyClass extends FeedInfo
10+
{
11+
public function toArray(): array
12+
{
13+
return [
14+
//
15+
];
16+
}
17+
}

0 commit comments

Comments
 (0)