Skip to content

Instantly share code, notes, and snippets.

@kalmanolah
Last active January 29, 2019 20:18
Show Gist options
  • Save kalmanolah/7182108 to your computer and use it in GitHub Desktop.
Save kalmanolah/7182108 to your computer and use it in GitHub Desktop.
A Symfony2 Service & Doctrine entity combo for persisting variables and configuration to the database. Inspired heavily by Drupal's variable_get() and variable_set() functions, which can be found here: https://api.drupal.org/api/drupal/includes!bootstrap.inc/function/variable_set/7 .
<?php
namespace Your\ExampleBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
/**
* @ORM\Entity
* @UniqueEntity("name")
*/
class Variable
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @ORM\Column(type="string", length=32)
*/
private $name;
/**
* @ORM\Column(type="object", nullable=true)
*/
private $value;
public function getId()
{
return $this->id;
}
public function getName()
{
return $this->name;
}
public function setName($name)
{
$this->name = $name;
return $this;
}
public function getValue()
{
return $this->value;
}
public function setValue($value)
{
$this->value = $value;
return $this;
}
}
<?php
namespace Your\ExampleBundle\Service;
use Your\ExampleBundle\Entity\Variable;
use Doctrine\ORM\EntityManager;
/**
* A Symfony2 Service for persisting variables and configuration to the database as an entity.
* Service definition:
* variables:
* class: Your\ExampleBundle\Service\VariablesService
* arguments: [@doctrine.orm.entity_manager]
*
* @author Kalman Olah <hello _at_ kalmanolah _dot_ net>
*/
class VariablesService {
private $em;
/**
* Constructs the Service
* @param EntityManager $em The Doctrine EntityManager
*/
public function __construct(EntityManager $em) {
$this->em = $em;
}
/**
* Loads a Variable
* @param string $name Name of the Variable to load
* @param mixed $default Default value to return if the Variable is not found
* @return Variable|mixed The Variable object, or the default value
*/
public function load($name, $default = null)
{
$variable = $this->em->getRepository('YourExampleBundle:Variable')->findOneByName($name);
return $variable ? $variable : $default;
}
/**
* Removes a Variable
* @param string $name Name of the Variable to remove
*/
public function remove($name)
{
$variable = $this->load($name);
if ($variable) {
$this->em->remove($variable);
$this->em->flush();
}
}
/**
* Sets the value of a Variable
* @param string $name Name of the Variable whose value to set; if this Variable does not exist it will be created
* @param mixed $value Value to give the Variable: this can be any object that can be saved into a Doctrine ORM Object-type field
*/
public function set($name, $value = null)
{
if ($value == null) {
$this->remove($name);
return;
}
$variable = $this->load($name);
if (!$variable) {
$variable = new Variable();
$variable->setName($name);
}
$variable->setValue($value);
$this->em->persist($variable);
$this->em->flush();
}
/**
* Gets the value of a Variable
* @param string $name Name of the Variable whose value to get
* @param mixed $default Default value to return if the Variable is not found
* @return mixed The value of the Variable object, or the default value
*/
public function get($name, $default = null)
{
$variable = $this->load($name);
return $variable ? $variable->getValue() : $default;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment