1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace TheCodingMachine\CMS\StaticRegistry\Twig; |
5
|
|
|
|
6
|
|
|
use Barclays\Dao\BaseBlockDao; |
|
|
|
|
7
|
|
|
use Barclays\Dao\StaticBlockDao; |
|
|
|
|
8
|
|
|
use Barclays\Model\BaseBlock; |
|
|
|
|
9
|
|
|
use Barclays\Model\PageVersion; |
|
|
|
|
10
|
|
|
use Barclays\Services\SerializationContext; |
|
|
|
|
11
|
|
|
use TheCodingMachine\CMS\CMSException; |
12
|
|
|
use TheCodingMachine\CMS\StaticRegistry\Loaders\Block; |
13
|
|
|
use TheCodingMachine\CMS\StaticRegistry\Loaders\Page; |
14
|
|
|
use TheCodingMachine\CMS\StaticRegistry\Registry\BlockRegistry; |
15
|
|
|
use TheCodingMachine\CMS\StaticRegistry\Registry\PageRegistry; |
16
|
|
|
use TheCodingMachine\TDBM\UncheckedOrderBy; |
|
|
|
|
17
|
|
|
|
18
|
|
|
class CmsPageExtension extends \Twig_Extension |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @var PageRegistry |
22
|
|
|
*/ |
23
|
|
|
private $pageRegistry; |
24
|
|
|
/** |
25
|
|
|
* @var BlockRegistry |
26
|
|
|
*/ |
27
|
|
|
private $blockRegistry; |
28
|
|
|
/** |
29
|
|
|
* @var string |
30
|
|
|
*/ |
31
|
|
|
private $rootUrl; |
32
|
|
|
|
33
|
|
|
public function __construct(PageRegistry $pageRegistry, BlockRegistry $blockRegistry, string $rootUrl) |
34
|
|
|
{ |
35
|
|
|
$this->pageRegistry = $pageRegistry; |
36
|
|
|
$this->blockRegistry = $blockRegistry; |
37
|
|
|
$this->rootUrl = '/'.trim($rootUrl, '/').'/'; |
38
|
|
|
if ($this->rootUrl === '//') { |
39
|
|
|
$this->rootUrl = '/'; |
40
|
|
|
} |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Returns a list of functions to add to the existing list. |
45
|
|
|
* |
46
|
|
|
* @return \Twig_Function[] |
47
|
|
|
*/ |
48
|
|
|
public function getFunctions() |
49
|
|
|
{ |
50
|
|
|
return [ |
51
|
|
|
new \Twig_Function('cmsBlocksById', [$this, 'getCmsBlocksById']), |
52
|
|
|
new \Twig_Function('cmsPagesByTag', [$this, 'getCmsPagesByTag']), |
53
|
|
|
new \Twig_Function('cmsBlocksByTag', [$this, 'getCmsBlocksByTag']), |
54
|
|
|
new \Twig_Function('cmsPageByUrl', [$this, 'getCmsPageByUrl']), |
55
|
|
|
new \Twig_Function('url', [$this, 'getUrl']), |
56
|
|
|
]; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @return Block[] |
61
|
|
|
*/ |
62
|
|
|
public function getCmsBlocksById(string $blockId): array |
63
|
|
|
{ |
64
|
|
|
$blocks = $this->blockRegistry->getBlocks($blockId); |
65
|
|
|
|
66
|
|
|
return $blocks; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @param string $tag |
71
|
|
|
* @param null|string $domain |
72
|
|
|
* @param string $orderBy One of the properties of the context. For instance, if you set this to 'date', it means you should add a "date" key to your context it you want to sort upon it. |
73
|
|
|
* @param string $direction |
74
|
|
|
* @param int|null $limit |
75
|
|
|
* @param int|null $offset |
76
|
|
|
* @return Page[] |
77
|
|
|
* @throws CMSException |
78
|
|
|
*/ |
79
|
|
|
public function getCmsPagesByTag(string $tag, ?string $domain = null, ?string $orderBy = null, string $direction = 'desc', int $limit = null, int $offset = null): array |
80
|
|
|
{ |
81
|
|
|
if (!\in_array($direction, ['asc', 'desc'], true)) { |
82
|
|
|
throw new CMSException("Error while using getCmsPagesByTag. The fourth parameter (direction) must be either 'asc' or 'desc'."); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
$pages = $this->pageRegistry->findPagesByTag($tag, $domain); |
86
|
|
|
|
87
|
|
|
if ($orderBy !== null) { |
88
|
|
|
usort($pages, function(Page $page1, Page $page2) use ($orderBy, $direction) { |
89
|
|
|
if ($direction === 'asc') { |
90
|
|
|
return ($page1->getContext()[$orderBy] ?? null) <=> ($page2->getContext()[$orderBy] ?? null); |
91
|
|
|
} else { |
92
|
|
|
return ($page2->getContext()[$orderBy] ?? null) <=> ($page1->getContext()[$orderBy] ?? null); |
93
|
|
|
} |
94
|
|
|
}); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
if ($limit !== null || $offset !== null) { |
98
|
|
|
$pages = \array_slice($pages, $offset, $limit); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
return $pages; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @param string $tag |
106
|
|
|
* @param string $orderBy One of the properties of the context. For instance, if you set this to 'date', it means you should add a "date" key to your context it you want to sort upon it. |
107
|
|
|
* @param string $direction |
108
|
|
|
* @param int|null $limit |
109
|
|
|
* @param int|null $offset |
110
|
|
|
* @return Block[] |
111
|
|
|
* @throws CMSException |
112
|
|
|
*/ |
113
|
|
|
public function getCmsBlocksByTag(string $tag, ?string $orderBy = null, string $direction = 'desc', int $limit = null, int $offset = null): array |
114
|
|
|
{ |
115
|
|
|
if (!\in_array($direction, ['asc', 'desc'], true)) { |
116
|
|
|
throw new CMSException("Error while using getCmsBlocksByTag. The third parameter (direction) must be either 'asc' or 'desc'."); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
$blocks = $this->blockRegistry->findBlocksByTag($tag); |
120
|
|
|
|
121
|
|
|
if ($orderBy !== null) { |
122
|
|
|
usort($blocks, function(Block $block1, Block $block2) use ($orderBy, $direction) { |
123
|
|
|
if ($direction === 'asc') { |
124
|
|
|
return ($block1->getContext()[$orderBy] ?? null) <=> ($block2->getContext()[$orderBy] ?? null); |
125
|
|
|
} else { |
126
|
|
|
return ($block2->getContext()[$orderBy] ?? null) <=> ($block1->getContext()[$orderBy] ?? null); |
127
|
|
|
} |
128
|
|
|
}); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
if ($limit !== null || $offset !== null) { |
132
|
|
|
$blocks = \array_slice($blocks, $offset, $limit); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
return $blocks; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* @return Page |
140
|
|
|
*/ |
141
|
|
|
public function getCmsPageByUrl(string $url, string $domain = null): Page |
142
|
|
|
{ |
143
|
|
|
return $this->pageRegistry->getPage($url, $domain ?: '<any>'); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Prepends the URL with the "base" url. |
148
|
|
|
* |
149
|
|
|
* @param string|null $url |
150
|
|
|
* @return string|null |
151
|
|
|
*/ |
152
|
|
|
public function getUrl(string $url = null): ?string |
153
|
|
|
{ |
154
|
|
|
if ($url === null) { |
155
|
|
|
return null; |
156
|
|
|
} |
157
|
|
|
$isAbsolute = parse_url($url, PHP_URL_SCHEME) !== null; |
158
|
|
|
if ($isAbsolute) { |
159
|
|
|
return $url; |
160
|
|
|
} else { |
161
|
|
|
return $this->rootUrl.ltrim($url, '/'); |
162
|
|
|
} |
163
|
|
|
} |
164
|
|
|
} |
165
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths