1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Astrotomic\Stancy\Factories; |
4
|
|
|
|
5
|
|
|
use Astrotomic\Stancy\Contracts\ExportFactory as ExportFactoryContract; |
6
|
|
|
use Astrotomic\Stancy\Contracts\Page as PageContract; |
7
|
|
|
use Astrotomic\Stancy\Contracts\PageFactory as PageFactoryContract; |
8
|
|
|
use Astrotomic\Stancy\Traits\ConvertsSheetToPage; |
9
|
|
|
use Illuminate\Contracts\Routing\UrlGenerator as UrlGeneratorContract; |
10
|
|
|
use Illuminate\Support\Str; |
11
|
|
|
use Spatie\Export\Exporter; |
12
|
|
|
use Spatie\Sheets\Facades\Sheets; |
13
|
|
|
|
14
|
|
|
class ExportFactory implements ExportFactoryContract |
15
|
|
|
{ |
16
|
|
|
use ConvertsSheetToPage; |
17
|
|
|
|
18
|
|
|
/** @var Exporter */ |
19
|
|
|
protected $exporter; |
20
|
|
|
|
21
|
|
|
/** @var UrlGeneratorContract */ |
22
|
|
|
protected $urlGenerator; |
23
|
|
|
|
24
|
5 |
|
public function __construct(PageFactoryContract $pageFactory, Exporter $exporter, UrlGeneratorContract $urlGenerator) |
25
|
|
|
{ |
26
|
5 |
|
$this->pageFactory = $pageFactory; |
27
|
5 |
|
$this->exporter = $exporter; |
28
|
5 |
|
$this->urlGenerator = $urlGenerator; |
29
|
5 |
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @param string[] $list |
33
|
|
|
* |
34
|
|
|
* @return ExportFactoryContract |
|
|
|
|
35
|
|
|
*/ |
36
|
3 |
|
public function addSheetList(array $list): ExportFactoryContract |
37
|
|
|
{ |
38
|
3 |
|
foreach ($list as $entry) { |
39
|
3 |
|
if (Str::contains($entry, ':')) { |
40
|
2 |
|
[$collection, $path] = explode(':', $entry); |
41
|
|
|
|
42
|
2 |
|
$this->addPages([$this->sheetToPage(Sheets::collection($collection)->get($path))]); |
|
|
|
|
43
|
|
|
|
44
|
1 |
|
continue; |
45
|
|
|
} |
46
|
|
|
|
47
|
1 |
|
$this->addPages($this->sheetsToPages(Sheets::collection($entry)->all()->all())); |
48
|
|
|
} |
49
|
|
|
|
50
|
2 |
|
return $this; |
51
|
|
|
} |
52
|
|
|
|
53
|
1 |
|
public function addSheetCollectionName(string $name): ExportFactoryContract |
54
|
|
|
{ |
55
|
1 |
|
$this->addSheetList([$name]); |
56
|
|
|
|
57
|
1 |
|
return $this; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public function addFeeds(array $except = []): ExportFactoryContract |
|
|
|
|
61
|
|
|
{ |
62
|
|
|
collect(config('feed.feeds'))->except($except)->each(function (array $config): void { |
63
|
|
|
$this->exporter->paths($config['url']); |
64
|
|
|
}); |
65
|
|
|
|
66
|
|
|
return $this; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @param PageContract[] $pages |
71
|
|
|
* |
72
|
|
|
* @return void |
|
|
|
|
73
|
|
|
*/ |
74
|
3 |
|
protected function addPages(array $pages): void |
75
|
|
|
{ |
76
|
|
|
$this->exporter->urls(array_map(function (PageContract $page): string { |
77
|
3 |
|
return $page->getUrl(); |
78
|
3 |
|
}, $pages)); |
79
|
2 |
|
} |
80
|
|
|
} |
81
|
|
|
|