1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Superdesk Web Publisher Templates System. |
5
|
|
|
* |
6
|
|
|
* Copyright 2015 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 2015 Sourcefabric z.ú |
12
|
|
|
* @license http://www.superdesk.org/license |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
namespace SWP\Component\TemplatesSystem\Twig\Extension; |
16
|
|
|
|
17
|
|
|
use SWP\Component\TemplatesSystem\Gimme\Context\Context; |
18
|
|
|
use SWP\Component\TemplatesSystem\Gimme\Loader\LoaderInterface; |
19
|
|
|
use SWP\Component\TemplatesSystem\Twig\TokenParser\GimmeListTokenParser; |
20
|
|
|
use SWP\Component\TemplatesSystem\Twig\TokenParser\GimmeTokenParser; |
21
|
|
|
use Twig\Extension\AbstractExtension; |
22
|
|
|
use Twig\Extension\GlobalsInterface; |
23
|
|
|
use Twig\TwigFilter; |
24
|
|
|
|
25
|
|
|
class GimmeExtension extends AbstractExtension implements GlobalsInterface |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* @var LoaderInterface |
29
|
|
|
*/ |
30
|
|
|
protected $loader; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var Context |
34
|
|
|
*/ |
35
|
|
|
protected $context; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* GimmeExtension constructor. |
39
|
|
|
* |
40
|
|
|
* @param Context $context |
41
|
|
|
* @param LoaderInterface $loader |
42
|
|
|
*/ |
43
|
|
|
public function __construct(Context $context, LoaderInterface $loader) |
44
|
|
|
{ |
45
|
|
|
$this->context = $context; |
46
|
|
|
$this->loader = $loader; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @return LoaderInterface |
51
|
|
|
*/ |
52
|
|
|
public function getLoader() |
53
|
|
|
{ |
54
|
|
|
return $this->loader; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @return Context |
59
|
|
|
*/ |
60
|
|
|
public function getContext() |
61
|
|
|
{ |
62
|
|
|
return $this->context; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @return array |
67
|
|
|
*/ |
68
|
|
|
public function getTokenParsers() |
69
|
|
|
{ |
70
|
|
|
return [ |
71
|
|
|
new GimmeTokenParser(), |
72
|
|
|
new GimmeListTokenParser(), |
73
|
|
|
]; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @return array |
78
|
|
|
*/ |
79
|
|
|
public function getFilters() |
80
|
|
|
{ |
81
|
|
|
return [ |
82
|
|
|
new TwigFilter('start', function ($node, $value) { |
83
|
|
|
$node['_collection_type_filters']['start'] = $value; |
84
|
|
|
|
85
|
|
|
return $node; |
86
|
|
|
}, ['needs_context' => false]), |
87
|
|
|
new TwigFilter('limit', function ($node, $value) { |
88
|
|
|
$node['_collection_type_filters']['limit'] = $value; |
89
|
|
|
|
90
|
|
|
return $node; |
91
|
|
|
}, ['needs_context' => false]), |
92
|
|
|
new TwigFilter('order', function ($node, $value1, $value2) { |
93
|
|
|
$node['_collection_type_filters']['order'][] = [$value1, $value2]; |
94
|
|
|
|
95
|
|
|
return $node; |
96
|
|
|
}, ['needs_context' => false]), |
97
|
|
|
new TwigFilter('dateRange', function ($node, $value1, $value2) { |
98
|
|
|
$node['_collection_type_filters']['date_range'] = [$value1, $value2]; |
99
|
|
|
|
100
|
|
|
return $node; |
101
|
|
|
}, ['needs_context' => false]), |
102
|
|
|
]; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @return array |
107
|
|
|
*/ |
108
|
|
|
public function getGlobals() |
109
|
|
|
{ |
110
|
|
|
return ['gimme' => $this->context]; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @return string |
115
|
|
|
*/ |
116
|
|
|
public function getName() |
117
|
|
|
{ |
118
|
|
|
return self::class; |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|