Skip to content

Commit bde532a

Browse files
author
Cosmologist
committed
Twig bridge refactoring
1 parent 20ac867 commit bde532a

File tree

2 files changed

+49
-54
lines changed

2 files changed

+49
-54
lines changed

DependencyInjection/Configuration.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,20 @@ class Configuration implements ConfigurationInterface
1616
*/
1717
public function getConfigTreeBuilder()
1818
{
19+
$twigFunctionsNormalizer = function ($list) {
20+
$normalized = [];
21+
22+
foreach ($list as $item) {
23+
if (is_string($item)) {
24+
$normalized[$item] = $item;
25+
} else {
26+
$normalized += $item;
27+
}
28+
}
29+
30+
return $normalized;
31+
};
32+
1933
$treeBuilder = new TreeBuilder();
2034
$rootNode = $treeBuilder->root('symfony_common');
2135

@@ -50,10 +64,18 @@ public function getConfigTreeBuilder()
5064
->arrayNode('php_extension')
5165
->children()
5266
->arrayNode('filters')
67+
->beforeNormalization()
68+
->always($twigFunctionsNormalizer)
69+
->end()
70+
5371
->prototype('variable')
5472
->end()
5573
->end()
5674
->arrayNode('functions')
75+
->beforeNormalization()
76+
->always($twigFunctionsNormalizer)
77+
->end()
78+
5779
->prototype('variable')
5880
->end()
5981
->end()

Twig/PhpExtension.php

Lines changed: 27 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -2,96 +2,69 @@
22

33
namespace Cosmologist\Bundle\SymfonyCommonBundle\Twig;
44

5-
use Twig_Extension;
5+
use Cosmologist\Bundle\SymfonyCommonBundle\Type\CallableType;
6+
use Twig\Extension\AbstractExtension;
7+
use Twig\TwigFilter;
8+
use Twig\TwigFunction;
69

710
/**
8-
* Extension brings pre-configured list of functions and static class methods into Twig as filters and functions
11+
* The extension provides various callable objects (functions, method of static classes methods, methods of container services) to Twig templates.
912
*/
10-
class PhpExtension extends Twig_Extension
13+
class PhpExtension extends AbstractExtension
1114
{
1215
/**
1316
* @var array
1417
*/
15-
private $availableFunctions;
18+
private $functions;
1619

1720
/**
1821
* @var array
1922
*/
20-
private $availableFilters;
23+
private $filters;
2124

2225
/**
23-
* @param array $availableFunctions
24-
* @param array $availableFilters
26+
* @param array $functions
27+
* @param array $filters
2528
*/
26-
public function __construct(array $availableFunctions, array $availableFilters)
29+
public function __construct(array $functions, array $filters)
2730
{
28-
$this->availableFunctions = $availableFunctions;
29-
$this->availableFilters = $availableFilters;
31+
$this->functions = $functions;
32+
$this->filters = $filters;
3033
}
3134

3235
/**
3336
* {@inheritdoc}
3437
*/
3538
public function getFilters()
3639
{
37-
$callbacks = $this->getCallbacks($this->availableFilters);
38-
39-
return
40-
\array_map(
41-
function ($function, $callback) {
42-
return new \Twig_SimpleFilter($function, $callback);
43-
},
44-
\array_keys($callbacks), $callbacks
45-
);
40+
return $this->prepare($this->filters, TwigFilter::class);
4641
}
4742

4843
/**
4944
* {@inheritdoc}
5045
*/
5146
public function getFunctions()
5247
{
53-
$callbacks = $this->getCallbacks($this->availableFunctions);
54-
55-
return
56-
\array_map(
57-
function ($function, $callback) {
58-
return new \Twig_SimpleFunction($function, $callback);
59-
},
60-
\array_keys($callbacks), $callbacks
61-
);
48+
return $this->prepare($this->functions, TwigFunction::class);
6249
}
6350

6451
/**
65-
* Build callbacks for callables from configuration
52+
* Prepares callable objects defined in the application config for Twig
6653
*
67-
* @param string|array $callables
54+
* @param array $functions
55+
* @param string $twigCallableClass
6856
*
6957
* @return array
7058
*/
71-
private function getCallbacks($callables)
72-
{
73-
$result = array();
74-
75-
foreach ($callables as $function) {
76-
77-
if (is_array($function) && !is_numeric(key($function))) {
78-
$callback = current($function);
79-
$function = key($function);
80-
} else {
81-
$callback = $function;
82-
}
83-
84-
$result[$function] = $callback;
85-
}
86-
87-
return $result;
88-
}
89-
90-
/**
91-
* {@inheritdoc};
92-
*/
93-
public function getName()
59+
protected function prepare(array $functions, string $twigCallableClass): array
9460
{
95-
return 'symfony_common_php_extension';
61+
return
62+
array_map(
63+
function ($expression, $name) use ($twigCallableClass) {
64+
return new $twigCallableClass($name, CallableType::toCallable($expression));
65+
},
66+
$functions,
67+
array_keys($functions)
68+
);
9669
}
9770
}

0 commit comments

Comments
 (0)