00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 class CommandsFactory {
00020
00021
00022
00023
00024
00025 public static function create_command($obj, $cmd_name, $params) {
00026 $inst_name = self::get_instance_name($obj);
00027
00028 $key = 'cmdfac#' . $inst_name . '#' . $cmd_name;
00029 $cls = RuntimeCache::get($key, null);
00030 if (is_null($cls)) {
00031 $cls = self::get_command_class($inst_name, $cmd_name);
00032 RuntimeCache::set($key, $cls);
00033 }
00034
00035 if ($cls) {
00036 return new $cls($obj, $params);
00037 }
00038
00039 return false;
00040 }
00041
00042
00043
00044
00045 private static function get_instance_name($obj) {
00046 if ($obj === '') {
00047 return 'app';
00048 }
00049 if ($obj instanceof IActionSource) {
00050
00051 return $obj->get_action_source_name();
00052 }
00053 if (is_string($obj)) {
00054 return $obj;
00055 }
00056 return false;
00057 }
00058
00059
00060
00061
00062 private static function get_command_class($inst_name, $cmd_name) {
00063 $cmd_class_name_fragment = Load::filename_to_classname($cmd_name);
00064
00065 $ret = false;
00066 if (!empty($inst_name)) {
00067 $file = $inst_name . '/' . $cmd_name;
00068 $class = $cmd_class_name_fragment . ucfirst($inst_name) . 'Command';
00069 $ret = self::do_find_command($file, $class);
00070 }
00071
00072 if ($ret === false) {
00073
00074 $file = 'generics/' . $cmd_name;
00075 $class = $cmd_class_name_fragment . 'Command';
00076 $ret = self::do_find_command($file, $class);
00077 }
00078
00079 return $ret;
00080 }
00081
00082
00083
00084
00085 private static function do_find_command($filename, $classname) {
00086 $ret = false;
00087 $ok = class_exists($classname);
00088 if (!$ok) {
00089 Load::classes_in_directory('behaviour/commands/', $filename, 'cmd', false);
00090 }
00091 if ($ok || class_exists($classname)) {
00092 $ret = $classname;
00093 }
00094 return $ret;
00095 }
00096 }