1 <?php
2 /**
3 * Option.php
4 *
5 * @author Elvyrra SAS
6 * @license http://rem.mit-license.org/ MIT
7 */
8
9 namespace Hawk;
10
11 /**
12 * This class is used to access and set application options
13 *
14 * @package Core
15 */
16 class Option{
17 /**
18 * The options loaded from database and saved in memory
19 */
20 private static $options = array();
21
22 /**
23 * The file containing the cache of options
24 */
25
26 /**
27 * Get the value of an option
28 *
29 * @param string $name the option to get the value of, formatted like : <plugin>.<key>
30 *
31 * @return string the value og the option
32 */
33 public static function get($name){
34 list($plugin, $key) = explode('.', $name);
35 if(! isset(self::$options[$plugin][$key])) {
36 self::getPluginOptions($plugin);
37 }
38
39 return isset(self::$options[$plugin][$key]) ? self::$options[$plugin][$key] : null;
40 }
41
42 /**
43 * Load all the options of a given plugin
44 *
45 * @param string $plugin The plugin to find the options of
46 *
47 * @return array All the options of the plugin with their values
48 */
49 public static function getPluginOptions($plugin){
50 if(!App::conf()->has('db')) {
51 return array();
52 }
53
54 if(!isset(self::$options[$plugin])) {
55 $options = App::db()->select(
56 array(
57 'from' => DB::getFullTablename('Option'),
58 'where' => new DBExample(array('plugin' => $plugin))
59 )
60 );
61
62 self::$options[$plugin] = array();
63 foreach($options as $option){
64 self::$options[$plugin][$option['key']] = $option['value'];
65 }
66 }
67 return self::$options[$plugin];
68 }
69
70
71 /**
72 * Add an option or update an existing option value
73 *
74 * @param string $name The name of the option, formatted like : <plugin>.<key>
75 * @param mixed $value The value to set for this option
76 */
77 public static function set($name, $value){
78 list($plugin, $key) = explode('.', $name);
79 self::$options[$plugin][$key] = $value;
80
81 App::db()->replace(
82 DB::getFullTablename('Option'), array(
83 'plugin' => $plugin,
84 'key' => $key,
85 'value' => $value
86 )
87 );
88 }
89
90
91 /**
92 * Remove an option
93 *
94 * @param string $name The name of the option, formatted like : <plugin>.<key>
95 */
96 public static function delete($name){
97 list($plugin, $key) = explode('.', $name);
98 App::db()->delete(
99 DB::getFullTablename('Option'), new DBExample(
100 array(
101 'plugin' => $plugin,
102 'key' => $key
103 )
104 )
105 );
106 }
107
108 /**
109 * Get All options
110 *
111 * @return array The array containing all options
112 */
113 public static function getAll(){
114 return self::$options;
115 }
116 }
117
118