Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,18 @@ public function getConfigTreeBuilder()
SettingsManagerInterface::SCOPE_USER,
);

$supportedDrivers = array('orm', 'mongodb');

$rootNode
->children()
->scalarNode('db_driver')
->validate()
->ifNotInArray($supportedDrivers)
->thenInvalid('The driver %s is not supported. Please choose one of '.json_encode($supportedDrivers))
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could be implode instead of json_encode I think? Rest of the code looks quite okay, but I've got no MongoDB experience to validate this.

->end()
->isRequired()
->cannotBeEmpty()
->end()
->scalarNode('layout')
->defaultValue('DmishhSettingsBundle::layout.html.twig')
->end()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ public function load(array $configs, ContainerBuilder $container)
$loader = new YamlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config'));
$loader->load('services.yml');

$db_driver = $config['db_driver'];
$loader->load(sprintf('services_%s.yml',$db_driver));
// Configure the correct storage
if ($config['cache_service'] !== null) {
$storage = new Reference($config['cache_service']);
Expand Down
105 changes: 105 additions & 0 deletions src/Dmishh/Bundle/SettingsBundle/Document/Setting.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
<?php

/**
* This file is part of the DmishhSettingsBundle package.
*
* (c) 2013 Dmitriy Scherbina <http://dmishh.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Dmishh\Bundle\SettingsBundle\Document;

use Doctrine\ORM\Mapping as ORM;
use Doctrine\ODM\MongoDB\Mapping\Annotations as MongoDB;

/**
* @MongoDB\Document
*/
class Setting
{
/**
* @MongoDB\Id
*/
private $id;

/**
* @MongoDB\String
*/
private $name;

/**
* @MongoDB\String
*/
private $value;

/**
* @MongoDB\ReferenceOne
*/
private $owner;

/**
* @return mixed
*/
public function getId()
{
return $this->id;
}

/**
* @param mixed $id
*/
public function setId($id)
{
$this->id = $id;
}

/**
* @return mixed
*/
public function getName()
{
return $this->name;
}

/**
* @param mixed $name
*/
public function setName($name)
{
$this->name = $name;
}

/**
* @return mixed
*/
public function getValue()
{
return $this->value;
}

/**
* @param mixed $value
*/
public function setValue($value)
{
$this->value = $value;
}

/**
* @return mixed
*/
public function getOwner()
{
return $this->owner;
}

/**
* @param mixed $owner
*/
public function setOwner($owner)
{
$this->owner = $owner;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?php
/**
* Created by PhpStorm.
* User: Victor
* Date: 22/10/15
* Time: 12:58
*/
namespace Dmishh\Bundle\SettingsBundle\Manager\MongoDB;
use Dmishh\Bundle\SettingsBundle\Document\Setting;
use Dmishh\Bundle\SettingsBundle\Entity\SettingsOwnerInterface;
use Dmishh\Bundle\SettingsBundle\Exception\WrongScopeException;
use Dmishh\Bundle\SettingsBundle\Serializer\SerializerInterface;
use Doctrine\ODM\MongoDB\DocumentManager;

class SettingsManager extends \Dmishh\Bundle\SettingsBundle\Manager\SettingsManager {

/**
* @param DocumentManager $em
* @param SerializerInterface $serializer
* @param array $settingsConfiguration
*/
public function __construct(
DocumentManager $em,
SerializerInterface $serializer,
array $settingsConfiguration = array()
) {
$this->em = $em;
$this->repository = $em->getRepository('Dmishh\Bundle\SettingsBundle\Document\Setting');
$this->serializer = $serializer;
$this->settingsConfiguration = $settingsConfiguration;
}

/**
* Retreives settings from repository.
*
* @param SettingsOwnerInterface|null $owner
*
* @throws \Dmishh\Bundle\SettingsBundle\Exception\UnknownSerializerException
* @return array
*/
protected function getSettingsFromRepository(SettingsOwnerInterface $owner = null)
{
$settings = array();

foreach (array_keys($this->settingsConfiguration) as $name) {
try {
$this->validateSetting($name, $owner);
$settings[$name] = null;
} catch (WrongScopeException $e) {
continue;
}
}

/** @var Setting $setting */
foreach ($this->repository->findBy(
array('owner.id' => $owner === null ? null : $owner->getSettingIdentifier())
) as $setting) {
if (array_key_exists($setting->getName(), $settings)) {
$settings[$setting->getName()] = $this->serializer->unserialize($setting->getValue());
}
}

return $settings;
}

protected function getNewSetting(SettingsOwnerInterface $owner = null){
$setting = new Setting();
if ($owner !== null) {
$setting->setOwner($owner);
}
return $setting;

}

protected function getSettingsFromRepositoryByNames($names,SettingsOwnerInterface $owner = null){
$settings = $this->repository->findBy(array(
'name' => array('$in' => $names),
'owner.$id' => $owner === null ? null : new \MongoId($owner->getSettingIdentifier())
)
);
return $settings;
}
}
45 changes: 28 additions & 17 deletions src/Dmishh/Bundle/SettingsBundle/Manager/SettingsManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,32 +27,32 @@ class SettingsManager implements SettingsManagerInterface
/**
* @var array
*/
private $globalSettings;
protected $globalSettings;

/**
* @var array
*/
private $ownerSettings;
protected $ownerSettings;

/**
* @var \Doctrine\Common\Persistence\ObjectManager
*/
private $em;
protected $em;

/**
* @var \Doctrine\ORM\EntityRepository
*/
private $repository;
protected $repository;

/**
* @var SerializerInterface
*/
private $serializer;
protected $serializer;

/**
* @var array
*/
private $settingsConfiguration;
protected $settingsConfiguration;

/**
* @param ObjectManager $em
Expand Down Expand Up @@ -188,11 +188,7 @@ private function flush($names, SettingsOwnerInterface $owner = null)
{
$names = (array) $names;

$settings = $this->repository->findBy(array(
'name' => $names,
'ownerId' => $owner === null ? null : $owner->getSettingIdentifier()
)
);
$settings = $this->getSettingsFromRepositoryByNames($names,$owner);

// Assert: $settings might be a smaller set than $names

Expand All @@ -209,11 +205,8 @@ private function flush($names, SettingsOwnerInterface $owner = null)

if (!$setting) {
// if the setting does not exist in DB, create it
$setting = new Setting();
$setting = $this->getNewSetting($owner);
$setting->setName($name);
if ($owner !== null) {
$setting->setOwnerId($owner->getSettingIdentifier());
}
$this->em->persist($setting);
}

Expand Down Expand Up @@ -251,7 +244,7 @@ protected function findSettingByName($haystack, $needle) {
* @throws \Dmishh\Bundle\SettingsBundle\Exception\UnknownSettingException
* @throws \Dmishh\Bundle\SettingsBundle\Exception\WrongScopeException
*/
private function validateSetting($name, SettingsOwnerInterface $owner = null)
protected function validateSetting($name, SettingsOwnerInterface $owner = null)
{
// Name validation
if (!is_string($name) || !array_key_exists($name, $this->settingsConfiguration)) {
Expand Down Expand Up @@ -303,7 +296,7 @@ private function loadSettings(SettingsOwnerInterface $owner = null)
* @throws \Dmishh\Bundle\SettingsBundle\Exception\UnknownSerializerException
* @return array
*/
private function getSettingsFromRepository(SettingsOwnerInterface $owner = null)
protected function getSettingsFromRepository(SettingsOwnerInterface $owner = null)
{
$settings = array();

Expand All @@ -327,4 +320,22 @@ private function getSettingsFromRepository(SettingsOwnerInterface $owner = null)

return $settings;
}

protected function getNewSetting(SettingsOwnerInterface $owner = null){
$setting = new Setting();
if ($owner !== null) {
$setting->setOwnerId($owner->getSettingIdentifier());
}
return new $setting();

}

protected function getSettingsFromRepositoryByNames($names,SettingsOwnerInterface $owner = null){
$settings = $this->repository->findBy(array(
'name' => $names,
'ownerId' => $owner === null ? null : $owner->getSettingIdentifier()
)
);
return $settings;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,6 @@ services:
settings_manager:
alias: dmishh.settings.settings_manager

dmishh.settings.settings_manager:
class: Dmishh\Bundle\SettingsBundle\Manager\SettingsManager
arguments:
- @doctrine.orm.entity_manager
- @dmishh.settings.serializer
- %settings_manager.settings%

dmishh.settings.cached_settings_manager:
class: Dmishh\Bundle\SettingsBundle\Manager\CachedSettingsManager
arguments: [@dmishh.settings.settings_manager, %dmishh.settings.cache.lifetime%]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
services:
dmishh.settings.settings_manager:
class: Dmishh\Bundle\SettingsBundle\Manager\MongoDB\SettingsManager
arguments:
- @doctrine.odm.mongodb.document_manager
- @dmishh.settings.serializer
- %settings_manager.settings%
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
services:
dmishh.settings.settings_manager:
class: Dmishh\Bundle\SettingsBundle\Manager\SettingsManager
arguments:
- @doctrine.orm.entity_manager
- @dmishh.settings.serializer
- %settings_manager.settings%