1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of the Superdesk Web Publisher Core Bundle. |
5
|
|
|
* |
6
|
|
|
* Copyright 2016 Sourcefabric z.u. 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
|
|
|
* Some parts of that file were taken from the Liip/ThemeBundle |
12
|
|
|
* (c) Liip AG |
13
|
|
|
* |
14
|
|
|
* @copyright 2016 Sourcefabric z.ú |
15
|
|
|
* @license http://www.superdesk.org/license |
16
|
|
|
*/ |
17
|
|
|
|
18
|
|
|
namespace SWP\Bundle\CoreBundle\Resolver; |
19
|
|
|
|
20
|
|
|
use SWP\Bundle\ContentBundle\Doctrine\ODM\PHPCR\RouteObjectInterface; |
21
|
|
|
use SWP\Bundle\ContentBundle\Model\ArticleInterface; |
22
|
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Class TemplateNameResolver. |
26
|
|
|
*/ |
27
|
|
|
class TemplateNameResolver implements TemplateNameResolverInterface |
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* {@inheritdoc} |
31
|
|
|
*/ |
32
|
14 |
|
public function resolve($object, $defaultFileName = TemplateNameResolverInterface::TEMPLATE_NAME) |
33
|
|
|
{ |
34
|
14 |
|
if ($object instanceof ArticleInterface) { |
35
|
8 |
|
return $this->resolveFromArticle($object); |
36
|
|
|
} elseif ($object instanceof RouteObjectInterface) { |
37
|
6 |
|
return $this->resolveFromRoute($object); |
|
|
|
|
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
return $defaultFileName; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* {@inheritdoc} |
45
|
|
|
*/ |
46
|
8 |
|
public function resolveFromArticle(ArticleInterface $article, $templateName = TemplateNameResolverInterface::TEMPLATE_NAME) |
47
|
|
|
{ |
48
|
|
|
/** @param $route RouteObjectInterface */ |
49
|
8 |
|
if (null !== ($route = $article->getRoute())) { |
50
|
8 |
|
if (null !== $route->getTemplateName()) { |
51
|
1 |
|
$templateName = $route->getTemplateName(); |
52
|
|
|
} |
53
|
|
|
|
54
|
8 |
|
if (RouteObjectInterface::TYPE_COLLECTION === $route->getType() && null !== $route->getArticlesTemplateName()) { |
55
|
1 |
|
$templateName = $route->getArticlesTemplateName(); |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
|
59
|
8 |
|
if (null !== $article->getTemplateName()) { |
60
|
|
|
$templateName = $article->getTemplateName(); |
61
|
|
|
} |
62
|
|
|
|
63
|
8 |
|
return $templateName; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* {@inheritdoc} |
68
|
|
|
*/ |
69
|
6 |
|
public function resolveFromRoute(RouteObjectInterface $route, $templateName = TemplateNameResolverInterface::TEMPLATE_NAME) |
70
|
|
|
{ |
71
|
6 |
|
if (null !== $route->getTemplateName()) { |
72
|
2 |
|
$templateName = $route->getTemplateName(); |
73
|
|
|
} |
74
|
|
|
|
75
|
6 |
|
if (RouteObjectInterface::TYPE_COLLECTION === $route->getType() && null === $route->getTemplateName()) { |
76
|
2 |
|
if ($contentTemplateName = $this->getTemplateNameFromRouteContent($route)) { |
|
|
|
|
77
|
|
|
$templateName = $contentTemplateName; |
78
|
|
|
} else { |
79
|
2 |
|
throw new NotFoundHttpException(sprintf('There is no template file defined for "%s" route!', $route->getId())); |
80
|
|
|
} |
81
|
4 |
|
} elseif (RouteObjectInterface::TYPE_CONTENT === $route->getType()) { |
82
|
3 |
|
if ($contentTemplateName = $this->getTemplateNameFromRouteContent($route)) { |
|
|
|
|
83
|
|
|
$templateName = $contentTemplateName; |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
87
|
4 |
|
return $templateName; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @param RouteObjectInterface $route |
92
|
|
|
* |
93
|
|
|
* @return bool |
94
|
|
|
*/ |
95
|
5 |
|
private function getTemplateNameFromRouteContent(RouteObjectInterface $route) |
96
|
|
|
{ |
97
|
5 |
|
if (null !== $route->getContent()) { |
98
|
|
|
if (null !== $templateName = $route->getContent()->getTemplateName()) { |
99
|
|
|
return $templateName; |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|
103
|
5 |
|
return false; |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|