初始提交

This commit is contained in:
wanyongkang
2020-10-03 17:23:32 +08:00
commit a331e3f1d5
11 changed files with 676 additions and 0 deletions

View File

@@ -0,0 +1,74 @@
<?php
namespace app\test\controller;
use app\test\model\Test;
use app\test\model\User;
class Index
{
public function index()
{
$test = new Test();
$user = new User();
$list = $test->fetchAll();
$userlist = $user->field('`LoginCode`')->fetchAll();
$user_manager = [];
$user_product = [];
$user_vip_price = [];
$user_contact = [];
$user_next = [];
$sell_order = [];
$sell_order_next = [];
foreach ($list as $info) {
switch ($info['operation']) {
case '点击客户管理':
$user_manager[] = $info;
break;
case '点击客户信息产品账号':
$user_product[] = $info;
break;
case '点击客户信息会员价':
$user_vip_price[] = $info;
break;
case '点击客户更多联系方式':
$user_contact[] = $info;
break;
case '点击客户信息下一页':
$user_next[] = $info;
break;
case '点击销售订单':
$sell_order[] = $info;
break;
case '点击销售订单下一页':
$sell_order_next[] = $info;
break;
}
}
$i = 0;
$j = 0;
$user_f_list = [];
$user_l = '';
foreach ($userlist as $info) {
//处理客户管理
if ($i % 50 != 0 || $i==0) {
$user_l .= $info['LoginCode'].',';
} else {
$user_l = '';
$j++;
}
$user_f_list[$j] = $user_l;
$i++;
}
foreach($user_manager as $k=>$v){
}
}
}

11
app/test/model/Test.php Normal file
View File

@@ -0,0 +1,11 @@
<?php
namespace app\test\model;
use fastphp\base\Model;
class Test extends Model
{
protected $table = 'test';
}

11
app/test/model/User.php Normal file
View File

@@ -0,0 +1,11 @@
<?php
namespace app\test\model;
use fastphp\base\Model;
class User extends Model
{
protected $table = 'user';
}

22
config/config.php Normal file
View File

@@ -0,0 +1,22 @@
<?php
return [
//默认控制器和操作名
'defaultController' => 'test\controller\Index',
'defaultAction' => 'index',
//根命名空间
'namespace' => [
'app' => APP_PATH.DIRECTORY_SEPARATOR.'app',
'fastphp' => APP_PATH.DIRECTORY_SEPARATOR.'fastphp',
],
// 数据库连接
'db' => [
'host' => '127.0.0.1',
'port' => 3306,
'dbname' => 'test',
'username' => 'root',
'password' => '123456789'
],
];

133
fastphp/Fastphp.php Normal file
View File

