Factory class by Author
Factory class by Author Posted by Alexander Shatalov PHP abstract class Factory { private static $_instances = array(); private static $_libs = array ( 'category' => array('inc/Category.php','Category'), 'content' => array('mod/Content.php','Content'), 'pages' => array('mod/Pages.php','Pages'), 'grid' => array('mod/Grid.php','Grid'), 'menu' => array('inc/Menu.php','Menu'), 'im' => array('inc/IM.php','IM'), 'world' => array('inc/World.php','World'), 'mail' => array('inc/Mail.php','Mail'), 'email' => array('inc/Email.php','Email'), 'hash' => array('inc/lib/Hash.php','Hash'), 'datacontent' => array('mod/DataContent.php','DataContent'), 'image' => array('inc/Image.php','Image'), 'ai' => array('inc/AI.php','AI'), // 'forum' => array('inc/Forum.php','Forum'), 'calendar' => array('inc/Calendar.php','Calendar'), 'poll' => array('mod/Poll.php','Poll'), 'form' => array('inc/Form.php','Form'), 'user' => array('mod/User.php','User'), 'keyword' => array('inc/lib/Keyword.php','Keyword'), 'tree' => array('inc/Tree.php','Tree'), 'basket' => array('inc/Basket.php','Basket'), 'dbfunc' => array('inc/DBfunc.php','DBfunc'), 'spam' => array('inc/Spam.php','Spam'), 'uploadify' => array('inc/Uploadify.php','Uploadify'), 'translate' => array('inc/lib/Translate.php', 'Translate'), 'pdf' => array('inc/lib/dompdf/dompdf_config.inc.php', 'DOMPDF'), 'rssreader' => array('inc/lib/rssreader.php', 'RssReader'), 'zip_extract' => array('inc/lib/PclZip.php', 'PclZip'), 'zip_compress' => array('inc/lib/ZipFile.php', 'ZipFile'), // 'html_dom' => array('inc/lib/simple_html_dom.php', 'simple_html_dom',NULL,true), // 'mailer' => array('inc/lib/PHPMailer.php', 'PHPMailer'), 'pass' => array('inc/lib/PasswordHash.php', 'PasswordHash'), // 'phpexcel' => array('inc/lib/PHPExcel.php', 'PHPExcel', 'inc/lib/'), 'pChart' => array('inc/lib/Image/pChart.php','pChart'), 'pData' => array('inc/lib/Image/pData.php','pData'), 'pCache' => array('inc/lib/Image/pCache.php','pCache'), 'minifier' => array('inc/lib/Minifier.php','Minifier'), 'json' => array('inc/lib/json.php', 'Services_JSON'), 'protectdir' => array('inc/lib/protectdir.php', 'ProtectDir'), // 'simplepie' => array('inc/lib/simplepie.php', 'SimplePie'), // 'snoopy' => array('inc/lib/snoopy.php', 'Snoopy'), // 'browser' => array('inc/lib/Browser.php', 'Browser'), // 'http' => array('inc/lib/HTTP_Connection.php', 'HTTP_Connection'), // 'ftp' => array('inc/lib/FtpConnection.php', 'FtpConnection') ); public static function add($name, $path, $class, $include_path = NULL, $static = false) { self::$_libs[$name] = array($path, $class, $include_path, $static); } public static function get($name = NULL) { return $name ? self::$_libs[$name] : self::$_libs; } public function instance($name) { return self::$_instances[$name]; } public static function call($name, $a1 = NULL, $a2 = NULL, $a3 = NULL, $a4 = NULL, $a5 = NULL) { $new = false; if (substr($name,0,4)=='new:') { $name = substr($name,4); $new = true; } if (isset(self::$_instances[$name]) && !$new) { if (!is_bool(self::$_instances[$name])/* && is_a(self::$_instances[$name], self::$_libs[$name][1])*/) { if ($a1!==NULL) { if (method_exists(self::$_instances[$name],'__construct')) self::$_instances[$name]->__construct($a1,$a2,$a3,$a4,$a5); } return self::$_instances[$name]; } elseif (is_bool(self::$_instances[$name])) { return self::$_libs[$name][1]($a1,$a2,$a3,$a4,$a5); } else { Message::halt('User error','Cannot find function or class',Debug::backTrace()); return false; } } elseif (array_key_exists($name,self::$_libs)) { if (isset(self::$_libs[$name][2]) && self::$_libs[$name][2]) set_include_path(FTP_DIR_ROOT.self::$_libs[$name][2]); else restore_include_path(); require_once str_replace('/',DIRECTORY_SEPARATOR,FTP_DIR_ROOT.self::$_libs[$name][0]); if (!isset(self::$_libs[$name][3]) || !self::$_libs[$name][3]) { if (class_exists(self::$_libs[$name][1])) { self::$_instances[$name] = new self::$_libs[$name][1]($a1,$a2,$a3,$a4,$a5); } elseif (function_exists(self::$_libs[$name][1])) { self::$_instances[$name] = true; return self::$_libs[$name][1]($a1,$a2,$a3,$a4,$a5); } else { Message::halt('User error','Cannot find function or class',Debug::backTrace()); } return self::$_instances[$name]; } else { self::$_instances[$name] = self::$_libs[$name][1]; } } elseif (substr($name,0,1)=='#') { // mods } else { Message::halt('Error','The library <em>'.$name.'</em> is not defined',Debug::backTrace()); } } public static function remove($name) { unset(self::$_instances[$name]); } } ok, that's enough I think.. Sharing this all from encoded core files like crazy |