diff --git a/lib/puppet/parser/functions/ensure_prefix.rb b/lib/puppet/parser/functions/ensure_prefix.rb new file mode 100644 index 00000000..631f2704 --- /dev/null +++ b/lib/puppet/parser/functions/ensure_prefix.rb @@ -0,0 +1,56 @@ + +module Puppet::Parser::Functions + newfunction(:ensure_prefix, :type => :rvalue, :doc => <<-EOS +This function ensures a prefix for all elements in an array or the keys in a hash. + +*Examples:* + + ensure_prefix({'a' => 1, 'b' => 2, 'p.c' => 3}, 'p.') + +Will return: + { + 'p.a' => 1, + 'p.b' => 2, + 'p.c' => 3, + } + + ensure_prefix(['a', 'p.b', 'c'], 'p.') + +Will return: + ['p.a', 'p.b', 'p.c'] +EOS + ) do |arguments| + + raise(Puppet::ParseError, "ensure_prefix(): Wrong number of arguments " + + "given (#{arguments.size} for 2)") if arguments.size < 2 + + enumerable = arguments[0] + + unless enumerable.is_a?(Array) or enumerable.is_a?(Hash) + raise Puppet::ParseError, "ensure_prefix(): expected first argument to be an Array or a Hash, got #{enumerable.inspect}" + end + + prefix = arguments[1] if arguments[1] + + if prefix + unless prefix.is_a?(String) + raise Puppet::ParseError, "ensure_prefix(): expected second argument to be a String, got #{prefix.inspect}" + end + end + + if enumerable.is_a?(Array) + # Turn everything into string same as join would do ... + result = enumerable.collect do |i| + i = i.to_s + prefix && !i.start_with?(prefix) ? prefix + i : i + end + else + result = Hash[enumerable.map do |k,v| + k = k.to_s + [ prefix && !k.start_with?(prefix) ? prefix + k : k, v ] + end] + end + + return result + end +end diff --git a/manifests/extension.pp b/manifests/extension.pp index 1a86dece..8032b55c 100644 --- a/manifests/extension.pp +++ b/manifests/extension.pp @@ -33,6 +33,10 @@ # [*settings*] # Nested hash of global config parameters for php.ini # +# [*settings_prefix*] +# Boolean parameter, whether to prefix all setting keys with +# the extension name. Defaults to true. +# define php::extension( $ensure = 'installed', $provider = undef, @@ -43,6 +47,7 @@ $compiler_packages = $::php::params::compiler_packages, $zend = false, $settings = {}, + $settings_prefix = true, ) { if $caller_module_name != $module_name { @@ -110,23 +115,36 @@ $lowercase_title = downcase($title) + # Ensure "." prefix is present in setting keys if requested + if $settings_prefix { + $full_settings = ensure_prefix($settings, "${name}.") + } else { + $full_settings = $settings + } + if $provider == 'pecl' { - $real_settings = deep_merge({"${extension_key}"=>"${name}.so"},$settings) + $final_settings = deep_merge( + {"${extension_key}" => "${name}.so"}, + $full_settings + ) } else { # On FreeBSD systems the settings file is required for every module # (regardless of provider) to allow for proper module management. if $::osfamily == 'FreeBSD' { - $real_settings = deep_merge({"${extension_key}"=>"${name}.so"},$settings) + $final_settings = deep_merge( + {"${extension_key}" => "${name}.so"}, + $full_settings + ) } else { - $real_settings = $settings + $final_settings = $full_settings } } ::php::config { $title: file => "${::php::params::config_root_ini}/${lowercase_title}.ini", - config => $real_settings, + config => $final_settings, } # Ubuntu/Debian systems use the mods-available folder. We need to enable diff --git a/spec/defines/extension_spec.rb b/spec/defines/extension_spec.rb index 7722fd5b..2c8a0ed0 100644 --- a/spec/defines/extension_spec.rb +++ b/spec/defines/extension_spec.rb @@ -19,7 +19,7 @@ should contain_php__config('json').with({ :file => '/etc/php5/mods-available/json.ini', :config => { - 'test' => 'foo' + 'json.test' => 'foo' }, }) } @@ -53,7 +53,7 @@ :file => '/etc/php5/mods-available/json.ini', :config => { 'extension' => 'nice_name.so', - 'test' => 'foo' + 'nice_name.test' => 'foo' }, }) }