@@ -0,0 +1,133 @@
<?php
namespace fastphp;
//框架根目录
//use function Composer\Autoload\includeFile;
define('CORE_PATH',__DIR__);
/**
* fastphp框架核心
*/
class Fastphp
{
//配置内容
protected $config = [];
public function __construct($config)
{
$this->config = $config;
require CORE_PATH.'/func/common.php';
}
//运行程序
public function run(){
spl_autoload_register(array($this,'loadClass'));
$this->setReporting();
$this->unregisterGlobals();
$this->setDbConfig();
$this->route();
}
//路由处理
public function route(){
$controllerName = $this->config['defaultController'];
$actionName = $this->config['defaultAction'];
$param = array();
$url = $_SERVER['REQUEST_URI'];
//清除?之后的内容
$position = strpos($url,'?');
$url = $position === false?$url:substr($url,0,$position);
//删除前后的/
$url = trim($url,'/');
if($url){
//使用 / 分割
$urlArray = explode('/',$url);
//删除空的数组元素
$urlArray = array_filter($urlArray);
//获取控制器名
$controllerName = array_shift($urlArray).'\\controller\\'.ucfirst(array_shift($urlArray));
//获取动作名
$actionName = $urlArray?array_shift($urlArray):$actionName;
//获取url参数
$param = $urlArray?$urlArray:array();
}
//判断控制器和操作是否存在
$controller = 'app\\'.$controllerName;
if(!class_exists($controller)){
exit($controller.'控制器不存在!');
}
if(!method_exists($controller,$actionName)){
exit($actionName.'方法不存在!');
}
//实例化控制器这个后续结合controller基类一起看
$dispatch = new $controller($controllerName,$actionName);
//以下等同于$dispatch->$actionName($param);
call_user_func_array(array($dispatch,$actionName),$param);
}
//检测开发环境
public function setReporting(){
if(APP_DEBUG === true){
error_reporting(E_ALL);
ini_set('display_errors','On');
} else {
error_reporting(E_ALL);
ini_set('display_errors','Off');
ini_set('log_errors','On');
}
}
//删除敏感字符
public function stripSlashesDeep($value){
$value = is_array($value)?array_map(array($this,'stripSlashesDeep'),$value):stripSlashes($value);
return $value;
}
//检测自定义全局变量并移除
public function unregisterGlobals(){
if(ini_get('register_globals')){
$array = array('_SESSION','_POST','_GET','_COOKIE','_REQUEST','_SERVER','_ENV','_FILES');
foreach ($array as $value){
foreach ($GLOBALS[$value] as $key=>$var){
if($var===$GLOBALS[$key]){
unset($GLOBALS[$key]);
}
}
}
}
}
//配置数据库信息
public function setDbConfig(){
if(isset($this->config['db'])){
define('DB_HOST',$this->config['db']['host']);
define('DB_PORT',$this->config['db']['port']);
define('DB_NAME',$this->config['db']['dbname']);
define('DB_USER',$this->config['db']['username']);
define('DB_PASS',$this->config['db']['password']);
}
}
//自动加载类
public function loadClass($className){
$vendor = substr($className,0,strpos($className,'\\'));
$vendorDIR = $this->config['namespace'][$vendor];
$filePath = substr($className,strlen($vendor)).'.php';
//包含应用文件
$file = strtr($vendorDIR.$filePath,'\\',DIRECTORY_SEPARATOR);
if(!is_file($file)){
return;
}
include $file;
}
}

View File

@@ -0,0 +1,33 @@
<?php
namespace fastphp\base;
/**
* 控制器基类
* Class Controller
* @package fastphp\base
*/
class Controller
{
protected $_controller;
protected $_action;
// protected $_view;
//初始化属性,实例化对应模型
public function __construct($controller,$action)
{
$this->_controller = $controller;
$this->_action = $action;
// $this->_view = new View($controller,$action);
}
// //分配变量
// public function assign($name,$value){
// $this->_view->assign($name,$value);
// }
//
// //渲染视图
// public function render(){
// $this->_view->render();
// }
}

26
fastphp/base/Model.php Normal file
View File

@@ -0,0 +1,26 @@
<?php
namespace fastphp\base;
use fastphp\db\Sql;
class Model extends Sql
{
protected $model;
public function __construct()
{
//获取数据库表名
if(!$this->table){
//获取模型类名称
$this->model = get_class($this);
//删除模型名最后的model
$this->model = substr($this->model,0,-5);
//数据库表名与类名一致
$this->table = strtolower($this->model);
}
}
}

39
fastphp/db/Db.php Normal file
View File

@@ -0,0 +1,39 @@
<?php
namespace fastphp\db;
use PDO;
use PDOException;
/**
* 数据库操作类
* 其属性$pdo为静态属性所以在页面执行周期内
* 只要一次赋值,以后获取的还是首次赋值的内容
* 这里就是pdo对象这样可以保证运行期间只有
* 一个数据库连接对象,简单的单例模式
* Class Db
* @package fastphp\db
*/
class Db
{
private static $pdo = null;
public static function pdo(){
if(self::$pdo !== null){
return self::$pdo;
}
try{
$dns = sprintf('mysql:host=%s;post=%s;dbname=%s;chartset=utf8',DB_HOST,DB_PORT,DB_NAME);
$option = array(PDO::ATTR_DEFAULT_FETCH_MODE=>PDO::FETCH_ASSOC);
self::$pdo = new PDO($dns,DB_USER,DB_PASS,$option);
self::$pdo->query("set names utf8");
return self::$pdo;
} catch (PDOException $e){
exit($e->getMessage());
}
}
}

