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() 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']); 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; + } +} 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..fa8cb42 --- /dev/null +++ b/src/Dmishh/Bundle/SettingsBundle/Manager/MongoDB/SettingsManager.php @@ -0,0 +1,83 @@ +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; + } +} \ 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..5d552b1 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 @@ -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 @@ -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); } @@ -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)) { @@ -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(); @@ -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; + } } 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