From 1fb3a39325d1343adef3211b17a74421ef8eaeb0 Mon Sep 17 00:00:00 2001 From: Juan Pablo Date: Thu, 18 Feb 2016 14:29:33 +0100 Subject: [PATCH] add new scope based on class --- .../SettingsBundle/DependencyInjection/Configuration.php | 7 +++++++ .../SettingsBundle/Exception/WrongScopeException.php | 8 ++++++-- .../Bundle/SettingsBundle/Manager/SettingsManager.php | 5 +++++ 3 files changed, 18 insertions(+), 2 deletions(-) diff --git a/src/Dmishh/Bundle/SettingsBundle/DependencyInjection/Configuration.php b/src/Dmishh/Bundle/SettingsBundle/DependencyInjection/Configuration.php index 43e3b22..b0f585e 100644 --- a/src/Dmishh/Bundle/SettingsBundle/DependencyInjection/Configuration.php +++ b/src/Dmishh/Bundle/SettingsBundle/DependencyInjection/Configuration.php @@ -56,6 +56,10 @@ public function getConfigTreeBuilder() ->arrayNode('settings') ->prototype('array') ->addDefaultsIfNotSet() + ->validate() + ->ifTrue(function($v){ return SettingsManagerInterface::SCOPE_USER === $v['scope'] && !isset($v['namespace']);}) + ->thenInvalid('You must define a namespace for the scope user.') + ->end() ->children() ->scalarNode('scope') ->defaultValue('all') @@ -64,6 +68,9 @@ public function getConfigTreeBuilder() ->thenInvalid('Invalid scope %s. Valid scopes are: ' . implode(', ', array_map(function ($s) { return '"' . $s . '"'; }, $scopes)) . '.') ->end() ->end() + ->scalarNode('namespace') + ->defaultValue(null) + ->end() ->arrayNode('validation') ->addDefaultsIfNotSet() ->children() diff --git a/src/Dmishh/Bundle/SettingsBundle/Exception/WrongScopeException.php b/src/Dmishh/Bundle/SettingsBundle/Exception/WrongScopeException.php index eb6b0c0..e7bb237 100644 --- a/src/Dmishh/Bundle/SettingsBundle/Exception/WrongScopeException.php +++ b/src/Dmishh/Bundle/SettingsBundle/Exception/WrongScopeException.php @@ -15,12 +15,16 @@ class WrongScopeException extends SettingsException { - public function __construct($scope, $settingName) + public function __construct($scope, $settingName, $namespace = null) { if ($scope === SettingsManagerInterface::SCOPE_GLOBAL) { $message = sprintf('You tried to access setting "%s" but it is in the "%s" scope which means you must not use a SettingOwnerInterface object with this option.', $settingName, $scope); } elseif ($scope === SettingsManagerInterface::SCOPE_USER) { - $message = sprintf('You tried to access setting "%s" but it is in the "%s" scope which means you have to pass a SettingOwnerInterface object with this option.', $settingName, $scope); + if ($namespace !== null) { + $message = sprintf('You tried to access setting "%s" but it\'s namespace scope is for %s .', $settingName, $namespace); + } else { + $message = sprintf('You tried to access setting "%s" but it is in the "%s" scope which means you have to pass a SettingOwnerInterface object with this option.', $settingName, $scope); + } } else { $message = sprintf('Wrong scope "%s" for setting "%s". Check your configuration.', $scope, $settingName); } diff --git a/src/Dmishh/Bundle/SettingsBundle/Manager/SettingsManager.php b/src/Dmishh/Bundle/SettingsBundle/Manager/SettingsManager.php index 0ae9c5f..1b01ccf 100644 --- a/src/Dmishh/Bundle/SettingsBundle/Manager/SettingsManager.php +++ b/src/Dmishh/Bundle/SettingsBundle/Manager/SettingsManager.php @@ -263,6 +263,11 @@ private function validateSetting($name, SettingsOwnerInterface $owner = null) if ($scope !== SettingsManagerInterface::SCOPE_ALL) { if ($scope === SettingsManagerInterface::SCOPE_GLOBAL && $owner !== null || $scope === SettingsManagerInterface::SCOPE_USER && $owner === null) { throw new WrongScopeException($scope, $name); + } elseif ($owner !== null) { + $namespace = $this->settingsConfiguration[$name]['namespace']; + if (!$owner instanceof $namespace) { + throw new WrongScopeException($scope, $name, $namespace); + } } }