247
fastphp/db/Sql.php Normal file
View File

@@ -0,0 +1,247 @@
<?php
namespace fastphp\db;
use \PDOStatement;
class Sql
{
//数据库表名
protected $table;
//where 和 order拼装后的条件
private $filter = '';
//pdo 绑定的参数集合
private $param = array();
private $field = '*';
/**
* 查询条件拼接
*
* @param $where 条件
* @return $this
* ['id'=>1] 或者 '`id`=1'
*/
public function where($where){
$this->param = [];
$this->filter = '';
if($where){
$this->filter .= 'WHERE ';
if(is_string($where)){
$this->filter .= $where;
} else {
$flag = true;
foreach ($where as $key => $value){
if($flag){
$flag = false;
if(is_array($value)){
$this->filter .= '`'.$key.'` '.$value[0].' :'.$key;
$this->param[$key] = $value[1];
} else {
$this->filter .= '`'.$key.'` = :'.$key;
$this->param[$key] = $value;
}
} else {
if(is_array($value)){
$this->filter .= ' AND `'.$key.'` '.$value[0].' :'.$key;
$this->param[$key] = $value[1];
} else {
$this->filter .= ' AND `'.$key.'` = :'.$key;
$this->param[$key] = $value;
}
}
}
}
}
return $this;
}
/**
* 拼装排序条件
* @param array $order 排序条件
* @return $this
*/
public function order($order = []){
if($order){
$this->filter .= 'ORDER BY ';
$this->filter .= implode(' ,',$order);
}
return $this;
}
/**
* group by
* @param array $order
* @return $this
*/
public function group($order = []){
if($order){
$this->filter .= 'GROUP BY ';
$this->filter .= implode(' ,',$order);
}
return $this;
}
/**
* 查询字段
* @param string $field
* @return $this
*/
public function field($field = '*'){
$this->field = $field;
return $this;
}
/**
* 查询所有
* @return mixed
*/
public function fetchAll(){
$sql = sprintf('SELECT %s FROM `%s` %s',$this->field,$this->table,$this->filter);
$this->filter = '';
$sth = Db::pdo()->prepare($sql);
$sth = $this->formatParam($sth,$this->param);
$sth->execute();
return $sth->fetchAll();
}
/**
* 查询一条
* @return mixed
*/
public function fetch(){
$sql = sprintf('SELECT %s FROM `%s` %s',$this->field,$this->table,$this->filter);
$this->filter = '';
$sth = Db::pdo()->prepare($sql);
$sth = $this->formatParam($sth,$this->param);
$sth->execute();
return $sth->fetch();
}
/**
* 根据条件id删除
* @param $id
* @return mixed
*/
public function delete(){
$sql = sprintf('DELETE FROM `%s` %s',$this->table,$this->filter);
$this->filter = '';
$sth = Db::pdo()->prepare($sql);
$sth = $this->formatParam($sth,$this->param);
$sth->execute();
return $sth->rowCount();
}
/**
* 新增数据
* @param $data
* @return mixed
*/
public function add($data){
$sql = sprintf('INSERT INTO `%s` %s',$this->table,$this->formatInsert($data));
$sth = Db::pdo()->prepare($sql);
$sth = $this->formatParam($sth,$data);
$sth->execute();
return $sth->rowCount();
}
/**
* 新增多条数据
* @param $data
* @return int
*/
public function addAll($data){
$sql = sprintf('INSERT INTO `%s` %s',$this->table,$this->formatInsertAll($data));
$sth = Db::pdo()->prepare($sql);
$sth->execute();
return $sth->rowCount();
}
/**
* 修改数据
* @param $data
* @return mixed
*/
public function update($data){
$sql = sprintf('UPDATE `%s` SET %s %s',$this->table,$this->formatUpdate($data),$this->filter);
$this->filter = '';
$sth = Db::pdo()->prepare($sql);
$sth = $this->formatParam($sth,$data);
$sth = $this->formatParam($sth,$this->param);
$sth->execute();
return $sth->rowCount();
}
/**
* 绑定具体变量值
* @param PDOStatement $sth
* @param array $params
* @return PDOStatement
*/
public function formatParam(PDOStatement $sth,$params = []){
foreach ($params as $param=>&$value){
$param = is_int($param)?$param+1:':'.ltrim($param,':');
$sth->bindParam($param,$value);
}
return $sth;
}
/**
* 将数组转化为插入格式的sql语句
* @param $data
* @return string
*/
private function formatInsert($data){
$fields = [];
$names = [];
foreach ($data as $key=>$value) {
$fields[] = sprintf('`%s`',$key);
$names[] = sprintf(':%s',$key);
}
$field = implode(',',$fields);
$name = implode(',',$names);
return sprintf('(%s) VALUES(%s)',$field,$name);
}
/**
* 格式化插入数据
* @param $data
* @return string
*/
private function formatInsertAll($data){
$fields = [];
$values ='';
foreach ($data[0] as $key=>$value) {
$fields[] = sprintf('`%s`',$key);
}
foreach ($data as $k=>$v){
$values .= '(\''.implode('\',\'',$v).'\'),';
}
$field = implode(',',$fields);
$values = rtrim($values,',');
return sprintf('(%s) VALUES %s',$field,$values);
}
/**
* 更新数据的格式转换
* @param $data
* @return string
*/
private function formatUpdate($data){
$fields = [];
foreach ($data as $key=>$value){
$fields[] = sprintf('`%s`=:%s',$key,$key);
$this->param[$key] = $value;
}
return implode(',',$fields);
}
}

