Skip to content

Commit

Permalink
Ensure extension setting prefix
Browse files Browse the repository at this point in the history
By default extension setting keys are now prefixed with "<name>.".
If the prefix is already present, nothing is changed.

This allows for simpler extension configuration by simply passing
the setting name without prefix.

Fixes jippi#81
  • Loading branch information
mbrodala committed May 6, 2015
1 parent 5c018b5 commit 5a09c9b
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 6 deletions.
56 changes: 56 additions & 0 deletions lib/puppet/parser/functions/ensure_prefix.rb
Original file line number Diff line number Diff line change
@@ -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
26 changes: 22 additions & 4 deletions manifests/extension.pp
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -43,6 +47,7 @@
$compiler_packages = $::php::params::compiler_packages,
$zend = false,
$settings = {},
$settings_prefix = true,
) {

if $caller_module_name != $module_name {
Expand Down Expand Up @@ -110,23 +115,36 @@

$lowercase_title = downcase($title)

# Ensure "<extension>." 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
Expand Down
4 changes: 2 additions & 2 deletions spec/defines/extension_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
should contain_php__config('json').with({
:file => '/etc/php5/mods-available/json.ini',
:config => {
'test' => 'foo'
'json.test' => 'foo'
},
})
}
Expand Down Expand Up @@ -53,7 +53,7 @@
:file => '/etc/php5/mods-available/json.ini',
:config => {
'extension' => 'nice_name.so',
'test' => 'foo'
'nice_name.test' => 'foo'
},
})
}
Expand Down

0 comments on commit 5a09c9b

Please sign in to comment.