diff options
author | mattab <matthieu.aubry@gmail.com> | 2013-03-28 03:42:39 +0400 |
---|---|---|
committer | mattab <matthieu.aubry@gmail.com> | 2013-03-28 03:42:40 +0400 |
commit | ae4b03163792f0b6e933933e5d37df87dc3fd566 (patch) | |
tree | d1d7510a9728f587d3d63ebd03e4ecf3d904838b /core/Option.php | |
parent | 158c2150f5f2e13ece459b8d131244c11b763997 (diff) |
Mass conversion of all files to the newly agreed coding standard: PSR 1/2
Converting Piwik core source files, PHP, JS, TPL, CSS
More info: http://piwik.org/participate/coding-standards/
Diffstat (limited to 'core/Option.php')
-rw-r--r-- | core/Option.php | 323 |
1 files changed, 159 insertions, 164 deletions
diff --git a/core/Option.php b/core/Option.php index 7fa0b2b42c..4275b8afa1 100644 --- a/core/Option.php +++ b/core/Option.php @@ -1,10 +1,10 @@ <?php /** * Piwik - Open source web analytics - * + * * @link http://piwik.org * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later - * + * * @category Piwik * @package Piwik */ @@ -12,189 +12,184 @@ /** * Piwik_Option provides a very simple mechanism to save/retrieve key-values pair * from the database (persistent key-value datastore). - * + * * This is useful to save Piwik-wide preferences, configuration values. - * + * * @package Piwik */ class Piwik_Option { - /** - * @var array - */ - private $all = array(); - - /** - * @var bool - */ - private $loaded = false; - - /** - * Singleton instance - * @var self - */ - static private $instance = null; - - /** - * Returns Singleton instance - * - * @return Piwik_Option - */ - static public function getInstance() - { - if (self::$instance == null) - { - self::$instance = new self; - } - return self::$instance; - } - - /** - * Private Constructor - */ - private function __construct() {} - - /** - * Returns the option value for the requested option $name, fetching from database, if not in cache. - * - * @param string $name Key - * @return string|false Value or false, if not found - */ - public function get($name) - { - $this->autoload(); - if(isset($this->all[$name])) - { - return $this->all[$name]; - } - $value = Piwik_FetchOne( 'SELECT option_value '. - 'FROM `' . Piwik_Common::prefixTable('option') . '`'. - 'WHERE option_name = ?', $name); - if($value === false) - { - return false; - } - $this->all[$name] = $value; - return $value; - } - - /** - * Sets the option value in the database and cache - * - * @param string $name - * @param string $value - * @param int $autoload if set to 1, this option value will be automatically loaded; should be set to 1 for options that will always be used in the Piwik request. - */ - public function set($name, $value, $autoload = 0) - { - $autoload = (int)$autoload; - Piwik_Query('INSERT INTO `'. Piwik_Common::prefixTable('option') . '` (option_name, option_value, autoload) '. - ' VALUES (?, ?, ?) '. - ' ON DUPLICATE KEY UPDATE option_value = ?', - array($name, $value, $autoload, $value)); - $this->all[$name] = $value; - } - - /** - * Delete key-value pair from database and reload cache. - * - * @param string $name Key to match exactly - * @param string $value Optional value - */ - public function delete($name, $value = null) - { - $sql = 'DELETE FROM `'. Piwik_Common::prefixTable('option') . '` WHERE option_name = ?'; - $bind[] = $name; - - if(isset($value)) - { - $sql .= ' AND option_value = ?'; - $bind[] = $value; - } - - Piwik_Query($sql, $bind); - - $this->clearCache(); - } - - /** - * Delete key-value pair(s) from database and reload cache. - * The supplied pattern should use '%' as wildcards, and literal '_' should be escaped. - * - * @param string $name Pattern of key to match. - * @param string $value Optional value - */ - public function deleteLike($name, $value = null) - { - $sql = 'DELETE FROM `'. Piwik_Common::prefixTable('option') . '` WHERE option_name LIKE ?'; - $bind[] = $name; - - if(isset($value)) - { - $sql .= ' AND option_value = ?'; - $bind[] = $value; - } - - Piwik_Query($sql, $bind); - - $this->clearCache(); - } - - /** - * Initialize cache with autoload settings. - * - * @return void - */ - private function autoload() - { - if($this->loaded) - { - return; - } - - $all = Piwik_FetchAll('SELECT option_value, option_name - FROM `'. Piwik_Common::prefixTable('option') . '` + /** + * @var array + */ + private $all = array(); + + /** + * @var bool + */ + private $loaded = false; + + /** + * Singleton instance + * @var self + */ + static private $instance = null; + + /** + * Returns Singleton instance + * + * @return Piwik_Option + */ + static public function getInstance() + { + if (self::$instance == null) { + self::$instance = new self; + } + return self::$instance; + } + + /** + * Private Constructor + */ + private function __construct() + { + } + + /** + * Returns the option value for the requested option $name, fetching from database, if not in cache. + * + * @param string $name Key + * @return string|false Value or false, if not found + */ + public function get($name) + { + $this->autoload(); + if (isset($this->all[$name])) { + return $this->all[$name]; + } + $value = Piwik_FetchOne('SELECT option_value ' . + 'FROM `' . Piwik_Common::prefixTable('option') . '`' . + 'WHERE option_name = ?', $name); + if ($value === false) { + return false; + } + $this->all[$name] = $value; + return $value; + } + + /** + * Sets the option value in the database and cache + * + * @param string $name + * @param string $value + * @param int $autoload if set to 1, this option value will be automatically loaded; should be set to 1 for options that will always be used in the Piwik request. + */ + public function set($name, $value, $autoload = 0) + { + $autoload = (int)$autoload; + Piwik_Query('INSERT INTO `' . Piwik_Common::prefixTable('option') . '` (option_name, option_value, autoload) ' . + ' VALUES (?, ?, ?) ' . + ' ON DUPLICATE KEY UPDATE option_value = ?', + array($name, $value, $autoload, $value)); + $this->all[$name] = $value; + } + + /** + * Delete key-value pair from database and reload cache. + * + * @param string $name Key to match exactly + * @param string $value Optional value + */ + public function delete($name, $value = null) + { + $sql = 'DELETE FROM `' . Piwik_Common::prefixTable('option') . '` WHERE option_name = ?'; + $bind[] = $name; + + if (isset($value)) { + $sql .= ' AND option_value = ?'; + $bind[] = $value; + } + + Piwik_Query($sql, $bind); + + $this->clearCache(); + } + + /** + * Delete key-value pair(s) from database and reload cache. + * The supplied pattern should use '%' as wildcards, and literal '_' should be escaped. + * + * @param string $name Pattern of key to match. + * @param string $value Optional value + */ + public function deleteLike($name, $value = null) + { + $sql = 'DELETE FROM `' . Piwik_Common::prefixTable('option') . '` WHERE option_name LIKE ?'; + $bind[] = $name; + + if (isset($value)) { + $sql .= ' AND option_value = ?'; + $bind[] = $value; + } + + Piwik_Query($sql, $bind); + + $this->clearCache(); + } + + /** + * Initialize cache with autoload settings. + * + * @return void + */ + private function autoload() + { + if ($this->loaded) { + return; + } + + $all = Piwik_FetchAll('SELECT option_value, option_name + FROM `' . Piwik_Common::prefixTable('option') . '` WHERE autoload = 1'); - foreach($all as $option) - { - $this->all[$option['option_name']] = $option['option_value']; - } - - $this->loaded = true; - } - - /** - * Clears the cache - * Used in unit tests to reset the state of the object between tests - * - * @return void - */ - public function clearCache() - { - $this->loaded = false; - $this->all = array(); - } + foreach ($all as $option) { + $this->all[$option['option_name']] = $option['option_value']; + } + + $this->loaded = true; + } + + /** + * Clears the cache + * Used in unit tests to reset the state of the object between tests + * + * @return void + */ + public function clearCache() + { + $this->loaded = false; + $this->all = array(); + } } /** * Returns the option value for the requested option $name * - * @param string $name Key + * @param string $name Key * @return string|false Value or false, if not found */ function Piwik_GetOption($name) { - return Piwik_Option::getInstance()->get($name); + return Piwik_Option::getInstance()->get($name); } /** * Sets the option value in the database * - * @param string $name - * @param string $value - * @param int $autoload if set to 1, this option value will be automatically loaded; should be set to 1 for options that will always be used in the Piwik request. + * @param string $name + * @param string $value + * @param int $autoload if set to 1, this option value will be automatically loaded; should be set to 1 for options that will always be used in the Piwik request. */ function Piwik_SetOption($name, $value, $autoload = 0) { - Piwik_Option::getInstance()->set($name, $value, $autoload); + Piwik_Option::getInstance()->set($name, $value, $autoload); } |