|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the Superdesk Web Publisher Core Bundle. |
|
5
|
|
|
* |
|
6
|
|
|
* Copyright 2016 Sourcefabric z.ú. and contributors. |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please see the |
|
9
|
|
|
* AUTHORS and LICENSE files distributed with this source code. |
|
10
|
|
|
* |
|
11
|
|
|
* @copyright 2016 Sourcefabric z.ú |
|
12
|
|
|
* @license http://www.superdesk.org/license |
|
13
|
|
|
*/ |
|
14
|
|
|
|
|
15
|
|
|
namespace SWP\Bundle\CoreBundle\Enhancer; |
|
16
|
|
|
|
|
17
|
|
|
use SWP\Component\Common\Exception\ArticleNotFoundException; |
|
18
|
|
|
use SWP\Component\TemplatesSystem\Gimme\Context\Context; |
|
19
|
|
|
use SWP\Component\TemplatesSystem\Gimme\Meta\Meta; |
|
20
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\RedirectController; |
|
21
|
|
|
use Symfony\Cmf\Component\Routing\Enhancer\RouteEnhancerInterface; |
|
22
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
23
|
|
|
use SWP\Component\TemplatesSystem\Gimme\Loader\LoaderInterface; |
|
24
|
|
|
use SWP\Bundle\CoreBundle\Resolver\TemplateNameResolverInterface; |
|
25
|
|
|
use SWP\Bundle\ContentBundle\Model\ArticleInterface; |
|
26
|
|
|
use Symfony\Cmf\Component\Routing\RouteObjectInterface; |
|
27
|
|
|
use SWP\Bundle\ContentBundle\Model\RouteInterface; |
|
28
|
|
|
use SWP\Bundle\CoreBundle\Controller\ContentController; |
|
29
|
|
|
|
|
30
|
|
|
class RouteEnhancer implements RouteEnhancerInterface |
|
31
|
|
|
{ |
|
32
|
|
|
public const ARTICLE_META = '_article_meta'; |
|
33
|
|
|
|
|
34
|
|
|
public const ROUTE_META = '_route_meta'; |
|
35
|
|
|
|
|
36
|
|
|
protected $templateNameResolver; |
|
37
|
|
|
|
|
38
|
|
|
protected $metaLoader; |
|
39
|
|
|
|
|
40
|
|
|
protected $context; |
|
41
|
|
|
|
|
42
|
|
|
private $enhancedRoutesDefaults = []; |
|
43
|
|
|
|
|
44
|
|
|
public function __construct(TemplateNameResolverInterface $templateNameResolver, LoaderInterface $metaLoader, Context $context) |
|
45
|
|
|
{ |
|
46
|
|
|
$this->templateNameResolver = $templateNameResolver; |
|
47
|
|
|
$this->metaLoader = $metaLoader; |
|
48
|
|
|
$this->context = $context; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
public function enhance(array $defaults, Request $request): array |
|
52
|
|
|
{ |
|
53
|
|
|
$defaultsKey = md5(json_encode($defaults)); |
|
54
|
111 |
|
if (!isset($this->enhancedRoutesDefaults[$defaultsKey])) { |
|
55
|
|
|
$route = $defaults[RouteObjectInterface::ROUTE_OBJECT]; |
|
56
|
|
|
|
|
57
|
|
|
if (!isset($defaults['_controller']) || (isset($defaults['_controller']) && sprintf('%s::urlRedirectAction', RedirectController::class) !== $defaults['_controller'])) { |
|
58
|
|
|
$defaults['_controller'] = ContentController::class.'::renderPageAction'; |
|
59
|
111 |
|
} |
|
60
|
111 |
|
|
|
61
|
111 |
|
$defaults = $this->setRouteMeta($defaults); |
|
62
|
111 |
|
$request->attributes->set('routeMeta', $defaults[self::ROUTE_META]); |
|
63
|
|
|
|
|
64
|
|
|
$defaults = $this->setArticleMeta($this->getContentFromDefaults($defaults), $defaults); |
|
65
|
|
|
$request->attributes->set('articleMeta', $defaults[self::ARTICLE_META]); |
|
66
|
|
|
|
|
67
|
|
|
$defaults = $this->setTemplateName($this->getContentFromDefaults($defaults), $defaults); |
|
68
|
|
|
|
|
69
|
|
|
if (null !== ($article = $this->getContentFromDefaults($defaults)) && !isset($defaults['slug']) && RouteInterface::TYPE_CONTENT === $route->getType()) { |
|
70
|
|
|
$defaults['slug'] = $article->getSlug(); |
|
71
|
|
|
} |
|
72
|
14 |
|
$this->enhancedRoutesDefaults[$defaultsKey] = $defaults; |
|
73
|
|
|
} else { |
|
74
|
14 |
|
$defaults = $this->enhancedRoutesDefaults[$defaultsKey]; |
|
75
|
14 |
|
} |
|
76
|
12 |
|
|
|
77
|
10 |
|
return $defaults; |
|
78
|
|
|
} |
|
79
|
10 |
|
|
|
80
|
|
|
public function setArticleMeta($content, array $defaults): array |
|
81
|
|
|
{ |
|
82
|
|
|
$articleMeta = false; |
|
83
|
|
|
if (isset($defaults['slug'])) { |
|
84
|
|
|
$articleMeta = $this->metaLoader->load('article', ['slug' => $defaults['slug']], LoaderInterface::SINGLE); |
|
85
|
|
|
$defaults['type'] = RouteInterface::TYPE_COLLECTION; |
|
86
|
|
|
if (false === $articleMeta || ($articleMeta->getValues()->getRoute()->getId() !== $defaults[RouteObjectInterface::ROUTE_OBJECT]->getId())) { |
|
87
|
|
|
throw new ArticleNotFoundException(sprintf('Article for slug: %s was not found.', $defaults['slug'])); |
|
88
|
|
|
} |
|
89
|
|
|
} elseif ($content instanceof ArticleInterface) { |
|
90
|
|
|
$articleMeta = $this->metaLoader->load('article', ['article' => $content], LoaderInterface::SINGLE); |
|
91
|
|
|
$defaults['type'] = RouteInterface::TYPE_CONTENT; |
|
92
|
|
|
if (false === $articleMeta) { |
|
93
|
14 |
|
throw new ArticleNotFoundException(sprintf('Content with id: %s was not found.', $content->getId())); |
|
94
|
|
|
} |
|
95
|
14 |
|
} |
|
96
|
14 |
|
if ($articleMeta && $articleMeta->getValues() instanceof ArticleInterface) { |
|
97
|
7 |
|
$defaults[RouteObjectInterface::CONTENT_OBJECT] = $articleMeta->getValues(); |
|
98
|
7 |
|
} |
|
99
|
7 |
|
|
|
100
|
7 |
|
$defaults[self::ARTICLE_META] = $articleMeta; |
|
101
|
|
|
|
|
102
|
|
|
return $defaults; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
public function setTemplateName($content, array $defaults): array |
|
106
|
|
|
{ |
|
107
|
|
|
$route = $defaults[RouteObjectInterface::ROUTE_OBJECT] ?? null; |
|
108
|
|
|
if ($content && isset($defaults['slug'])) { |
|
109
|
12 |
|
$defaults[RouteObjectInterface::TEMPLATE_NAME] = $this->templateNameResolver->resolve($content); |
|
110
|
5 |
|
} elseif (null !== $route) { |
|
111
|
|
|
$defaults[RouteObjectInterface::TEMPLATE_NAME] = $this->templateNameResolver->resolve($route); |
|
112
|
|
|
} |
|
113
|
12 |
|
|
|
114
|
12 |
|
return $defaults; |
|
115
|
|
|
} |
|
116
|
12 |
|
|
|
117
|
|
|
public function setRouteMeta(array $defaults): array |
|
118
|
|
|
{ |
|
119
|
|
|
$routeMeta = $this->metaLoader->load('route', ['route_object' => $defaults[RouteObjectInterface::ROUTE_OBJECT]]); |
|
120
|
|
|
$defaults[self::ROUTE_META] = $routeMeta; |
|
121
|
|
|
|
|
122
|
|
|
if ($routeMeta instanceof Meta) { |
|
123
|
|
|
$this->context->setCurrentPage($routeMeta); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
return $defaults; |
|
127
|
12 |
|
} |
|
128
|
|
|
|
|
129
|
12 |
|
private function getContentFromDefaults(array $defaults) |
|
130
|
12 |
|
{ |
|
131
|
5 |
|
if (isset($defaults[RouteObjectInterface::CONTENT_OBJECT])) { |
|
132
|
7 |
|
return $defaults[RouteObjectInterface::CONTENT_OBJECT]; |
|
133
|
7 |
|
} |
|
134
|
|
|
} |
|
135
|
|
|
} |
|
136
|
|
|
|