1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Alchemy\PhraseanetBundle\DependencyInjection\Configuration; |
4
|
|
|
|
5
|
|
|
use Symfony\Component\Config\Definition\Builder\TreeBuilder; |
6
|
|
|
use Symfony\Component\Config\Definition\ConfigurationInterface; |
7
|
|
|
|
8
|
|
|
class CacheNodeBuilder implements ConfigurationInterface |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* Generates the configuration tree builder. |
12
|
|
|
* |
13
|
|
|
* @return \Symfony\Component\Config\Definition\Builder\TreeBuilder The tree builder |
14
|
|
|
*/ |
15
|
6 |
|
public function getConfigTreeBuilder() |
16
|
|
|
{ |
17
|
6 |
|
$treeBuilder = new TreeBuilder(); |
18
|
|
|
|
19
|
6 |
|
$this->getNode($treeBuilder); |
20
|
|
|
|
21
|
6 |
|
return $treeBuilder; |
22
|
|
|
} |
23
|
|
|
|
24
|
7 |
|
public function getNode(TreeBuilder $builder = null) |
25
|
|
|
{ |
26
|
7 |
|
$builder = $builder ?: new TreeBuilder(); |
27
|
7 |
|
$node = $builder->root('cache'); |
28
|
|
|
|
29
|
|
|
$node |
30
|
7 |
|
->treatNullLike(array('type' => 'none')) |
31
|
7 |
|
->validate() |
32
|
|
|
->always(function ($v) { |
33
|
7 |
|
return $this->validate($v); |
34
|
7 |
|
}) |
35
|
7 |
|
->end() |
36
|
7 |
|
->children() |
37
|
7 |
|
->scalarNode('path')->end() |
38
|
7 |
|
->scalarNode('host')->end() |
39
|
7 |
|
->scalarNode('port')->end() |
40
|
7 |
|
->scalarNode('ttl') |
41
|
7 |
|
->validate() |
42
|
7 |
|
->ifTrue(function ($value) { |
43
|
|
|
return ! is_int($value) || $value < 0; |
44
|
7 |
|
}) |
45
|
7 |
|
->thenInvalid('TTL must be a non-negative integer') |
46
|
7 |
|
->end() |
47
|
7 |
|
->end() |
48
|
7 |
|
->enumNode('type') |
49
|
7 |
|
->values([ null, 'none', 'array', 'file', 'redis', 'memcached' ]) |
50
|
7 |
|
->treatNullLike('none') |
51
|
7 |
|
->end() |
52
|
7 |
|
->enumNode('validation') |
53
|
7 |
|
->values([ 'skip', 'deny' ]) |
54
|
7 |
|
->end(); |
55
|
|
|
|
56
|
|
|
|
57
|
7 |
|
return $node; |
58
|
|
|
} |
59
|
|
|
|
60
|
7 |
|
private function validate($value) |
61
|
|
|
{ |
62
|
7 |
|
if (! is_array($value)) { |
63
|
|
|
return $value; |
64
|
|
|
} |
65
|
|
|
|
66
|
7 |
|
if ($value['type'] == 'redis' || $value == 'memcached') { |
67
|
4 |
|
$this->validateServerCache($value['type'], $value); |
68
|
|
|
} |
69
|
|
|
|
70
|
5 |
|
if ($value['type'] == 'file' && ! isset($value['path'])) { |
71
|
|
|
throw new \InvalidArgumentException("'path' property is required for 'file' cache"); |
72
|
|
|
} |
73
|
|
|
|
74
|
5 |
|
if ($value['type'] !== 'none' && ! isset($value['validation'])) { |
75
|
|
|
throw new \InvalidArgumentException("'validation' property is required when cache is enabled."); |
76
|
|
|
} |
77
|
|
|
|
78
|
5 |
|
return $value; |
79
|
|
|
} |
80
|
|
|
|
81
|
4 |
|
private function validateServerCache($type, $value) |
82
|
|
|
{ |
83
|
4 |
|
if (! isset($value['host'])) { |
84
|
2 |
|
throw new \InvalidArgumentException("'host' is required for '$type' cache"); |
85
|
|
|
} |
86
|
|
|
|
87
|
2 |
|
if (! isset($value['port'])) { |
88
|
|
|
throw new \InvalidArgumentException("'port' is required for '$type' cache"); |
89
|
|
|
} |
90
|
2 |
|
} |
91
|
|
|
} |
92
|
|
|
|