1 | <?php |
||
7 | class Valiable |
||
8 | { |
||
9 | protected $prefix = "sonar_valiable_"; |
||
10 | protected $name_list = "sonar_valiables"; |
||
11 | private $cache; |
||
12 | |||
13 | 12 | public function __construct(Cache $cache) |
|
37 | 5 | public function getNames() |
|
38 | { |
||
39 | 5 | $names = $this->cache->get($this->name_list); |
|
40 | 5 | if ( $names ) { |
|
41 | 2 | $names = $this->decode($names); |
|
42 | } else { |
||
43 | 3 | $names = []; |
|
44 | } |
||
45 | 5 | return $names; |
|
46 | } |
||
47 | 2 | public function set($name,$data) |
|
52 | 3 | public function importYaml($yaml_data) |
|
53 | { |
||
54 | 3 | $data = Yaml::parse($yaml_data); |
|
55 | 3 | if ( is_array($data) === false) { |
|
56 | 1 | return; |
|
57 | } |
||
58 | 2 | foreach ( $data as $name => $rec ) { |
|
59 | 2 | foreach ( $rec as $key => $values ) { |
|
60 | 2 | if ( isset($values['value']) === true) { |
|
61 | 1 | $this->set($name . '_' . $key,$values['value']); |
|
62 | } else { |
||
63 | 1 | throw new \Exception('valueが見つかりません。key=' . $key); |
|
64 | } |
||
65 | } |
||
66 | } |
||
67 | 1 | } |
|
68 | 1 | public function clear() |
|
69 | { |
||
70 | 1 | $names = $this->getNames(); |
|
71 | 1 | if ( count($names) > 0 ) { |
|
72 | 1 | foreach ( $names as $name ) { |
|
73 | 1 | $this->cache->forget($this->prefix . $name); |
|
74 | } |
||
75 | } |
||
76 | 1 | $this->cache->forget($this->name_list); |
|
77 | 1 | } |
|
78 | 2 | protected function addNames($name) |
|
79 | { |
||
80 | 2 | $names = $this->getNames(); |
|
81 | 2 | if ( in_array($name,$names) === false ) { |
|
82 | 2 | $names[] = $name; |
|
83 | } |
||
84 | 2 | $this->cache->forever($this->name_list,$this->encode($names)); |
|
85 | 2 | } |
|
86 | 2 | protected function encode($data) |
|
94 | } |
||
95 |