1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Superdesk Web Publisher Core Bundle. |
5
|
|
|
* |
6
|
|
|
* Copyright 2016 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 2016 Sourcefabric z.ú |
12
|
|
|
* @license http://www.superdesk.org/license |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
namespace SWP\Bundle\CoreBundle\Twig; |
16
|
|
|
|
17
|
|
|
class StringyExtension extends \Twig_Extension |
18
|
|
|
{ |
19
|
|
|
const EXCLUDE_FUNCTIONS = ['__construct', '__toString', 'create']; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var \Twig_Environment |
23
|
|
|
*/ |
24
|
|
|
protected $environment; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var array |
28
|
|
|
*/ |
29
|
|
|
protected $functions = []; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var array |
33
|
|
|
*/ |
34
|
|
|
protected $filters = []; |
35
|
|
|
|
36
|
99 |
|
public function __construct(\Twig_Environment $environment) |
37
|
|
|
{ |
38
|
99 |
|
$this->environment = $environment; |
39
|
99 |
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* {@inheritdoc} |
43
|
|
|
*/ |
44
|
12 |
|
public function getFilters() |
45
|
|
|
{ |
46
|
12 |
|
$this->lazyInit(); |
47
|
|
|
|
48
|
12 |
|
return $this->filters; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* {@inheritdoc} |
53
|
|
|
*/ |
54
|
12 |
|
public function getFunctions() |
55
|
|
|
{ |
56
|
12 |
|
$this->lazyInit(); |
57
|
|
|
|
58
|
12 |
|
return $this->functions; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Initializes arrays of filters and functions. |
63
|
|
|
*/ |
64
|
12 |
|
private function lazyInit() |
65
|
|
|
{ |
66
|
12 |
|
$stringyClass = new \ReflectionClass('Stringy\Stringy'); |
67
|
12 |
|
$methods = $stringyClass->getMethods(\ReflectionMethod::IS_PUBLIC); |
68
|
|
|
$names = array_map(function ($value) { |
69
|
12 |
|
return $value->getName(); |
70
|
12 |
|
}, $methods); |
71
|
|
|
|
72
|
12 |
|
foreach ($names as $name) { |
73
|
12 |
|
if (in_array($name, self::EXCLUDE_FUNCTIONS)) { |
74
|
12 |
|
continue; |
75
|
|
|
} |
76
|
|
|
|
77
|
12 |
|
$method = $stringyClass->getMethod($name); |
78
|
|
|
|
79
|
|
|
// Get the return type from the doc comment |
80
|
12 |
|
$doc = $method->getDocComment(); |
81
|
12 |
|
if (strpos($doc, '@return bool')) { |
82
|
|
|
// Don't add functions which have the same name as any already in the environment |
83
|
12 |
|
if ($this->environment->getFunction($name)) { |
84
|
|
|
continue; |
85
|
|
|
} |
86
|
|
|
$this->functions[$name] = new \Twig_SimpleFunction($name, function () use ($name) { |
87
|
2 |
|
return call_user_func_array(['Stringy\StaticStringy', $name], func_get_args()); |
88
|
12 |
|
}); |
89
|
|
|
} else { |
90
|
|
|
// Don't add filters which have the same name as any already in the environment |
91
|
12 |
|
if ($this->environment->getFilter($name)) { |
92
|
12 |
|
continue; |
93
|
|
|
} |
94
|
12 |
|
$this->filters[$name] = new \Twig_SimpleFilter($name, function () use ($name) { |
95
|
3 |
|
return call_user_func_array(['Stringy\StaticStringy', $name], func_get_args()); |
96
|
12 |
|
}); |
97
|
|
|
} |
98
|
|
|
} |
99
|
12 |
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* {@inheritdoc} |
103
|
|
|
*/ |
104
|
99 |
|
public function getName() |
105
|
|
|
{ |
106
|
99 |
|
return 'stringy_extension'; |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|