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
81 changes: 81 additions & 0 deletions src/Services/Filesystem.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?php

declare(strict_types=1);

namespace DragonCode\LaravelFeed\Services;

use Illuminate\Filesystem\Filesystem as File;

use function dirname;
use function fclose;
use function fopen;
use function fwrite;

class Filesystem
{
public function __construct(
protected File $file,
) {}

/**
* @return resource
*/
public function open(string $path)
{
$path = $this->draft($path);

$this->ensureFileDelete($path);
$this->ensureDirectory($path);

return fopen($path, 'ab');
}

public function append($resource, string $content): void
{
if (! empty($content)) {
fwrite($resource, $content);
}
}

/**
* @param resource $resource
*/
public function release($resource, string $path): void
{
$this->close($resource);

if ($this->file->exists($path)) {
$this->file->delete($path);
}

$this->file->move(
$this->draft($path),
$path
);
}

/**
* @param resource $resource
*/
public function close($resource): void
{
fclose($resource);
}

protected function ensureFileDelete(string $path): void
{
if ($this->file->exists($path)) {
$this->file->delete($path);
}
}

protected function ensureDirectory(string $path): void
{
$this->file->ensureDirectoryExists(dirname($path));
}

protected function draft(string $path): string
{
return $path . '.draft';
}
}
43 changes: 7 additions & 36 deletions src/Services/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,8 @@
use DragonCode\LaravelFeed\Data\ElementData;
use DragonCode\LaravelFeed\Feeds\Feed;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Filesystem\Filesystem;

use function collect;
use function dirname;
use function fclose;
use function fopen;
use function fwrite;
use function implode;
use function sprintf;

