|
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
|
|
|
|
|
22
|
|
|
class GimmeExtension extends \Twig_Extension implements \Twig_Extension_GlobalsInterface |
|
23
|
|
|
{ |
|
24
|
|
|
/** |
|
25
|
|
|
* @var LoaderInterface |
|
26
|
|
|
*/ |
|
27
|
|
|
protected $loader; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @var Context |
|
31
|
|
|
*/ |
|
32
|
|
|
protected $context; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* GimmeExtension constructor. |
|
36
|
|
|
* |
|
37
|
|
|
* @param Context $context |
|
38
|
|
|
* @param LoaderInterface $loader |
|
39
|
|
|
*/ |
|
40
|
110 |
|
public function __construct(Context $context, LoaderInterface $loader) |
|
41
|
|
|
{ |
|
42
|
110 |
|
$this->context = $context; |
|
43
|
110 |
|
$this->loader = $loader; |
|
44
|
110 |
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @return LoaderInterface |
|
48
|
|
|
*/ |
|
49
|
9 |
|
public function getLoader() |
|
50
|
|
|
{ |
|
51
|
9 |
|
return $this->loader; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @return Context |
|
56
|
|
|
*/ |
|
57
|
1 |
|
public function getContext() |
|
58
|
|
|
{ |
|
59
|
1 |
|
return $this->context; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @return array |
|
64
|
|
|
*/ |
|
65
|
19 |
|
public function getTokenParsers() |
|
66
|
|
|
{ |
|
67
|
|
|
return [ |
|
68
|
19 |
|
new GimmeTokenParser(), |
|
69
|
19 |
|
new GimmeListTokenParser(), |
|
70
|
|
|
]; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* @return array |
|
75
|
|
|
*/ |
|
76
|
19 |
|
public function getFilters() |
|
77
|
|
|
{ |
|
78
|
|
|
return [ |
|
79
|
|
|
new \Twig_SimpleFilter('start', function ($node, $value) { |
|
80
|
|
|
$node['_collection_type_filters']['start'] = $value; |
|
81
|
|
|
|
|
82
|
|
|
return $node; |
|
83
|
19 |
|
}, ['needs_context' => false]), |
|
84
|
|
|
new \Twig_SimpleFilter('limit', function ($node, $value) { |
|
85
|
|
|
$node['_collection_type_filters']['limit'] = $value; |
|
86
|
|
|
|
|
87
|
|
|
return $node; |
|
88
|
19 |
|
}, ['needs_context' => false]), |
|
89
|
19 |
|
new \Twig_SimpleFilter('order', function ($node, $value1, $value2) { |
|
90
|
|
|
$node['_collection_type_filters']['order'] = [$value1, $value2]; |
|
91
|
|
|
|
|
92
|
|
|
return $node; |
|
93
|
19 |
|
}, ['needs_context' => false]), |
|
94
|
|
|
new \Twig_SimpleFilter('dateRange', function ($node, $value1, $value2) { |
|
95
|
|
|
$node['_collection_type_filters']['date_range'] = [$value1, $value2]; |
|
96
|
|
|
|
|
97
|
|
|
return $node; |
|
98
|
|
|
}, ['needs_context' => false]), |
|
99
|
|
|
]; |
|
100
|
26 |
|
} |
|
101
|
|
|
|
|
102
|
26 |
|
/** |
|
103
|
|
|
* @return array |
|
104
|
|
|
*/ |
|
105
|
|
|
public function getGlobals() |
|
106
|
|
|
{ |
|
107
|
|
|
return ['gimme' => $this->context]; |
|
108
|
109 |
|
} |
|
109
|
|
|
|
|
110
|
109 |
|
/** |
|
111
|
|
|
* @return string |
|
112
|
|
|
*/ |
|
113
|
|
|
public function getName() |
|
114
|
|
|
{ |
|
115
|
|
|
return self::class; |
|
116
|
|
|
} |
|
117
|
|
|
} |
|
118
|
|
|
|