1 | <?php
|
---|
2 | /**
|
---|
3 | * Copyright 2006, 2007, 2008, 2009 Eric D. Hough (http://ehough.com)
|
---|
4 | *
|
---|
5 | * This file is part of TubePress (http://tubepress.org)
|
---|
6 | *
|
---|
7 | * TubePress is free software: you can redistribute it and/or modify
|
---|
8 | * it under the terms of the GNU General Public License as published by
|
---|
9 | * the Free Software Foundation, either version 3 of the License, or
|
---|
10 | * (at your option) any later version.
|
---|
11 | *
|
---|
12 | * TubePress is distributed in the hope that it will be useful,
|
---|
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
15 | * GNU General Public License for more details.
|
---|
16 | *
|
---|
17 | * You should have received a copy of the GNU General Public License
|
---|
18 | * along with TubePress. If not, see <http://www.gnu.org/licenses/>.
|
---|
19 | *
|
---|
20 | */
|
---|
21 |
|
---|
22 | function_exists('tubepress_load_classes')
|
---|
23 | || require(dirname(__FILE__) . '/../../../../tubepress_classloader.php');
|
---|
24 | tubepress_load_classes(array('org_tubepress_options_storage_StorageManager',
|
---|
25 | 'org_tubepress_options_Type',
|
---|
26 | 'org_tubepress_options_validation_InputValidationService',
|
---|
27 | 'org_tubepress_options_reference_OptionsReference'));
|
---|
28 |
|
---|
29 | /**
|
---|
30 | * Handles persistent storage of TubePress options
|
---|
31 | *
|
---|
32 | */
|
---|
33 | abstract class org_tubepress_options_storage_AbstractStorageManager implements org_tubepress_options_storage_StorageManager
|
---|
34 | {
|
---|
35 | private $_validationService;
|
---|
36 | private $_optionsReference;
|
---|
37 |
|
---|
38 | /**
|
---|
39 | * Creates an option in storage
|
---|
40 | *
|
---|
41 | * @param unknown_type $optionName The name of the option to create
|
---|
42 | * @param unknown_type $optionValue The default value of the new option
|
---|
43 | *
|
---|
44 | * @return void
|
---|
45 | */
|
---|
46 | protected abstract function create($optionName, $optionValue);
|
---|
47 |
|
---|
48 | /**
|
---|
49 | * Print out debugging info for this
|
---|
50 | * storage manager
|
---|
51 | *
|
---|
52 | * @return void
|
---|
53 | */
|
---|
54 | public final function debug()
|
---|
55 | {
|
---|
56 | $allOpts = $this->_optionsReference->getAllOptionNames();
|
---|
57 |
|
---|
58 | $result = "Should have " . sizeof($allOpts) . " options total";
|
---|
59 |
|
---|
60 | $result .= "<ol>";
|
---|
61 | foreach ($allOpts as $opt) {
|
---|
62 | if ($this->exists($opt)) {
|
---|
63 | $result .= "<li><font color=\"green\">" .
|
---|
64 | "$opt exists and its value is \"" . $this->get($opt) .
|
---|
65 | "\"</font></li>";
|
---|
66 | } else {
|
---|
67 | $result .= "<li><font color=\"red\">" .
|
---|
68 | "$opt does not exist!</font></li>";
|
---|
69 | }
|
---|
70 |
|
---|
71 | }
|
---|
72 | $result .= "</ol>";
|
---|
73 | return $result;
|
---|
74 | }
|
---|
75 |
|
---|
76 | /**
|
---|
77 | * Deletes an option from storage
|
---|
78 | *
|
---|
79 | * @param unknown_type $optionName The name of the option to delete
|
---|
80 | *
|
---|
81 | * @return void
|
---|
82 | */
|
---|
83 | protected abstract function delete($optionName);
|
---|
84 |
|
---|
85 | /**
|
---|
86 | * Initialize the persistent storage
|
---|
87 | *
|
---|
88 | * @return void
|
---|
89 | */
|
---|
90 | public final function init()
|
---|
91 | {
|
---|
92 | $allOptionNames = $this->_optionsReference->getAllOptionNames();
|
---|
93 | $vals = array();
|
---|
94 | foreach ($allOptionNames as $optionName) {
|
---|
95 | $vals[$optionName] = $this->_optionsReference->getDefaultValue($optionName);
|
---|
96 | }
|
---|
97 |
|
---|
98 | foreach($vals as $val => $key) {
|
---|
99 | $this->_init($val, $key);
|
---|
100 | }
|
---|
101 | }
|
---|
102 |
|
---|
103 | private function _init($name, $value)
|
---|
104 | {
|
---|
105 | if (!$this->exists($name)) {
|
---|
106 | $this->delete($name);
|
---|
107 | $this->create($name, $value);
|
---|
108 | }
|
---|
109 | if ($this->_optionsReference->getType($name) != org_tubepress_options_Type::BOOL
|
---|
110 | && $this->get($name) == "") {
|
---|
111 | $this->setOption($name, $value);
|
---|
112 | }
|
---|
113 | }
|
---|
114 |
|
---|
115 | /**
|
---|
116 | * Sets an option value
|
---|
117 | *
|
---|
118 | * @param string $optionName The option name
|
---|
119 | * @param unknown_type $optionValue The option value
|
---|
120 | *
|
---|
121 | * @return void
|
---|
122 | */
|
---|
123 | public final function set($optionName, $optionValue)
|
---|
124 | {
|
---|
125 | $this->_validationService->validate($optionName, $optionValue);
|
---|
126 | $this->setOption($optionName, $optionValue);
|
---|
127 | }
|
---|
128 |
|
---|
129 | /**
|
---|
130 | * Sets an option to a new value, without validation
|
---|
131 | *
|
---|
132 | * @param string $optionName The name of the option to update
|
---|
133 | * @param unknown_type $optionValue The new option value
|
---|
134 | *
|
---|
135 | * @return void
|
---|
136 | */
|
---|
137 | protected abstract function setOption($optionName, $optionValue);
|
---|
138 |
|
---|
139 | /**
|
---|
140 | * Set the org_tubepress_options_validation_InputValidationService
|
---|
141 | *
|
---|
142 | * @param org_tubepress_options_validation_InputValidationService $validationService The validation service
|
---|
143 | */
|
---|
144 | public function setValidationService(org_tubepress_options_validation_InputValidationService $validationService)
|
---|
145 | {
|
---|
146 | $this->_validationService = $validationService;
|
---|
147 | }
|
---|
148 |
|
---|
149 | /**
|
---|
150 | * Set the org_tubepress_options_reference_OptionsReference
|
---|
151 | *
|
---|
152 | * @param org_tubepress_options_reference_OptionsReference $reference The options reference
|
---|
153 | */
|
---|
154 | public function setOptionsReference(org_tubepress_options_reference_OptionsReference $reference)
|
---|
155 | {
|
---|
156 | $this->_optionsReference = $reference;
|
---|
157 | }
|
---|
158 | }
|
---|