| 1 | 
joko | 
1.1 | 
<? | 
| 2 | 
  | 
  | 
// --------------------------------------------------------------------------- | 
| 3 | 
joko | 
1.7 | 
//  $Id: Object.php,v 1.6 2003/02/28 04:18:13 joko Exp $ | 
| 4 | 
joko | 
1.1 | 
// --------------------------------------------------------------------------- | 
| 5 | 
joko | 
1.2 | 
//  $Log: Object.php,v $ | 
| 6 | 
joko | 
1.7 | 
//  Revision 1.6  2003/02/28 04:18:13  joko | 
| 7 | 
  | 
  | 
//  + special-case-handling for single argument pass-in | 
| 8 | 
  | 
  | 
//  + testing with constructor autocalling: not used for now since weird sideeffects occoured! | 
| 9 | 
  | 
  | 
//     o however: rethink it completely! | 
| 10 | 
  | 
  | 
// | 
| 11 | 
joko | 
1.6 | 
//  Revision 1.5  2003/02/27 16:36:02  joko | 
| 12 | 
  | 
  | 
//  re-allowed underscores ('_') in php classnames | 
| 13 | 
  | 
  | 
//  does this break something? | 
| 14 | 
  | 
  | 
// | 
| 15 | 
joko | 
1.5 | 
//  Revision 1.4  2003/02/22 16:34:16  joko | 
| 16 | 
  | 
  | 
//  + minor fix: can seperate namespace identifier with slashes ('/') now | 
| 17 | 
  | 
  | 
// | 
| 18 | 
joko | 
1.4 | 
//  Revision 1.3  2003/02/09 17:16:06  joko | 
| 19 | 
  | 
  | 
//  + handling arguments perl style ;-) | 
| 20 | 
  | 
  | 
// | 
| 21 | 
joko | 
1.3 | 
//  Revision 1.2  2003/02/03 05:01:48  joko | 
| 22 | 
  | 
  | 
//  + now attributes can get passed in to the constructors | 
| 23 | 
  | 
  | 
// | 
| 24 | 
joko | 
1.2 | 
//  Revision 1.1  2003/02/03 03:33:48  joko | 
| 25 | 
  | 
  | 
//  + initial commit | 
| 26 | 
  | 
  | 
// | 
| 27 | 
joko | 
1.1 | 
// --------------------------------------------------------------------------- | 
| 28 | 
  | 
  | 
 | 
| 29 | 
  | 
  | 
 | 
| 30 | 
  | 
  | 
/* | 
| 31 | 
  | 
  | 
class DesignPattern_Object { | 
| 32 | 
  | 
  | 
  function DesignPattern_Object() { | 
| 33 | 
  | 
  | 
     | 
| 34 | 
  | 
  | 
  } | 
| 35 | 
  | 
  | 
} | 
| 36 | 
  | 
  | 
*/ | 
| 37 | 
  | 
  | 
 | 
| 38 | 
  | 
  | 
function _ns2file($nsName) { | 
| 39 | 
  | 
  | 
  if ($filename  = str_replace('::', '/', $nsName)) { | 
| 40 | 
  | 
  | 
    return $filename; | 
| 41 | 
  | 
  | 
  } | 
| 42 | 
  | 
  | 
} | 
| 43 | 
  | 
  | 
 | 
