|
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
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Returns a list of functions to add to the existing list. |
|
42
|
|
|
* |
|
43
|
|
|
* @return \Twig_Function[] |
|
44
|
|
|
*/ |
|
45
|
|
|
public function getFunctions() |
|
46
|
|
|
{ |
|
47
|
|
|
return [ |
|
48
|
|
|
new \Twig_Function('cmsBlocksById', [$this, 'getCmsBlocksById']), |
|
49
|
|
|
new \Twig_Function('cmsPagesByTag', [$this, 'getCmsPagesByTag']), |
|
50
|
|
|
new \Twig_Function('cmsBlocksByTag', [$this, 'getCmsBlocksByTag']), |
|
51
|
|
|
new \Twig_Function('cmsPageByUrl', [$this, 'getCmsPageByUrl']), |
|
52
|
|
|
new \Twig_Function('url', [$this, 'getUrl']), |
|
53
|
|
|
]; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @return Block[] |
|
58
|
|
|
*/ |
|
59
|
|
|
public function getCmsBlocksById(string $blockId): array |
|
60
|
|
|
{ |
|
61
|
|
|
$blocks = $this->blockRegistry->getBlocks($blockId); |
|
62
|
|
|
|
|
63
|
|
|
return $blocks; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @param string $tag |
|
68
|
|
|
* @param null|string $domain |
|
69
|
|
|
* @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. |
|
70
|
|
|
* @param string $direction |
|
71
|
|
|
* @param int|null $limit |
|
72
|
|
|
* @param int|null $offset |
|
73
|
|
|
* @return Page[] |
|
74
|
|
|
* @throws CMSException |
|
75
|
|
|
*/ |
|
76
|
|
|
public function getCmsPagesByTag(string $tag, ?string $domain = null, ?string $orderBy = null, string $direction = 'desc', int $limit = null, int $offset = null): array |
|
77
|
|
|
{ |
|
78
|
|
|
if (!\in_array($direction, ['asc', 'desc'], true)) { |
|
79
|
|
|
throw new CMSException("Error while using getCmsPagesByTag. The fourth parameter (direction) must be either 'asc' or 'desc'."); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
$pages = $this->pageRegistry->findPagesByTag($tag, $domain); |
|
83
|
|
|
|
|
84
|
|
|
if ($orderBy !== null) { |
|
85
|
|
|
usort($pages, function(Page $page1, Page $page2) use ($orderBy, $direction) { |
|
86
|
|
|
if ($direction === 'asc') { |
|
87
|
|
|
return ($page1->getContext()[$orderBy] ?? null) <=> ($page2->getContext()[$orderBy] ?? null); |
|
88
|
|
|
} else { |
|
89
|
|
|
return ($page2->getContext()[$orderBy] ?? null) <=> ($page1->getContext()[$orderBy] ?? null); |
|
90
|
|
|
} |
|
91
|
|
|
}); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
if ($limit !== null || $offset !== null) { |
|
95
|
|
|
$pages = \array_slice($pages, $offset, $limit); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
return $pages; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* @param string $tag |
|
103
|
|
|
* @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. |
|
104
|
|
|
* @param string $direction |
|
105
|
|
|
* @param int|null $limit |
|
106
|
|
|
* @param int|null $offset |
|
107
|
|
|
* @return Block[] |
|
108
|
|
|
* @throws CMSException |
|
109
|
|
|
*/ |
|
110
|
|
|
public function getCmsBlocksByTag(string $tag, ?string $orderBy = null, string $direction = 'desc', int $limit = null, int $offset = null): array |
|
111
|
|
|
{ |
|
112
|
|
|
if (!\in_array($direction, ['asc', 'desc'], true)) { |
|
113
|
|
|
throw new CMSException("Error while using getCmsBlocksByTag. The third parameter (direction) must be either 'asc' or 'desc'."); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
$blocks = $this->blockRegistry->findBlocksByTag($tag); |
|
117
|
|
|
|
|
118
|
|
|
if ($orderBy !== null) { |
|
119
|
|
|
usort($blocks, function(Block $block1, Block $block2) use ($orderBy, $direction) { |
|
120
|
|
|
if ($direction === 'asc') { |
|
121
|
|
|
return ($block1->getContext()[$orderBy] ?? null) <=> ($block2->getContext()[$orderBy] ?? null); |
|
122
|
|
|
} else { |
|
123
|
|
|
return ($block2->getContext()[$orderBy] ?? null) <=> ($block1->getContext()[$orderBy] ?? null); |
|
124
|
|
|
} |
|
125
|
|
|
}); |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
if ($limit !== null || $offset !== null) { |
|
129
|
|
|
$blocks = \array_slice($blocks, $offset, $limit); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
return $blocks; |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* @return Page |
|
137
|
|
|
*/ |
|
138
|
|
|
public function getCmsPageByUrl(string $url, string $domain = null): Page |
|
139
|
|
|
{ |
|
140
|
|
|
return $this->pageRegistry->getPage($url, $domain ?: '<any>'); |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
/** |
|
144
|
|
|
* Prepends the URL with the "base" url. |
|
145
|
|
|
* |
|
146
|
|
|
* @param string|null $url |
|
147
|
|
|
* @return string|null |
|
148
|
|
|
*/ |
|
149
|
|
|
public function getUrl(string $url = null): ?string |
|
150
|
|
|
{ |
|
151
|
|
|
if ($url === null) { |
|
152
|
|
|
return null; |
|
153
|
|
|
} |
|
154
|
|
|
$isAbsolute = parse_url($url, PHP_URL_SCHEME) !== null; |
|
155
|
|
|
if ($isAbsolute) { |
|
156
|
|
|
return $url; |
|
157
|
|
|
} else { |
|
158
|
|
|
return $this->rootUrl.ltrim($url, '/'); |
|
159
|
|
|
} |
|
160
|
|
|
} |
|
161
|
|
|
} |
|
162
|
|
|
|
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