|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace PiedWeb\CMSBundle\Service; |
|
4
|
|
|
|
|
5
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
|
6
|
|
|
use Symfony\Component\Filesystem\Filesystem; |
|
7
|
|
|
use Doctrine\ORM\EntityManager; |
|
8
|
|
|
use Twig_Environment; |
|
9
|
|
|
use PiedWeb\CMSBundle\Service\PageCanonicalService; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Inspired by https://github.com/eko/FeedBundle. |
|
13
|
|
|
*/ |
|
14
|
|
|
class FeedDumpService |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* @var EntityManagerInterface |
|
18
|
|
|
*/ |
|
19
|
|
|
private $em; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @var \PiedWeb\CMSBundle\Service\PageCanonicalService |
|
23
|
|
|
*/ |
|
24
|
|
|
private $pc; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @var \Symfony\Component\Filesystem\Filesystem |
|
28
|
|
|
*/ |
|
29
|
|
|
private $filesystem; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @var \Twig_Environment |
|
33
|
|
|
*/ |
|
34
|
|
|
private $twig; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @var string |
|
38
|
|
|
*/ |
|
39
|
|
|
private $webDir; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @var string |
|
43
|
|
|
*/ |
|
44
|
|
|
private $page_class; |
|
45
|
|
|
|
|
46
|
|
|
public function __construct( |
|
47
|
|
|
EntityManager $em, |
|
48
|
|
|
Twig_Environment $twig, |
|
49
|
|
|
PageCanonicalService $pc, |
|
50
|
|
|
string $webDir, |
|
51
|
|
|
string $page_class |
|
52
|
|
|
) { |
|
53
|
|
|
$this->em = $em; |
|
54
|
|
|
$this->pc = $pc; |
|
55
|
|
|
$this->filesystem = new Filesystem(); |
|
56
|
|
|
$this->twig = $twig; |
|
57
|
|
|
$this->webDir = $webDir; |
|
58
|
|
|
$this->page_class = $page_class; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @throws \RuntimeException |
|
63
|
|
|
* @throws \LogicException |
|
64
|
|
|
*/ |
|
65
|
|
|
public function dump() |
|
66
|
|
|
{ |
|
67
|
|
|
$this->dumpFeed(); |
|
68
|
|
|
$this->dumpSitemap(); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
protected function dumpFeed() |
|
72
|
|
|
{ |
|
73
|
|
|
$dump = $this->renderFeed(); |
|
74
|
|
|
$filepath = $this->webDir.'/feed.xml'; |
|
75
|
|
|
|
|
76
|
|
|
$this->filesystem->dumpFile($filepath, $dump); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
protected function dumpSitemap() |
|
80
|
|
|
{ |
|
81
|
|
|
$pages = $this->getPages(); |
|
82
|
|
|
$this->filesystem->dumpFile($this->webDir.'/sitemap.txt', $this->renderSitemapTxt($pages)); |
|
83
|
|
|
$this->filesystem->dumpFile($this->webDir.'/sitemap.xml', $this->renderSitemapXml($pages)); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
protected function getPages(?int $limit = null) |
|
87
|
|
|
{ |
|
88
|
|
|
$qb = $this->em->getRepository($this->page_class)->getQueryToFindPublished('p'); |
|
89
|
|
|
$qb->andWhere('p.metaRobots IS NULL OR p.metaRobots NOT LIKE :noi')->setParameter('noi', '%no-index%'); |
|
90
|
|
|
$qb->andWhere('p.mainContent NOT LIKE :noi')->setParameter('noi', 'Location:%'); |
|
91
|
|
|
|
|
92
|
|
|
if (null !== $limit) { |
|
93
|
|
|
$qb->setMaxResults($limit); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
$pages = $qb->getQuery()->getResult(); |
|
97
|
|
|
$links = $this->getLinks($pages); |
|
|
|
|
|
|
98
|
|
|
|
|
99
|
|
|
return $pages; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
protected function getLinks($pages) |
|
103
|
|
|
{ |
|
104
|
|
|
$links = []; |
|
105
|
|
|
foreach ($pages as $key => $page) { |
|
106
|
|
|
$links[$key] = 'homepage' == $page->getSlug() ? |
|
107
|
|
|
$this->pc->generatePathForHomepage() : |
|
108
|
|
|
$this->pc->generatePathForPage($page->getRealSlug()) |
|
109
|
|
|
; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
return $links; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
protected function renderFeed() |
|
116
|
|
|
{ |
|
117
|
|
|
return $this->twig->render('@PiedWebCMS/page/rss.xml.twig', ['pages' => $this->getPages(5)]); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
protected function renderSitemapTxt($pages) |
|
121
|
|
|
{ |
|
122
|
|
|
return $this->twig->render('@PiedWebCMS/page/sitemap.txt.twig', ['pages' => $pages]); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
protected function renderSitemapXml($pages) |
|
126
|
|
|
{ |
|
127
|
|
|
return $this->twig->render('@PiedWebCMS/page/sitemap.xml.twig', ['pages' => $pages]); |
|
128
|
|
|
} |
|
129
|
|
|
} |
|
130
|
|
|
|