Ori.livneh has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/164149

Change subject: Move php_ini() from HHVM module to wmflib
......................................................................

Move php_ini() from HHVM module to wmflib

* Move it to wmflib, so it can be used for non-HHVM ini files as well.
* Add documentation.
* Handle arrays per HHVM conventions.

Change-Id: I029425df001394a65787ef19b87ae5cc4e71df8d
---
D modules/hhvm/lib/puppet/parser/functions/php_ini.rb
M modules/wmflib/README.md
A modules/wmflib/lib/puppet/parser/functions/php_ini.rb
3 files changed, 50 insertions(+), 45 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/operations/puppet 
refs/changes/49/164149/1

diff --git a/modules/hhvm/lib/puppet/parser/functions/php_ini.rb 
b/modules/hhvm/lib/puppet/parser/functions/php_ini.rb
deleted file mode 100644
index 98b5903..0000000
--- a/modules/hhvm/lib/puppet/parser/functions/php_ini.rb
+++ /dev/null
@@ -1,45 +0,0 @@
-# == Function: php_ini
-#
-# Serialize a hash into php.ini-style format. Takes one or more hashes
-# as arguments. If the argument list contains more than one hash, they
-# are merged together. In case of duplicate keys, hashes to the right
-# win.
-#
-# === Example
-#
-#   php_ini({'server' => {'port' => 80}}) # => server.port = 80
-#
-
-def flatten_map(map, prefix=nil)
-    map.inject({}) do |flat,(k,v)|
-        k = [prefix, k].compact.join('.')
-        v = v.include?('.') ? Float(v) : Integer(v) rescue v
-        flat.merge! v.is_a?(Hash) ? flatten_map(v, k) : Hash[k, v]
-    end
-end
-
-module Puppet::Parser::Functions
-  newfunction(
-    :php_ini,
-    :type => :rvalue,
-    :doc  => <<-END
-      Serialize a hash into php.ini-style format. Takes one or more hashes
-      as arguments. If the argument list contains more than one hash, they
-      are merged together. In case of duplicate keys, hashes to the right
-      win.
-
-      Example:
-
-         php_ini({'server' => {'port' => 80}}) # => server.port = 80
-
-    END
-  ) do |args|
-    fail 'php_ini() operates on hashes' if args.map(&:class).uniq != [Hash]
-    args.map { |arg| flatten_map(arg) }.
-         inject(:merge).
-         sort.
-         map { |kv| kv.join(' = ') }.
-         push('').
-         join("\n")
-  end
-end
diff --git a/modules/wmflib/README.md b/modules/wmflib/README.md
index f12bb30..882a17a 100644
--- a/modules/wmflib/README.md
+++ b/modules/wmflib/README.md
@@ -129,6 +129,19 @@
     }
 
 
+## php_ini
+
+`php_ini( hash $ini_settings [, hash $... ] )`
+
+Serialize a hash into php.ini-style format. Takes one or more hashes as
+arguments. If the argument list contains more than one hash, they are
+merged together. In case of duplicate keys, hashes to the right win.
+
+### Example
+
+    php_ini({'server' => {'port' => 80}}) # => server.port = 80
+
+
 ## require_package
 
 `require_package( string $package_name [, string $... ] )`
diff --git a/modules/wmflib/lib/puppet/parser/functions/php_ini.rb 
b/modules/wmflib/lib/puppet/parser/functions/php_ini.rb
new file mode 100644
index 0000000..2d0dde3
--- /dev/null
+++ b/modules/wmflib/lib/puppet/parser/functions/php_ini.rb
@@ -0,0 +1,37 @@
+# == Function: php_ini( hash $ini_settings [, hash $... ] )
+#
+# Serialize a hash into php.ini-style format. Takes one or more hashes
+# as arguments. If the argument list contains more than one hash, they
+# are merged together. In case of duplicate keys, hashes to the right
+# win.
+#
+# === Example
+#
+#   php_ini({'server' => {'port' => 80}}) # => server.port = 80
+#
+def ini_flatten(map, prefix = nil)
+  map.reduce({}) do |flat, (k, v)|
+    k = [prefix, k].compact.join('.')
+    flat.merge! v.is_a?(Hash) ? ini_flatten(v, k) : Hash[k, v]
+  end
+end
+
+def ini_cast(v)
+  v.include?('.') ? Float(v) : Integer(v) rescue v
+end
+
+module Puppet::Parser::Functions
+  newfunction(:php_ini, :type => :rvalue, :arity => -2) do |args|
+    fail(ArgumentError, 'php_ini(): hash arguments required')
+      if args.map(&:class).uniq != [Hash]
+    end
+    flat = args.map { |arg| ini_flatten(arg) }.inject(:merge)
+    options = flat.map do |k, vs|
+      case vs
+      when Array then vs.map { |v| "#{k}[*] = #{ini_cast(v)}" }
+      else "#{k} = #{ini_cast(vs)}"
+      end
+    end
+    options.flatten.sort.push('').join("\n")
+  end
+end

-- 
To view, visit https://gerrit.wikimedia.org/r/164149
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I029425df001394a65787ef19b87ae5cc4e71df8d
Gerrit-PatchSet: 1
Gerrit-Project: operations/puppet
Gerrit-Branch: production
Gerrit-Owner: Ori.livneh <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to