1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of the Sonata Project package. |
7
|
|
|
* |
8
|
|
|
* (c) Thomas Rabaix <[email protected]> |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
11
|
|
|
* file that was distributed with this source code. |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Sonata\NewsBundle\Twig\Extension; |
15
|
|
|
|
16
|
|
|
use Sonata\ClassificationBundle\Model\TagManagerInterface; |
17
|
|
|
use Sonata\Doctrine\Model\ManagerInterface; |
18
|
|
|
use Sonata\NewsBundle\Model\BlogInterface; |
19
|
|
|
use Sonata\NewsBundle\Model\PostInterface; |
20
|
|
|
use Symfony\Component\Routing\Generator\UrlGeneratorInterface; |
21
|
|
|
use Symfony\Component\Routing\RouterInterface; |
22
|
|
|
use Twig\Environment; |
23
|
|
|
use Twig\Extension\AbstractExtension; |
24
|
|
|
use Twig\Extension\InitRuntimeInterface; |
25
|
|
|
use Twig\TwigFunction; |
26
|
|
|
|
27
|
|
|
class NewsExtension extends AbstractExtension implements InitRuntimeInterface |
|
|
|
|
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* @var RouterInterface |
31
|
|
|
*/ |
32
|
|
|
private $router; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var TagManagerInterface |
36
|
|
|
*/ |
37
|
|
|
private $tagManager; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var Environment |
41
|
|
|
*/ |
42
|
|
|
private $environment; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var BlogInterface |
46
|
|
|
*/ |
47
|
|
|
private $blog; |
48
|
|
|
|
49
|
|
|
public function __construct(RouterInterface $router, ManagerInterface $tagManager, BlogInterface $blog) |
50
|
|
|
{ |
51
|
|
|
if (!$tagManager instanceof TagManagerInterface) { |
52
|
|
|
@trigger_error( |
|
|
|
|
53
|
|
|
'Calling the '.__METHOD__.' method with a Sonata\Doctrine\Model\ManagerInterface is deprecated' |
54
|
|
|
.' since version 2.4 and will be removed in 4.0.' |
55
|
|
|
.' Use the new signature with a Sonata\ClassificationBundle\Model\TagManagerInterface instead.', |
56
|
|
|
E_USER_DEPRECATED |
57
|
|
|
); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
$this->router = $router; |
61
|
|
|
$this->tagManager = $tagManager; |
|
|
|
|
62
|
|
|
$this->blog = $blog; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Returns a list of functions to add to the existing list. |
67
|
|
|
* |
68
|
|
|
* @return array An array of functions |
69
|
|
|
*/ |
70
|
|
|
public function getFunctions() |
71
|
|
|
{ |
72
|
|
|
return [ |
73
|
|
|
new TwigFunction( |
74
|
|
|
'sonata_news_link_tag_rss', |
75
|
|
|
[$this, 'renderTagRss', ['is_safe' => ['html']]] |
76
|
|
|
), |
77
|
|
|
new TwigFunction( |
78
|
|
|
'sonata_news_permalink', |
79
|
|
|
[$this, 'generatePermalink'] |
80
|
|
|
), |
81
|
|
|
]; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
public function initRuntime(Environment $environment): void |
85
|
|
|
{ |
86
|
|
|
$this->environment = $environment; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Returns the name of the extension. |
91
|
|
|
* |
92
|
|
|
* @return string The extension name |
93
|
|
|
*/ |
94
|
|
|
public function getName() |
95
|
|
|
{ |
96
|
|
|
return 'sonata_news'; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @return string |
101
|
|
|
*/ |
102
|
|
|
public function renderTagRss() |
103
|
|
|
{ |
104
|
|
|
$rss = []; |
105
|
|
|
foreach ($this->tagManager->findBy(['enabled' => true]) as $tag) { |
106
|
|
|
$rss[] = sprintf( |
107
|
|
|
'<link href="%s" title="%s : %s" type="application/rss+xml" rel="alternate" />', |
108
|
|
|
$this->router->generate('sonata_news_tag', ['tag' => $tag->getSlug(), '_format' => 'rss'], UrlGeneratorInterface::ABSOLUTE_URL), |
109
|
|
|
$this->blog->getTitle(), |
110
|
|
|
$tag->getName() |
111
|
|
|
); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
return implode("\n", $rss); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @return string |
119
|
|
|
*/ |
120
|
|
|
public function generatePermalink(PostInterface $post) |
121
|
|
|
{ |
122
|
|
|
return $this->blog->getPermalinkGenerator()->generate($post); |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|
This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.