diff -u b/config_readonly.routing.yml b/config_readonly.routing.yml --- b/config_readonly.routing.yml +++ b/config_readonly.routing.yml @@ -4,7 +4,7 @@ defaults: _form: '\Drupal\config_readonly\Form\ConfigReadonlyExceptionsForm' - _title: 'Config Readonly Exceptions' + _title: 'Config Read-only Exceptions' requirements: - _permission: 'access administration pages' + _permission: 'administer config readonly' options: _admin_route: TRUE diff -u b/config_readonly.services.yml b/config_readonly.services.yml --- b/config_readonly.services.yml +++ b/config_readonly.services.yml @@ -6,3 +6,3 @@ config_readonly_exceptions: - class: Drupal\config_readonly\ConfigReadonlyExceptionsServiceProvider + class: Drupal\config_readonly\ConfigReadonlyExceptionsService arguments: ["@config.factory"] diff -u b/src/Config/ConfigReadonlyStorage.php b/src/Config/ConfigReadonlyStorage.php --- b/src/Config/ConfigReadonlyStorage.php +++ b/src/Config/ConfigReadonlyStorage.php @@ -8,7 +8,7 @@ use Drupal\Core\Cache\CacheBackendInterface; use Drupal\Core\Lock\LockBackendInterface; use Drupal\Core\Site\Settings; -use Drupal\config_readonly\ConfigReadonlyExceptionsServiceProvider; +use Drupal\config_readonly\ConfigReadonlyExceptionsService; use Symfony\Cmf\Component\Routing\RouteObjectInterface; use Symfony\Component\HttpFoundation\RequestStack; reverted: --- b/src/ConfigReadonlyExceptionsServiceProvider.php +++ /dev/null @@ -1,249 +0,0 @@ -configFactory = $config_factory; - $this->config = $this->configFactory->get('config_readonly.config_readonly_exceptions'); - } - - /** - * Whether or not to throw exceptions. Defaults to true. - * - * In validation we don't want to throw exceptions, we instead want to show - * validation errors. - * - * @param bool $bool - * Whether or not throw exceptions. - */ - public function throwExceptions($bool) { - $this->throwExceptions = ($bool ? TRUE : FALSE); - } - - /** - * Get the errors. - * - * If we aren't throwing exceptions, we may want to retrieve any errors - * in for instance parsing the config ignore file. - * - * @return array - * An array of errors. - */ - public function getErrors() { - return $this->errors; - } - - /** - * Set an error or throw exception. - * - * @param string $error - * The error. - */ - protected function setError($error) { - $this->errors[] = $error; - if ($this->throwExceptions) { - throw new ConfigReadonlyDrushParseException($error); - } - } - - /** - * Get exceptions. - * - * @return array - * An array of exceptions. - */ - public function getExceptionPatterns() { - $patterns = []; - $exceptions_type = $this->config->get('exceptions_type'); - switch ($exceptions_type) { - case 'from_manual_config': - $patterns = $this->getPatternsFromManualConfig(); - break; - - case 'from_drush_config_ignore': - $patterns = $this->getPatternsFromDrushConfigIgnore(); - break; - - } - return $patterns; - } - - /** - * Get patterns from config ignore exceptions. - * - * @param string $config_ignore_path - * Potentially specify a path to test otherwise use the path specified - * in the config settings. - * - * @return array - * The patterns. - */ - public function getExceptionsFromDrushConfigIgnore($config_ignore_path = '') { - if (!$config_ignore_path) { - $config_ignore_path = $this->config->get('config_ignore_path'); - } - if ($ignore_file = $config_ignore_path) { - if (!is_file($ignore_file)) { - $this->setError('The drush config ignore file does not appear to exist.'); - return []; - } - if ($string = file_get_contents($ignore_file)) { - $parsed = FALSE; - try { - $parsed = Yaml::decode($string); - } - catch (InvalidDataTypeException $e) { - $this->setError('Unable to parse the drush config ignore file.'); - return []; - } - if (!isset($parsed['ignore']) || !is_array($parsed['ignore'])) { - $this->setError('The drush config ignore file does not contain a valid ignore array.'); - return []; - } - return $parsed['ignore']; - } - } - return []; - } - - /** - * Get exceptions from Drush config-ignore file. - * - * Based off the code from PreviousNext drush cexy / cimy to ensure - * consistency of result. - * - * @return array - * The patterns. - * - * @link https://www.previousnext.com.au/blog/introducing-drush-cmi-tools - * Drush CMI Tools from PreviousNext. - */ - public function getPatternsFromDrushConfigIgnore() { - $patterns = []; - $exceptions = $this->getExceptionsFromDrushConfigIgnore(); - if ($exceptions) { - foreach ($exceptions as $exception) { - $pattern = $this->getSinglePattern($exception); - if ($pattern) { - $patterns[] = $pattern; - } - } - } - return $patterns; - } - - /** - * Get exceptions from manual config settings. - * - * @return array - * The patterns. - */ - protected function getPatternsFromManualConfig() { - $patterns = []; - $exceptions = $this->config->get('exceptions'); - if ($exceptions) { - foreach ($exceptions as $exception) { - $pattern = $this->getSinglePattern($exception); - if ($pattern) { - $patterns[] = $pattern; - } - } - } - return $patterns; - } - - /** - * Get single pattern from an exception. - * - * Based off the code from PreviousNext drush cexy / cimy to ensure - * consistency of result. - * - * @param string $exception - * The raw exception potentially with wildcard. - * - * @return string - * The pattern. - * - * @link https://www.previousnext.com.au/blog/introducing-drush-cmi-tools - * Drush CMI Tools from PreviousNext. - */ - protected function getSinglePattern($exception) { - // Allow for accidental .yml extension. - if (substr($exception, -4) === '.yml') { - $exception = substr($exception, 0, -4); - } - return '/' . str_replace('\*', '(.*)', preg_quote($exception)) . '\.yml/'; - } - - /** - * Check if the given name matches any exception. - * - * @param string $name - * The config name. - * - * @return bool - * Whether or not there is a match. - */ - public function matchesException($name) { - // Allow for accidental .yml extension. - if (substr($name, -4) !== '.yml') { - $name .= '.yml'; - } - - // Check for matches. - $patterns = $this->getExceptionPatterns(); - if ($patterns) { - foreach ($patterns as $pattern) { - if (preg_match($pattern, $name)) { - return TRUE; - } - } - } - return FALSE; - } - -} diff -u b/src/ConfigReadonlyServiceProvider.php b/src/ConfigReadonlyServiceProvider.php --- b/src/ConfigReadonlyServiceProvider.php +++ b/src/ConfigReadonlyServiceProvider.php @@ -30,7 +30,6 @@ new Reference('cache.config'), new Reference('lock'), new Reference('request_stack'), - new Reference('request_stack'), ]); } } diff -u b/src/Form/ConfigReadonlyExceptionsForm.php b/src/Form/ConfigReadonlyExceptionsForm.php --- b/src/Form/ConfigReadonlyExceptionsForm.php +++ b/src/Form/ConfigReadonlyExceptionsForm.php @@ -7,7 +7,7 @@ use Drupal\Core\Config\ConfigFactoryInterface; use Symfony\Component\DependencyInjection\ContainerInterface; use Drupal\Component\Serialization\Yaml; -use Drupal\config_readonly\ConfigReadonlyExceptionsServiceProvider; +use Drupal\config_readonly\ConfigReadonlyExceptionsService; /** * Class ConfigReadonlyExceptionsForm. @@ -24,9 +24,9 @@ protected $yaml; /** - * Drupal\config_readonly\ConfigReadonlyExceptionsServiceProvider definition. + * Drupal\config_readonly\ConfigReadonlyExceptionsService definition. * - * @var \Drupal\config_readonly\ConfigReadonlyExceptionsServiceProvider + * @var \Drupal\config_readonly\ConfigReadonlyExceptionsService */ protected $exceptionsService; @@ -36,7 +36,7 @@ public function __construct( ConfigFactoryInterface $config_factory, Yaml $serialization_yaml, - ConfigReadonlyExceptionsServiceProvider $exceptions_service + ConfigReadonlyExceptionsService $exceptions_service ) { parent::__construct($config_factory); $this->yaml = $serialization_yaml; only in patch2: unchanged: --- /dev/null +++ b/config_readonly.permissions.yml @@ -0,0 +1,4 @@ +administer config readonly: + title: 'Administer Configuration Read-only Exceptions' + description: 'Allow the user to control the read-only exceptions' + restrict access: TRUE only in patch2: unchanged: --- /dev/null +++ b/src/ConfigReadonlyExceptionsService.php @@ -0,0 +1,249 @@ +configFactory = $config_factory; + $this->config = $this->configFactory->get('config_readonly.config_readonly_exceptions'); + } + + /** + * Whether or not to throw exceptions. Defaults to true. + * + * In validation we don't want to throw exceptions, we instead want to show + * validation errors. + * + * @param bool $bool + * Whether or not throw exceptions. + */ + public function throwExceptions($bool) { + $this->throwExceptions = ($bool ? TRUE : FALSE); + } + + /** + * Get the errors. + * + * If we aren't throwing exceptions, we may want to retrieve any errors + * in for instance parsing the config ignore file. + * + * @return array + * An array of errors. + */ + public function getErrors() { + return $this->errors; + } + + /** + * Set an error or throw exception. + * + * @param string $error + * The error. + */ + protected function setError($error) { + $this->errors[] = $error; + if ($this->throwExceptions) { + throw new ConfigReadonlyDrushParseException($error); + } + } + + /** + * Get exceptions. + * + * @return array + * An array of exceptions. + */ + public function getExceptionPatterns() { + $patterns = []; + $exceptions_type = $this->config->get('exceptions_type'); + switch ($exceptions_type) { + case 'from_manual_config': + $patterns = $this->getPatternsFromManualConfig(); + break; + + case 'from_drush_config_ignore': + $patterns = $this->getPatternsFromDrushConfigIgnore(); + break; + + } + return $patterns; + } + + /** + * Get patterns from config ignore exceptions. + * + * @param string $config_ignore_path + * Potentially specify a path to test otherwise use the path specified + * in the config settings. + * + * @return array + * The patterns. + */ + public function getExceptionsFromDrushConfigIgnore($config_ignore_path = '') { + if (!$config_ignore_path) { + $config_ignore_path = $this->config->get('config_ignore_path'); + } + if ($ignore_file = $config_ignore_path) { + if (!is_file($ignore_file)) { + $this->setError('The drush config ignore file does not appear to exist.'); + return []; + } + if ($string = file_get_contents($ignore_file)) { + $parsed = FALSE; + try { + $parsed = Yaml::decode($string); + } + catch (InvalidDataTypeException $e) { + $this->setError('Unable to parse the drush config ignore file.'); + return []; + } + if (!isset($parsed['ignore']) || !is_array($parsed['ignore'])) { + $this->setError('The drush config ignore file does not contain a valid ignore array.'); + return []; + } + return $parsed['ignore']; + } + } + return []; + } + + /** + * Get exceptions from Drush config-ignore file. + * + * Based off the code from PreviousNext drush cexy / cimy to ensure + * consistency of result. + * + * @return array + * The patterns. + * + * @link https://www.previousnext.com.au/blog/introducing-drush-cmi-tools + * Drush CMI Tools from PreviousNext. + */ + public function getPatternsFromDrushConfigIgnore() { + $patterns = []; + $exceptions = $this->getExceptionsFromDrushConfigIgnore(); + if ($exceptions) { + foreach ($exceptions as $exception) { + $pattern = $this->getSinglePattern($exception); + if ($pattern) { + $patterns[] = $pattern; + } + } + } + return $patterns; + } + + /** + * Get exceptions from manual config settings. + * + * @return array + * The patterns. + */ + protected function getPatternsFromManualConfig() { + $patterns = []; + $exceptions = $this->config->get('exceptions'); + if ($exceptions) { + foreach ($exceptions as $exception) { + $pattern = $this->getSinglePattern($exception); + if ($pattern) { + $patterns[] = $pattern; + } + } + } + return $patterns; + } + + /** + * Get single pattern from an exception. + * + * Based off the code from PreviousNext drush cexy / cimy to ensure + * consistency of result. + * + * @param string $exception + * The raw exception potentially with wildcard. + * + * @return string + * The pattern. + * + * @link https://www.previousnext.com.au/blog/introducing-drush-cmi-tools + * Drush CMI Tools from PreviousNext. + */ + protected function getSinglePattern($exception) { + // Allow for accidental .yml extension. + if (substr($exception, -4) === '.yml') { + $exception = substr($exception, 0, -4); + } + return '/' . str_replace('\*', '(.*)', preg_quote($exception)) . '\.yml/'; + } + + /** + * Check if the given name matches any exception. + * + * @param string $name + * The config name. + * + * @return bool + * Whether or not there is a match. + */ + public function matchesException($name) { + // Allow for accidental .yml extension. + if (substr($name, -4) !== '.yml') { + $name .= '.yml'; + } + + // Check for matches. + $patterns = $this->getExceptionPatterns(); + if ($patterns) { + foreach ($patterns as $pattern) { + if (preg_match($pattern, $name)) { + return TRUE; + } + } + } + return FALSE; + } + +}