<?php
namespace App\Helpers\System;
use Symfony\Component\DependencyInjection\ContainerInterface as Container;
use Symfony\Component\Yaml\Parser;
use Doctrine\ORM\EntityManagerInterface;
class ConfigHelper
{
protected $container;
protected $em;
protected $config;
public function __construct(Container $container, EntityManagerInterface $em)
{
$this->container = $container;
$this->em = $em;
$this->config = array();
// Get everything into an array
$allConfig = $this->em->getRepository(\App\Entity\System\Config::class)->findAll();
foreach($allConfig AS $someConfig)
{
$this->config[$someConfig->getKey()] = $someConfig;
}
}
public function getAll()
{
return $this->config;
}
public function get($key)
{
if(!array_key_exists($key, $this->config))
return null;
else
return $this->config[$key];
}
public function getValue($key, $default = null)
{
if(!array_key_exists($key, $this->config))
return $default;
else
return $this->config[$key]->getValue();
}
public function setValue($key, $value)
{
if(!array_key_exists($key, $this->config))
{
throw new \Exception("Config item not found: " . $key);
}
else
{
$this->config[$key]->setValue($value);
}
// Persist and flush
$this->em->persist($this->config[$key]);
$this->em->flush($this->config[$key]);
}
public function create($opt = array())
{
$newItem = new \App\Entity\System\Config();
$newItem->setKey($opt['key']);
$newItem->setValue($opt['value']);
$newItem->setLabel($opt['label']);
$newItem->setCategory($opt['category']);
// Persist and flush
$this->em->persist($newItem);
$this->em->flush();
$this->config[$opt['key']] = $newItem;
}
}