#18 - Options class for PHP website

Date: 2018-07-21 12:00 - PHP

Options class with options saved on a database

<?php
class Options {
    private static $loaded = false;
    private static $values = array();
    public static function load() {
        if (static::$loaded)
            return;

        static::$values = Database::getConnection('main', function($db) {
            $statement = $db->query('SELECT `key`, value, autoload, serialized FROM options WHERE autoload = 1');
            if ($statement->rowCount() == 0)
                return array();
            $result = array();
            foreach ($statement as $option) {
                if ($option['serialized'])
                    $option['value'] = unserialize($option['value']);
                $result[$option['key']] = array($option['value'], $option['autoload'], $option['serialized']);
            }
            return $result;
        }, function() {
            return array();
        });

        static::$loaded = true;
    }

    public static function getData($key, $default = null) {
        if (!static::$loaded)
            static::load();
        if (array_key_exists($key, static::$values))
            return static::$values[$key][0];
        static::$values[$key] = Database::getConnection('main', function($db) use($key, $default) {
            $statement = $db->prepare('SELECT value, autoload, serialized FROM options WHERE `key` = :key');
            $statement->execute(array(
                ':key' => $key,
            ));
            if ($statement->rowCount() == 0)
                return array($default, false, false);
            $result = $statement->fetch(\PDO::FETCH_ASSOC);
            $value = $result['value'];
            if ($result['serialized'])
                $value = unserialize($value);
            return array($value, $result['autoload'], $result['serialized']);
        }, function() use($default) {
            return array($default, false, false);
        });
        return static::$values[$key][0];
    }

    public static function setData($key, $value, $autoload = null, $serialize = null) {
        if (!static::$loaded)
            static::load();

        if ($autoload == null)
            $autoload = array_key_exists($key, static::$values) ? static::$values[$key][1] : false;
        if ($serialize == null)
            $serialize = array_key_exists($key, static::$values) ? static::$values[$key][2] : false;
        static::$values[$key] = array($value, $autoload, $serialize);
        if ($serialize)
            $value = serialize($value);
        Database::getConnection('main', function($db) use($key, $value, $autoload, $serialize) {
            $statement = $db->prepare('REPLACE INTO options (`key`, value, autoload, serialized) VALUES (:key, :value, :autoload, :serialized)');
            $statement->execute(array(
                ':key' => $key,
                ':value' => $value,
                ':autoload' => $autoload,
                ':serialized' => $serialize,
            ));
        });
    }
}

Previous snippet | Next snippet