├── demo.txt └── fireflyPHP-1.0-master ├── core ├── function.php ├── MyException.class.php ├── Controller.class.php ├── App.class.php └── Model.class.php ├── app ├── views │ ├── index │ │ └── index.php │ └── error │ │ └── error.php └── controllers │ ├── Test.class.php │ └── Home.class.php ├── .htaccess ├── config └── constants.php └── index.php /demo.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /fireflyPHP-1.0-master/core/function.php: -------------------------------------------------------------------------------- 1 | 报错信息:".$msg.""; 10 | 11 | -------------------------------------------------------------------------------- /fireflyPHP-1.0-master/app/controllers/Test.class.php: -------------------------------------------------------------------------------- 1 | show('index/index',$data); 15 | } 16 | 17 | } 18 | 19 | 20 | 21 | 22 | 23 | -------------------------------------------------------------------------------- /fireflyPHP-1.0-master/core/MyException.class.php: -------------------------------------------------------------------------------- 1 | "; 9 | require_once 'core/App.class.php'; 10 | require_once 'config/constants.php'; 11 | require_once 'core/function.php'; 12 | //注册一个 13 | define('APP','app');//定义一个常量 14 | spl_autoload_register(array('App','myAutoloader')); 15 | try{ 16 | //在执行的过程中,如果捕获到了任何异常,就跳传到 catch 17 | App::run(); 18 | }catch(MyException $e){ 19 | $e->showError($e->getMessage()); 20 | } 21 | 22 | $db = Model::getStringleton(); 23 | 24 | //直接执行sql语句 25 | //$result = $db->queryString('select * from users where username=:username',array(':username'=>'zhang')); 26 | 27 | //查询某个表 28 | //$result = $db->select('users'); 29 | 30 | //查询某个表,并增加where条件 31 | //$result = $db->where('where username=:username')->select('users',array(':username'=>'zhang')); 32 | 33 | //查询单条数据 34 | //$result = $db->where('where id=:id')->find('users',array(':id'=>'3')); 35 | 36 | //插入数据 37 | //$data = array(':username'=>'zhang6',':userpass'=>md5(123456),':create_time2'=>time()); 38 | //$result = $db->insert('users',$data); 39 | 40 | //更新数据 41 | //$data = array(':username'=>'lisi',':userpass'=>md5(456789)); 42 | //$where = array(':id'=>9); 43 | //$result = $db->where('where id=:id')->update('users',$data,$where); 44 | 45 | //删除数据 46 | $result = $db->where('where id=:id')->delete('users',array('id'=>9)); 47 | 48 | echo "
";
49 | print_r($result);


--------------------------------------------------------------------------------
/fireflyPHP-1.0-master/core/App.class.php:
--------------------------------------------------------------------------------
  1 | ";
 21 |         if(!empty($_GET['url'])){
 22 |             $url = explode('/',$_GET['url']);
 23 |             //得到控制器
 24 |             if(isset($url[0])){
 25 |                 self::$controller = $url[0];
 26 |                 unset($url[0]);
 27 |             }
 28 |             //得到方法名
 29 |             if(isset($url[1])){
 30 |                 self::$method = $url[1];
 31 |                 unset($url[1]);
 32 |             }
 33 |             //判断是否有其他参数
 34 |             if(isset($url)){
 35 |                 self::$pams = array_values($url);
 36 |                 // var_dump(self::$pams);die;
 37 |             }
 38 |         }
 39 |     }
 40 | 
 41 |     /**
 42 |      * 项目的入口方法
 43 |      * @throws Exception
 44 |      */
 45 |     public static function run(){
 46 |         // echo "run";
 47 |         // echo "
"; 48 | 49 | self::paseUrl(); 50 | //判断是前台还是后台 51 | if(APP == 'app'){ 52 | //得到控制器的路径 53 | $url = 'app/controllers/'.self::$controller.'.class.php'; 54 | } 55 | if(APP == 'web'){ 56 | //得到控制器的路径 57 | $url = 'controllers/'.self::$controller.'.class.php'; 58 | } 59 | //判断控制器文件是否存在 60 | if(file_exists($url)){ 61 | // var_dump(self::$controller); 62 | $c = new self::$controller; 63 | }else{ 64 | throw new MyException('控制器不存在'); 65 | } 66 | 67 | //执行方法,判断在home类里面,method是否存在 68 | if(method_exists($c,self::$method)){ 69 | // var_dump($c);die; 70 | $m = self::$method; 71 | 72 | // var_dump($m);die; 73 | $new_pams = array(); 74 | $num = count(self::$pams); 75 | // var_dump(self::$pams[0]); 76 | // var_dump(self::$pams[1]);die; 77 | // echo "
"; 78 | //传递参数,判断是否有参数 79 | 80 | if($num > 0){ 81 | //判断传递的参数的数量是否是2的倍数 82 | if($num % 2 == 0){ 83 | //将参数进行处理 84 | //$i = $i + 2 85 | for($i=0;$i<$num;$i+=2){ 86 | // echo $i; 87 | $new_pams[self::$pams[$i]] = self::$pams[$i+1]; 88 | } 89 | // var_dump($new_pams); 90 | }else{ 91 | throw new MyException('非法参数!'); 92 | } 93 | } 94 | $c->$m($new_pams); 95 | }else{ 96 | throw new MyException('方法不存在'); 97 | } 98 | } 99 | 100 | /** 101 | * 自动加载类方法 102 | * @param $className 103 | * @throws Exception 104 | */ 105 | public static function myAutoloader($className){ 106 | // echo "myAutoloader"; 107 | // echo "
"; 108 | if(APP == 'app'){ 109 | //控制器类文件目录 110 | $controller = 'app/controllers/'.$className.'.class.php'; 111 | //模型类文件目录 112 | $model = 'app/models/'.$className.'.class.php'; 113 | //核心类文件目录 114 | $core = 'core/'.$className.'.class.php'; 115 | } 116 | 117 | if(APP == 'web'){ 118 | //控制器类文件目录 119 | $controller = 'controllers/'.$className.'.class.php'; 120 | //模型类文件目录 121 | $model = 'models/'.$className.'.class.php'; 122 | //核心类文件目录 123 | $core = '../core/'.$className.'.class.php'; 124 | } 125 | 126 | 127 | if(file_exists($controller)){ 128 | require_once $controller; 129 | }else if(file_exists($model)){ 130 | require_once $model; 131 | }else if(file_exists($core)){ 132 | require_once $core; 133 | }else{ 134 | throw new MyException('类文件不存在'); 135 | } 136 | } 137 | } -------------------------------------------------------------------------------- /fireflyPHP-1.0-master/core/Model.class.php: -------------------------------------------------------------------------------- 1 | setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE,PDO::FETCH_ASSOC); 47 | //设置操作数据库的报错模式 48 | // self::$_link->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_WARNING); 49 | // if(self::$_link){ 50 | // echo '连接数据库成功'; 51 | // } 52 | }catch (PDOException $e){ 53 | MyException::showError($e->getMessage()); 54 | } 55 | 56 | } 57 | 58 | /** 59 | * 直接执行sql语句查询数据库的方法 60 | * @param $sql mysql语句 61 | * @param array $where 条件数据 62 | * @return mixed 成功数组 63 | */ 64 | public function queryString($sql,$where=array()){ 65 | try{ 66 | //使用预处理语句来执行sql 67 | $stmt = self::$_link->prepare($sql); 68 | 69 | //判断是否有条件数组 70 | if(empty($where)){ 71 | $stmt->execute(); 72 | }else{ 73 | $stmt->execute($where); 74 | } 75 | 76 | //判断执行是否成功 77 | if($stmt->rowCount() > 0){ 78 | return $stmt->fetchAll(); 79 | }else { 80 | //得到错误信息 81 | $err = $stmt->errorInfo(); 82 | throw new PDOException($err[2]); 83 | } 84 | }catch(PDOException $e){ 85 | MyException::showError($e->getMessage()); 86 | } 87 | } 88 | 89 | /** 90 | * 内部sql处理好的查询方法 91 | * @param $table 表名 92 | * @param array $where 查询条件 93 | * @return mixed 成功返回数组 94 | */ 95 | public function select($table,$where = array()){ 96 | $sql = "select * from {$table} "; 97 | if(!empty($this->whereStr)){ 98 | $sql .= $this->whereStr; 99 | } 100 | try{ 101 | //执行sql语句 102 | $stmt = self::$_link->prepare($sql); 103 | if(empty($where)){ 104 | $stmt->execute(); 105 | }else{ 106 | $stmt->execute($where); 107 | } 108 | 109 | //判断是否成功,如果不成功爆出异常 110 | if($stmt->rowCount() > 0){ 111 | return $stmt->fetchAll(); 112 | }else{ 113 | $err = $stmt->errorInfo(); 114 | return $err[2]; 115 | } 116 | }catch(PDOException $e){ 117 | MyException::showError($e->getMessage()); 118 | } 119 | } 120 | 121 | /** 122 | * where条件方法 123 | * @param string $whereStr 124 | * @return $this 125 | */ 126 | public function where($whereStr = ''){ 127 | $this->whereStr = $whereStr; 128 | return $this;//返回当前对象 129 | } 130 | 131 | /** 132 | * 查询单条数据的方法 133 | * @param $table 表名 134 | * @param array $where 查询的条件,:key=value 135 | * @return mixed 成功返回数组 136 | */ 137 | public function find($table,$where = array()){ 138 | $sql = "select * from {$table} "; 139 | 140 | if(!empty($this->whereStr)){ 141 | $sql .= $this->whereStr; 142 | } 143 | 144 | try{ 145 | //执行sql 146 | $stmt = self::$_link->prepare($sql); 147 | if(empty($where)){ 148 | $stmt->execute(); 149 | }else{ 150 | $stmt->execute($where); 151 | } 152 | 153 | //判断是否成功 154 | if($stmt->rowCount() > 0){ 155 | $result = $stmt->fetchAll(); 156 | return $result[0]; 157 | }else{ 158 | $err = $stmt->errorInfo(); 159 | throw new PDOException($err[2]); 160 | } 161 | }catch(PDOException $e){ 162 | MyException::showError($e->getMessage()); 163 | } 164 | } 165 | 166 | /** 167 | * 添加单条数据的方法 168 | * @param $table 表名 169 | * @param array(':username'=>'zhang6',':userpass'=>md5(123456),':create_time'=>time()) $data 170 | * @return int 成功返回1 171 | */ 172 | public function insert($table,array $data){ 173 | $sql = "insert into {$table} "; 174 | $fields = ""; 175 | $values = ""; 176 | foreach($data as $k => $v){ 177 | $fields .= ltrim($k,":").","; 178 | $values .= "'".ltrim($v,":")."',"; 179 | } 180 | $sql .= "(".rtrim($fields,",").") values (".rtrim($values,",").")"; 181 | 182 | try{ 183 | //开启事务 184 | self::$_link->beginTransaction(); 185 | 186 | $stmt = self::$_link->prepare($sql); 187 | $stmt->execute($data); 188 | 189 | if($stmt->rowCount() > 0){ 190 | self::$_link->commit(); 191 | return 1; 192 | }else{ 193 | self::$_link->rollback(); 194 | $err = $stmt->errorInfo(); 195 | throw new PDOException($err[2]); 196 | } 197 | 198 | }catch(PDOException $e){ 199 | MyException::showError($e->getMessage()); 200 | } 201 | } 202 | 203 | /** 204 | * 更新数据 205 | * @param $table 表名 206 | * @param array $data array(':username'=>'lisi',':userpass'=>md5(456789)); 207 | * @param array $where array(':id'=>9); 208 | * @return int 209 | */ 210 | public function update($table,array $data,array $where){ 211 | $sql = "update {$table} set "; 212 | $set_str = ''; 213 | foreach($data as $k => $v){ 214 | $set_str .= ltrim($k,":")."=$k,"; 215 | } 216 | 217 | $sql .= rtrim($set_str,',').' '.$this->whereStr; 218 | 219 | try{ 220 | self::$_link->beginTransaction(); 221 | $stmt = self::$_link->prepare($sql); 222 | $data2 = array_merge($data,$where); 223 | $stmt->execute($data2); 224 | 225 | if($stmt->rowCount() > 0){ 226 | self::$_link->commit(); 227 | return 1; 228 | }else{ 229 | self::$_link->rollback(); 230 | $err = $stmt->errorInfo(); 231 | throw new PDOException($err[2]); 232 | } 233 | }catch(PDOException $e){ 234 | MyException::showError($e->getMessage()); 235 | } 236 | } 237 | 238 | /** 239 | * 删除数据方法 240 | * @param $table 表名 241 | * @param array $where 242 | * @return int 243 | */ 244 | public function delete($table,array $where){ 245 | $sql = "delete from {$table} ".$this->whereStr; 246 | try{ 247 | self::$_link->beginTransaction(); 248 | $stmt = self::$_link->prepare($sql); 249 | $stmt->execute($where); 250 | 251 | if($stmt->rowCount() > 0){ 252 | self::$_link->commit(); 253 | return 1; 254 | }else{ 255 | self::$_link->rollback(); 256 | $err = $stmt->errorInfo(); 257 | throw new PDOException($err[2]); 258 | } 259 | }catch(PDOException $e){ 260 | MyException::showError($e->getMessage()); 261 | } 262 | } 263 | 264 | /** 265 | * 析构方法 266 | * 销毁对象 267 | */ 268 | public function __destruct(){ 269 | self::$_link = null; 270 | } 271 | } --------------------------------------------------------------------------------