1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Kunstmaan\ConfigBundle\Twig; |
4
|
|
|
|
5
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
6
|
|
|
use Kunstmaan\ConfigBundle\Entity\AbstractConfig; |
7
|
|
|
use Symfony\Component\Routing\Generator\UrlGeneratorInterface; |
8
|
|
|
use Twig_Extension; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Extension to fetch config |
12
|
|
|
*/ |
13
|
|
|
class ConfigTwigExtension extends Twig_Extension |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @var EntityManagerInterface $em |
17
|
|
|
*/ |
18
|
|
|
private $em; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var array $configuration |
22
|
|
|
*/ |
23
|
|
|
private $configuration; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var array |
27
|
|
|
*/ |
28
|
|
|
private $configs = array(); |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @param \Doctrine\ORM\EntityManagerInterface $em |
32
|
|
|
*/ |
33
|
|
|
public function __construct(EntityManagerInterface $em, $configuration) |
34
|
|
|
{ |
35
|
|
|
$this->em = $em; |
36
|
|
|
$this->configuration = $configuration; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Returns a list of functions to add to the existing list. |
41
|
|
|
* |
42
|
|
|
* @return array An array of functions |
|
|
|
|
43
|
|
|
*/ |
44
|
|
|
public function getFunctions() |
45
|
|
|
{ |
46
|
|
|
return array( |
47
|
|
|
new \Twig_SimpleFunction( |
48
|
|
|
'get_config_by_internal_name', array($this, 'getConfigByInternalName') |
49
|
|
|
), |
50
|
|
|
); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @param string $internalName Internal name of the site config entity |
55
|
|
|
* |
56
|
|
|
* @return AbstractConfig |
57
|
|
|
*/ |
58
|
|
|
public function getConfigByInternalName($internalName) |
59
|
|
|
{ |
60
|
|
|
if (in_array($internalName, $this->configs)) { |
61
|
|
|
return $this->configs[$internalName]; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
foreach ($this->configuration['entities'] as $class) { |
65
|
|
|
$entity = new $class; |
66
|
|
|
|
67
|
|
|
if ($entity->getInternalName() == $internalName) { |
68
|
|
|
$repo = $this->em->getRepository($class); |
69
|
|
|
$config = $repo->findOneBy(array()); |
70
|
|
|
|
71
|
|
|
$this->configs[$internalName] = $config; |
72
|
|
|
|
73
|
|
|
return $config; |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
return null; |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|
This check looks for the generic type
array
as a return type and suggests a more specific type. This type is inferred from the actual code.