Best Mockery code snippet using name.self
php_fast_cache.php
Source:php_fast_cache.php
...56575859 public static function systemInfo() {60 // self::startDebug(self::$sys,"Check Sys",__LINE__,__FUNCTION__);6162 if(count(self::$sys) == 0 ) {6364 // self::startDebug("Start System Info");6566 self::$sys['os'] = self::getOS();6768 self::$sys['errors'] = array();69 self::$sys['storage'] = "";70 self::$sys['method'] = "pdo";71 self::$sys['drivers'] = array(72 "apc" => false,73 "xcache" => false,74 "memcache" => false,75 "memcached" => false,76 "wincache" => false,77 "pdo" => false,78 "mpdo" => false,79 "files" => false,8081 );82838485 // Check apc86 if(extension_loaded('apc') && ini_get('apc.enabled'))87 {88 self::$sys['drivers']['apc'] = true;89 self::$sys['storage'] = "memory";90 self::$sys['method'] = "apc";91 }9293 // Check xcache94 if(extension_loaded('xcache') && function_exists("xcache_get"))95 {96 self::$sys['drivers']['xcache'] = true;97 self::$sys['storage'] = "memory";98 self::$sys['method'] = "xcache";99 }100101 if(extension_loaded('wincache') && function_exists("wincache_ucache_set"))102 {103 self::$sys['drivers']['wincache'] = true;104 self::$sys['storage'] = "memory";105 self::$sys['method'] = "wincache";106 }107108 // Check memcache109 if(function_exists("memcache_connect")) {110 self::$sys['drivers']['memcache'] = true;111112 try {113 memcache_connect("127.0.0.1");114 self::$sys['storage'] = "memory";115 self::$sys['method'] = "memcache";116 } catch (Exception $e) {117118 }119 }120121122 // Check memcached123 if(class_exists("memcached")) {124 self::$sys['drivers']['memcached'] = true;125126 try {127 $memcached = new memcached();128 $memcached->addServer("127.0.0.1","11211");129 self::$sys['storage'] = "memory";130 self::$sys['method'] = "memcached";131132 } catch (Exception $e) {133134 }135 }136137 if(extension_loaded('pdo_sqlite')) {138 self::$sys['drivers']['pdo'] = true;139 self::$sys['drivers']['mpdo'] = true;140 }141142 if(is_writable(self::getPath(true))) {143 self::$sys['drivers']['files'] = true;144 }145146 if(self::$sys['storage'] == "") {147148 if(extension_loaded('pdo_sqlite')) {149 self::$sys['storage'] = "disk";150 self::$sys['method'] = "pdo";151152 } else {153154 self::$sys['storage'] = "disk";155 self::$sys['method'] = "files";156157 }158159 }160161162163 if(self::$sys['storage'] == "disk" && !is_writable(self::getPath())) {164 self::$sys['errors'][] = "Please Create & CHMOD 0777 or any Writeable Mode for ".self::getPath();165 }166167168169170 }171172 // self::startDebug(self::$sys);173 return self::$sys;174 }175176 // return Folder Cache PATH177 // PATH Edit by SecurityKey178 // Auto create, Chmod and Warning179180 // Revision 618181 // PHP_SAPI = apache2handler should go to tmp182 private static function isPHPModule() {183 if(PHP_SAPI == "apache2handler") {184 return true;185 } else {186 if(strpos(PHP_SAPI,"handler") !== false) {187 return true;188 }189 }190 return false;191 }192 // Revision 618193 // Security with .htaccess194 static function htaccessGen($path = "") {195 if(self::$securityHtAccess == true) {196197 if(!file_exists($path."/.htaccess")) {198 // echo "write me";199 $html = "order deny, allow \r\n200deny from all \r\n201allow from 127.0.0.1";202 $f = @fopen($path."/.htaccess","w+");203 @fwrite($f,$html);204 @fclose($f);205 } else {206 // echo "got me";207 }208 }209210 }211212 private static function getPath($skip_create = false) {213214 if (self::$path=='')215 {216 // revision 618217 if(self::isPHPModule()) {218 $tmp_dir = ini_get('upload_tmp_dir') ? ini_get('upload_tmp_dir') : sys_get_temp_dir();219 self::$path = $tmp_dir;220 } else {221 self::$path = dirname(__FILE__);222 }223224 }225226 if($skip_create == false && self::$checked['path'] == false) {227 if(!file_exists(self::$path."/".self::$securityKey."/") || !is_writable(self::$path."/".self::$securityKey."/")) {228 if(!file_exists(self::$path."/".self::$securityKey."/")) {229 @mkdir(self::$path."/".self::$securityKey."/",0777);230 }231 if(!is_writable(self::$path."/".self::$securityKey."/")) {232 @chmod(self::$path."/".self::$securityKey."/",0777);233 }234 if(!file_exists(self::$path."/".self::$securityKey."/") || !is_writable(self::$path."/".self::$securityKey."/")) {235 die("Sorry, Please create ".self::$path."/".self::$securityKey."/ and SET Mode 0777 or any Writable Permission!" );236 }237238 }239240 self::$checked['path'] = true;241 // Revision 618242 self::htaccessGen(self::$path."/".self::$securityKey."/");243244 }245246247248 return self::$path."/".self::$securityKey."/";249250251 }252253 // return method automatic;254 // APC will be TOP, then Memcached, Memcache, PDO and Files255 public static function autoconfig($name = "") {256 // self::startDebug($name,"Check Name",__LINE__,__FUNCTION__);257258 $cache = self::cacheMethod($name);259 if($cache != "" && $cache != self::$storage && $cache!="auto") {260 return $cache;261 }262263 // self::startDebug($cache,"Check Cache",__LINE__,__FUNCTION__);264265 $os = self::getOS();266 // self::startDebug(self::$storage,"User Set",__LINE__,__FUNCTION__);267 if(self::$storage == "" || self::$storage == "auto") {268 // self::startDebug(self::$storage,"User Set Auto",__LINE__,__FUNCTION__);269270 if(extension_loaded('apc') && ini_get('apc.enabled') && strpos(PHP_SAPI,"CGI") === false)271 {272273 self::$sys['drivers']['apc'] = true;274 self::$sys['storage'] = "memory";275 self::$sys['method'] = "apc";276277 // self::startDebug(self::$sys,"GOT APC",__LINE__,__FUNCTION__);278279 }elseif(extension_loaded('xcache'))280 {281 self::$sys['drivers']['xcache'] = true;282 self::$sys['storage'] = "memory";283 self::$sys['method'] = "xcache";284 // self::startDebug(self::$sys,"GOT XCACHE",__LINE__,__FUNCTION__);285286 } else {287 // fix PATH for existing288 $reconfig = false;289 // self::startDebug(self::getPath()."/config.".$os['unique'].".cache.ini","CHECK CONFIG FILE",__LINE__,__FUNCTION__);290291292 if (file_exists(self::getPath()."/config.".$os['unique'].".cache.ini"))293 {294 $info = self::decode(file_get_contents(self::getPath()."/config.".$os['unique'].".cache.ini"));295296 // self::startDebug($info,"CHECK INFO",__LINE__,__FUNCTION__);297298 if(!isset($info['value'])) {299 $reconfig = true;300301 } else {302 $info = $info['value'];303 self::$sys = $info;304305 }306307308 } else {309310 $info = self::systemInfo();311 // self::startDebug($info,"CHECK INFO BY SYSTEM INFO",__LINE__,__FUNCTION__);312 }313314 if(isset($info['os']['unique'])) {315316 if($info['os']['unique'] != $os['unique']) {317 $reconfig = true;318 }319 } else {320 $reconfig = true;321 }322323 if(!file_exists(self::getPath()."/config.".$os['unique'].".cache.ini") || $reconfig == true) {324325 $info = self::systemInfo();326 self::$sys = $info;327 // self::startDebug($info,"Check Info",__LINE__,__FUNCTION__);328329 try {330 $f = fopen(self::getPath()."/config.".$os['unique'].".cache.ini","w+");331 fwrite($f,self::encode($info));332 fclose($f);333334 } catch (Exception $e) {335 die("Please chmod 0777 ".self::getPath()."/config.".$os['unique'].".cache.ini");336 }337 } else {338339 }340341 }342343344345 self::$storage = self::$sys['method'];346347348 } else {349350 if(in_array(self::$storage,array("files","pdo","mpdo"))) {351 self::$sys['storage'] = "disk";352 }elseif(in_array(self::$storage,array("apc","memcache","memcached","wincache","xcache"))) {353 self::$sys['storage'] = "memory";354 } else {355 self::$sys['storage'] = "";356 }357358 if(self::$sys['storage'] == "" || !in_array(self::$storage,self::$supported_api)) {359 die("Don't have this Cache ".self::$storage." In your System! Please double check!");360 }361362 self::$sys['method'] = strtolower(self::$storage);363364 }365366 if(self::$sys['method'] == "files") {367 $last_cleanup = self::files_get("last_cleanup_cache");368 if($last_cleanup == null) {369 self::files_cleanup();370 self::files_set("last_cleanup_cache",@date("U"),3600*self::$files_cleanup_after);371 }372 }373374 // self::startDebug(self::$sys,"Check RETURN SYS",__LINE__,__FUNCTION__);375376 return self::$sys['method'];377378 }379380381382 private static function cacheMethod($name = "") {383 $cache = self::$storage;384 if(is_array($name)) {385 $key = array_keys($name);386 $key = $key[0];387 if(in_array($key,self::$supported_api)) {388 $cache = $key;389 }390 }391 return $cache;392 }393394395 public static function safename($name) {396 return strtolower(preg_replace("/[^a-zA-Z0-9_\s\.]+/","",$name));397 }398399400401402403 private static function encode($value,$time_in_second = "") {404 $value = serialize(array(405 "time" => @date("U"),406 "value" => $value,407 "endin" => $time_in_second408 ));409 return $value;410 }411412 private static function decode($value) {413 $x = @unserialize($value);414 if($x == false) {415 return $value;416 } else {417 return $x;418 }419 }420421 /*422 * Start Public Static423 */424425 public static function cleanup($option = "") {426 $api = self::autoconfig();427 self::$Tmp = array();428429 switch ($api) {430 case "pdo":431 return self::pdo_cleanup($option);432 break;433 case "mpdo":434 return self::pdo_cleanup($option);435 break;436 case "files":437 return self::files_cleanup($option);438 break;439 case "memcache":440 return self::memcache_cleanup($option);441 break;442 case "memcached":443 return self::memcached_cleanup($option);444 break;445 case "wincache":446 return self::wincache_cleanup($option);447 break;448 case "apc":449 return self::apc_cleanup($option);450 break;451 case "xcache":452 return self::xcache_cleanup($option);453 break;454 default:455 return self::pdo_cleanup($option);456 break;457 }458459 }460461 public static function delete($name = "string|array(db->item)") {462463 $api = self::autoconfig($name);464 if(self::$useTmpCache == true) {465 $tmp_name = md5(serialize($api.$name));466 if(isset(self::$Tmp[$tmp_name])) {467 unset(self::$Tmp[$tmp_name]);468 }469 }470471 switch ($api) {472 case "pdo":473 return self::pdo_delete($name);474 break;475 case "mpdo":476 return self::pdo_delete($name);477 break;478 case "files":479 return self::files_delete($name);480 break;481 case "memcache":482 return self::memcache_delete($name);483 break;484 case "memcached":485 return self::memcached_delete($name);486 break;487 case "wincache":488 return self::wincache_delete($name);489 break;490 case "apc":491 return self::apc_delete($name);492 break;493 case "xcache":494 return self::xcache_delete($name);495 break;496 default:497 return self::pdo_delete($name);498 break;499 }500501 }502503504 public static function exists($name = "string|array(db->item)") {505506 $api = self::autoconfig($name);507 switch ($api) {508 case "pdo":509 return self::pdo_exist($name);510 break;511 case "mpdo":512 return self::pdo_exist($name);513 break;514 case "files":515 return self::files_exist($name);516 break;517 case "memcache":518 return self::memcache_exist($name);519 break;520 case "memcached":521 return self::memcached_exist($name);522 break;523 case "wincache":524 return self::wincache_exist($name);525 break;526 case "apc":527 return self::apc_exist($name);528 break;529 case "xcache":530 return self::xcache_exist($name);531 break;532 default:533 return self::pdo_exist($name);534 break;535 }536537 }538539 public static function deleteMulti($object = array()) {540 $res = array();541 foreach($object as $driver=>$name) {542 if(!is_numeric($driver)) {543 $n = $driver."_".$name;544 $name = array($driver=>$name);545 } else {546 $n = $name;547 }548 $res[$n] = self::delete($name);549 }550 return $res;551552 }553554 public static function setMulti($mname = array(), $time_in_second_for_all = 600, $skip_for_all = false) {555 $res = array();556557 foreach($mname as $object){558 // print_r($object);559560 $keys = array_keys($object);561562 if($keys[0] != "0") {563 $k = $keys[0];564 $name = isset($object[$k]) ? array($k => $object[$k]) : "";565 $n = $k."_".$object[$k];566 $x=0;567 } else {568 $name = isset($object[0]) ? $object[0] : "";569 $x=1;570 $n = $name;571 }572573 $value = isset($object[$x]) ? $object[$x] : "";$x++;574 $time = isset($object[$x]) ? $object[$x] : $time_in_second_for_all;$x++;575 $skip = isset($object[$x]) ? $object[$x] : $skip_for_all;$x++;576577 if($name!="" && $value!="") {578 $res[$n] = self::set($name,$value, $time, $skip);579 }580 // echo "<br> ----- <br>";581582 }583584 return $res;585 }586587588589 public static function set($name,$value,$time_in_second = 600, $skip_if_existing = false) {590 $api = self::autoconfig($name);591 if(self::$useTmpCache == true) {592 $tmp_name = md5(serialize($api.$name));593 self::$Tmp[$tmp_name] = $value;594 }595596 switch ($api) {597 case "pdo":598 return self::pdo_set($name,$value,$time_in_second, $skip_if_existing);599 break;600 case "mpdo":601 return self::pdo_set($name,$value,$time_in_second, $skip_if_existing);602 break;603 case "files":604 return self::files_set($name,$value,$time_in_second, $skip_if_existing);605 break;606 case "memcache":607 return self::memcache_set($name,$value,$time_in_second, $skip_if_existing);608 break;609 case "memcached":610 return self::memcached_set($name,$value,$time_in_second, $skip_if_existing);611 break;612 case "wincache":613 return self::wincache_set($name,$value,$time_in_second, $skip_if_existing);614 break;615 case "apc":616 return self::apc_set($name,$value,$time_in_second, $skip_if_existing);617 break;618 case "xcache":619 return self::xcache_set($name,$value,$time_in_second, $skip_if_existing);620 break;621 default:622 return self::pdo_set($name,$value,$time_in_second, $skip_if_existing);623 break;624 }625626 }627628629630631632633 public static function decrement($name, $step = 1) {634 $api = self::autoconfig($name);635 if(self::$useTmpCache == true) {636 $tmp_name = md5(serialize($api.$name));637 if(isset(self::$Tmp[$tmp_name])) {638 self::$Tmp[$tmp_name] = (Int)self::$Tmp[$tmp_name] - $step;639 } else {640 self::$Tmp[$tmp_name] = $step;641 }642643 }644 switch ($api) {645 case "pdo":646 return self::pdo_decrement($name, $step);647 break;648 case "mpdo":649 return self::pdo_decrement($name, $step);650 break;651 case "files":652 return self::files_decrement($name, $step);653 break;654 case "memcache":655 return self::memcache_decrement($name, $step);656 break;657 case "memcached":658 return self::memcached_decrement($name, $step);659 break;660 case "wincache":661 return self::wincache_decrement($name, $step);662 break;663 case "apc":664 return self::apc_decrement($name, $step);665 break;666 case "xcache":667 return self::xcache_decrement($name, $step);668 break;669 default:670 return self::pdo_decrement($name, $step);671 break;672 }673 }674675676677 public static function get($name) {678 $api = self::autoconfig($name);679 if(self::$useTmpCache == true) {680 $tmp_name = md5(serialize($api.$name));681 if(isset(self::$Tmp[$tmp_name])) {682 return self::$Tmp[$tmp_name];683 }684 }685686 // self::startDebug($api,"API",__LINE__,__FUNCTION__);687688 // for files, check it if NULL and "empty" string689 switch ($api) {690 case "pdo":691 return self::pdo_get($name);692 break;693 case "mpdo":694 return self::pdo_get($name);695696 break;697 case "files":698 return self::files_get($name);699 break;700 case "memcache":701 return self::memcache_get($name);702 break;703 case "memcached":704 return self::memcached_get($name);705 break;706 case "wincache":707 return self::wincache_get($name);708 break;709 case "apc":710 return self::apc_get($name);711 break;712 case "xcache":713 return self::xcache_get($name);714 break;715 default:716 return self::pdo_get($name);717 break;718 }719720 }721722723 public static function getMulti($object = array()) {724 $res = array();725 foreach($object as $driver=>$name) {726 if(!is_numeric($driver)) {727 $n = $driver."_".$name;728 $name = array($driver=>$name);729 } else {730 $n = $name;731 }732 $res[$n] = self::get($name);733 }734 return $res;735736 }737738739740 public static function stats() {741 $api = self::autoconfig();742 switch ($api) {743 case "pdo":744 return self::pdo_stats();745 break;746 case "mpdo":747 return self::pdo_stats();748 break;749 case "files":750 return self::files_stats();751 break;752 case "memcache":753 return self::memcache_stats();754 break;755 case "memcached":756 return self::memcached_stats();757 break;758 case "wincache":759 return self::wincache_stats();760 break;761 case "apc":762 return self::apc_stats();763 break;764 case "xcache":765 return self::xcache_stats();766 break;767 default:768 return self::pdo_stats();769 break;770 }771 }772773 public static function increment($name, $step = 1) {774 $api = self::autoconfig($name);775776 if(self::$useTmpCache == true) {777 $tmp_name = md5(serialize($api.$name));778 if(isset(self::$Tmp[$tmp_name])) {779 self::$Tmp[$tmp_name] = (Int)self::$Tmp[$tmp_name] + $step;780 } else {781 self::$Tmp[$tmp_name] = $step;782 }783784 }785786 switch ($api) {787 case "pdo":788 return self::pdo_increment($name, $step);789 break;790 case "mpdo":791 return self::pdo_increment($name, $step);792 break;793 case "files":794 return self::files_increment($name, $step);795 break;796 case "memcache":797 return self::memcache_increment($name, $step);798 break;799 case "memcached":800 return self::memcached_increment($name, $step);801 break;802 case "wincache":803 return self::wincache_increment($name, $step);804 break;805 case "apc":806 return self::apc_increment($name, $step);807 break;808 case "xcache":809 return self::xcache_increment($name, $step);810 break;811 default:812 return self::pdo_increment($name, $step);813 break;814 }815 }816817818 /*819 * Begin FILES Cache Static820 * Use Files & Folders to cache821 */822823 private static function files_exist($name) {824 $data = self::files_get($name);825 if($data == null) {826 return false;827 } else {828 return true;829 }830 }831832833834 private static function files_set($name,$value,$time_in_second = 600, $skip_if_existing = false) {835836 $db = self::selectDB($name);837 $name = $db['item'];838 $folder = $db['db'];839840 $path = self::getPath();841 $tmp = explode("/",$folder);842 foreach($tmp as $dir) {843 if($dir!="" && $dir !="." && $dir!="..") {844 $path.="/".$dir;845 if(!file_exists($path)) {846 mkdir($path,0777);847 }848 }849 }850851 $file = $path."/".$name.".c.html";852853 $write = true;854 if(file_exists($file)) {855 $data = self::decode(file_get_contents($file));856 if($skip_if_existing == true && ((Int)$data['time'] + (Int)$data['endin'] > @date("U")) ) {857 $write = false;858 }859 }860861 if($write == true ) {862 try {863 $f = fopen($file,"w+");864 fwrite($f,self::encode($value,$time_in_second));865 fclose($f);866 } catch (Exception $e) {867 die("Sorry, can't write cache to file :".$file );868 }869 }870871 return $value;872 }873874 private static function files_get($name) {875 $db = self::selectDB($name);876 $name = $db['item'];877 $folder = $db['db'];878879 $path = self::getPath();880 $tmp = explode("/",$folder);881 foreach($tmp as $dir) {882 if($dir!="" && $dir !="." && $dir!="..") {883 $path.="/".$dir;884 }885 }886887 $file = $path."/".$name.".c.html";888889 if(!file_exists($file)) {890 return null;891 }892893 $data = self::decode(file_get_contents($file));894895 if(!isset($data['time']) || !isset($data['endin']) || !isset($data['value'])) {896 return null;897 }898899 if($data['time'] + $data['endin'] < @date("U")) {900 // exp901 unlink($file);902 return null;903 }904905 return isset($data['value']) ? $data['value'] : null;906 }907908 private static function files_stats($dir = "") {909 $total = array(910 "expired" => 0,911 "size" => 0,912 "files" => 0913 );914 if($dir == "") {915 $dir = self::getPath();916 }917 $d = opendir($dir);918 while($file = readdir($d))919 {920 if($file!="." && $file != "..") {921 $path = $dir."/".$file;922 if(is_dir($path)) {923 $in = self::files_stats($path);924 $total['expired'] = $total['expired'] + $in['expired'];925 $total['size'] = $total['size'] + $in['size'];926 $total['files'] = $total['files'] + $in['files'];927 }928929 elseif(strpos($path,".c.html")!== false) {930 $data = self::decode($path);931 if(isset($data['value']) && isset($data['time']) && isset($data['endin'])) {932 $total['files']++;933 if($data['time'] + $data['endin'] < @date("U")) {934 $total['expired']++;935 }936 $total['size'] = $total['size'] + filesize($path);937 }938 }939 }940941 }942 if($total['size'] > 0) {943 $total['size'] = $total['size']/1024/1024;944 }945 return $total;946 }947948 private static function files_cleanup($dir = "") {949 $total = 0;950 if($dir == "") {951 $dir = self::getPath();952 }953 $d = opendir($dir);954 while($file = readdir($d))955 {956 if($file!="." && $file != "..") {957 $path = $dir."/".$file;958 if(is_dir($path)) {959 $total = $total + self::files_cleanup($path);960 try {961 @unlink($path);962 } catch (Exception $e) {963 // nothing;964 }965 }966 elseif(strpos($path,".c.html")!==false) {967 $data = self::decode($path);968 if(isset($data['value']) && isset($data['time']) && isset($data['endin'])) {969 if((Int)$data['time'] + (Int)$data['endin'] < @date("U")) {970 unlink($path);971 $total++;972 }973 } else {974 unlink($path);975 $total++;976 }977 }978 }979980 }981 return $total;982 }983984 private static function files_delete($name) {985 $db = self::selectDB($name);986 $name = $db['item'];987 $folder = $db['db'];988989 $path = self::getPath();990 $tmp = explode("/",$folder);991 foreach($tmp as $dir) {992 if($dir!="" && $dir !="." && $dir!="..") {993 $path.="/".$dir;994 }995 }996997 $file = $path."/".$name.".c.html";998 if(file_exists($file)) {999 try {1000 unlink($file);1001 return true;1002 } catch(Exception $e) {1003 return false;1004 }10051006 }1007 return true;1008 }10091010 private static function files_increment($name, $step = 1) {1011 $db = self::selectDB($name);1012 $name = $db['item'];1013 $folder = $db['db'];10141015 $path = self::getPath();1016 $tmp = explode("/",$folder);1017 foreach($tmp as $dir) {1018 if($dir!="" && $dir !="." && $dir!="..") {1019 $path.="/".$dir;1020 }1021 }10221023 $file = $path."/".$name.".c.html";1024 if(!file_exists($file)) {1025 self::files_set($name,$step,3600);1026 return $step;1027 }10281029 $data = self::decode(file_get_contents($file));1030 if(isset($data['time']) && isset($data['value']) && isset($data['endin'])) {1031 $data['value'] = $data['value'] + $step;1032 self::files_set($name,$data['value'],$data['endin']);1033 }1034 return $data['value'];1035 }10361037 private static function files_decrement($name, $step = 1) {1038 $db = self::selectDB($name);1039 $name = $db['item'];1040 $folder = $db['db'];10411042 $path = self::getPath();1043 $tmp = explode("/",$folder);1044 foreach($tmp as $dir) {1045 if($dir!="" && $dir !="." && $dir!="..") {1046 $path.="/".$dir;1047 }1048 }10491050 $file = $path."/".$name.".c.html";1051 if(!file_exists($file)) {1052 self::files_set($name,$step,3600);1053 return $step;1054 }10551056 $data = self::decode(file_get_contents($file));1057 if(isset($data['time']) && isset($data['value']) && isset($data['endin'])) {1058 $data['value'] = $data['value'] - $step;1059 self::files_set($name,$data['value'],$data['endin']);1060 }1061 return $data['value'];1062 }10631064 private static function getMemoryName($name) {1065 $db = self::selectDB($name);1066 $name = $db['item'];1067 $folder = $db['db'];1068 $name = $folder."_".$name;10691070 // connect memory server1071 if(self::$sys['method'] == "memcache" || $db['db'] == "memcache") {1072 self::memcache_addserver();1073 }elseif(self::$sys['method'] == "memcached" || $db['db'] == "memcached") {1074 self::memcached_addserver();1075 }elseif(self::$sys['method'] == "wincache") {1076 // init WinCache here10771078 }10791080 return $name;1081 }108210831084 /*1085 * Begin XCache Static1086 * http://xcache.lighttpd.net/wiki/XcacheApi1087 */10881089 private static function xcache_exist($name) {1090 $name = self::getMemoryName($name);1091 if(xcache_isset($name)) {1092 return true;1093 } else {1094 return false;1095 }1096 }109710981099 private static function xcache_set($name,$value,$time_in_second = 600, $skip_if_existing = false) {1100 $name = self::getMemoryName($name);1101 if($skip_if_existing == true) {1102 if(!self::xcache_exist($name)) {1103 return xcache_set($name,$value,$time_in_second);1104 }1105 } else {1106 return xcache_set($name,$value,$time_in_second);1107 }1108 return false;1109 }11101111 private static function xcache_get($name) {11121113 $name = self::getMemoryName($name);11141115 $data = xcache_get($name);11161117 if($data === false || $data == "") {1118 return null;1119 }1120 return $data;11211122 }11231124 private static function xcache_stats() {1125 try {1126 return xcache_list(XC_TYPE_VAR,100);1127 } catch(Exception $e) {1128 return array();1129 }1130 }11311132 private static function xcache_cleanup($option = array()) {1133 xcache_clear_cache(XC_TYPE_VAR);1134 return true;1135 }11361137 private static function xcache_delete($name) {1138 $name = self::getMemoryName($name);1139 return xcache_unset($name);1140 }11411142 private static function xcache_increment($name, $step = 1) {1143 $orgi = $name;1144 $name = self::getMemoryName($name);1145 $ret =xcache_inc($name, $step);1146 if($ret === false) {1147 self::xcache_set($orgi,$step,3600);1148 return $step;1149 } else {1150 return $ret;1151 }1152 }11531154 private static function xcache_decrement($name, $step = 1) {1155 $orgi = $name;1156 $name = self::getMemoryName($name);1157 $ret = xcache_dec($name, $step);1158 if($ret === false) {1159 self::xcache_set($orgi,$step,3600);1160 return $step;1161 } else {1162 return $ret;1163 }1164 }116511661167 /*1168 * Begin APC Static1169 * http://www.php.net/manual/en/ref.apc.php1170 */11711172 private static function apc_exist($name) {1173 $name = self::getMemoryName($name);1174 if(apc_exists($name)) {1175 return true;1176 } else {1177 return false;1178 }1179 }118011811182 private static function apc_set($name,$value,$time_in_second = 600, $skip_if_existing = false) {1183 $name = self::getMemoryName($name);1184 if($skip_if_existing == true) {1185 return apc_add($name,$value,$time_in_second);1186 } else {1187 return apc_store($name,$value,$time_in_second);1188 }1189 }11901191 private static function apc_get($name) {11921193 $name = self::getMemoryName($name);11941195 $data = apc_fetch($name,$bo);11961197 if($bo === false) {1198 return null;1199 }1200 return $data;12011202 }12031204 private static function apc_stats() {1205 try {1206 return apc_cache_info("user");1207 } catch(Exception $e) {1208 return array();1209 }1210 }12111212 private static function apc_cleanup($option = array()) {1213 return apc_clear_cache("user");1214 }12151216 private static function apc_delete($name) {1217 $name = self::getMemoryName($name);1218 return apc_delete($name);1219 }12201221 private static function apc_increment($name, $step = 1) {1222 $orgi = $name;1223 $name = self::getMemoryName($name);1224 $ret = apc_inc($name, $step, $fail);1225 if($ret === false) {1226 self::apc_set($orgi,$step,3600);1227 return $step;1228 } else {1229 return $ret;1230 }1231 }12321233 private static function apc_decrement($name, $step = 1) {1234 $orgi = $name;1235 $name = self::getMemoryName($name);1236 $ret = apc_dec($name, $step, $fail);1237 if($ret === false) {1238 self::apc_set($orgi,$step,3600);1239 return $step;1240 } else {1241 return $ret;1242 }1243 }124412451246 /*1247 * Begin Memcache Static1248 * http://www.php.net/manual/en/class.memcache.php1249 */1250 public static function memcache_addserver() {1251 if(!isset(self::$checked['memcache'])) {1252 self::$checked['memcache'] = array();1253 }12541255 if(self::$objects['memcache'] == "") {1256 self::$objects['memcache'] = new Memcache();12571258 foreach(self::$server as $server) {1259 $name = isset($server[0]) ? $server[0] : "";1260 $port = isset($server[1]) ? $server[1] : 11211;1261 if(!in_array($server, self::$checked['memcache']) && $name !="") {1262 self::$objects['memcache']->addServer($name,$port);1263 self::$checked['memcache'][] = $name;1264 }1265 }12661267 }12681269 }1270127112721273 private static function memcache_exist($name) {1274 $x = self::memcache_get($name);1275 if($x == null) {1276 return false;1277 } else {1278 return true;1279 }1280 }1281128212831284128512861287 private static function memcache_set($name,$value,$time_in_second = 600, $skip_if_existing = false) {1288 $orgi = $name;1289 $name = self::getMemoryName($name);1290 if($skip_if_existing == false) {1291 return self::$objects['memcache']->set($name, $value, false, $time_in_second );1292 } else {1293 return self::$objects['memcache']->add($name, $value, false, $time_in_second );1294 }12951296 }12971298 private static function memcache_get($name) {1299 $name = self::getMemoryName($name);1300 $x = self::$objects['memcache']->get($name);1301 if($x == false) {1302 return null;1303 } else {1304 return $x;1305 }1306 }13071308 private static function memcache_stats() {1309 self::memcache_addserver();1310 return self::$objects['memcache']->getStats();1311 }13121313 private static function memcache_cleanup($option = "") {1314 self::memcache_addserver();1315 self::$objects['memcache']->flush();1316 return true;1317 }13181319 private static function memcache_delete($name) {1320 $name = self::getMemoryName($name);1321 return self::$objects['memcache']->delete($name);1322 }13231324 private static function memcache_increment($name, $step = 1) {1325 $name = self::getMemoryName($name);1326 return self::$objects['memcache']->increment($name, $step);1327 }13281329 private static function memcache_decrement($name, $step = 1) {1330 $name = self::getMemoryName($name);1331 return self::$objects['memcache']->decrement($name, $step);1332 }1333133413351336 /*1337 * Begin Memcached Static1338 */13391340 public static function memcached_addserver() {1341 if(!isset(self::$checked['memcached'])) {1342 self::$checked['memcached'] = array();1343 }13441345 if(self::$objects['memcached'] == "") {1346 self::$objects['memcached'] = new Memcached();13471348 foreach(self::$server as $server) {1349 $name = isset($server[0]) ? $server[0] : "";1350 $port = isset($server[1]) ? $server[1] : 11211;1351 $sharing = isset($server[2]) ? $server[2] : 0;1352 if(!in_array($server, self::$checked['memcached']) && $name !="") {1353 if($sharing >0 ) {1354 self::$objects['memcached']->addServer($name,$port,$sharing);1355 } else {1356 self::$objects['memcached']->addServer($name,$port);1357 }13581359 self::$checked['memcached'][] = $name;1360 }1361 }13621363 }1364 }136513661367 private static function memcached_exist($name) {1368 $x = self::memcached_get($name);1369 if($x == null) {1370 return false;1371 } else {1372 return true;1373 }1374 }1375137613771378 private static function memcached_set($name,$value,$time_in_second = 600, $skip_if_existing = false) {1379 $orgi = $name;1380 $name = self::getMemoryName($name);1381 if($skip_if_existing == false) {1382 return self::$objects['memcached']->set($name, $value, time() + $time_in_second );1383 } else {1384 return self::$objects['memcached']->add($name, $value, time() + $time_in_second );1385 }13861387 }13881389 private static function memcached_get($name) {1390 $name = self::getMemoryName($name);1391 $x = self::$objects['memcached']->get($name);1392 if($x == false) {1393 return null;1394 } else {1395 return $x;1396 }1397 }13981399 private static function memcached_stats() {1400 self::memcached_addserver();1401 return self::$objects['memcached']->getStats();1402 }14031404 private static function memcached_cleanup($option = "") {1405 self::memcached_addserver();1406 self::$objects['memcached']->flush();1407 return true;1408 }14091410 private static function memcached_delete($name) {1411 $name = self::getMemoryName($name);1412 return self::$objects['memcached']->delete($name);1413 }14141415 private static function memcached_increment($name, $step = 1) {1416 $name = self::getMemoryName($name);1417 return self::$objects['memcached']->increment($name, $step);1418 }14191420 private static function memcached_decrement($name, $step = 1) {1421 $name = self::getMemoryName($name);1422 return self::$objects['memcached']->decrement($name, $step);1423 }14241425 /*1426 * Begin WinCache Static1427 */14281429 private static function wincache_exist($name) {1430 $name = self::getMemoryName($name);1431 if(wincache_ucache_exists($name)) {1432 return true;1433 } else {1434 return false;1435 }1436 }14371438143914401441 private static function wincache_set($name,$value,$time_in_second = 600, $skip_if_existing = false) {1442 $orgi = $name;1443 $name = self::getMemoryName($name);1444 if($skip_if_existing == false) {1445 return wincache_ucache_set($name, $value, $time_in_second );1446 } else {1447 return wincache_ucache_add($name, $value, $time_in_second );1448 }14491450 }14511452 private static function wincache_get($name) {1453 $name = self::getMemoryName($name);14541455 $x = wincache_ucache_get($name,$suc);14561457 if($suc == false) {1458 return null;1459 } else {1460 return $x;1461 }1462 }14631464 private static function wincache_stats() {1465 return wincache_scache_info();1466 }14671468 private static function wincache_cleanup($option = "") {1469 wincache_ucache_clear();1470 return true;1471 }14721473 private static function wincache_delete($name) {1474 $name = self::getMemoryName($name);1475 return wincache_ucache_delete($name);1476 }14771478 private static function wincache_increment($name, $step = 1) {1479 $name = self::getMemoryName($name);1480 return wincache_ucache_inc($name, $step);1481 }14821483 private static function wincache_decrement($name, $step = 1) {1484 $name = self::getMemoryName($name);1485 return wincache_ucache_dec($name, $step);1486 }148714881489 /*1490 * Begin PDO Static1491 */14921493 private static function pdo_exist($name) {1494 $db = self::selectDB($name);1495 $name = $db['item'];14961497 $x = self::db(array('db'=>$db['db']))->prepare("SELECT COUNT(*) as `total` FROM ".self::$table." WHERE `name`=:name");14981499 $x->execute(array(1500 ":name" => $name,1501 ));15021503 $row = $x->fetch(PDO::FETCH_ASSOC);1504 if($row['total'] >0 ){1505 return true;1506 } else {1507 return false;1508 }15091510 }151115121513 private static function pdo_cleanup($option = "") {1514 self::db(array("skip_clean" => true))->exec("drop table if exists ".self::$table);1515 self::initDatabase();1516 return true;1517 }15181519 private static function pdo_stats($full = false) {1520 $res = array();1521 if($full == true) {1522 $stm = self::db()->prepare("SELECT * FROM ".self::$table."");1523 $stm->execute();1524 $result = $stm->fetchAll();1525 $res['data'] = $result;1526 }1527 $stm = self::db()->prepare("SELECT COUNT(*) as `total` FROM ".self::$table."");1528 $stm->execute();1529 $result = $stm->fetch();1530 $res['record'] = $result['total'];1531 if(self::$path!="memory") {1532 $res['size'] = filesize(self::getPath()."/".self::$filename);1533 }15341535 return $res;1536 }153715381539 // for PDO return DB name,1540 // For Files, return Dir1541 private static function selectDB($object) {1542 $res = array(1543 'db' => "",1544 'item' => "",1545 );1546 if(is_array($object)) {1547 $key = array_keys($object);1548 $key = $key[0];1549 $res['db'] = $key;1550 $res['item'] = self::safename($object[$key]);1551 } else {1552 $res['item'] = self::safename($object);1553 }15541555 if($res['db'] == "" && self::$sys['method'] == "files") {1556 $res['db'] = "files";1557 }15581559 // for auto database1560 if($res['db'] == "" && self::$storage== "mpdo") {1561 $create_table = false;1562 if(!file_exists('sqlite:'.self::getPath().'/phpfastcache.c')) {1563 $create_table = true;1564 }1565 if(self::$autodb == "") {1566 try {1567 self::$autodb = new PDO('sqlite:'.self::getPath().'/phpfastcache.c');1568 self::$autodb->setAttribute(PDO::ATTR_ERRMODE,1569 PDO::ERRMODE_EXCEPTION);15701571 } catch (PDOexception $e) {1572 die("Please CHMOD 0777 or Writable Permission for ".self::getPath());1573 }15741575 }15761577 if($create_table == true) {1578 self::$autodb->exec('CREATE TABLE IF NOT EXISTS "main"."db" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL UNIQUE , "item" VARCHAR NOT NULL UNIQUE , "dbname" INTEGER NOT NULL )');1579 }15801581 $db = self::$autodb->prepare("SELECT * FROM `db` WHERE `item`=:item");1582 $db->execute(array(1583 ":item" => $res['item'],1584 ));1585 $row = $db->fetch(PDO::FETCH_ASSOC);1586 if(isset($row['dbname'])) {1587 // found key1588 $res['db'] = $row['dbname'];1589 } else {1590 // not key // check filesize1591 if((Int)self::$autosize < 10) {1592 self::$autosize = 10;1593 }1594 // get last key1595 $db = self::$autodb->prepare("SELECT * FROM `db` ORDER BY `id` DESC");1596 $db->execute();1597 $row = $db->fetch(PDO::FETCH_ASSOC);1598 $dbname = isset($row['dbname']) ? $row['dbname'] : 1;1599 $fsize = file_exists(self::getPath()."/".$dbname.".cache") ? filesize(self::getPath()."/".$dbname.".cache") : 0;1600 if($fsize > (1024*1024*(Int)self::$autosize)) {1601 $dbname = (Int)$dbname + 1;1602 }1603 try {1604 $insert = self::$autodb->prepare("INSERT INTO `db` (`item`,`dbname`) VALUES(:item,:dbname)");1605 $insert->execute(array(1606 ":item" => $res['item'],1607 ":dbname" => $dbname1608 ));1609 } catch (PDOexception $e) {1610 die('Database Error - Check A look at self::$autodb->prepare("INSERT INTO ');1611 }16121613 $res['db'] = $dbname;16141615 }1616 }16171618 return $res;16191620 }16211622 private static function pdo_get($name) {1623 $db = self::selectDB($name);1624 $name = $db['item'];1625 // array('db'=>$db['db'])16261627 // self::startDebug($db,"",__LINE__,__FUNCTION__);16281629 $stm = self::db(array('db'=>$db['db']))->prepare("SELECT * FROM ".self::$table." WHERE `name`='".$name."'");1630 $stm->execute();1631 $res = $stm->fetch(PDO::FETCH_ASSOC);16321633 if(!isset($res['value'])) {1634 return null;1635 } elseif((Int)$res['added'] + (Int)$res['endin'] <= (Int)@date("U")) {1636 return null;1637 } else {1638 // decode value on SQL;1639 $data = self::decode($res['value']);1640 // check if VALUE on string encode1641 return isset($data['value']) ? $data['value'] : null;1642 }1643 }16441645 private static function pdo_decrement($name, $step = 1) {1646 $db = self::selectDB($name);1647 $name = $db['item'];1648 // array('db'=>$db['db'])16491650 $int = self::get($name);1651 try {1652 $stm = self::db(array('db'=>$db['db']))->prepare("UPDATE ".self::$table." SET `value`=:new WHERE `name`=:name ");1653 $stm->execute(array(1654 ":new" => self::encode($int - $step),1655 ":name" => $name,1656 ));16571658 } catch (PDOexception $e) {1659 die("Sorry! phpFastCache don't allow this type of value - Name: ".$name." -> Decrement: ".$step);1660 }1661 return $int - $step;16621663 }16641665 private static function pdo_increment($name ,$step = 1) {1666 $db = self::selectDB($name);1667 $name = $db['item'];1668 // array('db'=>$db['db'])16691670 $int = self::get($name);1671 // echo $int."xxx";1672 try {1673 $stm = self::db(array('db'=>$db['db']))->prepare("UPDATE ".self::$table." SET `value`=:new WHERE `name`=:name ");1674 $stm->execute(array(1675 ":new" => self::encode($int + $step),1676 ":name" => $name,1677 ));16781679 } catch (PDOexception $e) {1680 die("Sorry! phpFastCache don't allow this type of value - Name: ".$name." -> Increment: ".$step);1681 }1682 return $int + $step;16831684 }16851686 private static function pdo_delete($name) {1687 $db = self::selectDB($name);1688 $name = $db['item'];16891690 return self::db(array('db'=>$db['db']))->exec("DELETE FROM ".self::$table." WHERE `name`='".$name."'");1691 }16921693 private static function pdo_set($name,$value,$time_in_second = 600, $skip_if_existing = false) {1694 $db = self::selectDB($name);1695 $name = $db['item'];1696 // array('db'=>$db['db'])16971698 if($skip_if_existing == true) {1699 try {1700 $insert = self::db(array('db'=>$db['db']))->prepare("INSERT OR IGNORE INTO ".self::$table." (name,value,added,endin) VALUES(:name,:value,:added,:endin)");1701 try {1702 $value = self::encode($value);1703 } catch(Exception $e) {1704 die("Sorry! phpFastCache don't allow this type of value - Name: ".$name);1705 }17061707 $insert->execute(array(1708 ":name" => $name,1709 ":value" => $value,1710 ":added" => @date("U"),1711 ":endin" => (Int)$time_in_second1712 ));17131714 return true;1715 } catch (PDOexception $e) {1716 return false;1717 }17181719 } else {1720 try {1721 $insert = self::db(array('db'=>$db['db']))->prepare("INSERT OR REPLACE INTO ".self::$table." (name,value,added,endin) VALUES(:name,:value,:added,:endin)");1722 try {1723 $value = self::encode($value);1724 } catch(Exception $e) {1725 die("Sorry! phpFastCache don't allow this type of value - Name: ".$name);1726 }17271728 $insert->execute(array(1729 ":name" => $name,1730 ":value" => $value,1731 ":added" => @date("U"),1732 ":endin" => (Int)$time_in_second1733 ));17341735 return true;1736 } catch (PDOexception $e) {1737 return false;1738 }1739 }1740 }1741174217431744 private static function db($option = array()) {1745 $vacuum = false;1746 $dbname = isset($option['db']) ? $option['db'] : "";1747 $dbname = $dbname != "" ? $dbname : self::$filename;1748 if($dbname!=self::$filename) {1749 $dbname = $dbname.".cache";1750 }1751 // debuging1752 // self::startDebug(self::$storage,"Check Storage",__LINE__,__FUNCTION__);1753 $initDB = false;17541755 if(self::$storage == "pdo") {1756 // start self PDO1757 if(self::$objects['pdo']=="") {17581759 // self::$objects['pdo'] == new PDO("sqlite:".self::$path."/cachedb.sqlite");1760 if(!file_exists(self::getPath()."/".$dbname)) {1761 $initDB = true;1762 } else {1763 if(!is_writable(self::getPath()."/".$dbname)) {1764 @chmod(self::getPath()."/".$dbname,0777);1765 if(!is_writable(self::getPath()."/".$dbname)) {1766 die("Please CHMOD 0777 or any Writable Permission for ".self::getPath()."/".$dbname);1767 }1768 }1769 }1770177117721773 try {1774 self::$objects['pdo'] = new PDO("sqlite:".self::getPath()."/".$dbname);1775 self::$objects['pdo']->setAttribute(PDO::ATTR_ERRMODE,1776 PDO::ERRMODE_EXCEPTION);17771778 if($initDB == true) {1779 self::initDatabase();1780 }17811782 $time = filemtime(self::getPath()."/".$dbname);1783 if($time + (3600*24) < @date("U")) {1784 $vacuum = true;1785 }17861787 // Revision 6191788 // auto Vaccuum() every 48 hours1789 if($vacuum == true) {1790 if(!isset($option['skip_clean'])) {1791 self::$objects['pdo']->exec("DELETE FROM ".self::$table." WHERE (`added` + `endin`) < ".@date("U"));1792 }1793 self::$objects['pdo']->exec('VACUUM');1794 }1795179617971798 } catch (PDOexception $e) {1799 die("Can't connect to caching file ".self::getPath()."/".$dbname);1800 }1801180218031804180518061807 return self::$objects['pdo'];18081809 } else {1810 return self::$objects['pdo'];1811 }1812 // end self pdo18131814 } elseif(self::$storage == "mpdo") {18151816 // start self PDO1817 if(!isset(self::$multiPDO[$dbname])) {1818 // self::$objects['pdo'] == new PDO("sqlite:".self::$path."/cachedb.sqlite");1819 if(self::$path!="memory") {1820 if(!file_exists(self::getPath()."/".$dbname)) {1821 $initDB = true;1822 } else {1823 if(!is_writable(self::getPath()."/".$dbname)) {1824 @chmod(self::getPath()."/".$dbname,0777);1825 if(!is_writable(self::getPath()."/".$dbname)) {1826 die("Please CHMOD 0777 or any Writable Permission for PATH ".self::getPath());1827 }1828 }1829 }1830183118321833 try {1834 self::$multiPDO[$dbname] = new PDO("sqlite:".self::getPath()."/".$dbname);1835 self::$multiPDO[$dbname]->setAttribute(PDO::ATTR_ERRMODE,1836 PDO::ERRMODE_EXCEPTION);18371838 if($initDB == true) {1839 self::initDatabase(self::$multiPDO[$dbname]);1840 }18411842 $time = filemtime(self::getPath()."/".$dbname);1843 if($time + (3600*24) < @date("U")) {1844 $vacuum = true;1845 }18461847 // Revision 6191848 if($vacuum == true) {1849 if(!isset($option['skip_clean'])) {1850 self::$multiPDO[$dbname]->exec("DELETE FROM ".self::$table." WHERE (`added` + `endin`) < ".@date("U"));1851 }1852 self::$multiPDO[$dbname]->exec('VACUUM');1853 }18541855 } catch (PDOexception $e) {1856 // Revision 6191857 die("Can't connect to caching file ".self::getPath()."/".$dbname);1858 }185918601861 }18621863186418651866 return self::$multiPDO[$dbname];18671868 } else {1869 return self::$multiPDO[$dbname];1870 }1871 // end self pdo18721873 }187418751876187718781879 }18801881 private static function initDatabase($object = null) {1882 if($object == null) {1883 self::db(array("skip_clean" => true))->exec('CREATE TABLE IF NOT EXISTS "'.self::$table.'" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL UNIQUE , "name" VARCHAR UNIQUE NOT NULL , "value" BLOB, "added" INTEGER NOT NULL DEFAULT 0, "endin" INTEGER NOT NULL DEFAULT 0)');1884 self::db(array("skip_clean" => true))->exec('CREATE INDEX "lookup" ON "'.self::$table.'" ("added" ASC, "endin" ASC)');1885 self::db(array("skip_clean" => true))->exec('VACUUM');1886 } else {1887 $object->exec('CREATE TABLE IF NOT EXISTS "'.self::$table.'" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL UNIQUE , "name" VARCHAR UNIQUE NOT NULL , "value" BLOB, "added" INTEGER NOT NULL DEFAULT 0, "endin" INTEGER NOT NULL DEFAULT 0)');1888 $object->exec('CREATE INDEX "lookup" ON "'.self::$table.'" ("added" ASC, "endin" ASC)');1889 $object->exec('VACUUM');1890 }1891 }18921893 // send all bugs to my email1894 // you can replace it to your email1895 // maximum 1 email per hour1896 // you can use phpFastCache::bugs($title, $e) in any code1897 public static function bugs($title, $e) {1898 $code = md5("error_".$title);1899 $send = self::get($code);1900 if($send == null) {1901 $to = "khoaofgod@yahoo.com";1902 $subject = "Bugs: ".$title;1903 $message = "Error Serialize:".serialize($e);1904 $from = "root@".$_SERVER['HTTP_HOST'];1905 $headers = "From:" . $from;1906 @mail($to,$subject,$message,$headers);1907 self::set($code,1,3600);1908 }1909 }19101911 // use for debug1912 // public function, you can use phpFastCache::debug($e|array|string) any time in any code1913 public static function debug($e, $exit = false) {1914 echo "<pre>";1915 print_r($e);1916 echo "</pre>";1917 if($exit == true) {1918 exit;1919 }1920 }19211922 public static function startDebug($value,$text = "", $line = __LINE__, $func = __FUNCTION__) {1923 if(self::$debugging == true) {1924 self::$step_debugging++;1925 if(!is_array($value)) {1926 echo "<br>".self::$step_debugging." => ".$line." | ".$func." | ".$text." | ".$value;1927 } else {1928 echo "<br>".self::$step_debugging." => ".$line." | ".$func." | ".$text." | ";1929 print_r($value);1930 }19311932 }1933 }19341935}19361937
...
PhpModelGenerator.php
Source:PhpModelGenerator.php
...26 * @param string $folder27 * @static28 */29 public static function LoadInitialValues($folder){30 self::$Folder = $folder;31 }32 33 /**34 * This function handles the process35 *36 * @static37 */38 public static function Run(){39 foreach(ProjectGenerator::$Entities as $entity){40 $class = (string) $entity['name'];41 self::CreateHeader($class);42 self::CreateSave($class, $entity);43 self::CreateFindById($class, $entity);44 self::CreateFindBy($class, $entity);45 self::CreateFindByMultipleValues($class, $entity);46 self::CreateFindByInnerObjectProperties($class, $entity);47 self::CreateFetchAll($class, $entity);48 self::CreateSearch($class, $entity);49 self::CreateGetCount($entity);50 self::CreateDelete($class, $entity);51 self::GenerateCreateBundleFromArray($class, $entity);52 self::$Code .= '}';53 self::CreateFile($class);54 }55 }56 57 /**58 * Creates the header59 * 60 * @param string $class61 * @static62 */63 public static function CreateHeader($class){64 self::$Code = '<?php'."\n";65 self::$Code .= '/**'."\n";66 self::$Code .= ' * @author'."\t\t".ProjectGenerator::$Author."\n";67 self::$Code .= ' * @version'."\t\t".date("F j, Y"). "\n";68 self::$Code .= ' * @filesource'."\t\t".'/Models/'.$class.'Model.php'."\n";69 self::$Code .= ' */'."\n\n";70 self::$Code .= 'class '.$class.'Model extends AbstractModel {'."\n\n";71 }72 73 /**74 * Creates the Save method75 * 76 * @param string $class77 * @param SimpleXmlElement $entity78 * @static79 */80 public static function CreateSave($class, $entity){81 self::$Code .= "\t".'/**'."\n";82 self::$Code .= "\t".' * Saves the '.$class.' in the Data Base'."\n";83 self::$Code .= "\t".' * '."\n";84 self::$Code .= "\t".' * @param'."\t\t".$class."\t\t".'$'.lcfirst($class)."\n";85 self::$Code .= "\t".' * @static'."\n";86 self::$Code .= "\t".' */'."\n";87 self::$Code .= "\t".'public static function Save(&$'.lcfirst($class).'){'."\n";88 //TODO: Improve this to allow more than one primary key field89 $pkName = (string)$entity->pk[0]->field['name'];90 $xmlPkType = ProjectGenerator::$Entities->xpath('/entities/entity[@name="'.$class.'"]/fields/field[@name="'.$pkName.'"]/@type');91 $pkType = (string) $xmlPkType[0]->type;92 $autoincrement = trim($entity->table['autoincrement']) == 'true';93 self::$Code .= "\t\t".'$id = $'.lcfirst($class).'->get'.ucfirst($pkName).'();'."\n";94 //Prepare the properties, required and non required objects list95 $properties = array();96 $requiredFields = array();97 $nonRequiredObjects = array();98 foreach($entity->fields->field as $field){99 $fieldName = (string)$field['name'];100 //TODO: Improve this to compare with more than one pk field101 if($fieldName != $pkName){102 $fieldType = (string)$field['type'];103 $fieldRequired = trim(strtolower($field['required'])) == 'true';104 switch(strtoupper($fieldType)){105 case 'STRING':106 case 'TEXT':107 array_push($properties, "\t\t\t\t".'"'.lcfirst($fieldName).'" => self::$IsUsingUtf8 ? htmlentities(utf8_decode($'.lcfirst($class).'->get'.ucfirst($fieldName).'()), ENT_COMPAT, self::$Charset, false) : htmlentities($'.lcfirst($class).'->get'.ucfirst($fieldName).'(), ENT_COMPAT, self::$Charset, false)');108 break;109 case 'INT':110 case 'FLOAT':111 array_push($properties, "\t\t\t\t".'"'.lcfirst($fieldName).'" => $'.lcfirst($class).'->get'.ucfirst($fieldName).'()');112 break;113 case 'BOOL':114 array_push($properties, "\t\t\t\t".'"'.lcfirst($fieldName).'" => intval($'.lcfirst($class).'->get'.ucfirst($fieldName).'())');115 break;116 case 'DATE':117 array_push($properties, "\t\t\t\t".'"'.lcfirst($fieldName).'" => Date::ParseDate($'.lcfirst($class).'->get'.ucfirst($fieldName).'())');118 break;119 default:120 $xmlRelatedEntityId = ProjectGenerator::$Entities->xpath('/entities/entity[@name="'.$fieldType.'"]/pk/field/@name');121 $relatedEntityId = (string) $xmlRelatedEntityId[0]->name;122 $fieldName = lcfirst($fieldType) . ucfirst($relatedEntityId);123 if($fieldRequired){124 array_push($properties, "\t\t\t\t".'"'.lcfirst($fieldName).'" => $'.lcfirst($class).'->get'.$fieldType.'()->get'.ucfirst($relatedEntityId).'()'); 125 } else {126 array_push($properties, "\t\t\t\t".'"'.lcfirst($fieldName).'" => null');127 $nonRequiredObjects[$fieldName] = array($fieldType, $relatedEntityId);128 }129 break;130 }131 if($fieldRequired){132 array_push($requiredFields, $fieldName);133 }134 }135 }136 //Adding properties137 self::$Code .= "\t\t".'$properties = array('."\n";138 self::$Code .= implode(",\n", $properties)."\n";139 self::$Code .= "\t\t\t".');'."\n";140 foreach($nonRequiredObjects as $field => $type){141 self::$Code .= "\t\t".'if(is_object($'.lcfirst($class).'->get'.$type[0].'())){'."\n";142 self::$Code .= "\t\t\t".'$properties["'.lcfirst($field).'"] = $'.lcfirst($class).'->get'.$type[0].'()->get'.ucfirst($type[1]).'();'."\n";143 self::$Code .= "\t\t".'}'."\n";144 }145 //Validating146 self::$Code .= "\t\t".'$emptyValues = \'\';'."\n";147 if(($pkType != 'int') || !$autoincrement){148 self::$Code .= "\t\t".'if(empty($id)){'."\n";149 self::$Code .= "\t\t\t".'$emptyValues .= \' '.$pkName.'\';'."\n";150 self::$Code .= "\t\t".'}'."\n";151 }152 foreach($requiredFields as $field){153 self::$Code .= "\t\t".'if(empty($properties["'.$field.'"])){'."\n";154 self::$Code .= "\t\t\t".'$emptyValues .= \' '.$field.'\';'."\n";155 self::$Code .= "\t\t".'}'."\n";156 }157 self::$Code .= "\t\t".'if(empty($emptyValues)){'."\n";158 //Adding queries159 self::$Code .= "\t\t\t".'$query = new Query();'."\n";160 if(($pkType == 'int') && $autoincrement){161 self::$Code .= "\t\t\t".'if(!empty($id) && is_int($id)){'."\n";162 } else {163 self::$Code .= "\t\t\t".'$db'.$class.' = self::FindById($id);'."\n";164 self::$Code .= "\t\t\t".'if(!empty($db'.$class.')){'."\n";165 }166 self::$Code .= "\t\t\t\t".'$query->createUpdate(\''.$entity->table['name'].'\', $properties, \''.$pkName.' = "\'.$id.\'"\', true);'."\n";167 self::$Code .= "\t\t\t\t".'$isExecuted = $query->execute();'."\n";168 self::$Code .= "\t\t\t\t".'if(!$isExecuted){'."\n";169 self::$Code .= "\t\t\t\t\t".'throw new Exception(\'Unable to update '.$class.' "\'.$'.$pkName.'.\'" in database. ('.$class.'Model::save())\');'."\n";170 self::$Code .= "\t\t\t\t".'}'."\n";171 self::$Code .= "\t\t\t".'} else {'."\n";172 if(($pkType != 'int') || ($autoincrement != true)){173 self::$Code .= "\t\t\t\t".'$properties[\''.$pkName.'\'] = $id;'."\n";174 }175 self::$Code .= "\t\t\t\t".'$query->createInsert(\''.$entity->table['name'].'\', $properties, true);'."\n";176 self::$Code .= "\t\t\t\t".'$isExecuted = $query->execute();'."\n";177 if(($pkType == 'int') && ($autoincrement == true)){178 self::$Code .= "\t\t\t\t".'if($isExecuted){'."\n";179 self::$Code .= "\t\t\t\t\t".'//get the last inserted id'."\n";180 self::$Code .= "\t\t\t\t\t".'$query->createSelect(array(\'MAX('.$pkName.') as '.$pkName.'\'), \''.$entity->table['name'].'\');'."\n";181 self::$Code .= "\t\t\t\t\t".'$value = $query->execute();'."\n";182 self::$Code .= "\t\t\t\t\t".'$'.lcfirst($class).'->set'.ucfirst($pkName).'($value[\''.$pkName.'\']);'."\n";183 self::$Code .= "\t\t\t\t".'} else {'."\n";184 self::$Code .= "\t\t\t\t\t".'throw new Exception(\'Unable to insert '.$class.' in database. ('.$class.'Model::save())\');'."\n";185 self::$Code .= "\t\t\t\t".'}'."\n";186 } else {187 self::$Code .= "\t\t\t\t".'if(!$isExecuted){'."\n";188 self::$Code .= "\t\t\t\t\t".'throw new Exception(\'Unable to insert '.$class.' in database. ('.$class.'Model::save())\');'."\n";189 self::$Code .= "\t\t\t\t".'}'."\n";190 }191 self::$Code .= "\t\t\t".'}'."\n";192 self::$Code .= "\t\t".'} else {'."\n";193 self::$Code .= "\t\t\t".'throw new Exception(\'Unable to save '.$class.' with empty required values:\'.$emptyValues.\'. ('.$class.'Model::save())\');'."\n";194 self::$Code .= "\t\t".'}'."\n";195 self::$Code .= "\t\t".'return true;'."\n";196 self::$Code .= "\t".'}'."\n\n";197 }198 199 /**200 * Creates the FindById method201 * 202 * @param string $class203 * @param SimpleXmlElement $entity204 * @static205 */206 public static function CreateFindById($class, $entity){207 self::$Code .= "\t".'/**'."\n";208 self::$Code .= "\t".' * Finds a '.$class.' by id'."\n";209 self::$Code .= "\t".' * '."\n";210 self::$Code .= "\t".' * @param'."\t\t".'int'."\t\t".'$id'."\n";211 self::$Code .= "\t".' * @return'."\t\t".$class."\n";212 self::$Code .= "\t".' * @static'."\n";213 self::$Code .= "\t".' */'."\n";214 self::$Code .= "\t".'public static function FindById($id){'."\n";215 self::$Code .= "\t\t".'$query = new Query();'."\n";216 //TODO: Improve this to allow more than one primary key field217 $pkName = (string)$entity->pk[0]->field['name'];218 self::$Code .= "\t\t".'$query->createSelect(array(\'*\'), \''.$entity->table['name'].'\', array(), \''.$pkName.' = "\'.$id.\'"\');'."\n";219 self::$Code .= "\t\t".'$'.lcfirst($class).'Array = $query->execute();'."\n";220 self::$Code .= "\t\t".'$'.lcfirst($class).' = false;'."\n";221 self::$Code .= "\t\t".'if(!empty($'.lcfirst($class).'Array)){'."\n";222 self::$Code .= "\t\t\t".'$'.lcfirst($class).' = self::CreateObjectFromArray($'.lcfirst($class).'Array);'."\n";223 self::$Code .= "\t\t".'}'."\n";224 self::$Code .= "\t\t".'return $'.lcfirst($class).';'."\n";225 self::$Code .= "\t".'}'."\n\n";226 }227 228 /**229 * Creates the FindBy method230 * 231 * @param string $class232 * @param SimpleXmlElement $entity233 * @static234 */235 public static function CreateFindBy($class, $entity){236 self::$Code .= "\t".'/**'."\n";237 self::$Code .= "\t".' * Finds stored '.ucfirst($entity->table['name']).' by specific values'."\n";238 self::$Code .= "\t".' * '."\n";239 self::$Code .= "\t".' * @param'."\t\t".'array|string'."\t\t".'$params'."\n";240 self::$Code .= "\t".' * @param'."\t\t".'bool'."\t\t\t\t".'$expectsOne'."\n";241 self::$Code .= "\t".' * @return'."\t\t".'array|'.$class."\n";242 self::$Code .= "\t".' * @static'."\n";243 self::$Code .= "\t".' */'."\n";244 self::$Code .= "\t".'public static function FindBy($params, $expectsOne=false){'."\n";245 self::$Code .= "\t\t".'$'.$entity->table['name'].'Array = array();'."\n";246 self::$Code .= "\t\t".'if(is_array($params)){'."\n";247 self::$Code .= "\t\t\t".'$params = self::CheckParams($params);'."\n";248 self::$Code .= "\t\t\t".'$where = \'\';'."\n";249 self::$Code .= "\t\t\t".'if(is_array($params[\'where\'])){'."\n";250 self::$Code .= "\t\t\t\t".'//TODO: Use Query::Make() !!!'."\n";251 self::$Code .= "\t\t\t\t".'$whereArray = array();'."\n";252 self::$Code .= "\t\t\t\t".'foreach ($params[\'where\'] as $key => $value){'."\n";253 self::$Code .= "\t\t\t\t\t".'if(!empty($value)){'."\n";254 self::$Code .= "\t\t\t\t\t\t".'$parsedValue = self::$IsUsingUtf8 ? htmlentities(utf8_decode($value), ENT_COMPAT, self::$Charset, false) : htmlentities($value, ENT_COMPAT, self::$Charset, false);'."\n";255 self::$Code .= "\t\t\t\t\t\t".'array_push($whereArray, $key.\' = "\'.$parsedValue.\'"\');'."\n";256 self::$Code .= "\t\t\t\t\t".'}'."\n";257 self::$Code .= "\t\t\t\t".'}'."\n";258 self::$Code .= "\t\t\t\t".'$where = implode(\' AND \', $whereArray);'."\n";259 self::$Code .= "\t\t\t".'} else {'."\n";260 self::$Code .= "\t\t\t\t".'$where = trim($params[\'where\']);'."\n";261 self::$Code .= "\t\t\t".'}'."\n";262 self::$Code .= "\t\t\t".'$orderBy = array();'."\n";263 self::$Code .= "\t\t\t".'if(!empty($params[\'orderBy\'])){'."\n";264 self::$Code .= "\t\t\t\t".'$orderBy = implode(\',\', $params[\'orderBy\']);'."\n";265 self::$Code .= "\t\t\t".'}'."\n";266 self::$Code .= "\t\t\t".'$limit = \'\';'."\n";267 self::$Code .= "\t\t\t".'if(!empty($params[\'from\'])){'."\n";268 self::$Code .= "\t\t\t\t".'$limit = \'\'.$params[\'from\'];'."\n";269 self::$Code .= "\t\t\t\t".'if(!empty($params[\'amount\'])){'."\n";270 self::$Code .= "\t\t\t\t\t".'$limit .= \', \'.$params[\'amount\'];'."\n";271 self::$Code .= "\t\t\t\t".'} else {'."\n";272 self::$Code .= "\t\t\t\t\t".'$limit .= \', 10\';'."\n";273 self::$Code .= "\t\t\t\t".'}'."\n";274 self::$Code .= "\t\t\t".'}'."\n";275 self::$Code .= "\t\t\t".'$query = new Query();'."\n";276 self::$Code .= "\t\t\t".'$query->createSelect(array(\'*\'), \''.$entity->table['name'].'\', null, $where, $orderBy, $limit);'."\n";277 self::$Code .= "\t\t\t".'$arrayArrays'.$class.' = $query->execute(true);'."\n";278 self::$Code .= "\t\t\t".'if(!empty($arrayArrays'.$class.')){'."\n";279 self::$Code .= "\t\t\t\t".'if($expectsOne){'."\n";280 self::$Code .= "\t\t\t\t\t".'return self::CreateObjectFromArray($arrayArrays'.ucfirst($class).'[0]);'."\n";281 self::$Code .= "\t\t\t\t".'}'."\n";282 self::$Code .= "\t\t\t\t".'foreach($arrayArrays'.$class.' as $array'.ucfirst($class).'){'."\n";283 self::$Code .= "\t\t\t\t\t".'array_push($'.$entity->table['name'].'Array, self::CreateObjectFromArray($array'.ucfirst($class).'));'."\n";284 self::$Code .= "\t\t\t\t".'}'."\n";285 self::$Code .= "\t\t\t".'} elseif($expectsOne){'."\n";286 self::$Code .= "\t\t\t\t".'return false;'."\n";287 self::$Code .= "\t\t\t".'}'."\n";288 self::$Code .= "\t\t".'} else {'."\n";289 self::$Code .= "\t\t\t".'throw new Exception(\'Invalid argument passed, expects param to be Array in '.ucfirst($class).'Model::FindBy()\');'."\n";290 self::$Code .= "\t\t".'}'."\n";291 self::$Code .= "\t\t".'return $'.$entity->table['name'].'Array;'."\n";292 self::$Code .= "\t".'}'."\n\n";293 }294 295 /**296 * Creates the FindByMultipleValues method297 * 298 * @param string $class299 * @param SimpleXmlElement $entity300 * @static301 */302 public static function CreateFindByMultipleValues($class, $entity){303 self::$Code .= "\t".'/**'."\n";304 self::$Code .= "\t".' * Finds stored '.ucfirst($entity->table['name']).' by multiple values of an specific field'."\n";305 self::$Code .= "\t".' * '."\n";306 self::$Code .= "\t".' * @param'."\t\t".'array|string'."\t\t".'$params'."\n";307 self::$Code .= "\t".' * @return'."\t\t".'array|'.$class."\n";308 self::$Code .= "\t".' * @static'."\n";309 self::$Code .= "\t".' */'."\n";310 self::$Code .= "\t".'public static function FindByMultipleValues($params, $expectsOne=false){'."\n";311 self::$Code .= "\t\t".'$'.$entity->table['name'].'Array = array();'."\n";312 self::$Code .= "\t\t".'if(is_array($params)){'."\n";313 self::$Code .= "\t\t\t".'$params = self::CheckParams($params);'."\n";314 self::$Code .= "\t\t\t".'//TODO: Use Query::Make() !!!'."\n";315 self::$Code .= "\t\t\t".'$whereArray = array();'."\n";316 self::$Code .= "\t\t\t".'foreach ($params[\'where\'] as $key => $value){'."\n";317 self::$Code .= "\t\t\t\t".'if(!empty($value) && is_array($value)){'."\n";318 self::$Code .= "\t\t\t\t\t".'array_push($whereArray, $key.\' IN (\'.implode(\', \', $value).\')\');'."\n";319 self::$Code .= "\t\t\t\t".'} else {'."\n";320 self::$Code .= "\t\t\t\t\t".'throw new Exception(\'Invalid param, array expected in '.ucfirst($class).'Model::FindByMultipleValues()\');'."\n";321 self::$Code .= "\t\t\t\t".'}'."\n";322 self::$Code .= "\t\t\t".'}'."\n";323 self::$Code .= "\t\t\t".'$where = implode(\' OR \', $whereArray);'."\n";324 self::$Code .= "\t\t\t".'$orderBy = array();'."\n";325 self::$Code .= "\t\t\t".'if(!empty($params[\'orderBy\'])){'."\n";326 self::$Code .= "\t\t\t\t".'$orderBy = implode(\',\', $params[\'orderBy\']);'."\n";327 self::$Code .= "\t\t\t".'}'."\n";328 self::$Code .= "\t\t\t".'$limit = \'\';'."\n";329 self::$Code .= "\t\t\t".'if(!empty($params[\'from\'])){'."\n";330 self::$Code .= "\t\t\t\t".'$limit = \'\'.$params[\'from\'];'."\n";331 self::$Code .= "\t\t\t\t".'if(!empty($params[\'amount\'])){'."\n";332 self::$Code .= "\t\t\t\t\t".'$limit .= \', \'.$params[\'amount\'];'."\n";333 self::$Code .= "\t\t\t\t".'} else {'."\n";334 self::$Code .= "\t\t\t\t\t".'$limit .= \', 10\';'."\n";335 self::$Code .= "\t\t\t\t".'}'."\n";336 self::$Code .= "\t\t\t".'}'."\n";337 self::$Code .= "\t\t\t".'$query = new Query();'."\n";338 self::$Code .= "\t\t\t".'$query->createSelect(array(\'*\'), \''.$entity->table['name'].'\', null, $where, $orderBy, $limit);'."\n";339 self::$Code .= "\t\t\t".'$arrayArrays'.$class.' = $query->execute(true);'."\n";340 self::$Code .= "\t\t\t".'if(!empty($arrayArrays'.$class.')){'."\n";341 self::$Code .= "\t\t\t\t".'foreach($arrayArrays'.$class.' as $array'.ucfirst($class).'){'."\n";342 self::$Code .= "\t\t\t\t\t".'array_push($'.$entity->table['name'].'Array, self::CreateObjectFromArray($array'.ucfirst($class).'));'."\n";343 self::$Code .= "\t\t\t\t".'}'."\n";344 self::$Code .= "\t\t\t".'}'."\n";345 self::$Code .= "\t\t".'} else {'."\n";346 self::$Code .= "\t\t\t".'throw new Exception(\'Invalid param, array expected in '.ucfirst($class).'Model::FindByMultipleValues()\');'."\n";347 self::$Code .= "\t\t".'}'."\n";348 self::$Code .= "\t\t".'return $'.$entity->table['name'].'Array;'."\n";349 self::$Code .= "\t".'}'."\n\n";350 }351 352 /**353 * Creates methods to find object by inner object properties354 * 355 * @param string $class356 * @param SimpleXmlElement $entity357 * @static358 */359 public static function CreateFindByInnerObjectProperties($class, $entity){360 foreach($entity->fields->field as $field){361 $fieldType = (string)$field['type'];362 //Validates that it is an object363 if((strtoupper($fieldType) != 'STRING') && (strtoupper($fieldType) != 'TEXT') && (strtoupper($fieldType) != 'INT') && (strtoupper($fieldType) != 'FLOAT') && (strtoupper($fieldType) != 'BOOL') && (strtoupper($fieldType) != 'DATE')){364 $xmlRelatedEntityId = ProjectGenerator::$Entities->xpath('/entities/entity[@name="'.$fieldType.'"]/pk/field/@name');365 $relatedEntityId = (string) $xmlRelatedEntityId[0]->name;366 $fieldName = lcfirst($fieldType) . ucfirst($relatedEntityId);367 self::$Code .= "\t".'/**'."\n";368 self::$Code .= "\t".' * Finds stored '.ucfirst($entity->table['name']).' by related '.ucfirst($fieldType).' properties'."\n";369 self::$Code .= "\t".' * '."\n";370 self::$Code .= "\t".' * @param'."\t\t".'array|string'."\t\t".'$params'."\n";371 self::$Code .= "\t".' * @param'."\t\t".'bool'."\t\t\t\t".'$expectsOne'."\n";372 self::$Code .= "\t".' * @return'."\t\t".'array|'.$class."\n";373 self::$Code .= "\t".' * @static'."\n";374 self::$Code .= "\t".' */'."\n";375 self::$Code .= "\t".'public static function FindBy'.ucfirst($fieldType).'Properties($params, $expectsOne=false){'."\n";376 self::$Code .= "\t\t".'$'.$entity->table['name'].'Array = array();'."\n";377 self::$Code .= "\t\t".'if(is_array($params)){'."\n";378 self::$Code .= "\t\t\t".'$params = self::CheckParams($params);'."\n";379 self::$Code .= "\t\t\t".'$selectFields = array('."\n";380 $properties = array();381 foreach($entity->fields->field as $tempField){382 $tempFieldType = (string)$tempField['type'];383 $tempFieldName = (string)$tempField['name'];384 if((strtoupper($tempFieldType) != 'STRING') && (strtoupper($tempFieldType) != 'TEXT') && (strtoupper($tempFieldType) != 'INT') && (strtoupper($tempFieldType) != 'FLOAT') && (strtoupper($tempFieldType) != 'BOOL') && (strtoupper($tempFieldType) != 'DATE')){385 $tempXmlRelatedEntityId = ProjectGenerator::$Entities->xpath('/entities/entity[@name="'.$tempFieldType.'"]/pk/field/@name');386 $tempRelatedEntityId = (string) $tempXmlRelatedEntityId[0]->name;387 $tempFieldName = lcfirst($tempFieldType) . ucfirst($tempRelatedEntityId);388 }389 array_push($properties, lcfirst($tempFieldName));390 }391 self::$Code .= "\t\t\t\t\t'".$entity->table['name'].'.'.implode("',\n\t\t\t\t\t'".$entity->table['name'].'.', $properties)."'\n";392 self::$Code .= "\t\t\t\t".');'."\n";393 $relatedEntity = ProjectGenerator::$Entities->xpath('/entities/entity[@name="'.(string)$field['type'].'"]');394 self::$Code .= "\t\t\t".'$joinArray = array(\''.$relatedEntity[0]->table['name'].'\'=>\''.$relatedEntity[0]->table['name'].'.'.$relatedEntity[0]->pk->field['name'].' = '.$entity->table['name'].'.'.lcfirst($fieldName).'\');'."\n";395 self::$Code .= "\t\t\t".'$whereArray = array();'."\n";396 self::$Code .= "\t\t\t".'foreach ($params[\'where\'] as $key => $value){'."\n";397 self::$Code .= "\t\t\t\t".'if(!empty($value)){'."\n";398 self::$Code .= "\t\t\t\t\t".'$parsedValue = self::$IsUsingUtf8 ? htmlentities(utf8_decode($value), ENT_COMPAT, self::$Charset, false) : htmlentities($value, ENT_COMPAT, self::$Charset, false);'."\n";399 self::$Code .= "\t\t\t\t\t".'array_push($whereArray, \''.$relatedEntity[0]->table['name'].'.\'.$key.\' = "\'.$parsedValue.\'"\');'."\n";400 self::$Code .= "\t\t\t\t".'}'."\n";401 self::$Code .= "\t\t\t".'}'."\n";402 self::$Code .= "\t\t\t".'$where = implode(\' AND \', $whereArray);'."\n";403 self::$Code .= "\t\t\t".'$orderBy = array();'."\n";404 self::$Code .= "\t\t\t".'if(!empty($params[\'orderBy\'])){'."\n";405 self::$Code .= "\t\t\t\t".'$orderBy = implode(\',\', $params[\'orderBy\']);'."\n";406 self::$Code .= "\t\t\t".'}'."\n";407 self::$Code .= "\t\t\t".'$limit = \'\';'."\n";408 self::$Code .= "\t\t\t".'if(!empty($params[\'from\'])){'."\n";409 self::$Code .= "\t\t\t\t".'$limit = \'\'.$params[\'from\'];'."\n";410 self::$Code .= "\t\t\t\t".'if(!empty($params[\'amount\'])){'."\n";411 self::$Code .= "\t\t\t\t\t".'$limit .= \', \'.$params[\'amount\'];'."\n";412 self::$Code .= "\t\t\t\t".'} else {'."\n";413 self::$Code .= "\t\t\t\t\t".'$limit .= \', 10\';'."\n";414 self::$Code .= "\t\t\t\t".'}'."\n";415 self::$Code .= "\t\t\t".'}'."\n";416 self::$Code .= "\t\t\t".'$query = new Query();'."\n";417 self::$Code .= "\t\t\t".'$query->createSelect(array(\'*\'), \''.$entity->table['name'].'\', $joinArray, $where, $orderBy, $limit);'."\n";418 self::$Code .= "\t\t\t".'$arrayArrays'.$class.' = $query->execute(true);'."\n";419 self::$Code .= "\t\t\t".'if(!empty($arrayArrays'.$class.')){'."\n";420 self::$Code .= "\t\t\t\t".'if($expectsOne){'."\n";421 self::$Code .= "\t\t\t\t\t".'return self::CreateObjectFromArray($arrayArrays'.ucfirst($class).'[0]);'."\n";422 self::$Code .= "\t\t\t\t".'}'."\n";423 self::$Code .= "\t\t\t\t".'foreach($arrayArrays'.$class.' as $array'.ucfirst($class).'){'."\n";424 self::$Code .= "\t\t\t\t\t".'array_push($'.$entity->table['name'].'Array, self::CreateObjectFromArray($array'.ucfirst($class).'));'."\n";425 self::$Code .= "\t\t\t\t".'}'."\n";426 self::$Code .= "\t\t\t".'} elseif($expectsOne){'."\n";427 self::$Code .= "\t\t\t\t".'return false;'."\n";428 self::$Code .= "\t\t\t".'}'."\n";429 self::$Code .= "\t\t".'} else {'."\n";430 self::$Code .= "\t\t\t".'throw new Exception(\'Invalid param, array expected in '.ucfirst($class).'Model::FindBy'.ucfirst($fieldType).'Properties()\');'."\n";431 self::$Code .= "\t\t".'}'."\n";432 self::$Code .= "\t\t".'return $'.$entity->table['name'].'Array;'."\n";433 self::$Code .= "\t".'}'."\n\n";434 }435 }436 }437 438 /**439 * Creates the FetchAll method440 * 441 * @param string $class442 * @param SimpleXmlElement $entity443 * @static444 */445 public static function CreateFetchAll($class, $entity){446 self::$Code .= "\t".'/**'."\n";447 self::$Code .= "\t".' * Retrieves all '.ucfirst($entity->table['name']).' stored in the data base'."\n";448 self::$Code .= "\t".' * '."\n";449 self::$Code .= "\t".' * @return'."\t\t".'array|'.$class."\n";450 self::$Code .= "\t".' * @static'."\n";451 self::$Code .= "\t".' */'."\n";452 self::$Code .= "\t".'public static function FetchAll($params=array(\'orderBy\', \'from\', \'amount\')){'."\n";453 self::$Code .= "\t\t".'$'.$entity->table['name'].'Array = array();'."\n";454 self::$Code .= "\t\t".'$params = self::CheckParams($params, self::FetchAll);'."\n";455 self::$Code .= "\t\t".'$orderBy = array();'."\n";456 self::$Code .= "\t\t".'if(!empty($params[\'orderBy\'])){'."\n";457 self::$Code .= "\t\t\t".'$orderBy = implode(\',\', $params[\'orderBy\']);'."\n";458 self::$Code .= "\t\t".'}'."\n";459 self::$Code .= "\t\t".'$limit = \'\';'."\n";460 self::$Code .= "\t\t".'if(!empty($params[\'from\'])){'."\n";461 self::$Code .= "\t\t\t".'$limit = \'\'.$params[\'from\'];'."\n";462 self::$Code .= "\t\t\t".'if(!empty($params[\'amount\'])){'."\n";463 self::$Code .= "\t\t\t\t".'$limit .= \', \'.$params[\'amount\'];'."\n";464 self::$Code .= "\t\t\t".'} else {'."\n";465 self::$Code .= "\t\t\t\t".'$limit .= \', 10\';'."\n";466 self::$Code .= "\t\t\t".'}'."\n";467 self::$Code .= "\t\t".'}'."\n";468 self::$Code .= "\t\t".'$query = new Query();'."\n";469 self::$Code .= "\t\t".'$query->createSelect(array(\'*\'), \''.$entity->table['name'].'\', null, null, $orderBy, $limit);'."\n";470 self::$Code .= "\t\t".'$arrayArrays'.$class.' = $query->execute(true);'."\n";471 self::$Code .= "\t\t".'if(!empty($arrayArrays'.$class.')){'."\n";472 self::$Code .= "\t\t\t".'foreach($arrayArrays'.$class.' as $array'.ucfirst($class).'){'."\n";473 self::$Code .= "\t\t\t\t".'array_push($'.$entity->table['name'].'Array, self::CreateObjectFromArray($array'.ucfirst($class).'));'."\n";474 self::$Code .= "\t\t\t".'}'."\n";475 self::$Code .= "\t\t".'}'."\n";476 self::$Code .= "\t\t".'return $'.$entity->table['name'].'Array;'."\n";477 self::$Code .= "\t".'}'."\n\n";478 }479 480 /**481 * Creates the FetchAll method482 * 483 * @param string $class484 * @param SimpleXmlElement $entity485 * @static486 */487 public static function CreateSearch($class, $entity){488 self::$Code .= "\t".'/**'."\n";489 self::$Code .= "\t".' * Retrieves all '.ucfirst($entity->table['name']).' that matches the search text'."\n";490 self::$Code .= "\t".' * '."\n";491 self::$Code .= "\t".' * @param'."\t\t".'array|string'."\t\t".'$params'."\n";492 self::$Code .= "\t".' * @param'."\t\t".'bool'."\t\t\t\t".'$expectsOne'."\n";493 self::$Code .= "\t".' * @return'."\t\t".'array|'.$class."\n";494 self::$Code .= "\t".' * @static'."\n";495 self::$Code .= "\t".' */'."\n";496 self::$Code .= "\t".'public static function Search($params, $expectsOne=false){'."\n";497 self::$Code .= "\t\t".'$'.$entity->table['name'].'Array = array();'."\n";498 self::$Code .= "\t\t".'if(is_array($params)){'."\n";499 self::$Code .= "\t\t\t".'$params = self::CheckParams($params);'."\n";500 self::$Code .= "\t\t\t".'if(is_array($params[\'where\']) && isset($params[\'where\'][\'text\']) && isset($params[\'where\'][\'fields\'])){'."\n";501 self::$Code .= "\t\t\t\t".'$text = trim($params[\'where\'][\'text\']);'."\n";502 self::$Code .= "\t\t\t\t".'$searchs = array();'."\n";503 self::$Code .= "\t\t\t\t".'foreach($params[\'where\'][\'fields\'] as $field){'."\n";504 self::$Code .= "\t\t\t\t\t".'array_push($searchs, trim($field).\' LIKE "%\'.$text.\'%"\');'."\n";505 self::$Code .= "\t\t\t\t".'}'."\n";506 self::$Code .= "\t\t\t\t".'$where = implode(\' OR \', $searchs);'."\n";507 self::$Code .= "\t\t\t\t".'$orderBy = array();'."\n";508 self::$Code .= "\t\t\t\t".'if(!empty($params[\'orderBy\'])){'."\n";509 self::$Code .= "\t\t\t\t\t".'$orderBy = implode(\',\', $params[\'orderBy\']);'."\n";510 self::$Code .= "\t\t\t\t".'}'."\n";511 self::$Code .= "\t\t\t\t".'$limit = \'\';'."\n";512 self::$Code .= "\t\t\t\t".'if(!empty($params[\'from\'])){'."\n";513 self::$Code .= "\t\t\t\t\t".'$limit = \'\'.$params[\'from\'];'."\n";514 self::$Code .= "\t\t\t\t\t".'if(!empty($params[\'amount\'])){'."\n";515 self::$Code .= "\t\t\t\t\t\t".'$limit .= \', \'.$params[\'amount\'];'."\n";516 self::$Code .= "\t\t\t\t\t".'} else {'."\n";517 self::$Code .= "\t\t\t\t\t\t".'$limit .= \', 10\';'."\n";518 self::$Code .= "\t\t\t\t\t".'}'."\n";519 self::$Code .= "\t\t\t\t".'}'."\n";520 self::$Code .= "\t\t\t\t".'$query = new Query();'."\n";521 self::$Code .= "\t\t\t\t".'$query->createSelect(array(\'*\'), \''.$entity->table['name'].'\', null, $where, $orderBy, $limit);'."\n";522 self::$Code .= "\t\t\t\t".'$arrayArrays'.$class.' = $query->execute(true);'."\n";523 self::$Code .= "\t\t\t\t".'if(!empty($arrayArrays'.$class.')){'."\n";524 self::$Code .= "\t\t\t\t\t".'if($expectsOne){'."\n";525 self::$Code .= "\t\t\t\t\t\t".'return self::CreateObjectFromArray($arrayArrays'.ucfirst($class).'[0]);'."\n";526 self::$Code .= "\t\t\t\t\t".'}'."\n";527 self::$Code .= "\t\t\t\t\t".'foreach($arrayArrays'.$class.' as $array'.ucfirst($class).'){'."\n";528 self::$Code .= "\t\t\t\t\t\t".'array_push($'.$entity->table['name'].'Array, self::CreateObjectFromArray($array'.ucfirst($class).'));'."\n";529 self::$Code .= "\t\t\t\t\t".'}'."\n";530 self::$Code .= "\t\t\t\t".'} elseif($expectsOne){'."\n";531 self::$Code .= "\t\t\t\t\t".'return false;'."\n";532 self::$Code .= "\t\t\t\t".'}'."\n";533 self::$Code .= "\t\t\t".'} else {'."\n";534 self::$Code .= "\t\t\t\t".'throw new Exception(\'Unable to perform search with invalid params. '.ucfirst($class).'Model::Search()\');'."\n";535 self::$Code .= "\t\t\t".'}'."\n";536 self::$Code .= "\t\t".'} else {'."\n";537 self::$Code .= "\t\t\t".'throw new Exception(\'Unable to perform search with invalid params. '.ucfirst($class).'Model::Search()\');'."\n";538 self::$Code .= "\t\t".'}'."\n";539 self::$Code .= "\t\t".'return $'.$entity->table['name'].'Array;'."\n";540 self::$Code .= "\t".'}'."\n\n";541 }542 543 /**544 * Creates the GetCount method545 * 546 * @param SimpleXmlElement $entity547 * @static548 */549 public static function CreateGetCount($entity){550 self::$Code .= "\t".'/**'."\n";551 self::$Code .= "\t".' * Retrieves the number of '.ucfirst($entity->table['name']).' stored in the data base'."\n";552 self::$Code .= "\t".' * '."\n";553 self::$Code .= "\t".' * @return'."\t\t".'int'."\n";554 self::$Code .= "\t".' * @static'."\n";555 self::$Code .= "\t".' */'."\n";556 self::$Code .= "\t".'public static function GetCount(){'."\n";557 self::$Code .= "\t\t".'$query = new Query();'."\n";558 self::$Code .= "\t\t".'$query->push(\'SELECT count(*) as count FROM '.$entity->table['name'].'\');'."\n";559 self::$Code .= "\t\t".'$result = $query->execute();'."\n";560 self::$Code .= "\t\t".'return $result[\'count\'];'."\n";561 self::$Code .= "\t".'}'."\n\n";562 }563 564 /**565 * Creates the Delete method566 * 567 * @param string $class568 * @param SimpleXmlElement $entity569 * @static570 */571 public static function CreateDelete($class, $entity){572 $pkName = (string)$entity->pk[0]->field['name'];573 $xmlPkType = ProjectGenerator::$Entities->xpath('/entities/entity[@name="'.$class.'"]/fields/field[@name="'.$pkName.'"]/@type');574 $pkType = (string) $xmlPkType[0]->type;575 $autoincrement = trim(strtolower($entity->table['autoincrement'])) == 'true';576 self::$Code .= "\t".'/**'."\n";577 self::$Code .= "\t".' * Deletes '.ucfirst($class).' by id'."\n";578 self::$Code .= "\t".' * '."\n";579 self::$Code .= "\t".' * @param'."\t\t".'int'."\t\t".'$id'."\n";580 self::$Code .= "\t".' * @static'."\n";581 self::$Code .= "\t".' */'."\n";582 self::$Code .= "\t".'public static function Delete($id){'."\n";583 self::$Code .= "\t\t".'$query = new Query();'."\n";584 //TODO: Improve this to allow more than one primary key field585 if(($pkType == 'int') && $autoincrement){586 self::$Code .= "\t\t".'$query->createDelete(\''.$entity->table['name'].'\', $id);'."\n";587 } else {588 self::$Code .= "\t\t".'$query->createDelete(\''.$entity->table['name'].'\', array(\''.$pkName.'\'=>$id));'."\n";589 }590 self::$Code .= "\t\t".'return $query->execute();'."\n";591 self::$Code .= "\t".'}'."\n\n";592 }593 594 /**595 * Generates the CreateBundleFromArray method596 * 597 * @param string $class598 * @param SimpleXmlElement $entity599 * @static600 */601 public static function GenerateCreateBundleFromArray($class, $entity){602 self::$Code .= "\t".'/**'."\n";603 self::$Code .= "\t".' * Creates '.ucfirst($class).' object from the basic properties'."\n";604 self::$Code .= "\t".' * '."\n";605 self::$Code .= "\t".' * @param'."\t\t".'array|string'."\t\t".'$properties'."\n";606 self::$Code .= "\t".' * @return'."\t\t".$class."\n";607 self::$Code .= "\t".' * @static'."\n";608 self::$Code .= "\t".' */'."\n";609 self::$Code .= "\t".'public static function CreateObjectFromArray($properties){'."\n";610 //TODO: Improve this to allow more than one primary key field611 $pkName = (string)$entity->pk[0]->field['name'];612 $xmlPkType = ProjectGenerator::$Entities->xpath('/entities/entity[@name="'.$class.'"]/fields/field[@name="'.$pkName.'"]/@type');613 $pkType = (string) $xmlPkType[0]->type;614 $autoincrement = trim($entity->table['autoincrement']) == 'true';615 $requiredFields = array();616 foreach($entity->fields->field as $field){617 $fieldName = (string)$field['name'];618 if(trim(strtolower($field['required'])) == 'true'){619 $fieldType = (string)$field['type'];620 if((strtoupper($fieldType) != 'STRING') && (strtoupper($fieldType) != 'TEXT') && (strtoupper($fieldType) != 'INT') && (strtoupper($fieldType) != 'FLOAT') && (strtoupper($fieldType) != 'BOOL') && (strtoupper($fieldType) != 'DATE')){621 $xmlRelatedEntityId = ProjectGenerator::$Entities->xpath('/entities/entity[@name="'.$fieldType.'"]/pk/field/@name');622 $relatedEntityId = (string) $xmlRelatedEntityId[0]->name;623 $fieldName = lcfirst($fieldType) . ucfirst($relatedEntityId);624 }625 array_push($requiredFields, $fieldName);626 }627 }628 //Validating629 self::$Code .= "\t\t".'$emptyValues = \'\';'."\n";630 foreach($requiredFields as $field){631 self::$Code .= "\t\t".'if(empty($properties["'.$field.'"])){'."\n";632 self::$Code .= "\t\t\t".'$emptyValues .= \' '.$field.'\';'."\n";633 self::$Code .= "\t\t".'}'."\n";634 }635 self::$Code .= "\t\t".'if(empty($emptyValues)){'."\n";636 foreach($entity->fields->field as $field){637 $fieldType = (string)$field['type'];638 $fieldName = (string)$field['name'];639 $fieldRequired = trim(strtolower($field['required'])) == 'true';640 if((strtoupper($fieldType) != 'STRING') && (strtoupper($fieldType) != 'TEXT') && (strtoupper($fieldType) != 'INT') && (strtoupper($fieldType) != 'FLOAT') && (strtoupper($fieldType) != 'BOOL') && (strtoupper($fieldType) != 'DATE')){641 $xmlRelatedEntityId = ProjectGenerator::$Entities->xpath('/entities/entity[@name="'.$fieldType.'"]/pk/field/@name');642 $relatedEntityId = (string) $xmlRelatedEntityId[0]->name;643 $fieldName = lcfirst($fieldType) . ucfirst($relatedEntityId);644 if($fieldRequired){645 self::$Code .= "\t\t\t".'$properties[\''.lcfirst($fieldType).'\'] = '.ucfirst($fieldType).'Model::FindById($properties[\''.$fieldName.'\']);'."\n";646 self::$Code .= "\t\t\t".'if(empty($properties[\''.lcfirst($fieldType).'\'])){'."\n";647 self::$Code .= "\t\t\t\t".'throw new Exception(\'Unable to find the '.ucfirst($fieldType).' for the '.$class.'.('.$class.'Model::CreateObjectFromArray())\');'."\n";648 self::$Code .= "\t\t\t".'}'."\n";649 } else {650 self::$Code .= "\t\t\t".'if(!empty($properties[\''.$fieldName.'\'])){'."\n";651 self::$Code .= "\t\t\t\t".'$properties[\''.lcfirst($fieldType).'\'] = '.ucfirst($fieldType).'Model::FindById($properties[\''.$fieldName.'\']);'."\n";652 self::$Code .= "\t\t\t\t".'if(empty($properties[\''.lcfirst($fieldType).'\'])){'."\n";653 self::$Code .= "\t\t\t\t\t".'throw new Exception(\'Unable to find the '.ucfirst($fieldType).' for the '.$class.'.('.$class.'Model::CreateObjectFromArray())\');'."\n";654 self::$Code .= "\t\t\t\t".'}'."\n";655 self::$Code .= "\t\t\t".'}'."\n";656 }657 }658 }659 self::$Code .= "\t\t\t".'return new '.$class.'($properties);'."\n";660 self::$Code .= "\t\t".'} else {'."\n";661 self::$Code .= "\t\t\t".'throw new Exception(\'Unable to create '.$class.' with empty required values:\'.$emptyValues.\' for '.$class.' "\'.$properties[\''.lcfirst($pkName).'\'].\'". ('.$class.'Model::CreateObjectFromArray())\');'."\n";662 self::$Code .= "\t\t".'}'."\n";663 self::$Code .= "\t".'}'."\n";664 }665 666 /**667 * Creates the .php file668 *669 * @param string $class670 * @static671 */672 public static function CreateFile($class){673 $fileName = self::$Folder.$class.'Model.php';674 $fh = fopen($fileName , 'w') or die("can't open file");675 fwrite($fh, self::$Code);676 fclose($fh);677 if(is_file($fileName)){678 chmod($fileName, 0774);679 }680 echo '<b>File created:</b>'.$fileName.'<br />';681 }682}...
JsClassGenerator.php
Source:JsClassGenerator.php
...26 * @param string $folder27 * @static28 */29 public static function LoadInitialValues($folder){30 self::$Folder = $folder;31 }32 33 /**34 * This function handles the process35 *36 * @static37 */38 public static function Run(){39 foreach(ProjectGenerator::$Entities as $entity){40 $class = (string) $entity['name'];41 self::CreateHeader($class);42 self::CreateProperties($entity);43 self::CreateConstructor($entity);44 self::CreateSetters($class, $entity);45 self::CreateGetters($entity);46 self::CreateConvertToArray($entity);47 self::CreateReturn($entity);48 self::CreateFile($class);49 }50 }51 52 /**53 * Creates the header54 *55 * @param string $class56 * @static57 */58 public static function CreateHeader($class){59 self::$Code = '/**' . "\n";60 self::$Code .= ' * This class represents a ' . $class . "\n";61 self::$Code .= ' *' . "\n";62 self::$Code .= ' * @author' . "\t\t" . ProjectGenerator::$Author . "\n";63 self::$Code .= ' * @version' . "\t\t" . date("F j, Y"). "\n";64 self::$Code .= ' */' . "\n";65 self::$Code .= 'var ' . $class . ' = function(genericObj) {' . "\n\n";66 }67 68 /**69 * Creates the properties70 *71 * @param array $entity72 * @static73 */74 public static function CreateProperties($entity){75 foreach($entity->fields->field as $field){76 $fieldName = (string)$field['name'];77 $fieldType = self::GetJavaScriptType((string)$field['type']);78 self::$Code .= "\t" . '/**' . "\n";79 self::$Code .= "\t" . ' * @var' . "\t\t" . $fieldType . "\n";80 self::$Code .= "\t" . ' */' . "\n";81 switch(strtoupper($fieldType)){82 case 'STRING':83 self::$Code .= "\t" . 'var _' . lcfirst($fieldName) . ' = \'\';' . "\n\n";84 break;85 case 'NUMBER':86 self::$Code .= "\t" . 'var _' . lcfirst($fieldName) . ' = 0;' . "\n\n";87 break;88 case 'BOOLEAN':89 self::$Code .= "\t" . 'var _' . lcfirst($fieldName) . ' = false;' . "\n\n";90 break;91 case 'ARRAY':92 self::$Code .= "\t" . 'var _' . lcfirst($fieldName) . ' = new Array();' . "\n\n";93 break;94 default:95 self::$Code .= "\t" . 'var _' . lcfirst($fieldName) . ' = new ' . $fieldType . '();' . "\n\n";96 }97 }98 }99 100 /**101 * Creates the properties102 *103 * @param array $entity104 * @static105 */106 public static function CreateConstructor($entity){107 self::$Code .= "\t" . '/**' . "\n";108 self::$Code .= "\t" . ' * Constructor' . "\n";109 self::$Code .= "\t" . ' */' . "\n";110 self::$Code .= "\t" . 'var init = function() {' . "\n";111 self::$Code .= "\t\t" . 'if(Validator.IsInstanceOf(\'Object\', genericObj)){' . "\n";112 self::$Code .= "\t\t\t" . '$.each(genericObj, function(property, value){' . "\n";113 self::$Code .= "\t\t\t\t" . "switch (property.toUpperCase()) {" . "\n";114 foreach($entity->fields->field as $field){115 $fieldName = (string)$field['name'];116 self::$Code .= "\t\t\t\t\t" . "case '" . strtoupper($fieldName) . "':" . "\n";117 self::$Code .= "\t\t\t\t\t\t_set" . ucfirst($fieldName) . '(value);' . "\n";118 self::$Code .= "\t\t\t\t\t\t" . 'break;' . "\n";119 }120 self::$Code .= "\t\t\t\t" . "}" . "\n";121 self::$Code .= "\t\t\t" . '});' . "\n";122 self::$Code .= "\t\t" . '}' . "\n";123 self::$Code .= "\t" . '};' . "\n\n";124 }125 126 /**127 * Creates the setter methods128 *129 * @param string $class130 * @param array $entity131 * @static132 */133 public static function CreateSetters($class, $entity){134 foreach($entity->fields->field as $field){135 $fieldName = (string)$field['name'];136 $fieldType = self::GetJavaScriptType((string)$field['type']);137 self::$Code .= "\t" . '/**' . "\n";138 self::$Code .= "\t" . ' * @param' . "\t\t" . $fieldType . "\t\t" . lcfirst($fieldName) . "\n";139 self::$Code .= "\t" . ' */' . "\n";140 self::$Code .= "\t" . 'var _set' . ucfirst($fieldName) . ' = function(' . lcfirst($fieldName) . '){' . "\n";141 switch(strtoupper($fieldType)){142 case 'STRING':143 self::$Code .= "\t\t_" . lcfirst($fieldName) . ' = String(' . lcfirst($fieldName) . ');' . "\n";144 break;145 case 'NUMBER':146 self::$Code .= "\t\t_" . lcfirst($fieldName) . ' = Number(' . lcfirst($fieldName) . ');' . "\n";147 break;148 case 'BOOLEAN':149 self::$Code .= "\t\t_" . lcfirst($fieldName) . ' = Boolean(' . lcfirst($fieldName) . ');' . "\n";150 break;151 case 'ARRAY':152 self::$Code .= "\t\t" . 'if($.isArray(' . lcfirst($fieldName) . ')){' . "\n";153 self::$Code .= "\t\t\t_" . lcfirst($fieldName) . ' = ' . lcfirst($fieldName) . ';' . "\n";154 self::$Code .= "\t\t" . '} else {' . "\n";155 self::$Code .= "\t\t\t" . 'console.error(\'Function expects an array as param. ( '.$class.'.set' . ucfirst($fieldName) . ' ))' . "\n";156 self::$Code .= "\t\t" . '}' . "\n";157 break;158 default:159 self::$Code .= "\t\t" . 'if(Validator.IsInstanceOf(\'Object\', ' . lcfirst($fieldName) . ')){' . "\n";160 self::$Code .= "\t\t\t_" . lcfirst($fieldName) . ' = new ' . ucfirst($fieldName) . '(' . lcfirst($fieldName) . ');' . "\n";161 self::$Code .= "\t\t" . '} else {' . "\n";162 self::$Code .= "\t\t\t" . 'console.error(\'Function expects an object as param. ( '.$class.'.set' . ucfirst($fieldName) . ' )\');' . "\n";163 self::$Code .= "\t\t" . '}' . "\n";164 }165 self::$Code .= "\t" . '};' . "\n\n";166 }167 }168 169 /**170 * Creates the getter methods171 *172 * @param array $entity173 * @static174 */175 public static function CreateGetters($entity){176 foreach($entity->fields->field as $field){177 $fieldName = (string)$field['name'];178 $fieldType = self::GetJavaScriptType((string)$field['type']);179 self::$Code .= "\t" . '/**' . "\n";180 self::$Code .= "\t" . ' * @return' . "\t\t" . $fieldType . "\n";181 self::$Code .= "\t" . ' */' . "\n";182 self::$Code .= "\t" . 'var _get' . ucfirst($fieldName) . ' = function(){' . "\n";183 self::$Code .= "\t\t" . 'return _' . lcfirst($fieldName) . ';' . "\n";184 self::$Code .= "\t" . '};' . "\n\n";185 }186 }187 188 /**189 * Creates the getter methods190 *191 * @param array $entity192 * @static193 */194 public static function CreateConvertToArray($entity){195 self::$Code .= "\t" . '/**' . "\n";196 self::$Code .= "\t" . ' * @return' . "\t\tJSON\n";197 self::$Code .= "\t" . ' */' . "\n";198 self::$Code .= "\t" . 'var _convertToArray = function(){' . "\n";199 self::$Code .= "\t\t" . 'return {' . "\n";200 $tempArray = array();201 foreach($entity->fields->field as $field){202 $fieldName = (string)$field['name'];203 array_push($tempArray, '"'.lcfirst($fieldName).'":_'.lcfirst($fieldName));204 }205 self::$Code .= "\t\t\t\t" . implode(",\n\t\t\t\t", $tempArray). "\n";206 self::$Code .= "\t\t\t" . '};' . "\n";207 self::$Code .= "\t" . '};' . "\n\n";208 }209 210 /**211 * Creates the return statement and the public methods212 *213 * @param array $entity214 * @static215 */216 public static function CreateReturn($entity){217 self::$Code .= "\t" . '/**' . "\n";218 self::$Code .= "\t" . ' * Executes constructor' . "\n";219 self::$Code .= "\t" . ' */' . "\n";220 self::$Code .= "\t" . 'init();' . "\n\n";221 self::$Code .= "\t" . '/**' . "\n";222 self::$Code .= "\t" . ' * Returns public functions' . "\n";223 self::$Code .= "\t" . ' */' . "\n";224 self::$Code .= "\t" . 'return{' . "\n\n";225 foreach($entity->fields->field as $field){226 $fieldName = (string)$field['name'];227 $fieldType = self::GetJavaScriptType((string)$field['type']);228 self::$Code .= "\t\t" . '/**' . "\n";229 self::$Code .= "\t\t" . ' * @param' . "\t\t" . $fieldType . "\t\t" . lcfirst($fieldName) . "\n";230 self::$Code .= "\t\t" . ' */' . "\n";231 self::$Code .= "\t\t" . 'set' . ucfirst($fieldName) . ' : function(' . lcfirst($fieldName) . '){' . "\n";232 self::$Code .= "\t\t\t" . '_set' . ucfirst($fieldName) . '(' . lcfirst($fieldName) . ');' . "\n";233 self::$Code .= "\t\t" . '},' . "\n\n";234 }235 foreach($entity->fields->field as $field){236 $fieldName = (string)$field['name'];237 $fieldType = self::GetJavaScriptType((string)$field['type']);238 self::$Code .= "\t\t" . '/**' . "\n";239 self::$Code .= "\t\t" . ' * @return' . "\t\t" . $fieldType . "\n";240 self::$Code .= "\t\t" . ' */' . "\n";241 self::$Code .= "\t\t" . 'get' . ucfirst($fieldName) . ' : function(){' . "\n";242 self::$Code .= "\t\t\t" . 'return _get' . ucfirst($fieldName) . '();' . "\n";243 self::$Code .= "\t\t" . '},' . "\n\n";244 }245 self::$Code .= "\t\t" . '/**' . "\n";246 self::$Code .= "\t\t" . ' * @return' . "\t\tJSON\n";247 self::$Code .= "\t\t" . ' */' . "\n";248 self::$Code .= "\t\t" . 'convertToArray : function(){' . "\n";249 self::$Code .= "\t\t\t" . 'return _convertToArray();' . "\n";250 self::$Code .= "\t\t" . '}' . "\n";251 self::$Code .= "\t" . '};' . "\n";252 self::$Code .= '};';253 }254 255 /**256 * Creates the .js file257 *258 * @param string $class259 * @static260 */261 public static function CreateFile($class){262 $fileName = self::$Folder . $class . '.js';263 $fh = fopen($fileName , 'w') or die("can't open file");264 fwrite($fh, self::$Code);265 fclose($fh);266 if(is_file($fileName)){267 chmod($fileName, 0775);268 }269 echo '<b>File created:</b>' . $fileName . '<br />';270 }271 272 /**273 * Sets the proper JavaScript type274 *275 * @param string $phpType276 * @return string277 * @static278 */...
self
Using AI Code Generation
1class name{2 public function self(){3 echo "this is self method";4 }5 public function static(){6 echo "this is static method";7 }8}9$name = new name();10$name->self();11$name->static();12class name{13 public function self(){14 echo "this is self method";15 }16 public static function static(){17 echo "this is static method";18 }19}20$name = new name();21$name->self();22$name::static();
self
Using AI Code Generation
1class name{2 public $name;3 public function __construct($name){4 $this->name = $name;5 }6 public function self(){7 return self::class;8 }9}10$object = new name('Amit');11echo $object->self();12class name{13 public $name;14 public function __construct($name){15 $this->name = $name;16 }17 public function self(){18 return self::class;19 }20}21$object = new name('Amit');22echo $object->self();23class name{24 public $name;25 public function __construct($name){26 $this->name = $name;27 }28 public function self(){29 return self::class;30 }31}32$object = new name('Amit');33echo $object->self();34class name{35 public $name;36 public function __construct($name){37 $this->name = $name;38 }39 public function self(){40 return self::class;41 }42}43$object = new name('Amit');44echo $object->self();45class name{46 public $name;47 public function __construct($name){48 $this->name = $name;49 }50 public function self(){51 return self::class;52 }53}54$object = new name('Amit');55echo $object->self();
self
Using AI Code Generation
1class name{2 public $name;3 public function __construct($name){4 $this->name = $name;5 }6 public function self(){7 return $this;8 }9}10$name = new name("Sajjad");11echo $name->self()->name;12class name{13 public $name;14 public function __construct($name){15 $this->name = $name;16 }17 public function self(){18 return $this;19 }20}21$name = new name("Sajjad");22echo $name->self()->name;23class name{24 public $name;25 public function __construct($name){26 $this->name = $name;27 }28 public function self(){29 return $this;30 }31}32$name = new name("Sajjad");33echo $name->self()->name;34class name{35 public $name;36 public function __construct($name){37 $this->name = $name;38 }39 public function self(){40 return $this;41 }42}43$name = new name("Sajjad");44echo $name->self()->name;45class name{46 public $name;47 public function __construct($name){48 $this->name = $name;49 }50 public function self(){51 return $this;52 }53}54$name = new name("Sajjad");55echo $name->self()->name;56class name{57 public $name;58 public function __construct($name){59 $this->name = $name;60 }61 public function self(){62 return $this;63 }64}65$name = new name("Sajjad");66echo $name->self()->name;67class name{68 public $name;69 public function __construct($name){70 $this->name = $name;71 }72 public function self(){73 return $this;74 }75}76$name = new name("Sajjad");
self
Using AI Code Generation
1class name{2 public function name(){3 echo "My name is Asif";4 }5}6$obj = new name();7$obj->name();8class name{9 public function name(){10 echo "My name is Asif";11 }12}13$obj = new name();14$obj->name();15class name{16 public function name(){17 echo "My name is Asif";18 }19}20$obj = new name();21$obj->name();22class name{23 public function name(){24 echo "My name is Asif";25 }26}27$obj = new name();28$obj->name();29class name{30 public function name(){31 echo "My name is Asif";32 }33}34$obj = new name();35$obj->name();36class name{37 public function name(){38 echo "My name is Asif";39 }40}41$obj = new name();42$obj->name();43class name{44 public function name(){45 echo "My name is Asif";46 }47}48$obj = new name();49$obj->name();50class name{51 public function name(){52 echo "My name is Asif";53 }54}55$obj = new name();56$obj->name();57class name{58 public function name(){59 echo "My name is Asif";60 }61}62$obj = new name();63$obj->name();64class name{65 public function name(){66 echo "My name is Asif";67 }68}69$obj = new name();70$obj->name();71class name{72 public function name(){73 echo "My name is Asif";74 }75}76$obj = new name();77$obj->name();
self
Using AI Code Generation
1{2 function self()3 {4 echo "Inside self method";5 }6}7$obj = new abc();8$obj->self();9{10 function self()11 {12 echo "Inside self method";13 }14}15$obj = new abc();16$obj->self();17{18 function self()19 {20 echo "Inside self method";21 }22}23$obj = new abc();24$obj->self();25{26 function self()27 {28 echo "Inside self method";29 }30}31$obj = new abc();32$obj->self();33{34 function self()35 {36 echo "Inside self method";37 }38}39$obj = new abc();40$obj->self();41{42 function self()43 {44 echo "Inside self method";45 }46}47$obj = new abc();48$obj->self();49{50 function self()51 {52 echo "Inside self method";53 }54}55$obj = new abc();56$obj->self();57{58 function self()59 {60 echo "Inside self method";61 }62}63$obj = new abc();64$obj->self();65{66 function self()67 {68 echo "Inside self method";69 }70}71$obj = new abc();72$obj->self();73{74 function self()75 {76 echo "Inside self method";77 }78}
self
Using AI Code Generation
1{2public static function myname()3{4echo "my name is rahul";5}6}7{8public static function myname()9{10echo "my name is rahul";11}12}13name1::myname();14{15public static function myname()16{17echo "my name is rahul";18}19}20{21public static function myname()22{23parent::myname();24echo "my name is rahul";25}26}27name1::myname();28{29public static function myname()30{31echo "my name is rahul";32}33}34{35public static function myname()36{37parent::myname();38echo "my name is rahul";39}40}41name1::myname();42{43public static function myname()44{45echo "my name is rahul";46}47}48{49}50name1::myname();51{52public static function myname()53{54echo "my name is rahul";55}56}57{58}59name1::myname();60{61public static function myname()62{63echo "my name is rahul";64}65}66{67}68name1::myname();69{70public static function myname()71{72echo "my name is rahul";73}74}75{76}77name1::myname();78{79public static function myname()80{81echo "my name is rahul";82}83}84{85}86name1::myname();87{88public static function myname()89{
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Execute automation tests with self on a cloud-based Grid of 3000+ real browsers and operating systems for both web and mobile applications.
Test now for FreeGet 100 minutes of automation test minutes FREE!!