Skip to content

Instantly share code, notes, and snippets.

@AnrDaemon
Created July 4, 2018 17:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AnrDaemon/7086fca4b128841c8cefdcc6ac996a63 to your computer and use it in GitHub Desktop.
Save AnrDaemon/7086fca4b128841c8cefdcc6ac996a63 to your computer and use it in GitHub Desktop.
<?php
namespace AnrDaemon\CcWeb;
use
AnrDaemon\CcWeb\Interfaces;
class SettingsManager
implements Interfaces\SettingsManager
{
protected $cache;
protected $pdo;
protected $reg;
protected $tpl;
protected $sec;
protected $dict;
// Interfaces\SettingsManager
public function getRouter()
{
return $this->reg['handler'];
}
public function getCacheManager()
{
return $this->cache['handler'];
}
public function getDatabaseManager()
{
return $this->pdo['handler'];
}
public function getDictionary($name)
{
return $this->dict[$name]['handler'];
}
public function getSecurityManager()
{
return $this->sec['handler'];
}
public function getTemplateManager()
{
return $this->tpl['handler'];
}
// Options handling
public function getOption(/*TODO:PHP7 string */$optName, /*TODO:PHP7 string */$optGroup = null)
{
return $this->getOptionMeta($optName, $optGroup)['val'];
}
public function getOptionMeta(/*TODO:PHP7 string */$optName, /*TODO:PHP7 string */$optGroup = null)
{
static $_q;
if(!isset($_q))
{
$_q = $this->pdo['handler']->prepare("SELECT c.`title`, c.`type`, c.`hidden`, c.`val` FROM `conf` c
INNER JOIN `conf_group` g ON c.`group_id` = g.`id`
WHERE c.`name` = :name AND :group IN (g.`name`, g.`id`)");
}
$_q->execute(['name' => $optName, 'group' => $optGroup ?: 1]);
$result = $_q->fetch();
if($result)
{
if($result['type'] == 4) // JSON json_encode($d, 1360); // JSON_FORCE_OBJECT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_PRESERVE_ZERO_FRACTION
{
$result['val'] = json_decode($result['val'], true, 512, JSON_BIGINT_AS_STRING);
}
return $result;
}
else
throw new \OutOfRangeException("Option '{$optGroup}/{$optName}' is not known in current configuration.");
}
public function getOptionGroup(/*TODO:PHP7 string */$optGroup)
{
static $_q;
if(!isset($_q))
{
$_q = $this->pdo['handler']->prepare("SELECT c.`name`, c.`title`, c.`type`, c.`hidden`, c.`val`
FROM `conf` c
INNER JOIN `conf_group` g ON c.`group_id` = g.`id`
WHERE :group IN (g.`name`, g.`id`)");
}
$_q->execute(['group' => $optGroup]);
$result = [];
foreach($_q as $option)
{
$name = $option['name'];
unset($option['name']);
if($option['type'] == 4) // JSON json_encode($d, 1360); // JSON_FORCE_OBJECT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_PRESERVE_ZERO_FRACTION
{
$option['val'] = json_decode($option['val'], true, 512, JSON_BIGINT_AS_STRING);
}
$result[$name] = $option;
}
return $result;
}
// Setters
public function setRouter(Interfaces\Router $reg)
{
if(isset($this->reg))
throw new \LogicException("Unable to redefine configuration", 0, $this->reg['defined']);
if(!($reg instanceof Interfaces\Registry))
throw new \InvalidArgumentException("Router must implement both basic Registry and Router interfaces.");
$this->reg['handler'] = $reg;
$this->reg['defined'] = new \LogicException("Already defined in");
}
public function setCacheManager(Interfaces\CacheManager $cache)
{
if(isset($this->cache))
throw new \LogicException("Unable to redefine configuration", 0, $this->cache['defined']);
$this->cache['handler'] = $cache;
$this->cache['defined'] = new \LogicException("Already defined in");
}
public function setDatabaseManager(Interfaces\DatabaseManager $pdo)
{
if(isset($this->pdo))
throw new \LogicException("Unable to redefine configuration", 0, $this->pdo['defined']);
$this->pdo['handler'] = $pdo;
$this->pdo['defined'] = new \LogicException("Already defined in");
}
public function setDictionary($name, $handler)
{
if(isset($this->dict[$name]))
throw new \LogicException("Unable to redefine configuration", 0, $this->dict[$name]['defined']);
$this->dict[$name]['handler'] = $handler;
$this->dict[$name]['defined'] = new \LogicException("Already defined in");
}
public function setSecurityManager(Interfaces\SecurityManager $sec)
{
if(isset($this->sec))
throw new \LogicException("Unable to redefine configuration", 0, $this->sec['defined']);
$this->sec['handler'] = $sec;
$this->sec['defined'] = new \LogicException("Already defined in");
}
public function setTemplateManager(Interfaces\TemplateManager $tpl)
{
if(isset($this->tpl))
throw new \LogicException("Unable to redefine configuration", 0, $this->tpl['defined']);
$this->tpl['handler'] = $tpl;
$this->tpl['defined'] = new \LogicException("Already defined in");
}
// Magic!
public function __construct(...$args)
{
foreach($args as $key)
{
if($key instanceof Interfaces\CacheManager)
$this->setCacheManager($key);
if($key instanceof Interfaces\DatabaseManager)
$this->setDatabaseManager($key);
if($key instanceof Interfaces\Router)
$this->setRouter($key);
if($key instanceof Interfaces\SecurityManager)
$this->setSecurityManager($key);
if($key instanceof Interfaces\TemplateManager)
$this->setTemplateManager($key);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment