Joe Topjian avatar Joe Topjian committed b38e251

Initial commit

Comments (0)

Files changed (2)

+Glue
+====
+
+Author: Joe Topjian, joe@topjian.net
+
+Glue is a simple PHP class that maps URLs to classes. The concepts are similar to web.py for Python.
+
+Information on how to use Glue can be found at http://gluephp.com.
+<?php
+
+    /**
+     * glue
+     *
+     * Provides an easy way to map URLs to classes. URLs can be literal
+     * strings or regular expressions.
+     *
+     * When the URLs are processed:
+     *      * deliminators (/) are automatically escaped: (\/)
+     *      * The beginning and end are anchored (^ $)
+     *      * An optional end slash is added (/?)
+     *	    * The i option is added for case-insensitive searches
+     *
+     * Example:
+     *
+     * $urls = array(
+     *     '/' => 'index',
+     *     '/page/(\d+) => 'page'
+     * );
+     *
+     * class page {
+     *      function GET($matches) {
+     *          echo "Your requested page " . $matches[1];
+     *      }
+     * }
+     *
+     * glue::stick($urls);
+     *
+     */
+    class glue {
+
+        /**
+         * stick
+         *
+         * the main static function of the glue class.
+         *
+         * @param   array    	$urls  	    The regex-based url to class mapping
+         * @throws  Exception               Thrown if corresponding class is not found
+         * @throws  Exception               Thrown if no match is found
+         * @throws  BadMethodCallException  Thrown if a corresponding GET,POST is not found
+         *
+         */
+        static function stick ($urls) {
+
+            $method = strtoupper($_SERVER['REQUEST_METHOD']);
+            $path = $_SERVER['REQUEST_URI'];
+
+            $found = false;
+
+            krsort($urls);
+
+            foreach ($urls as $regex => $class) {
+                $regex = str_replace('/', '\/', $regex);
+                $regex = '^' . $regex . '\/?$';
+                if (preg_match("/$regex/i", $path, $matches)) {
+                    $found = true;
+                    if (class_exists($class)) {
+                        $obj = new $class;
+                        if (method_exists($obj, $method)) {
+                            $obj->$method($matches);
+                        } else {
+                            throw new BadMethodCallException("Method, $method, not supported.");
+                        }
+                    } else {
+                        throw new Exception("Class, $class, not found.");
+                    }
+                    break;
+                }
+            }
+            if (!$found) {
+                throw new Exception("URL, $path, not found.");
+            }
+        }
+    }
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.