JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
add namespacify.sh
authorJason Woofenden <jason@jasonwoof.com>
Thu, 10 Oct 2013 13:01:10 +0000 (09:01 -0400)
committerJason Woofenden <jason@jasonwoof.com>
Thu, 10 Oct 2013 13:03:08 +0000 (09:03 -0400)
This script packs most of wfpl into a single file that works inside the wfpl
namespace. Optionally hacks db_*() functions to use the wordpress database.

namespacify.sh [new file with mode: 0644]
template.php

diff --git a/namespacify.sh b/namespacify.sh
new file mode 100644 (file)
index 0000000..f7bc6f0
--- /dev/null
@@ -0,0 +1,55 @@
+#!/bin/bash
+
+# This script is designed to make it easier to use wfpl in other projects, and
+# not worry about include paths or name collisions.
+#
+# This script packs most of wfpl into a single stream (to stdout) and makes it
+# work inside the \wfpl namespace
+#
+# Run like so:  bash ./namespacify > wfpl.php
+#
+# or:           bash ./namespacify wordpress > wfpl.php
+#
+# The "wordpress" option breaks/obsoletes db_connect and makes db_*() use the
+# wordpress database and automatically prepends the wordpress table name prefix
+#
+# And use in your project like so:
+#
+#     require_once('/path/to/wfpl.php');
+#     $template = new \wfpl\tem();
+#     $records = \wfpl\db_get_assocs('records', 'name,quantity,date');
+
+wfpl_rev=""
+if [ -d .git ]; then
+       wfpl_rev="-$(git rev-parse HEAD)"
+       if ! git diff --quiet; then
+               wfpl_rev="$wfpl_rev-dirty"
+       fi
+fi
+
+echo '<?php'
+echo
+echo '#  Copyright (C) 2005,2013 Jason Woofenden <jason@jasonwoof.com>,'
+echo '#                2008,2009 Joshua Grams <josh@qualdan.com>'
+echo '#'
+echo "#  This file is generated from wfpl$wfpl_rev"
+echo '#'
+# output license
+head -n 16 "template.php" | tail -n +5
+echo
+echo 'namespace wfpl;'
+wordpress_regex=''
+if [ "$1" = 'wordpress' ]; then
+       echo
+       echo '# wordpress tables are supposed to have a prefix.'
+       echo 'function add_wp_table_prefix($table) {'
+       echo '  # only add the wordpress table prefix once, just in case.'
+       echo '  if (substr($table, 0, strlen($GLOBALS['\''wpdb'\'']->prefix)) == $GLOBALS['wpdb']->prefix) {'
+       echo '          return $table;'
+       echo '  } else {'
+       echo '          return $GLOBALS['\''wpdb'\'']->prefix . $table;'
+       echo '  }'
+       echo '}'
+       wordpress_regex='s/function db_\(send_get\|insert_ish\|update\|delete\|\)(.*{/\0\n\t$table = add_wp_table_prefix($table);/;  s/\$GLOBALS\['\''wfpl_db_handle'\''\]/$GLOBALS['\''wpdb'\'']->dbh/g;'
+fi
+cat atexit.php csv.php db.php email.php encode.php file.php file_run.php format.php http.php messages.php misc.php template.php time.php | grep -v -e '<?php' -e '?>' -e '^\s*//' -e 'require_once' -e '^\s*$' | grep -v -P -e '^\s*#\s*((?!NAMESPACIFY).)*$' | sed -e "$wordpress_regex"'s/\(register_shutdown_function\|function_exists\|call_user_func_array\)(/\0'\''\\\\wfpl\\\\'\'' . /;  s/^\(\s*\)# NAMESPACIFY \(.*\)/\1\2 = '\''\\\\wfpl\\\\'\'' . \2;/'
index fc7302b..8e0028b 100644 (file)
@@ -209,10 +209,14 @@ function &tem_row_data($tem, $context)
 
        if(count($tem['args'])) {
                $auto_func = "tem_auto_" . $tem['args'][0];
-               function_exists($auto_func)
-                       or die("ERROR: template auto function '$auto_func' not found.<br>\n");
+               if (!function_exists($auto_func)) {
+                       die("ERROR: template auto function '$auto_func' not found.<br>\n");
+               }
+               # NAMESPACIFY $auto_func
+       }
+       if ($auto_func) {
+               $value = $auto_func($scope['data'][$key], $key, $scope, $tem['args']);
        }
-       if($auto_func) $value = $auto_func($scope['data'][$key], $key, $scope, $tem['args']);
        else $value = $scope['data'][$key];
 
        $rows = tem_data_as_rows($value, $key);
@@ -228,8 +232,12 @@ function tem_encoded_data($tag, $context)
        $value = tem_get_data($key, $context);
        foreach($tag['args'] as $encoding) {
                $func = "enc_$encoding";
-               if(function_exists($func)) $value = $func($value, $key);
-               else die("ERROR: encoder function '$func' not found.<br>\n");
+               if (function_exists($func)) {
+                       # NAMESPACIFY $func
+                       $value = $func($value, $key);
+               } else {
+                       die("ERROR: encoder function '$func' not found.<br>\n");
+               }
        }
        return $value;
 }