From 7e06d6a3784ecf476946b0ea1e9a2322b76aaa7a Mon Sep 17 00:00:00 2001 From: Victor Lennel Date: Thu, 22 Oct 2015 13:26:30 +0200 Subject: [PATCH 1/6] add document to work with mongodb --- .../SettingsBundle/Document/Setting.php | 105 ++++++++++++++++++ 1 file changed, 105 insertions(+) create mode 100644 src/Dmishh/Bundle/SettingsBundle/Document/Setting.php diff --git a/src/Dmishh/Bundle/SettingsBundle/Document/Setting.php b/src/Dmishh/Bundle/SettingsBundle/Document/Setting.php new file mode 100644 index 0000000..615a0d8 --- /dev/null +++ b/src/Dmishh/Bundle/SettingsBundle/Document/Setting.php @@ -0,0 +1,105 @@ + + * + * 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; + } +} From 06e2f5344819e37693cd0207ffd4fa9d9b31f550 Mon Sep 17 00:00:00 2001 From: Victor Lennel Date: Thu, 22 Oct 2015 13:36:13 +0200 Subject: [PATCH 2/6] update service to work with db driver --- .../Bundle/SettingsBundle/Resources/config/services.yml | 7 ------- .../SettingsBundle/Resources/config/services_mongodb.yml | 7 +++++++ .../SettingsBundle/Resources/config/services_orm.yml | 7 +++++++ 3 files changed, 14 insertions(+), 7 deletions(-) create mode 100644 src/Dmishh/Bundle/SettingsBundle/Resources/config/services_mongodb.yml create mode 100644 src/Dmishh/Bundle/SettingsBundle/Resources/config/services_orm.yml diff --git a/src/Dmishh/Bundle/SettingsBundle/Resources/config/services.yml b/src/Dmishh/Bundle/SettingsBundle/Resources/config/services.yml index f946537..3d7551d 100644 --- a/src/Dmishh/Bundle/SettingsBundle/Resources/config/services.yml +++ b/src/Dmishh/Bundle/SettingsBundle/Resources/config/services.yml @@ -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%] diff --git a/src/Dmishh/Bundle/SettingsBundle/Resources/config/services_mongodb.yml b/src/Dmishh/Bundle/SettingsBundle/Resources/config/services_mongodb.yml new file mode 100644 index 0000000..9406fd0 --- /dev/null +++ b/src/Dmishh/Bundle/SettingsBundle/Resources/config/services_mongodb.yml @@ -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% \ No newline at end of file diff --git a/src/Dmishh/Bundle/SettingsBundle/Resources/config/services_orm.yml b/src/Dmishh/Bundle/SettingsBundle/Resources/config/services_orm.yml new file mode 100644 index 0000000..a41a252 --- /dev/null +++ b/src/Dmishh/Bundle/SettingsBundle/Resources/config/services_orm.yml @@ -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% \ No newline at end of file From 48094290b3a96a49e11b26a15ab7fca6c0178b77 Mon Sep 17 00:00:00 2001 From: Victor Lennel Date: Thu, 22 Oct 2015 13:36:30 +0200 Subject: [PATCH 3/6] add configuration check for db driver --- .../DependencyInjection/Configuration.php | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/Dmishh/Bundle/SettingsBundle/DependencyInjection/Configuration.php b/src/Dmishh/Bundle/SettingsBundle/DependencyInjection/Configuration.php index 43e3b22..5e1ea4c 100644 --- a/src/Dmishh/Bundle/SettingsBundle/DependencyInjection/Configuration.php +++ b/src/Dmishh/Bundle/SettingsBundle/DependencyInjection/Configuration.php @@ -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)) + ->end() + ->isRequired() + ->cannotBeEmpty() + ->end() ->scalarNode('layout') ->defaultValue('DmishhSettingsBundle::layout.html.twig') ->end() From 591c969f42c07669da5592b823519e5942605154 Mon Sep 17 00:00:00 2001 From: Victor Lennel Date: Thu, 22 Oct 2015 13:36:59 +0200 Subject: [PATCH 4/6] load db services --- .../DependencyInjection/DmishhSettingsExtension.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/Dmishh/Bundle/SettingsBundle/DependencyInjection/DmishhSettingsExtension.php b/src/Dmishh/Bundle/SettingsBundle/DependencyInjection/DmishhSettingsExtension.php index c224dd1..134b2ff 100644 --- a/src/Dmishh/Bundle/SettingsBundle/DependencyInjection/DmishhSettingsExtension.php +++ b/src/Dmishh/Bundle/SettingsBundle/DependencyInjection/DmishhSettingsExtension.php @@ -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']); From 92b94697c41085a2878ba77e016a8b9e6097f9e4 Mon Sep 17 00:00:00 2001 From: Victor Lennel Date: Thu, 22 Oct 2015 13:37:23 +0200 Subject: [PATCH 5/6] refactor settings manager with mongodb --- .../Manager/MongoDB/SettingsManager.php | 74 +++++++++++++++++++ .../Manager/SettingsManager.php | 28 ++++--- 2 files changed, 91 insertions(+), 11 deletions(-) create mode 100644 src/Dmishh/Bundle/SettingsBundle/Manager/MongoDB/SettingsManager.php diff --git a/src/Dmishh/Bundle/SettingsBundle/Manager/MongoDB/SettingsManager.php b/src/Dmishh/Bundle/SettingsBundle/Manager/MongoDB/SettingsManager.php new file mode 100644 index 0000000..155af3d --- /dev/null +++ b/src/Dmishh/Bundle/SettingsBundle/Manager/MongoDB/SettingsManager.php @@ -0,0 +1,74 @@ +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 + */ + private 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; + + } +} \ No newline at end of file diff --git a/src/Dmishh/Bundle/SettingsBundle/Manager/SettingsManager.php b/src/Dmishh/Bundle/SettingsBundle/Manager/SettingsManager.php index 0ae9c5f..e98dcc6 100644 --- a/src/Dmishh/Bundle/SettingsBundle/Manager/SettingsManager.php +++ b/src/Dmishh/Bundle/SettingsBundle/Manager/SettingsManager.php @@ -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 @@ -209,11 +209,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); } @@ -251,7 +248,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)) { @@ -327,4 +324,13 @@ 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(); + + } } From 29be6e6f765ba92eab6697dc4c4792059e561900 Mon Sep 17 00:00:00 2001 From: Victor Lennel Date: Thu, 22 Oct 2015 14:11:58 +0200 Subject: [PATCH 6/6] fix setting retrieve with mongodb --- .../Manager/MongoDB/SettingsManager.php | 11 ++++++++++- .../SettingsBundle/Manager/SettingsManager.php | 17 +++++++++++------ 2 files changed, 21 insertions(+), 7 deletions(-) diff --git a/src/Dmishh/Bundle/SettingsBundle/Manager/MongoDB/SettingsManager.php b/src/Dmishh/Bundle/SettingsBundle/Manager/MongoDB/SettingsManager.php index 155af3d..fa8cb42 100644 --- a/src/Dmishh/Bundle/SettingsBundle/Manager/MongoDB/SettingsManager.php +++ b/src/Dmishh/Bundle/SettingsBundle/Manager/MongoDB/SettingsManager.php @@ -38,7 +38,7 @@ public function __construct( * @throws \Dmishh\Bundle\SettingsBundle\Exception\UnknownSerializerException * @return array */ - private function getSettingsFromRepository(SettingsOwnerInterface $owner = null) + protected function getSettingsFromRepository(SettingsOwnerInterface $owner = null) { $settings = array(); @@ -71,4 +71,13 @@ protected function getNewSetting(SettingsOwnerInterface $owner = null){ 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; + } } \ No newline at end of file diff --git a/src/Dmishh/Bundle/SettingsBundle/Manager/SettingsManager.php b/src/Dmishh/Bundle/SettingsBundle/Manager/SettingsManager.php index e98dcc6..5d552b1 100644 --- a/src/Dmishh/Bundle/SettingsBundle/Manager/SettingsManager.php +++ b/src/Dmishh/Bundle/SettingsBundle/Manager/SettingsManager.php @@ -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 @@ -300,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(); @@ -333,4 +329,13 @@ protected function getNewSetting(SettingsOwnerInterface $owner = null){ 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; + } }