]> git.mjollnir.org Git - moodle.git/commitdiff
General purpose function to produce a class based on a library
authormoodler <moodler>
Sat, 7 Dec 2002 04:31:23 +0000 (04:31 +0000)
committermoodler <moodler>
Sat, 7 Dec 2002 04:31:23 +0000 (04:31 +0000)
of simple functions.  Can be used to provide a class interface
to Moodle functions.

Thanks to Greg Barnett!

lib/class.moodlelib.php [deleted file]
lib/makeclass.php [new file with mode: 0644]

diff --git a/lib/class.moodlelib.php b/lib/class.moodlelib.php
deleted file mode 100644 (file)
index a3a3e09..0000000
+++ /dev/null
@@ -1,30 +0,0 @@
-<?PHP // $Id$
-
-// This file is not currently used within Moodle - see moodlelib.php
-//
-// It exists to provide a more object-oriented interface to some of 
-// Moodle's main library functions, for use by external programs.
-//
-// This code is based on code from Greg Barnett for Crown College
-
-$moodlelibfile = file("moodlelib.php");
-
-$append = false;
-$moodlelib = "";
-
-foreach ($moodlelibfile as $line) {
-    if (!$append) {
-        if (substr($line, 0, 5) == "<?PHP") {
-            $append = true;
-        }
-    } else {
-        if (substr($line, 0, 2) == "?>") {
-            break;
-        }
-        $moodlelib .= $line;
-    }
-}
-
-eval ("class moodlelib { $moodlelib }");
-
-?>
diff --git a/lib/makeclass.php b/lib/makeclass.php
new file mode 100644 (file)
index 0000000..1cb2db4
--- /dev/null
@@ -0,0 +1,24 @@
+<?PHP // $Id$
+
+// This file is currently optional within Moodle - see config-dist.php
+//
+// It exists to provide a more object-oriented interface to some of 
+// Moodle's main library functions, for use by external programs.
+
+
+function makeClassFromFile($file, $classname) {
+    # sanity checks
+    assert('is_file($file)');
+    assert('!class_exists($classname)');
+
+    # Load the file into an array, strip out php tags at beginning and end,
+    # This assumes that the php start and end tags are each on one line at the
+    # beginning and end of the file, and the rest of the file consists only of
+    # comments and functions.
+    $functions = file($file);
+    $functions = array_slice($functions, 1, -1);
+    $functions = join('', $functions);
+
+    eval ("class $classname { $functions }");
+}
+