Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add new scope based on class #54

Open
wants to merge 1 commit into
base: 3.x
Choose a base branch
from
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 @@ -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')
Expand 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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
5 changes: 5 additions & 0 deletions src/Dmishh/Bundle/SettingsBundle/Manager/SettingsManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}

Expand Down