Expand All @@ -27,15 +22,14 @@ public function __construct(
public function feed(Feed $feed): void
{
$file = $this->openFile(
$filename = $this->draft($feed)
$path = $feed->path()
);

$this->performHeader($file, $feed);
$this->performItem($file, $feed);
$this->performFooter($file, $feed);

$this->closeFile($file);
$this->release($feed, $filename);
$this->release($file, $path);
}

protected function performItem($file, Feed $feed): void
Expand Down Expand Up @@ -86,39 +80,16 @@ protected function makeRootAttributes(ElementData $item): string

protected function append($file, string $content): void
{
if (! empty($content)) {
fwrite($file, $content);
}
}

protected function release(Feed $feed, string $draft): void
{
if ($this->filesystem->exists($feed->path())) {
$this->filesystem->delete($feed->path());
}

$this->filesystem->move($draft, $feed->path());
}

protected function openFile(string $filename)
{
$this->ensureDirectory($filename);

return fopen($filename, 'ab');
}

protected function closeFile($file): void
{
fclose($file);
$this->filesystem->append($file, $content);
}

protected function ensureDirectory(string $filename): void
protected function release($file, string $path): void
{
$this->filesystem->ensureDirectoryExists(dirname($filename));
$this->filesystem->release($file, $path);
}

protected function draft(Feed $feed): string
protected function openFile(string $path)
{
return $feed->path() . '.draft';
return $this->filesystem->open($path);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<FilledFeed>
<FullFeed>
<news><record_title>[NEWS]:Some 1</record_title><record_content>Some content 1</record_content><extra>Some extra data</extra><with_attributes><Good_guy my-key-1="my value 1" my-key-2="my value 2"><name>Luke Skywalker</name><weapon>Lightsaber</weapon></Good_guy><Bad_guy><name><![CDATA[<h1>Sauron</h1>]]></name><weapon>Evil Eye</weapon></Bad_guy></with_attributes></news>
<news><record_title>[NEWS]:Some 2</record_title><record_content>Some content 2</record_content><extra>Some extra data</extra><with_attributes><Good_guy my-key-1="my value 1" my-key-2="my value 2"><name>Luke Skywalker</name><weapon>Lightsaber</weapon></Good_guy><Bad_guy><name><![CDATA[<h1>Sauron</h1>]]></name><weapon>Evil Eye</weapon></Bad_guy></with_attributes></news>
<news><record_title>[NEWS]:Some 3</record_title><record_content>Some content 3</record_content><extra>Some extra data</extra><with_attributes><Good_guy my-key-1="my value 1" my-key-2="my value 2"><name>Luke Skywalker</name><weapon>Lightsaber</weapon></Good_guy><Bad_guy><name><![CDATA[<h1>Sauron</h1>]]></name><weapon>Evil Eye</weapon></Bad_guy></with_attributes></news>
</FilledFeed>
</FullFeed>
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<FilledFeed>
<FullFeed>
<news>
<record_title>[NEWS]:Some 1</record_title>
<record_content>Some content 1</record_content>
Expand Down Expand Up @@ -45,4 +45,4 @@
</Bad_guy>
</with_attributes>
</news>
</FilledFeed>
</FullFeed>
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<FilledFeed>
<PartialFeed>
<news><record_title>[NEWS]:Some 1</record_title><record_content>Some content 1</record_content><extra>Some extra data</extra><with_attributes><Good_guy my-key-1="my value 1" my-key-2="my value 2"><name>Luke Skywalker</name><weapon>Lightsaber</weapon></Good_guy><Bad_guy><name><![CDATA[<h1>Sauron</h1>]]></name><weapon>Evil Eye</weapon></Bad_guy></with_attributes></news>
<news><record_title>[NEWS]:Some 2</record_title><record_content>Some content 2</record_content><extra>Some extra data</extra><with_attributes><Good_guy my-key-1="my value 1" my-key-2="my value 2"><name>Luke Skywalker</name><weapon>Lightsaber</weapon></Good_guy><Bad_guy><name><![CDATA[<h1>Sauron</h1>]]></name><weapon>Evil Eye</weapon></Bad_guy></with_attributes></news>
<news><record_title>[NEWS]:Some 3</record_title><record_content>Some content 3</record_content><extra>Some extra data</extra><with_attributes><Good_guy my-key-1="my value 1" my-key-2="my value 2"><name>Luke Skywalker</name><weapon>Lightsaber</weapon></Good_guy><Bad_guy><name><![CDATA[<h1>Sauron</h1>]]></name><weapon>Evil Eye</weapon></Bad_guy></with_attributes></news>
</FilledFeed>
</PartialFeed>
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<FilledFeed>
<PartialFeed>
<news>
<record_title>[NEWS]:Some 1</record_title>
<record_content>Some content 1</record_content>
Expand Down Expand Up @@ -45,4 +45,4 @@
</Bad_guy>
</with_attributes>
</news>
</FilledFeed>
</PartialFeed>
4 changes: 2 additions & 2 deletions tests/Feature/FullTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use DragonCode\LaravelFeed\Console\Commands\FeedGenerateCommand;
use Workbench\App\Data\NewsFakeData;
use Workbench\App\Feeds\FilledFeed;
use Workbench\App\Feeds\FullFeed;

use function Pest\Laravel\artisan;

Expand All @@ -13,7 +13,7 @@

createNews(...NewsFakeData::toArray());

$feed = app()->make(FilledFeed::class);
$feed = app()->make(FullFeed::class);

artisan(FeedGenerateCommand::class)->run();

Expand Down
4 changes: 2 additions & 2 deletions tests/Feature/PartialTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use DragonCode\LaravelFeed\Console\Commands\FeedGenerateCommand;
use Workbench\App\Data\NewsFakeData;
use Workbench\App\Feeds\FilledFeed;
use Workbench\App\Feeds\PartialFeed;

use function Pest\Laravel\artisan;

Expand All @@ -17,7 +17,7 @@

createNews(...NewsFakeData::toArray());

$feed = app()->make(FilledFeed::class);
$feed = app()->make(PartialFeed::class);

artisan(FeedGenerateCommand::class)->run();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
use function class_basename;
use function now;

class FilledFeed extends Feed
class FullFeed extends Feed
{
public function builder(): Builder
{
Expand All @@ -31,7 +31,7 @@ class_basename($this)

public function filename(): string
{
return 'nested/filled.xml';
return 'nested/full.xml';
}

public function item(Model $model): FeedItem
Expand Down
36 changes: 36 additions & 0 deletions workbench/app/Feeds/PartialFeed.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

declare(strict_types=1);

namespace Workbench\App\Feeds;

use DragonCode\LaravelFeed\Data\ElementData;
use DragonCode\LaravelFeed\Feeds\Feed;
use DragonCode\LaravelFeed\Feeds\Items\FeedItem;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Workbench\App\Feeds\Items\NewsFeedItem;
use Workbench\App\Models\News;

use function class_basename;
use function now;

class PartialFeed extends Feed
{
public function builder(): Builder
{
return News::query()->where('updated_at', '>', now()->subDay());
}

public function root(): ElementData
{
return new ElementData(
class_basename($this)
);
}

public function item(Model $model): FeedItem
{
return new NewsFeedItem($model);
}
}
7 changes: 5 additions & 2 deletions workbench/config/feeds.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,17 @@
declare(strict_types=1);

use Workbench\App\Feeds\EmptyFeed;
use Workbench\App\Feeds\FilledFeed;
use Workbench\App\Feeds\FullFeed;
use Workbench\App\Feeds\PartialFeed;
use Workbench\App\Feeds\SitemapFeed;
use Workbench\App\Feeds\YandexFeed;

return [
'channels' => [
EmptyFeed::class => true,
FilledFeed::class => true,
FullFeed::class => true,
PartialFeed::class => true,

SitemapFeed::class => true,
YandexFeed::class => true,
],
Expand Down