46
fastphp/func/common.php Normal file
View File

@@ -0,0 +1,46 @@
<?php
/**
* 浏览器友好的变量输出
* @param mixed $var 变量
* @param boolean $echo 是否输出 默认为True 如果为false 则返回输出字符串
* @param string $label 标签 默认为空
* @param boolean $strict 是否严谨 默认为true
* @return void|string
*/
function dd($var, $echo=true, $label=null, $strict=true) {
echo(echoBase($var, $echo=true, $label=null, $strict=true));
exit;
}
/**
* 浏览器友好的变量输出
* @param mixed $var 变量
* @param boolean $echo 是否输出 默认为True 如果为false 则返回输出字符串
* @param string $label 标签 默认为空
* @param boolean $strict 是否严谨 默认为true
* @return void|string
*/
function dump($var, $echo=true, $label=null, $strict=true) {
echo(echoBase($var, $echo=true, $label=null, $strict=true));
}
function echoBase($var, $echo=true, $label=null, $strict=true) {
$label = ($label === null) ? '' : rtrim($label) . ' ';
if (!$strict) {
if (ini_get('html_errors')) {
$output = print_r($var, true);
$output = '<pre>' . $label . htmlspecialchars($output, ENT_QUOTES) . '</pre>';
} else {
$output = $label . print_r($var, true);
}
} else {
ob_start();
var_dump($var);
$output = ob_get_clean();
if (!extension_loaded('xdebug')) {
$output = preg_replace('/\]\=\>\n(\s+)/m', '] => ', $output);
$output = '<pre>' . $label . htmlspecialchars($output, ENT_QUOTES) . '</pre>';
}
}
return $output;
}

34
index.php Normal file
View File

@@ -0,0 +1,34 @@
<?php
//error_reporting( E_ALL&~E_NOTICE );
//ini_set( 'display_errors', 'on' );
header("Content-type:text/html;charset=utf-8");
//内存限制
ini_set (memory_limit, 128M) ;
//应用目录为当前目录
define('APP_PATH',__DIR__.'/');
//开启调试模式
define('APP_DEBUG',true);
//加载框架文件
require APP_PATH.'fastphp/Fastphp.php';
//加载配置文件
$config = require APP_PATH.'config/config.php';
//实例化框架类
(new fastphp\Fastphp($config))->run();
//require './Autoload.php';
//
//
//spl_autoload_register('Autoload::loadTest');
//
//$inn = new \app\Demo();
//$inn->abs(-123);
//echo '<br>';
//
//$urls = explode('/',$_SERVER['REQUEST_URI']);
//
//var_dump($urls);