| 44 | 
joko | 
1.7 | 
function &mkObject() { | 
| 45 | 
joko | 
1.3 | 
  $arg_list = func_get_args(); | 
| 46 | 
joko | 
1.6 | 
   | 
| 47 | 
  | 
  | 
  // patch arglist if all arguments are passed in as a single array | 
| 48 | 
  | 
  | 
  if (count($arg_list) == 1 && is_array($arg_list[0])) { | 
| 49 | 
  | 
  | 
    $arg_list = $arg_list[0]; | 
| 50 | 
  | 
  | 
  } | 
| 51 | 
  | 
  | 
   | 
| 52 | 
joko | 
1.3 | 
//print Dumper($arg_list); | 
| 53 | 
  | 
  | 
  $namespacedClassname = array_shift($arg_list); | 
| 54 | 
  | 
  | 
  if (strstr($namespacedClassname, '_')) { | 
| 55 | 
joko | 
1.5 | 
    //print "mkObject: unallowed character in namespaced classname: '_' ($namespacedClassname)<br/>"; | 
| 56 | 
  | 
  | 
    //return; | 
| 57 | 
joko | 
1.3 | 
  } | 
| 58 | 
  | 
  | 
  //print "class: $classname<br>"; | 
| 59 | 
  | 
  | 
  $attributes = $arg_list; | 
| 60 | 
joko | 
1.1 | 
  $classname = $namespacedClassname; | 
| 61 | 
  | 
  | 
  if (loadModule($namespacedClassname)) { | 
| 62 | 
  | 
  | 
    $classname = str_replace('::', '_', $namespacedClassname); | 
| 63 | 
joko | 
1.4 | 
    $classname = str_replace('/', '_', $classname); | 
| 64 | 
joko | 
1.1 | 
  } | 
| 65 | 
joko | 
1.2 | 
  $obj = new DesignPattern_Bridge($classname, $attributes); | 
| 66 | 
joko | 
1.6 | 
 | 
| 67 | 
  | 
  | 
  // call constructor if possible | 
| 68 | 
  | 
  | 
  // FIXME: take care for side-effects!!! | 
| 69 | 
  | 
  | 
  /* | 
| 70 | 
  | 
  | 
  if (method_exists($obj, 'constructor')) { | 
| 71 | 
  | 
  | 
    $obj->constructor(); | 
| 72 | 
  | 
  | 
  } | 
| 73 | 
  | 
  | 
  */ | 
| 74 | 
  | 
  | 
 | 
| 75 | 
joko | 
1.1 | 
  return $obj; | 
| 76 | 
  | 
  | 
} | 
| 77 | 
  | 
  | 
 | 
| 78 | 
joko | 
1.7 | 
function loadModule($namespacedClassname, $extension = 'php') { | 
| 79 | 
joko | 
1.1 | 
  if ($filename = _ns2file($namespacedClassname)) { | 
| 80 | 
joko | 
1.7 | 
    if ($extension) { | 
| 81 | 
  | 
  | 
      $filename .= ".$extension"; | 
| 82 | 
  | 
  | 
    } | 
| 83 | 
  | 
  | 
 | 
| 84 | 
  | 
  | 
    // V1: just require | 
| 85 | 
  | 
  | 
      //require_once($filename); | 
| 86 | 
  | 
  | 
 | 
| 87 | 
  | 
  | 
    // V2: check file for existance (didn't work!!! problem with path-style conversion regarding platform running on?) | 
| 88 | 
  | 
  | 
      /* | 
| 89 | 
  | 
  | 
      if (file_exists($filename)) { | 
| 90 | 
  | 
  | 
        require_once($filename); | 
| 91 | 
  | 
  | 
      } else { | 
| 92 | 
  | 
  | 
        //user_error("Could not load module '$namespacedClassname', file '$filename' does not exist."); | 
| 93 | 
  | 
  | 
      } | 
| 94 | 
  | 
  | 
      */ | 
| 95 | 
  | 
  | 
 | 
| 96 | 
  | 
  | 
    // V3: wrapped through eval (weird) | 
| 97 | 
  | 
  | 
      /* | 
| 98 | 
  | 
  | 
      $evalstring = "return include_once('$filename');"; | 
| 99 | 
  | 
  | 
      $result = eval($evalstring); | 
| 100 | 
  | 
  | 
      if (!$result) { | 
| 101 | 
  | 
  | 
        user_error("DesignPattern::Object: Could not load module '$namespacedClassname', file '$filename' does not exist."); | 
| 102 | 
  | 
  | 
        return; | 
| 103 | 
  | 
  | 
      } | 
| 104 | 
  | 
  | 
      */ | 
| 105 | 
  | 
  | 
 | 
| 106 | 
  | 
  | 
    // V4: just include | 
| 107 | 
  | 
  | 
      if (!include_once($filename)) { | 
| 108 | 
  | 
  | 
        // TODO: do stack-backtracing here to determine function called two or three levels before!!! | 
| 109 | 
  | 
  | 
        user_error("DesignPattern::Object: Could not load module '$namespacedClassname', file '$filename' does not exist."); | 
| 110 | 
  | 
  | 
        return; | 
| 111 | 
  | 
  | 
      } | 
| 112 | 
  | 
  | 
 | 
| 113 | 
joko | 
1.1 | 
    return 1; | 
| 114 | 
  | 
  | 
  } | 
| 115 | 
  | 
  | 
} | 
| 116 | 
  | 
  | 
 | 
| 117 | 
  | 
  | 
?> |