├── ppython
├── log
│ ├── ppython.log
│ ├── python
│ │ └── all.log
│ ├── supervisord.log
│ └── ppython_error.log
├── ppython.sh
├── python
│ ├── sayhi
│ │ └── hi.py
│ ├── logger.py
│ └── php_python.py
├── supervisord
│ ├── super_update.sh
│ ├── ppython.ini
│ └── super_init_linux.sh
└── Ppython.php
├── example
├── application
│ ├── .htaccess
│ ├── common.php
│ ├── command.php
│ ├── route.php
│ ├── index
│ │ └── controller
│ │ │ └── Index.php
│ ├── tags.php
│ └── database.php
├── thinkphp
│ ├── .htaccess
│ ├── .gitignore
│ ├── logo.png
│ ├── library
│ │ ├── think
│ │ │ ├── console
│ │ │ │ ├── bin
│ │ │ │ │ ├── hiddeninput.exe
│ │ │ │ │ └── README.md
│ │ │ │ ├── command
│ │ │ │ │ ├── make
│ │ │ │ │ │ ├── stubs
│ │ │ │ │ │ │ ├── model.stub
│ │ │ │ │ │ │ ├── controller.plain.stub
│ │ │ │ │ │ │ └── controller.stub
│ │ │ │ │ │ ├── Model.php
│ │ │ │ │ │ └── Controller.php
│ │ │ │ │ ├── Clear.php
│ │ │ │ │ ├── Build.php
│ │ │ │ │ ├── Help.php
│ │ │ │ │ ├── Lists.php
│ │ │ │ │ ├── optimize
│ │ │ │ │ │ ├── Route.php
│ │ │ │ │ │ └── Config.php
│ │ │ │ │ └── Make.php
│ │ │ │ ├── LICENSE
│ │ │ │ ├── output
│ │ │ │ │ ├── driver
│ │ │ │ │ │ ├── Nothing.php
│ │ │ │ │ │ └── Buffer.php
│ │ │ │ │ ├── question
│ │ │ │ │ │ └── Confirmation.php
│ │ │ │ │ ├── formatter
│ │ │ │ │ │ └── Stack.php
│ │ │ │ │ └── descriptor
│ │ │ │ │ │ └── Console.php
│ │ │ │ └── input
│ │ │ │ │ └── Argument.php
│ │ │ ├── exception
│ │ │ │ ├── RouteNotFoundException.php
│ │ │ │ ├── HttpResponseException.php
│ │ │ │ ├── ClassNotFoundException.php
│ │ │ │ ├── TemplateNotFoundException.php
│ │ │ │ ├── ValidateException.php
│ │ │ │ ├── HttpException.php
│ │ │ │ ├── DbException.php
│ │ │ │ ├── PDOException.php
│ │ │ │ ├── ThrowableError.php
│ │ │ │ └── ErrorException.php
│ │ │ ├── config
│ │ │ │ └── driver
│ │ │ │ │ ├── Ini.php
│ │ │ │ │ ├── Json.php
│ │ │ │ │ └── Xml.php
│ │ │ ├── log
│ │ │ │ └── driver
│ │ │ │ │ └── Test.php
│ │ │ ├── db
│ │ │ │ ├── exception
│ │ │ │ │ ├── BindParamException.php
│ │ │ │ │ ├── ModelNotFoundException.php
│ │ │ │ │ └── DataNotFoundException.php
│ │ │ │ ├── builder
│ │ │ │ │ ├── Sqlite.php
│ │ │ │ │ ├── Mysql.php
│ │ │ │ │ ├── Pgsql.php
│ │ │ │ │ └── Sqlsrv.php
│ │ │ │ └── connector
│ │ │ │ │ ├── Sqlite.php
│ │ │ │ │ ├── Pgsql.php
│ │ │ │ │ ├── pgsql.sql
│ │ │ │ │ ├── Sqlsrv.php
│ │ │ │ │ └── Mysql.php
│ │ │ ├── Env.php
│ │ │ ├── model
│ │ │ │ ├── Pivot.php
│ │ │ │ ├── Collection.php
│ │ │ │ └── Relation.php
│ │ │ ├── controller
│ │ │ │ ├── Yar.php
│ │ │ │ └── Rest.php
│ │ │ ├── Exception.php
│ │ │ ├── process
│ │ │ │ ├── exception
│ │ │ │ │ ├── Failed.php
│ │ │ │ │ └── Timeout.php
│ │ │ │ ├── Utils.php
│ │ │ │ └── pipes
│ │ │ │ │ └── Pipes.php
│ │ │ ├── response
│ │ │ │ ├── Json.php
│ │ │ │ ├── Jsonp.php
│ │ │ │ ├── View.php
│ │ │ │ ├── Redirect.php
│ │ │ │ └── Xml.php
│ │ │ ├── template
│ │ │ │ └── driver
│ │ │ │ │ └── File.php
│ │ │ ├── session
│ │ │ │ └── driver
│ │ │ │ │ ├── Memcache.php
│ │ │ │ │ ├── Redis.php
│ │ │ │ │ └── Memcached.php
│ │ │ ├── Error.php
│ │ │ └── cache
│ │ │ │ └── driver
│ │ │ │ └── Wincache.php
│ │ └── traits
│ │ │ ├── think
│ │ │ └── Instance.php
│ │ │ └── model
│ │ │ └── SoftDelete.php
│ ├── codecov.yml
│ ├── start.php
│ ├── console.php
│ ├── composer.json
│ ├── tpl
│ │ ├── default_index.tpl
│ │ └── dispatch_jump.tpl
│ ├── .travis.yml
│ ├── phpunit.xml
│ ├── LICENSE.txt
│ ├── base.php
│ ├── CONTRIBUTING.md
│ ├── lang
│ │ └── zh-cn.php
│ └── README.md
├── vendor
│ └── .gitignore
├── README.md
├── runtime
│ └── .gitignore
├── public
│ ├── static
│ │ └── .gitignore
│ ├── robots.txt
│ ├── favicon.ico
│ ├── .htaccess
│ ├── index.php
│ └── router.php
├── composer.json
├── think
├── build.php
├── LICENSE.txt
└── extend
│ └── ppython
│ └── Ppython.php
├── super.png
├── jsonview.png
├── ppythonlog.png
└── LICENSE
/ppython/log/ppython.log:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/ppython/log/python/all.log:
--------------------------------------------------------------------------------
1 |
2 |
--------------------------------------------------------------------------------
/ppython/log/supervisord.log:
--------------------------------------------------------------------------------
1 |
2 |
--------------------------------------------------------------------------------
/ppython/log/ppython_error.log:
--------------------------------------------------------------------------------
1 |
2 |
--------------------------------------------------------------------------------
/example/application/.htaccess:
--------------------------------------------------------------------------------
1 | deny from all
--------------------------------------------------------------------------------
/example/thinkphp/.htaccess:
--------------------------------------------------------------------------------
1 | deny from all
--------------------------------------------------------------------------------
/example/vendor/.gitignore:
--------------------------------------------------------------------------------
1 | *
2 | !.gitignore
--------------------------------------------------------------------------------
/example/README.md:
--------------------------------------------------------------------------------
1 | ThinkPHP 5.0 使用 ppython
2 |
--------------------------------------------------------------------------------
/example/runtime/.gitignore:
--------------------------------------------------------------------------------
1 | *
2 | !.gitignore
--------------------------------------------------------------------------------
/example/public/static/.gitignore:
--------------------------------------------------------------------------------
1 | *
2 | !.gitignore
--------------------------------------------------------------------------------
/example/public/robots.txt:
--------------------------------------------------------------------------------
1 | User-agent: *
2 | Disallow:
3 |
--------------------------------------------------------------------------------
/super.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/liuzhen153/php-python/HEAD/super.png
--------------------------------------------------------------------------------
/jsonview.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/liuzhen153/php-python/HEAD/jsonview.png
--------------------------------------------------------------------------------
/example/thinkphp/.gitignore:
--------------------------------------------------------------------------------
1 | /composer.lock
2 | /vendor
3 | .idea
4 | .DS_Store
5 |
--------------------------------------------------------------------------------
/ppythonlog.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/liuzhen153/php-python/HEAD/ppythonlog.png
--------------------------------------------------------------------------------
/example/thinkphp/logo.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/liuzhen153/php-python/HEAD/example/thinkphp/logo.png
--------------------------------------------------------------------------------
/example/public/favicon.ico:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/liuzhen153/php-python/HEAD/example/public/favicon.ico
--------------------------------------------------------------------------------
/example/thinkphp/library/think/console/bin/hiddeninput.exe:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/liuzhen153/php-python/HEAD/example/thinkphp/library/think/console/bin/hiddeninput.exe
--------------------------------------------------------------------------------
/example/thinkphp/library/think/console/bin/README.md:
--------------------------------------------------------------------------------
1 | console 工具使用 hiddeninput.exe 在 windows 上隐藏密码输入,该二进制文件由第三方提供,相关源码和其他细节可以在 [Hidden Input](https://github.com/Seldaek/hidden-input) 找到。
2 |
--------------------------------------------------------------------------------
/example/thinkphp/library/think/console/command/make/stubs/model.stub:
--------------------------------------------------------------------------------
1 |
2 | Options +FollowSymlinks -Multiviews
3 | RewriteEngine On
4 |
5 | RewriteCond %{REQUEST_FILENAME} !-d
6 | RewriteCond %{REQUEST_FILENAME} !-f
7 | RewriteRule ^(.*)$ index.php/$1 [QSA,PT,L]
8 |
9 |
--------------------------------------------------------------------------------
/example/thinkphp/codecov.yml:
--------------------------------------------------------------------------------
1 | comment:
2 | layout: header, changes, diff
3 | coverage:
4 | ignore:
5 | - base.php
6 | - helper.php
7 | - convention.php
8 | - lang/zh-cn.php
9 | - start.php
10 | - console.php
11 | status:
12 | patch: false
13 |
--------------------------------------------------------------------------------
/ppython/ppython.sh:
--------------------------------------------------------------------------------
1 | #!/bin/sh
2 | # 启动或重启ppython服务
3 | ACT=$1 # start restart | stop
4 | ps -ef|grep 'php_python' |grep -v 'grep'| awk '{print $2}'|xargs kill -9
5 | if [[ ${ACT} != 'stop' ]]; then
6 | project_path=$(cd `dirname $0`; pwd)
7 | project_name="${project_path%/*}/ppython/python/php_python.py"
8 | python3 ${project_name} &
9 | fi
10 | echo 'done!'
11 |
--------------------------------------------------------------------------------
/ppython/python/sayhi/hi.py:
--------------------------------------------------------------------------------
1 | # 无参数
2 | def hello():
3 | return 'Hi , Tommy'
4 |
5 | # 传递参数
6 | def hello_name(name):
7 | return 'Hi , ' + name
8 |
9 | # 返回数组
10 | def return_arr():
11 | user = {}
12 | user['name'] = 'Tommy'
13 | user['age'] = 24
14 | return user
15 |
16 | # 由于PHP传过来的参数都会被处理成字符串类型,所以需要使用数字类型的地方请自行转换,如若是其他类型数据做类似处理,否则会报错。
17 | # 但是由Python传给PHP的结果的数据类型不受限制
18 | def dosum(num1 , num2):
19 | return int(num1) + int(num2)
20 |
--------------------------------------------------------------------------------
/ppython/supervisord/super_update.sh:
--------------------------------------------------------------------------------
1 | #!/bin/sh
2 | # 启动或重启supervisor服务
3 | project_path=$(cd `dirname $0`; pwd)
4 | ppython_path="${project_path%/*}/"
5 | super_path="${ppython_path}/supervisord/"
6 | ps -ef|grep 'supervisord.conf' |grep -v 'grep'| awk '{print $2}'|xargs kill -9
7 | ps -ef|grep 'ppython.sh' |grep -v 'grep'| awk '{print $2}'|xargs kill -9
8 | ps -ef|grep 'php_python.py' |grep -v 'grep'| awk '{print $2}'|xargs kill -9
9 | supervisord -c ${super_path}/supervisord.conf
10 | echo 'done'
11 |
--------------------------------------------------------------------------------
/ppython/supervisord/ppython.ini:
--------------------------------------------------------------------------------
1 | [program:ppython]
2 | command=python3 php_python.py & ; 命令
3 | directory=/Users/liuzhen20/work/2019/php-python/ppython/python ; 命令执行目录
4 | stdout_logfile=/Users/liuzhen20/work/2019/php-python/ppython/log/ppython.log ;
5 | stderr_logfile=/Users/liuzhen20/work/2019/php-python/ppython/log/ppython_error.log ;
6 | autostart=true ; start at supervisord start (default: true)
7 | startsecs=20 ; # of secs prog must stay up to be running (def. 1)
8 | autorestart=true ; autorestart if exited after running (def: unexpected)
9 | stopwaitsecs=60
10 | priority=990 ; 优先级
11 |
--------------------------------------------------------------------------------
/example/application/common.php:
--------------------------------------------------------------------------------
1 |
10 | // +----------------------------------------------------------------------
11 |
12 | // 应用公共文件
13 |
--------------------------------------------------------------------------------
/example/application/command.php:
--------------------------------------------------------------------------------
1 |
10 | // +----------------------------------------------------------------------
11 |
12 | return [];
13 |
--------------------------------------------------------------------------------
/example/composer.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "topthink/think",
3 | "description": "the new thinkphp framework",
4 | "type": "project",
5 | "keywords": [
6 | "framework",
7 | "thinkphp",
8 | "ORM"
9 | ],
10 | "homepage": "http://thinkphp.cn/",
11 | "license": "Apache-2.0",
12 | "authors": [
13 | {
14 | "name": "liu21st",
15 | "email": "liu21st@gmail.com"
16 | }
17 | ],
18 | "require": {
19 | "php": ">=5.4.0",
20 | "topthink/framework": "^5.0"
21 | },
22 | "extra": {
23 | "think-path": "thinkphp"
24 | },
25 | "config": {
26 | "preferred-install": "dist"
27 | }
28 | }
29 |
--------------------------------------------------------------------------------
/example/thinkphp/start.php:
--------------------------------------------------------------------------------
1 |
10 | // +----------------------------------------------------------------------
11 |
12 | namespace think;
13 |
14 | // ThinkPHP 引导文件
15 | // 加载基础文件
16 | require __DIR__ . '/base.php';
17 | // 执行应用
18 | App::run()->send();
19 |
--------------------------------------------------------------------------------
/example/think:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env php
2 |
11 | // +----------------------------------------------------------------------
12 |
13 | // 定义项目路径
14 | define('APP_PATH', __DIR__ . '/application/');
15 |
16 | // 加载框架引导文件
17 | require __DIR__.'/thinkphp/console.php';
18 |
--------------------------------------------------------------------------------
/example/public/index.php:
--------------------------------------------------------------------------------
1 |
10 | // +----------------------------------------------------------------------
11 |
12 | // [ 应用入口文件 ]
13 |
14 | // 定义应用目录
15 | define('APP_PATH', __DIR__ . '/../application/');
16 | // 加载框架引导文件
17 | require __DIR__ . '/../thinkphp/start.php';
18 |
--------------------------------------------------------------------------------
/example/public/router.php:
--------------------------------------------------------------------------------
1 |
10 | // +----------------------------------------------------------------------
11 | // $Id$
12 |
13 | if (is_file($_SERVER["DOCUMENT_ROOT"] . $_SERVER["REQUEST_URI"])) {
14 | return false;
15 | } else {
16 | require __DIR__ . "/index.php";
17 | }
18 |
--------------------------------------------------------------------------------
/example/thinkphp/console.php:
--------------------------------------------------------------------------------
1 |
10 | // +----------------------------------------------------------------------
11 |
12 | namespace think;
13 |
14 | // ThinkPHP 引导文件
15 | // 加载基础文件
16 | require __DIR__ . '/base.php';
17 |
18 | // 执行应用
19 | App::initCommon();
20 | Console::init();
21 |
--------------------------------------------------------------------------------
/ppython/python/logger.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env python3
2 | # -*- coding: UTF-8 -*-
3 | import os
4 | import logging
5 | import logging.handlers
6 |
7 | def loginfo(msg):
8 | '''打印日志
9 | :param args:string
10 | :param kwargs:dict | 可自主拓展
11 | '''
12 | # 获取上级目录下的log
13 | print(msg) # 控制台直接输出日志内容
14 | log_dir = os.path.dirname(os.path.dirname(
15 | __file__)) + '/log/'
16 | logger = logging.getLogger('ppython')
17 | logger.setLevel(logging.DEBUG)
18 | ppython_handler = logging.handlers.TimedRotatingFileHandler(log_dir + '/python/all.log', 'D', 1, 10) # 按天进行分割,日志只保留10天
19 | ppython_handler.setFormatter(logging.Formatter("%(asctime)s - %(levelname)s - %(message)s"))
20 | logger.addHandler(ppython_handler)
21 | logger.info(msg)
22 | # 添加下面一句,在记录日志之后移除句柄
23 | logger.removeHandler(ppython_handler)
24 |
--------------------------------------------------------------------------------
/example/thinkphp/library/think/exception/RouteNotFoundException.php:
--------------------------------------------------------------------------------
1 |
10 | // +----------------------------------------------------------------------
11 |
12 | namespace think\exception;
13 |
14 | class RouteNotFoundException extends HttpException
15 | {
16 |
17 | public function __construct()
18 | {
19 | parent::__construct(404);
20 | }
21 |
22 | }
23 |
--------------------------------------------------------------------------------
/example/application/route.php:
--------------------------------------------------------------------------------
1 |
10 | // +----------------------------------------------------------------------
11 |
12 | return [
13 | '__pattern__' => [
14 | 'name' => '\w+',
15 | ],
16 | '[hello]' => [
17 | ':id' => ['index/hello', ['method' => 'get'], ['id' => '\d+']],
18 | ':name' => ['index/hello', ['method' => 'post']],
19 | ],
20 |
21 | ];
22 |
--------------------------------------------------------------------------------
/example/application/index/controller/Index.php:
--------------------------------------------------------------------------------
1 | py("sayhi.hi::hello");
12 | dump($data);
13 | }
14 |
15 | //带参数的方法
16 | public function py_with_something($name)
17 | {
18 | $python = new Ppython();
19 | // 更多参数依次往后添加
20 | $data = $python->py("sayhi.hi::hello_name",$name);
21 | dump($data);
22 | }
23 |
24 | // python返回的是数组
25 | public function py_arr()
26 | {
27 | $python = new Ppython();
28 | $data = $python->py("sayhi.hi::return_arr");
29 | dump($data);
30 | }
31 |
32 | // PHP传递数字类型变量,计算2+3的和
33 | public function py_num()
34 | {
35 | $python = new Ppython();
36 | $data = $python->py("sayhi.hi::dosum",2,3);
37 | dump($data);
38 | }
39 | }
40 |
--------------------------------------------------------------------------------
/example/thinkphp/library/think/config/driver/Ini.php:
--------------------------------------------------------------------------------
1 |
10 | // +----------------------------------------------------------------------
11 |
12 | namespace think\config\driver;
13 |
14 | class Ini
15 | {
16 | public function parse($config)
17 | {
18 | if (is_file($config)) {
19 | return parse_ini_file($config, true);
20 | } else {
21 | return parse_ini_string($config, true);
22 | }
23 | }
24 | }
25 |
--------------------------------------------------------------------------------
/example/thinkphp/library/think/config/driver/Json.php:
--------------------------------------------------------------------------------
1 |
10 | // +----------------------------------------------------------------------
11 |
12 | namespace think\config\driver;
13 |
14 | class Json
15 | {
16 | public function parse($config)
17 | {
18 | if (is_file($config)) {
19 | $config = file_get_contents($config);
20 | }
21 | $result = json_decode($config, true);
22 | return $result;
23 | }
24 | }
25 |
--------------------------------------------------------------------------------
/ppython/supervisord/super_init_linux.sh:
--------------------------------------------------------------------------------
1 | #!/bin/sh
2 | # $1为IP地址
3 | #变量设置
4 | echo '该脚本不完善,请自行测试!!!!'
5 | pip2 install supervisor
6 | project_path=$(cd `dirname $0`; pwd)
7 | ppython_path="${project_path%/*}/"
8 | python_path="${ppython_path}/python/"
9 | super_path="${ppython_path}/supervisord/"
10 | ppython_sh="${ppython_path}/ppython.sh"
11 | ipaddr='172.0.0.1'
12 | if [ ! $1 ]; then
13 | local_ip=$(ip addr | awk '/^[0-9]+: / {}; /inet.*global/ {print gensub(/(.*)\/(.*)/, "\\1", "g", $2)}')
14 | else
15 | local_ip=$1
16 | fi
17 |
18 | echo '开始修改配置文件'
19 |
20 | CON3="directory=${python_path} ; 命令执行目录"
21 | CON5="stdout_logfile=${ppython_path}/log/ppython.log"
22 | CON6="stderr_logfile=${ppython_path}/log/ppython_error.log"
23 | FILE="${super_path}/ppython.ini"
24 | sed -i "3c${CON3}" $FILE
25 | sed -i "5c${CON5}" $FILE
26 | sed -i "6c${CON6}" $FILE
27 |
28 | CON23="port=${local_ip}:8886 ; ip_address:port specifier, *:port for all iface"
29 | FILE2="${super_path}/supervisord.conf"
30 | sed -i "23c${CON23}" $FILE2
31 |
--------------------------------------------------------------------------------
/example/thinkphp/library/think/log/driver/Test.php:
--------------------------------------------------------------------------------
1 |
10 | // +----------------------------------------------------------------------
11 |
12 | namespace think\log\driver;
13 |
14 | /**
15 | * 模拟测试输出
16 | */
17 | class Test
18 | {
19 | /**
20 | * 日志写入接口
21 | * @access public
22 | * @param array $log 日志信息
23 | * @return bool
24 | */
25 | public function save(array $log = [])
26 | {
27 | return true;
28 | }
29 |
30 | }
31 |
--------------------------------------------------------------------------------
/example/thinkphp/composer.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "topthink/framework",
3 | "description": "the new thinkphp framework",
4 | "type": "think-framework",
5 | "keywords": [
6 | "framework",
7 | "thinkphp",
8 | "ORM"
9 | ],
10 | "homepage": "http://thinkphp.cn/",
11 | "license": "Apache-2.0",
12 | "authors": [
13 | {
14 | "name": "liu21st",
15 | "email": "liu21st@gmail.com"
16 | }
17 | ],
18 | "require": {
19 | "php": ">=5.4.0",
20 | "topthink/think-installer": "~1.0"
21 | },
22 | "require-dev": {
23 | "phpunit/phpunit": "4.8.*",
24 | "johnkary/phpunit-speedtrap": "^1.0",
25 | "mikey179/vfsStream": "~1.6",
26 | "phploc/phploc": "2.*",
27 | "sebastian/phpcpd": "2.*",
28 | "phpdocumentor/reflection-docblock": "^2.0"
29 | },
30 | "autoload": {
31 | "psr-4": {
32 | "think\\": "library/think"
33 | }
34 | }
35 | }
36 |
--------------------------------------------------------------------------------
/example/thinkphp/tpl/default_index.tpl:
--------------------------------------------------------------------------------
1 | *{ padding: 0; margin: 0; } div{ padding: 4px 48px;} a{color:#2E5CD5;cursor: pointer;text-decoration: none} a:hover{text-decoration:underline; } body{ background: #fff; font-family: "Century Gothic","Microsoft yahei"; color: #333;font-size:18px;} h1{ font-size: 100px; font-weight: normal; margin-bottom: 12px; } p{ line-height: 1.6em; font-size: 42px }
:)
ThinkPHP V5
十年磨一剑 - 为API开发设计的高性能框架
[ V5.0 版本由 七牛云 独家赞助发布 ] ';
9 | }
10 | }
11 |
--------------------------------------------------------------------------------
/example/application/tags.php:
--------------------------------------------------------------------------------
1 |
10 | // +----------------------------------------------------------------------
11 |
12 | // 应用行为扩展定义文件
13 | return [
14 | // 应用初始化
15 | 'app_init' => [],
16 | // 应用开始
17 | 'app_begin' => [],
18 | // 模块初始化
19 | 'module_init' => [],
20 | // 操作开始执行
21 | 'action_begin' => [],
22 | // 视图内容过滤
23 | 'view_filter' => [],
24 | // 日志写入
25 | 'log_write' => [],
26 | // 应用结束
27 | 'app_end' => [],
28 | ];
29 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2017 Tommy.Liu
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/example/build.php:
--------------------------------------------------------------------------------
1 |
10 | // +----------------------------------------------------------------------
11 |
12 | return [
13 | // 生成应用公共文件
14 | '__file__' => ['common.php', 'config.php', 'database.php'],
15 |
16 | // 定义demo模块的自动生成 (按照实际定义的文件名生成)
17 | 'demo' => [
18 | '__file__' => ['common.php'],
19 | '__dir__' => ['behavior', 'controller', 'model', 'view'],
20 | 'controller' => ['Index', 'Test', 'UserType'],
21 | 'model' => ['User', 'UserType'],
22 | 'view' => ['index/index'],
23 | ],
24 | // 其他更多的模块定义
25 | ];
26 |
--------------------------------------------------------------------------------
/example/thinkphp/library/think/exception/HttpResponseException.php:
--------------------------------------------------------------------------------
1 |
10 | // +----------------------------------------------------------------------
11 |
12 | namespace think\exception;
13 |
14 | use think\Response;
15 |
16 | class HttpResponseException extends \RuntimeException
17 | {
18 | /**
19 | * @var Response
20 | */
21 | protected $response;
22 |
23 | public function __construct(Response $response)
24 | {
25 | $this->response = $response;
26 | }
27 |
28 | public function getResponse()
29 | {
30 | return $this->response;
31 | }
32 |
33 | }
34 |
--------------------------------------------------------------------------------
/example/thinkphp/library/think/console/LICENSE:
--------------------------------------------------------------------------------
1 | Copyright (c) 2004-2016 Fabien Potencier
2 |
3 | Permission is hereby granted, free of charge, to any person obtaining a copy
4 | of this software and associated documentation files (the "Software"), to deal
5 | in the Software without restriction, including without limitation the rights
6 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7 | copies of the Software, and to permit persons to whom the Software is furnished
8 | to do so, subject to the following conditions:
9 |
10 | The above copyright notice and this permission notice shall be included in all
11 | copies or substantial portions of the Software.
12 |
13 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19 | THE SOFTWARE.
--------------------------------------------------------------------------------
/example/thinkphp/library/think/console/output/driver/Nothing.php:
--------------------------------------------------------------------------------
1 |
10 | // +----------------------------------------------------------------------
11 |
12 | namespace think\console\output\driver;
13 |
14 | use think\console\Output;
15 |
16 | class Nothing
17 | {
18 |
19 | public function __construct(Output $output)
20 | {
21 | // do nothing
22 | }
23 |
24 | public function write($messages, $newline = false, $options = Output::OUTPUT_NORMAL)
25 | {
26 | // do nothing
27 | }
28 |
29 | public function renderException(\Exception $e)
30 | {
31 | // do nothing
32 | }
33 | }
34 |
--------------------------------------------------------------------------------
/example/thinkphp/library/think/exception/ClassNotFoundException.php:
--------------------------------------------------------------------------------
1 |
10 | // +----------------------------------------------------------------------
11 |
12 | namespace think\exception;
13 |
14 | class ClassNotFoundException extends \RuntimeException
15 | {
16 | protected $class;
17 | public function __construct($message, $class = '')
18 | {
19 | $this->message = $message;
20 | $this->class = $class;
21 | }
22 |
23 | /**
24 | * 获取类名
25 | * @access public
26 | * @return string
27 | */
28 | public function getClass()
29 | {
30 | return $this->class;
31 | }
32 | }
33 |
--------------------------------------------------------------------------------
/example/thinkphp/.travis.yml:
--------------------------------------------------------------------------------
1 | sudo: false
2 |
3 | language: php
4 |
5 | services:
6 | - memcached
7 | - mongodb
8 | - mysql
9 | - postgresql
10 | - redis-server
11 |
12 | matrix:
13 | fast_finish: true
14 | include:
15 | - php: 5.4
16 | - php: 5.5
17 | - php: 5.6
18 | - php: 7.0
19 | - php: hhvm
20 | allow_failures:
21 | - php: hhvm
22 |
23 | cache:
24 | directories:
25 | - $HOME/.composer/cache
26 |
27 | before_install:
28 | - composer self-update
29 | - mysql -e "create database IF NOT EXISTS test;" -uroot
30 | - psql -c 'DROP DATABASE IF EXISTS test;' -U postgres
31 | - psql -c 'create database test;' -U postgres
32 |
33 | install:
34 | - ./tests/script/install.sh
35 |
36 | script:
37 | ## LINT
38 | - find . -path ./vendor -prune -o -type f -name \*.php -exec php -l {} \;
39 | ## PHP Copy/Paste Detector
40 | - vendor/bin/phpcpd --verbose --exclude vendor ./ || true
41 | ## PHPLOC
42 | - vendor/bin/phploc --exclude vendor ./
43 | ## PHPUNIT
44 | - vendor/bin/phpunit --coverage-clover=coverage.xml --configuration=phpunit.xml
45 |
46 | after_success:
47 | - bash <(curl -s https://codecov.io/bash)
48 |
--------------------------------------------------------------------------------
/example/thinkphp/library/think/exception/TemplateNotFoundException.php:
--------------------------------------------------------------------------------
1 |
10 | // +----------------------------------------------------------------------
11 |
12 | namespace think\exception;
13 |
14 | class TemplateNotFoundException extends \RuntimeException
15 | {
16 | protected $template;
17 |
18 | public function __construct($message, $template = '')
19 | {
20 | $this->message = $message;
21 | $this->template = $template;
22 | }
23 |
24 | /**
25 | * 获取模板文件
26 | * @access public
27 | * @return string
28 | */
29 | public function getTemplate()
30 | {
31 | return $this->template;
32 | }
33 | }
34 |
--------------------------------------------------------------------------------
/example/thinkphp/library/think/exception/ValidateException.php:
--------------------------------------------------------------------------------
1 |
10 | // +----------------------------------------------------------------------
11 |
12 | namespace think\exception;
13 |
14 | class ValidateException extends \RuntimeException
15 | {
16 | protected $error;
17 |
18 | public function __construct($error)
19 | {
20 | $this->error = $error;
21 | $this->message = is_array($error) ? implode("\n\r", $error) : $error;
22 | }
23 |
24 | /**
25 | * 获取验证错误信息
26 | * @access public
27 | * @return array|string
28 | */
29 | public function getError()
30 | {
31 | return $this->error;
32 | }
33 | }
34 |
--------------------------------------------------------------------------------
/example/thinkphp/library/think/config/driver/Xml.php:
--------------------------------------------------------------------------------
1 |
10 | // +----------------------------------------------------------------------
11 |
12 | namespace think\config\driver;
13 |
14 | class Xml
15 | {
16 | public function parse($config)
17 | {
18 | if (is_file($config)) {
19 | $content = simplexml_load_file($config);
20 | } else {
21 | $content = simplexml_load_string($config);
22 | }
23 | $result = (array) $content;
24 | foreach ($result as $key => $val) {
25 | if (is_object($val)) {
26 | $result[$key] = (array) $val;
27 | }
28 | }
29 | return $result;
30 | }
31 | }
32 |
--------------------------------------------------------------------------------
/example/thinkphp/library/think/console/command/make/Model.php:
--------------------------------------------------------------------------------
1 |
10 | // +----------------------------------------------------------------------
11 |
12 | namespace think\console\command\make;
13 |
14 | use think\console\command\Make;
15 |
16 | class Model extends Make
17 | {
18 | protected $type = "Model";
19 |
20 | protected function configure()
21 | {
22 | parent::configure();
23 | $this->setName('make:model')
24 | ->setDescription('Create a new model class');
25 | }
26 |
27 | protected function getStub()
28 | {
29 | return __DIR__ . '/stubs/model.stub';
30 | }
31 |
32 | protected function getNamespace($appNamespace, $module)
33 | {
34 | return parent::getNamespace($appNamespace, $module) . '\model';
35 | }
36 | }
37 |
--------------------------------------------------------------------------------
/example/thinkphp/phpunit.xml:
--------------------------------------------------------------------------------
1 |
2 |
12 |
13 |
14 | ./tests/thinkphp/
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 | ./
23 |
24 | tests
25 | vendor
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
--------------------------------------------------------------------------------
/example/thinkphp/library/think/db/exception/BindParamException.php:
--------------------------------------------------------------------------------
1 |
10 | // +----------------------------------------------------------------------
11 |
12 | namespace think\db\exception;
13 |
14 | use think\exception\DbException;
15 |
16 | /**
17 | * PDO参数绑定异常
18 | */
19 | class BindParamException extends DbException
20 | {
21 |
22 | /**
23 | * BindParamException constructor.
24 | * @param string $message
25 | * @param array $config
26 | * @param string $sql
27 | * @param array $bind
28 | * @param int $code
29 | */
30 | public function __construct($message, $config, $sql, $bind, $code = 10502)
31 | {
32 | $this->setData('Bind Param', $bind);
33 | parent::__construct($message, $config, $sql, $code);
34 | }
35 | }
36 |
--------------------------------------------------------------------------------
/example/thinkphp/library/think/exception/HttpException.php:
--------------------------------------------------------------------------------
1 |
10 | // +----------------------------------------------------------------------
11 |
12 | namespace think\exception;
13 |
14 | class HttpException extends \RuntimeException
15 | {
16 | private $statusCode;
17 | private $headers;
18 |
19 | public function __construct($statusCode, $message = null, \Exception $previous = null, array $headers = [], $code = 0)
20 | {
21 | $this->statusCode = $statusCode;
22 | $this->headers = $headers;
23 |
24 | parent::__construct($message, $code, $previous);
25 | }
26 |
27 | public function getStatusCode()
28 | {
29 | return $this->statusCode;
30 | }
31 |
32 | public function getHeaders()
33 | {
34 | return $this->headers;
35 | }
36 | }
37 |
--------------------------------------------------------------------------------
/example/thinkphp/library/think/Env.php:
--------------------------------------------------------------------------------
1 |
10 | // +----------------------------------------------------------------------
11 |
12 | namespace think;
13 |
14 | class Env
15 | {
16 | /**
17 | * 获取环境变量值
18 | * @param string $name 环境变量名(支持二级 .号分割)
19 | * @param string $default 默认值
20 | * @return mixed
21 | */
22 | public static function get($name, $default = null)
23 | {
24 | $result = getenv(ENV_PREFIX . strtoupper(str_replace('.', '_', $name)));
25 | if (false !== $result) {
26 | if ('false' === $result) {
27 | $result = false;
28 | } elseif ('true' === $result) {
29 | $result = true;
30 | }
31 | return $result;
32 | } else {
33 | return $default;
34 | }
35 | }
36 | }
37 |
--------------------------------------------------------------------------------
/example/LICENSE.txt:
--------------------------------------------------------------------------------
1 |
2 | ThinkPHP遵循Apache2开源协议发布,并提供免费使用。
3 | 版权所有Copyright © 2006-2016 by ThinkPHP (http://thinkphp.cn)
4 | All rights reserved。
5 | ThinkPHP® 商标和著作权所有者为上海顶想信息科技有限公司。
6 |
7 | Apache Licence是著名的非盈利开源组织Apache采用的协议。
8 | 该协议和BSD类似,鼓励代码共享和尊重原作者的著作权,
9 | 允许代码修改,再作为开源或商业软件发布。需要满足
10 | 的条件:
11 | 1. 需要给代码的用户一份Apache Licence ;
12 | 2. 如果你修改了代码,需要在被修改的文件中说明;
13 | 3. 在延伸的代码中(修改和有源代码衍生的代码中)需要
14 | 带有原来代码中的协议,商标,专利声明和其他原来作者规
15 | 定需要包含的说明;
16 | 4. 如果再发布的产品中包含一个Notice文件,则在Notice文
17 | 件中需要带有本协议内容。你可以在Notice中增加自己的
18 | 许可,但不可以表现为对Apache Licence构成更改。
19 | 具体的协议参考:http://www.apache.org/licenses/LICENSE-2.0
20 |
21 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 | FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 | COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 | INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 | BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29 | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 | LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31 | ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 | POSSIBILITY OF SUCH DAMAGE.
33 |
--------------------------------------------------------------------------------
/example/thinkphp/LICENSE.txt:
--------------------------------------------------------------------------------
1 |
2 | ThinkPHP遵循Apache2开源协议发布,并提供免费使用。
3 | 版权所有Copyright © 2006-2016 by ThinkPHP (http://thinkphp.cn)
4 | All rights reserved。
5 | ThinkPHP® 商标和著作权所有者为上海顶想信息科技有限公司。
6 |
7 | Apache Licence是著名的非盈利开源组织Apache采用的协议。
8 | 该协议和BSD类似,鼓励代码共享和尊重原作者的著作权,
9 | 允许代码修改,再作为开源或商业软件发布。需要满足
10 | 的条件:
11 | 1. 需要给代码的用户一份Apache Licence ;
12 | 2. 如果你修改了代码,需要在被修改的文件中说明;
13 | 3. 在延伸的代码中(修改和有源代码衍生的代码中)需要
14 | 带有原来代码中的协议,商标,专利声明和其他原来作者规
15 | 定需要包含的说明;
16 | 4. 如果再发布的产品中包含一个Notice文件,则在Notice文
17 | 件中需要带有本协议内容。你可以在Notice中增加自己的
18 | 许可,但不可以表现为对Apache Licence构成更改。
19 | 具体的协议参考:http://www.apache.org/licenses/LICENSE-2.0
20 |
21 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 | FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 | COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 | INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 | BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29 | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 | LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31 | ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 | POSSIBILITY OF SUCH DAMAGE.
33 |
--------------------------------------------------------------------------------
/example/thinkphp/library/think/model/Pivot.php:
--------------------------------------------------------------------------------
1 |
10 | // +----------------------------------------------------------------------
11 |
12 | namespace think\model;
13 |
14 | use think\Model;
15 |
16 | class Pivot extends Model
17 | {
18 |
19 | /** @var Model */
20 | public $parent;
21 |
22 | protected $autoWriteTimestamp = false;
23 |
24 | /**
25 | * 架构函数
26 | * @access public
27 | * @param Model $parent 上级模型
28 | * @param array|object $data 数据
29 | * @param string $table 中间数据表名
30 | */
31 | public function __construct(Model $parent, $data = [], $table = '')
32 | {
33 | $this->parent = $parent;
34 |
35 | if (is_null($this->name)) {
36 | $this->name = $table;
37 | }
38 |
39 | parent::__construct($data);
40 |
41 | $this->class = $this->name;
42 | }
43 |
44 | }
45 |
--------------------------------------------------------------------------------
/example/thinkphp/library/think/db/exception/ModelNotFoundException.php:
--------------------------------------------------------------------------------
1 |
10 | // +----------------------------------------------------------------------
11 |
12 | namespace think\db\exception;
13 |
14 | use think\exception\DbException;
15 |
16 | class ModelNotFoundException extends DbException
17 | {
18 | protected $model;
19 |
20 | /**
21 | * 构造方法
22 | * @param string $message
23 | * @param string $model
24 | */
25 | public function __construct($message, $model = '', array $config = [])
26 | {
27 | $this->message = $message;
28 | $this->model = $model;
29 |
30 | $this->setData('Database Config', $config);
31 | }
32 |
33 | /**
34 | * 获取模型类名
35 | * @access public
36 | * @return string
37 | */
38 | public function getModel()
39 | {
40 | return $this->model;
41 | }
42 |
43 | }
44 |
--------------------------------------------------------------------------------
/example/thinkphp/library/think/db/exception/DataNotFoundException.php:
--------------------------------------------------------------------------------
1 |
10 | // +----------------------------------------------------------------------
11 |
12 | namespace think\db\exception;
13 |
14 | use think\exception\DbException;
15 |
16 | class DataNotFoundException extends DbException
17 | {
18 | protected $table;
19 |
20 | /**
21 | * DbException constructor.
22 | * @param string $message
23 | * @param string $table
24 | * @param array $config
25 | */
26 | public function __construct($message, $table = '', array $config = [])
27 | {
28 | $this->message = $message;
29 | $this->table = $table;
30 |
31 | $this->setData('Database Config', $config);
32 | }
33 |
34 | /**
35 | * 获取数据表名
36 | * @access public
37 | * @return string
38 | */
39 | public function getTable()
40 | {
41 | return $this->table;
42 | }
43 | }
44 |
--------------------------------------------------------------------------------
/example/thinkphp/library/think/exception/DbException.php:
--------------------------------------------------------------------------------
1 |
10 | // +----------------------------------------------------------------------
11 |
12 | namespace think\exception;
13 |
14 | use think\Exception;
15 |
16 | /**
17 | * Database相关异常处理类
18 | */
19 | class DbException extends Exception
20 | {
21 | /**
22 | * DbException constructor.
23 | * @param string $message
24 | * @param array $config
25 | * @param string $sql
26 | * @param int $code
27 | */
28 | public function __construct($message, array $config, $sql, $code = 10500)
29 | {
30 | $this->message = $message;
31 | $this->code = $code;
32 |
33 | $this->setData('Database Status', [
34 | 'Error Code' => $code,
35 | 'Error Message' => $message,
36 | 'Error SQL' => $sql,
37 | ]);
38 |
39 | unset($config['username'], $config['password']);
40 | $this->setData('Database Config', $config);
41 | }
42 |
43 | }
44 |
--------------------------------------------------------------------------------
/example/thinkphp/library/think/controller/Yar.php:
--------------------------------------------------------------------------------
1 |
10 | // +----------------------------------------------------------------------
11 |
12 | namespace think\controller;
13 |
14 | /**
15 | * ThinkPHP Yar控制器类
16 | */
17 | abstract class Yar
18 | {
19 |
20 | /**
21 | * 构造函数
22 | * @access public
23 | */
24 | public function __construct()
25 | {
26 | //控制器初始化
27 | if (method_exists($this, '_initialize')) {
28 | $this->_initialize();
29 | }
30 |
31 | //判断扩展是否存在
32 | if (!extension_loaded('yar')) {
33 | throw new \Exception('not support yar');
34 | }
35 |
36 | //实例化Yar_Server
37 | $server = new \Yar_Server($this);
38 | // 启动server
39 | $server->handle();
40 | }
41 |
42 | /**
43 | * 魔术方法 有不存在的操作的时候执行
44 | * @access public
45 | * @param string $method 方法名
46 | * @param array $args 参数
47 | * @return mixed
48 | */
49 | public function __call($method, $args)
50 | {}
51 | }
52 |
--------------------------------------------------------------------------------
/example/thinkphp/library/think/exception/PDOException.php:
--------------------------------------------------------------------------------
1 |
10 | // +----------------------------------------------------------------------
11 |
12 | namespace think\exception;
13 |
14 | /**
15 | * PDO异常处理类
16 | * 重新封装了系统的\PDOException类
17 | */
18 | class PDOException extends DbException
19 | {
20 | /**
21 | * PDOException constructor.
22 | * @param \PDOException $exception
23 | * @param array $config
24 | * @param string $sql
25 | * @param int $code
26 | */
27 | public function __construct(\PDOException $exception, array $config, $sql, $code = 10501)
28 | {
29 | $error = $exception->errorInfo;
30 |
31 | $this->setData('PDO Error Info', [
32 | 'SQLSTATE' => $error[0],
33 | 'Driver Error Code' => isset($error[1]) ? $error[1] : 0,
34 | 'Driver Error Message' => isset($error[2]) ? $error[2] : '',
35 | ]);
36 |
37 | parent::__construct($exception->getMessage(), $config, $sql, $code);
38 | }
39 | }
40 |
--------------------------------------------------------------------------------
/example/thinkphp/library/think/console/output/driver/Buffer.php:
--------------------------------------------------------------------------------
1 |
10 | // +----------------------------------------------------------------------
11 |
12 | namespace think\console\output\driver;
13 |
14 | use think\console\Output;
15 |
16 | class Buffer
17 | {
18 | /**
19 | * @var string
20 | */
21 | private $buffer = '';
22 |
23 | public function __construct(Output $output)
24 | {
25 | // do nothing
26 | }
27 |
28 | public function fetch()
29 | {
30 | $content = $this->buffer;
31 | $this->buffer = '';
32 | return $content;
33 | }
34 |
35 | public function write($messages, $newline = false, $options = Output::OUTPUT_NORMAL)
36 | {
37 | $messages = (array) $messages;
38 |
39 | foreach ($messages as $message) {
40 | $this->buffer .= $message;
41 | }
42 | if ($newline) {
43 | $this->buffer .= "\n";
44 | }
45 | }
46 |
47 | public function renderException(\Exception $e)
48 | {
49 | // do nothing
50 | }
51 |
52 | }
53 |
--------------------------------------------------------------------------------
/example/thinkphp/library/traits/think/Instance.php:
--------------------------------------------------------------------------------
1 |
10 | // +----------------------------------------------------------------------
11 |
12 | namespace traits\think;
13 |
14 | use think\Exception;
15 |
16 | trait Instance
17 | {
18 | protected static $instance = null;
19 |
20 | /**
21 | * @param array $options
22 | * @return static
23 | */
24 | public static function instance($options = [])
25 | {
26 | if (is_null(self::$instance)) {
27 | self::$instance = new self($options);
28 | }
29 | return self::$instance;
30 | }
31 |
32 | // 静态调用
33 | public static function __callStatic($method, $params)
34 | {
35 | if (is_null(self::$instance)) {
36 | self::$instance = new self();
37 | }
38 | $call = substr($method, 1);
39 | if (0 === strpos($method, '_') && is_callable([self::$instance, $call])) {
40 | return call_user_func_array([self::$instance, $call], $params);
41 | } else {
42 | throw new Exception("method not exists:" . $method);
43 | }
44 | }
45 | }
46 |
--------------------------------------------------------------------------------
/example/thinkphp/library/think/console/command/make/stubs/controller.stub:
--------------------------------------------------------------------------------
1 |
10 | // +----------------------------------------------------------------------
11 |
12 | namespace think;
13 |
14 | class Exception extends \Exception
15 | {
16 |
17 | /**
18 | * 保存异常页面显示的额外Debug数据
19 | * @var array
20 | */
21 | protected $data = [];
22 |
23 | /**
24 | * 设置异常额外的Debug数据
25 | * 数据将会显示为下面的格式
26 | *
27 | * Exception Data
28 | * --------------------------------------------------
29 | * Label 1
30 | * key1 value1
31 | * key2 value2
32 | * Label 2
33 | * key1 value1
34 | * key2 value2
35 | *
36 | * @param string $label 数据分类,用于异常页面显示
37 | * @param array $data 需要显示的数据,必须为关联数组
38 | */
39 | final protected function setData($label, array $data)
40 | {
41 | $this->data[$label] = $data;
42 | }
43 |
44 | /**
45 | * 获取异常额外Debug数据
46 | * 主要用于输出到异常页面便于调试
47 | * @return array 由setData设置的Debug数据
48 | */
49 | final public function getData()
50 | {
51 | return $this->data;
52 | }
53 |
54 | }
55 |
--------------------------------------------------------------------------------
/example/thinkphp/library/think/process/exception/Failed.php:
--------------------------------------------------------------------------------
1 |
10 | // +----------------------------------------------------------------------
11 |
12 | namespace think\process\exception;
13 |
14 | use think\Process;
15 |
16 | class Failed extends \RuntimeException
17 | {
18 |
19 | private $process;
20 |
21 | public function __construct(Process $process)
22 | {
23 | if ($process->isSuccessful()) {
24 | throw new \InvalidArgumentException('Expected a failed process, but the given process was successful.');
25 | }
26 |
27 | $error = sprintf('The command "%s" failed.' . "\nExit Code: %s(%s)", $process->getCommandLine(), $process->getExitCode(), $process->getExitCodeText());
28 |
29 | if (!$process->isOutputDisabled()) {
30 | $error .= sprintf("\n\nOutput:\n================\n%s\n\nError Output:\n================\n%s", $process->getOutput(), $process->getErrorOutput());
31 | }
32 |
33 | parent::__construct($error);
34 |
35 | $this->process = $process;
36 | }
37 |
38 | public function getProcess()
39 | {
40 | return $this->process;
41 | }
42 | }
43 |
--------------------------------------------------------------------------------
/example/thinkphp/library/think/response/Json.php:
--------------------------------------------------------------------------------
1 |
10 | // +----------------------------------------------------------------------
11 |
12 | namespace think\response;
13 |
14 | use think\Response;
15 |
16 | class Json extends Response
17 | {
18 | // 输出参数
19 | protected $options = [
20 | 'json_encode_param' => JSON_UNESCAPED_UNICODE,
21 | ];
22 |
23 | protected $contentType = 'application/json';
24 |
25 | /**
26 | * 处理数据
27 | * @access protected
28 | * @param mixed $data 要处理的数据
29 | * @return mixed
30 | * @throws \Exception
31 | */
32 | protected function output($data)
33 | {
34 | try {
35 | // 返回JSON数据格式到客户端 包含状态信息
36 | $data = json_encode($data, $this->options['json_encode_param']);
37 |
38 | if ($data === false) {
39 | throw new \InvalidArgumentException(json_last_error_msg());
40 | }
41 |
42 | return $data;
43 | } catch (\Exception $e) {
44 | if ($e->getPrevious()) {
45 | throw $e->getPrevious();
46 | }
47 | throw $e;
48 | }
49 | }
50 |
51 | }
52 |
--------------------------------------------------------------------------------
/example/thinkphp/library/think/exception/ThrowableError.php:
--------------------------------------------------------------------------------
1 |
10 | // +----------------------------------------------------------------------
11 |
12 | namespace think\exception;
13 |
14 | class ThrowableError extends \ErrorException
15 | {
16 | public function __construct(\Throwable $e)
17 | {
18 |
19 | if ($e instanceof \ParseError) {
20 | $message = 'Parse error: ' . $e->getMessage();
21 | $severity = E_PARSE;
22 | } elseif ($e instanceof \TypeError) {
23 | $message = 'Type error: ' . $e->getMessage();
24 | $severity = E_RECOVERABLE_ERROR;
25 | } else {
26 | $message = 'Fatal error: ' . $e->getMessage();
27 | $severity = E_ERROR;
28 | }
29 |
30 | parent::__construct(
31 | $message,
32 | $e->getCode(),
33 | $severity,
34 | $e->getFile(),
35 | $e->getLine()
36 | );
37 |
38 | $this->setTrace($e->getTrace());
39 | }
40 |
41 | protected function setTrace($trace)
42 | {
43 | $traceReflector = new \ReflectionProperty('Exception', 'trace');
44 | $traceReflector->setAccessible(true);
45 | $traceReflector->setValue($this, $trace);
46 | }
47 | }
48 |
--------------------------------------------------------------------------------
/example/application/database.php:
--------------------------------------------------------------------------------
1 |
10 | // +----------------------------------------------------------------------
11 |
12 | return [
13 | // 数据库类型
14 | 'type' => 'mysql',
15 | // 服务器地址
16 | 'hostname' => '127.0.0.1',
17 | // 数据库名
18 | 'database' => '',
19 | // 用户名
20 | 'username' => 'root',
21 | // 密码
22 | 'password' => '',
23 | // 端口
24 | 'hostport' => '',
25 | // 连接dsn
26 | 'dsn' => '',
27 | // 数据库连接参数
28 | 'params' => [],
29 | // 数据库编码默认采用utf8
30 | 'charset' => 'utf8',
31 | // 数据库表前缀
32 | 'prefix' => '',
33 | // 数据库调试模式
34 | 'debug' => true,
35 | // 数据库部署方式:0 集中式(单一服务器),1 分布式(主从服务器)
36 | 'deploy' => 0,
37 | // 数据库读写是否分离 主从式有效
38 | 'rw_separate' => false,
39 | // 读写分离后 主服务器数量
40 | 'master_num' => 1,
41 | // 指定从服务器序号
42 | 'slave_no' => '',
43 | // 是否严格检查字段是否存在
44 | 'fields_strict' => true,
45 | // 数据集返回类型
46 | 'resultset_type' => 'array',
47 | // 自动写入时间戳字段
48 | 'auto_timestamp' => false,
49 | // 时间字段取出后的默认时间格式
50 | 'datetime_format' => 'Y-m-d H:i:s',
51 | // 是否需要进行SQL性能分析
52 | 'sql_explain' => false,
53 | ];
54 |
--------------------------------------------------------------------------------
/example/thinkphp/library/think/console/command/make/Controller.php:
--------------------------------------------------------------------------------
1 |
10 | // +----------------------------------------------------------------------
11 |
12 | namespace think\console\command\make;
13 |
14 | use think\Config;
15 | use think\console\command\Make;
16 | use think\console\input\Option;
17 |
18 | class Controller extends Make
19 | {
20 |
21 | protected $type = "Controller";
22 |
23 | protected function configure()
24 | {
25 | parent::configure();
26 | $this->setName('make:controller')
27 | ->addOption('plain', null, Option::VALUE_NONE, 'Generate an empty controller class.')
28 | ->setDescription('Create a new resource controller class');
29 | }
30 |
31 | protected function getStub()
32 | {
33 | if ($this->input->getOption('plain')) {
34 | return __DIR__ . '/stubs/controller.plain.stub';
35 | }
36 |
37 | return __DIR__ . '/stubs/controller.stub';
38 | }
39 |
40 | protected function getClassName($name)
41 | {
42 | return parent::getClassName($name) . (Config::get('controller_suffix') ? ucfirst(Config::get('url_controller_layer')) : '');
43 | }
44 |
45 | protected function getNamespace($appNamespace, $module)
46 | {
47 | return parent::getNamespace($appNamespace, $module) . '\controller';
48 | }
49 |
50 | }
51 |
--------------------------------------------------------------------------------
/example/thinkphp/library/think/exception/ErrorException.php:
--------------------------------------------------------------------------------
1 |
10 | // +----------------------------------------------------------------------
11 |
12 | namespace think\exception;
13 |
14 | use think\Exception;
15 |
16 | /**
17 | * ThinkPHP错误异常
18 | * 主要用于封装 set_error_handler 和 register_shutdown_function 得到的错误
19 | * 除开从 think\Exception 继承的功能
20 | * 其他和PHP系统\ErrorException功能基本一样
21 | */
22 | class ErrorException extends Exception
23 | {
24 | /**
25 | * 用于保存错误级别
26 | * @var integer
27 | */
28 | protected $severity;
29 |
30 | /**
31 | * 错误异常构造函数
32 | * @param integer $severity 错误级别
33 | * @param string $message 错误详细信息
34 | * @param string $file 出错文件路径
35 | * @param integer $line 出错行号
36 | * @param array $context 错误上下文,会包含错误触发处作用域内所有变量的数组
37 | */
38 | public function __construct($severity, $message, $file, $line, array $context = [])
39 | {
40 | $this->severity = $severity;
41 | $this->message = $message;
42 | $this->file = $file;
43 | $this->line = $line;
44 | $this->code = 0;
45 |
46 | empty($context) || $this->setData('Error Context', $context);
47 | }
48 |
49 | /**
50 | * 获取错误级别
51 | * @return integer 错误级别
52 | */
53 | final public function getSeverity()
54 | {
55 | return $this->severity;
56 | }
57 | }
58 |
--------------------------------------------------------------------------------
/ppython/python/php_python.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env python3
2 | # -*- coding: UTF-8 -*-
3 | import time
4 | import socket
5 | import process
6 | import logger
7 |
8 | # -------------------------------------------------
9 | # 基本配置
10 | # -------------------------------------------------
11 | LISTEN_PORT = 21230 # 服务侦听端口
12 | CHARSET = "utf-8" # 设置字符集(和PHP交互的字符集)
13 | LISTEN_NUM = 5 # 操作系统可以挂起的最大连接数量。该值至少为1,大部分应用程序设为5就可以了
14 |
15 | # -------------------------------------------------
16 | # 主程序
17 | # 请不要随意修改下面的代码
18 | # -------------------------------------------------
19 | if __name__ == '__main__':
20 | s = None
21 | err_msg = ''
22 | for res in socket.getaddrinfo('', LISTEN_PORT, socket.AF_INET,
23 | socket.SOCK_STREAM, 0, socket.AI_PASSIVE):
24 | af, socktype, proto, canonname, sa = res
25 | try:
26 | s = socket.socket(af, socktype, proto)
27 | except OSError as msg:
28 | s = None
29 | err_msg = msg
30 | continue
31 | try:
32 | s.bind(sa)
33 | s.listen(LISTEN_NUM)
34 | except OSError as msg:
35 | s.close()
36 | s = None
37 | err_msg = msg
38 | continue
39 | break
40 | if s is None:
41 | logger.loginfo('could not open socket:' + str(err_msg))
42 | sys.exit(1)
43 |
44 | logger.loginfo("-------------------------------------------")
45 | logger.loginfo("- PPython Service")
46 | logger.loginfo("- Time: %s" %
47 | time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time())))
48 | logger.loginfo("-------------------------------------------")
49 | logger.loginfo("Listen port: %d" % LISTEN_PORT)
50 | logger.loginfo("charset: %s" % CHARSET)
51 | logger.loginfo("Server startup...")
52 |
53 | while True:
54 | conn, addr = s.accept()
55 | try:
56 | process.ProcessThread(conn).start()
57 | except Exception as e:
58 | logger.loginfo(str(e))
59 | pass
60 |
--------------------------------------------------------------------------------
/example/thinkphp/library/think/console/command/Clear.php:
--------------------------------------------------------------------------------
1 |
10 | // +----------------------------------------------------------------------
11 | namespace think\console\command;
12 |
13 | use think\console\Command;
14 | use think\console\Input;
15 | use think\console\input\Option;
16 | use think\console\Output;
17 |
18 | class Clear extends Command
19 | {
20 | protected function configure()
21 | {
22 | // 指令配置
23 | $this
24 | ->setName('clear')
25 | ->addOption('path', 'd', Option::VALUE_OPTIONAL, 'path to clear', null)
26 | ->setDescription('Clear runtime file');
27 | }
28 |
29 | protected function execute(Input $input, Output $output)
30 | {
31 | $path = $input->getOption('path') ?: RUNTIME_PATH;
32 |
33 | if (is_dir($path)) {
34 | $this->clearPath($path);
35 | }
36 |
37 | $output->writeln("Clear Successed");
38 | }
39 |
40 | protected function clearPath($path)
41 | {
42 | $path = realpath($path) . DS;
43 | $files = scandir($path);
44 | if ($files) {
45 | foreach ($files as $file) {
46 | if ('.' != $file && '..' != $file && is_dir($path . $file)) {
47 | $this->clearPath($path . $file);
48 | } elseif ('.gitignore' != $file && is_file($path . $file)) {
49 | unlink($path . $file);
50 | }
51 | }
52 | }
53 | }
54 | }
55 |
--------------------------------------------------------------------------------
/example/thinkphp/library/think/console/output/question/Confirmation.php:
--------------------------------------------------------------------------------
1 |
10 | // +----------------------------------------------------------------------
11 |
12 | namespace think\console\output\question;
13 |
14 | use think\console\output\Question;
15 |
16 | class Confirmation extends Question
17 | {
18 |
19 | private $trueAnswerRegex;
20 |
21 | /**
22 | * 构造方法
23 | * @param string $question 问题
24 | * @param bool $default 默认答案
25 | * @param string $trueAnswerRegex 验证正则
26 | */
27 | public function __construct($question, $default = true, $trueAnswerRegex = '/^y/i')
28 | {
29 | parent::__construct($question, (bool) $default);
30 |
31 | $this->trueAnswerRegex = $trueAnswerRegex;
32 | $this->setNormalizer($this->getDefaultNormalizer());
33 | }
34 |
35 | /**
36 | * 获取默认的答案回调
37 | * @return callable
38 | */
39 | private function getDefaultNormalizer()
40 | {
41 | $default = $this->getDefault();
42 | $regex = $this->trueAnswerRegex;
43 |
44 | return function ($answer) use ($default, $regex) {
45 | if (is_bool($answer)) {
46 | return $answer;
47 | }
48 |
49 | $answerIsTrue = (bool) preg_match($regex, $answer);
50 | if (false === $default) {
51 | return $answer && $answerIsTrue;
52 | }
53 |
54 | return !$answer || $answerIsTrue;
55 | };
56 | }
57 | }
58 |
--------------------------------------------------------------------------------
/example/thinkphp/library/think/console/command/Build.php:
--------------------------------------------------------------------------------
1 |
10 | // +----------------------------------------------------------------------
11 |
12 | namespace think\console\command;
13 |
14 | use think\console\Command;
15 | use think\console\Input;
16 | use think\console\input\Option;
17 | use think\console\Output;
18 |
19 | class Build extends Command
20 | {
21 |
22 | /**
23 | * {@inheritdoc}
24 | */
25 | protected function configure()
26 | {
27 | $this->setName('build')
28 | ->setDefinition([
29 | new Option('config', null, Option::VALUE_OPTIONAL, "build.php path"),
30 | new Option('module', null, Option::VALUE_OPTIONAL, "module name"),
31 | ])
32 | ->setDescription('Build Application Dirs');
33 | }
34 |
35 | protected function execute(Input $input, Output $output)
36 | {
37 | if ($input->hasOption('module')) {
38 | \think\Build::module($input->getOption('module'));
39 | $output->writeln("Successed");
40 | return;
41 | }
42 |
43 | if ($input->hasOption('config')) {
44 | $build = include $input->getOption('config');
45 | } else {
46 | $build = include APP_PATH . 'build.php';
47 | }
48 | if (empty($build)) {
49 | $output->writeln("Build Config Is Empty");
50 | return;
51 | }
52 | \think\Build::run($build);
53 | $output->writeln("Successed");
54 |
55 | }
56 | }
57 |
--------------------------------------------------------------------------------
/example/thinkphp/library/think/process/exception/Timeout.php:
--------------------------------------------------------------------------------
1 |
10 | // +----------------------------------------------------------------------
11 |
12 | namespace think\process\exception;
13 |
14 | use think\Process;
15 |
16 | class Timeout extends \RuntimeException
17 | {
18 |
19 | const TYPE_GENERAL = 1;
20 | const TYPE_IDLE = 2;
21 |
22 | private $process;
23 | private $timeoutType;
24 |
25 | public function __construct(Process $process, $timeoutType)
26 | {
27 | $this->process = $process;
28 | $this->timeoutType = $timeoutType;
29 |
30 | parent::__construct(sprintf('The process "%s" exceeded the timeout of %s seconds.', $process->getCommandLine(), $this->getExceededTimeout()));
31 | }
32 |
33 | public function getProcess()
34 | {
35 | return $this->process;
36 | }
37 |
38 | public function isGeneralTimeout()
39 | {
40 | return $this->timeoutType === self::TYPE_GENERAL;
41 | }
42 |
43 | public function isIdleTimeout()
44 | {
45 | return $this->timeoutType === self::TYPE_IDLE;
46 | }
47 |
48 | public function getExceededTimeout()
49 | {
50 | switch ($this->timeoutType) {
51 | case self::TYPE_GENERAL:
52 | return $this->process->getTimeout();
53 |
54 | case self::TYPE_IDLE:
55 | return $this->process->getIdleTimeout();
56 |
57 | default:
58 | throw new \LogicException(sprintf('Unknown timeout type "%d".', $this->timeoutType));
59 | }
60 | }
61 | }
62 |
--------------------------------------------------------------------------------
/example/thinkphp/library/think/response/Jsonp.php:
--------------------------------------------------------------------------------
1 |
10 | // +----------------------------------------------------------------------
11 |
12 | namespace think\response;
13 |
14 | use think\Request;
15 | use think\Response;
16 |
17 | class Jsonp extends Response
18 | {
19 | // 输出参数
20 | protected $options = [
21 | 'var_jsonp_handler' => 'callback',
22 | 'default_jsonp_handler' => 'jsonpReturn',
23 | 'json_encode_param' => JSON_UNESCAPED_UNICODE,
24 | ];
25 |
26 | protected $contentType = 'application/javascript';
27 |
28 | /**
29 | * 处理数据
30 | * @access protected
31 | * @param mixed $data 要处理的数据
32 | * @return mixed
33 | * @throws \Exception
34 | */
35 | protected function output($data)
36 | {
37 | try {
38 | // 返回JSON数据格式到客户端 包含状态信息 [当url_common_param为false时是无法获取到$_GET的数据的,故使用Request来获取]
39 | $var_jsonp_handler = Request::instance()->param($this->options['var_jsonp_handler'], "");
40 | $handler = !empty($var_jsonp_handler) ? $var_jsonp_handler : $this->options['default_jsonp_handler'];
41 |
42 | $data = json_encode($data, $this->options['json_encode_param']);
43 |
44 | if ($data === false) {
45 | throw new \InvalidArgumentException(json_last_error_msg());
46 | }
47 |
48 | $data = $handler . '(' . $data . ');';
49 | return $data;
50 | } catch (\Exception $e) {
51 | if ($e->getPrevious()) {
52 | throw $e->getPrevious();
53 | }
54 | throw $e;
55 | }
56 | }
57 |
58 | }
59 |
--------------------------------------------------------------------------------
/example/thinkphp/tpl/dispatch_jump.tpl:
--------------------------------------------------------------------------------
1 | {__NOLAYOUT__}
2 |
3 |
4 |
5 | 跳转提示
6 |
16 |
17 |
18 |
19 |
20 |
21 |
:)
22 |
23 |
24 |
25 |
:(
26 |
27 |
28 |
29 |
30 |
31 | 页面自动 跳转 等待时间:
32 |
33 |
34 |
47 |
48 |
49 |
--------------------------------------------------------------------------------
/example/thinkphp/library/think/db/builder/Sqlite.php:
--------------------------------------------------------------------------------
1 |
10 | // +----------------------------------------------------------------------
11 |
12 | namespace think\db\builder;
13 |
14 | use think\db\Builder;
15 |
16 | /**
17 | * Sqlite数据库驱动
18 | */
19 | class Sqlite extends Builder
20 | {
21 |
22 | /**
23 | * limit
24 | * @access public
25 | * @return string
26 | */
27 | public function parseLimit($limit)
28 | {
29 | $limitStr = '';
30 | if (!empty($limit)) {
31 | $limit = explode(',', $limit);
32 | if (count($limit) > 1) {
33 | $limitStr .= ' LIMIT ' . $limit[1] . ' OFFSET ' . $limit[0] . ' ';
34 | } else {
35 | $limitStr .= ' LIMIT ' . $limit[0] . ' ';
36 | }
37 | }
38 | return $limitStr;
39 | }
40 |
41 | /**
42 | * 随机排序
43 | * @access protected
44 | * @return string
45 | */
46 | protected function parseRand()
47 | {
48 | return 'RANDOM()';
49 | }
50 |
51 | /**
52 | * 字段和表名处理
53 | * @access protected
54 | * @param string $key
55 | * @param array $options
56 | * @return string
57 | */
58 | protected function parseKey($key, $options = [])
59 | {
60 | $key = trim($key);
61 | if (strpos($key, '.')) {
62 | list($table, $key) = explode('.', $key, 2);
63 | if ('__TABLE__' == $table) {
64 | $table = $this->query->getTable();
65 | }
66 | if (isset($options['alias'][$table])) {
67 | $table = $options['alias'][$table];
68 | }
69 | }
70 | if (isset($table)) {
71 | $key = $table . '.' . $key;
72 | }
73 | return $key;
74 | }
75 | }
76 |
--------------------------------------------------------------------------------
/example/thinkphp/library/think/template/driver/File.php:
--------------------------------------------------------------------------------
1 |
10 | // +----------------------------------------------------------------------
11 |
12 | namespace think\template\driver;
13 |
14 | use think\Exception;
15 |
16 | class File
17 | {
18 | /**
19 | * 写入编译缓存
20 | * @param string $cacheFile 缓存的文件名
21 | * @param string $content 缓存的内容
22 | * @return void|array
23 | */
24 | public function write($cacheFile, $content)
25 | {
26 | // 检测模板目录
27 | $dir = dirname($cacheFile);
28 | if (!is_dir($dir)) {
29 | mkdir($dir, 0755, true);
30 | }
31 | // 生成模板缓存文件
32 | if (false === file_put_contents($cacheFile, $content)) {
33 | throw new Exception('cache write error:' . $cacheFile, 11602);
34 | }
35 | }
36 |
37 | /**
38 | * 读取编译编译
39 | * @param string $cacheFile 缓存的文件名
40 | * @param array $vars 变量数组
41 | * @return void
42 | */
43 | public function read($cacheFile, $vars = [])
44 | {
45 | if (!empty($vars) && is_array($vars)) {
46 | // 模板阵列变量分解成为独立变量
47 | extract($vars, EXTR_OVERWRITE);
48 | }
49 | //载入模版缓存文件
50 | include $cacheFile;
51 | }
52 |
53 | /**
54 | * 检查编译缓存是否有效
55 | * @param string $cacheFile 缓存的文件名
56 | * @param int $cacheTime 缓存时间
57 | * @return boolean
58 | */
59 | public function check($cacheFile, $cacheTime)
60 | {
61 | // 缓存文件不存在, 直接返回false
62 | if (!file_exists($cacheFile)) {
63 | return false;
64 | }
65 | if (0 != $cacheTime && $_SERVER['REQUEST_TIME'] > filemtime($cacheFile) + $cacheTime) {
66 | // 缓存是否在有效期
67 | return false;
68 | }
69 | return true;
70 | }
71 | }
72 |
--------------------------------------------------------------------------------
/example/thinkphp/library/think/db/builder/Mysql.php:
--------------------------------------------------------------------------------
1 |
10 | // +----------------------------------------------------------------------
11 |
12 | namespace think\db\builder;
13 |
14 | use think\db\Builder;
15 |
16 | /**
17 | * mysql数据库驱动
18 | */
19 | class Mysql extends Builder
20 | {
21 | protected $updateSql = 'UPDATE %TABLE% %JOIN% SET %SET% %WHERE% %ORDER%%LIMIT% %LOCK%%COMMENT%';
22 |
23 | /**
24 | * 字段和表名处理
25 | * @access protected
26 | * @param string $key
27 | * @param array $options
28 | * @return string
29 | */
30 | protected function parseKey($key, $options = [])
31 | {
32 | $key = trim($key);
33 | if (strpos($key, '$.') && false === strpos($key, '(')) {
34 | // JSON字段支持
35 | list($field, $name) = explode('$.', $key);
36 | $key = 'json_extract(' . $field . ', \'$.' . $name . '\')';
37 | } elseif (strpos($key, '.') && !preg_match('/[,\'\"\(\)`\s]/', $key)) {
38 | list($table, $key) = explode('.', $key, 2);
39 | if ('__TABLE__' == $table) {
40 | $table = $this->query->getTable();
41 | }
42 | if (isset($options['alias'][$table])) {
43 | $table = $options['alias'][$table];
44 | }
45 | }
46 | if (!preg_match('/[,\'\"\*\(\)`.\s]/', $key)) {
47 | $key = '`' . $key . '`';
48 | }
49 | if (isset($table)) {
50 | if (strpos($table, '.')) {
51 | $table = str_replace('.', '`.`', $table);
52 | }
53 | $key = '`' . $table . '`.' . $key;
54 | }
55 | return $key;
56 | }
57 |
58 | /**
59 | * 随机排序
60 | * @access protected
61 | * @return string
62 | */
63 | protected function parseRand()
64 | {
65 | return 'rand()';
66 | }
67 |
68 | }
69 |
--------------------------------------------------------------------------------
/example/thinkphp/library/think/console/command/Help.php:
--------------------------------------------------------------------------------
1 |
10 | // +----------------------------------------------------------------------
11 |
12 | namespace think\console\command;
13 |
14 | use think\console\Command;
15 | use think\console\Input;
16 | use think\console\input\Argument as InputArgument;
17 | use think\console\input\Option as InputOption;
18 | use think\console\Output;
19 |
20 | class Help extends Command
21 | {
22 |
23 | private $command;
24 |
25 | /**
26 | * {@inheritdoc}
27 | */
28 | protected function configure()
29 | {
30 | $this->ignoreValidationErrors();
31 |
32 | $this->setName('help')->setDefinition([
33 | new InputArgument('command_name', InputArgument::OPTIONAL, 'The command name', 'help'),
34 | new InputOption('raw', null, InputOption::VALUE_NONE, 'To output raw command help'),
35 | ])->setDescription('Displays help for a command')->setHelp(<<%command.name% command displays help for a given command:
37 |
38 | php %command.full_name% list
39 |
40 | To display the list of available commands, please use the list command.
41 | EOF
42 | );
43 | }
44 |
45 | /**
46 | * Sets the command.
47 | * @param Command $command The command to set
48 | */
49 | public function setCommand(Command $command)
50 | {
51 | $this->command = $command;
52 | }
53 |
54 | /**
55 | * {@inheritdoc}
56 | */
57 | protected function execute(Input $input, Output $output)
58 | {
59 | if (null === $this->command) {
60 | $this->command = $this->getConsole()->find($input->getArgument('command_name'));
61 | }
62 |
63 | $output->describe($this->command, [
64 | 'raw_text' => $input->getOption('raw'),
65 | ]);
66 |
67 | $this->command = null;
68 | }
69 | }
70 |
--------------------------------------------------------------------------------
/example/thinkphp/library/think/console/command/Lists.php:
--------------------------------------------------------------------------------
1 |
10 | // +----------------------------------------------------------------------
11 |
12 | namespace think\console\command;
13 |
14 | use think\console\Command;
15 | use think\console\Input;
16 | use think\console\Output;
17 | use think\console\input\Argument as InputArgument;
18 | use think\console\input\Option as InputOption;
19 | use think\console\input\Definition as InputDefinition;
20 |
21 | class Lists extends Command
22 | {
23 |
24 | /**
25 | * {@inheritdoc}
26 | */
27 | protected function configure()
28 | {
29 | $this->setName('list')->setDefinition($this->createDefinition())->setDescription('Lists commands')->setHelp(<<%command.name% command lists all commands:
31 |
32 | php %command.full_name%
33 |
34 | You can also display the commands for a specific namespace:
35 |
36 | php %command.full_name% test
37 |
38 | It's also possible to get raw list of commands (useful for embedding command runner):
39 |
40 | php %command.full_name% --raw
41 | EOF
42 | );
43 | }
44 |
45 | /**
46 | * {@inheritdoc}
47 | */
48 | public function getNativeDefinition()
49 | {
50 | return $this->createDefinition();
51 | }
52 |
53 | /**
54 | * {@inheritdoc}
55 | */
56 | protected function execute(Input $input, Output $output)
57 | {
58 | $output->describe($this->getConsole(), [
59 | 'raw_text' => $input->getOption('raw'),
60 | 'namespace' => $input->getArgument('namespace'),
61 | ]);
62 | }
63 |
64 | /**
65 | * {@inheritdoc}
66 | */
67 | private function createDefinition()
68 | {
69 | return new InputDefinition([
70 | new InputArgument('namespace', InputArgument::OPTIONAL, 'The namespace name'),
71 | new InputOption('raw', null, InputOption::VALUE_NONE, 'To output raw command list')
72 | ]);
73 | }
74 | }
75 |
--------------------------------------------------------------------------------
/example/thinkphp/library/think/db/builder/Pgsql.php:
--------------------------------------------------------------------------------
1 |
10 | // +----------------------------------------------------------------------
11 |
12 | namespace think\db\builder;
13 |
14 | use think\db\Builder;
15 |
16 | /**
17 | * Pgsql数据库驱动
18 | */
19 | class Pgsql extends Builder
20 | {
21 |
22 | /**
23 | * limit分析
24 | * @access protected
25 | * @param mixed $limit
26 | * @return string
27 | */
28 | public function parseLimit($limit)
29 | {
30 | $limitStr = '';
31 | if (!empty($limit)) {
32 | $limit = explode(',', $limit);
33 | if (count($limit) > 1) {
34 | $limitStr .= ' LIMIT ' . $limit[1] . ' OFFSET ' . $limit[0] . ' ';
35 | } else {
36 | $limitStr .= ' LIMIT ' . $limit[0] . ' ';
37 | }
38 | }
39 | return $limitStr;
40 | }
41 |
42 | /**
43 | * 字段和表名处理
44 | * @access protected
45 | * @param string $key
46 | * @param array $options
47 | * @return string
48 | */
49 | protected function parseKey($key, $options = [])
50 | {
51 | $key = trim($key);
52 | if (strpos($key, '$.') && false === strpos($key, '(')) {
53 | // JSON字段支持
54 | list($field, $name) = explode('$.', $key);
55 | $key = $field . '->>\'' . $name . '\'';
56 | } elseif (strpos($key, '.')) {
57 | list($table, $key) = explode('.', $key, 2);
58 | if ('__TABLE__' == $table) {
59 | $table = $this->query->getTable();
60 | }
61 | if (isset($options['alias'][$table])) {
62 | $table = $options['alias'][$table];
63 | }
64 | }
65 | if (isset($table)) {
66 | $key = $table . '.' . $key;
67 | }
68 | return $key;
69 | }
70 |
71 | /**
72 | * 随机排序
73 | * @access protected
74 | * @return string
75 | */
76 | protected function parseRand()
77 | {
78 | return 'RANDOM()';
79 | }
80 |
81 | }
82 |
--------------------------------------------------------------------------------
/example/thinkphp/library/think/process/Utils.php:
--------------------------------------------------------------------------------
1 |
8 | // +----------------------------------------------------------------------
9 |
10 | namespace think\process;
11 |
12 | class Utils
13 | {
14 |
15 | /**
16 | * 转义字符串
17 | * @param string $argument
18 | * @return string
19 | */
20 | public static function escapeArgument($argument)
21 | {
22 |
23 | if ('' === $argument) {
24 | return escapeshellarg($argument);
25 | }
26 | $escapedArgument = '';
27 | $quote = false;
28 | foreach (preg_split('/(")/i', $argument, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE) as $part) {
29 | if ('"' === $part) {
30 | $escapedArgument .= '\\"';
31 | } elseif (self::isSurroundedBy($part, '%')) {
32 | // Avoid environment variable expansion
33 | $escapedArgument .= '^%"' . substr($part, 1, -1) . '"^%';
34 | } else {
35 | // escape trailing backslash
36 | if ('\\' === substr($part, -1)) {
37 | $part .= '\\';
38 | }
39 | $quote = true;
40 | $escapedArgument .= $part;
41 | }
42 | }
43 | if ($quote) {
44 | $escapedArgument = '"' . $escapedArgument . '"';
45 | }
46 | return $escapedArgument;
47 | }
48 |
49 | /**
50 | * 验证并进行规范化Process输入。
51 | * @param string $caller
52 | * @param mixed $input
53 | * @return string
54 | * @throws \InvalidArgumentException
55 | */
56 | public static function validateInput($caller, $input)
57 | {
58 | if (null !== $input) {
59 | if (is_resource($input)) {
60 | return $input;
61 | }
62 | if (is_scalar($input)) {
63 | return (string) $input;
64 | }
65 | throw new \InvalidArgumentException(sprintf('%s only accepts strings or stream resources.', $caller));
66 | }
67 | return $input;
68 | }
69 |
70 | private static function isSurroundedBy($arg, $char)
71 | {
72 | return 2 < strlen($arg) && $char === $arg[0] && $char === $arg[strlen($arg) - 1];
73 | }
74 |
75 | }
76 |
--------------------------------------------------------------------------------
/example/thinkphp/library/think/process/pipes/Pipes.php:
--------------------------------------------------------------------------------
1 |
10 | // +----------------------------------------------------------------------
11 |
12 | namespace think\process\pipes;
13 |
14 | abstract class Pipes
15 | {
16 |
17 | /** @var array */
18 | public $pipes = [];
19 |
20 | /** @var string */
21 | protected $inputBuffer = '';
22 | /** @var resource|null */
23 | protected $input;
24 |
25 | /** @var bool */
26 | private $blocked = true;
27 |
28 | const CHUNK_SIZE = 16384;
29 |
30 | /**
31 | * 返回用于 proc_open 描述符的数组
32 | * @return array
33 | */
34 | abstract public function getDescriptors();
35 |
36 | /**
37 | * 返回一个数组的索引由其相关的流,以防这些管道使用的临时文件的文件名。
38 | * @return string[]
39 | */
40 | abstract public function getFiles();
41 |
42 | /**
43 | * 文件句柄和管道中读取数据。
44 | * @param bool $blocking 是否使用阻塞调用
45 | * @param bool $close 是否要关闭管道,如果他们已经到达 EOF。
46 | * @return string[]
47 | */
48 | abstract public function readAndWrite($blocking, $close = false);
49 |
50 | /**
51 | * 返回当前状态如果有打开的文件句柄或管道。
52 | * @return bool
53 | */
54 | abstract public function areOpen();
55 |
56 | /**
57 | * {@inheritdoc}
58 | */
59 | public function close()
60 | {
61 | foreach ($this->pipes as $pipe) {
62 | fclose($pipe);
63 | }
64 | $this->pipes = [];
65 | }
66 |
67 | /**
68 | * 检查系统调用已被中断
69 | * @return bool
70 | */
71 | protected function hasSystemCallBeenInterrupted()
72 | {
73 | $lastError = error_get_last();
74 |
75 | return isset($lastError['message']) && false !== stripos($lastError['message'], 'interrupted system call');
76 | }
77 |
78 | protected function unblock()
79 | {
80 | if (!$this->blocked) {
81 | return;
82 | }
83 |
84 | foreach ($this->pipes as $pipe) {
85 | stream_set_blocking($pipe, 0);
86 | }
87 | if (null !== $this->input) {
88 | stream_set_blocking($this->input, 0);
89 | }
90 |
91 | $this->blocked = false;
92 | }
93 | }
94 |
--------------------------------------------------------------------------------
/example/thinkphp/base.php:
--------------------------------------------------------------------------------
1 |
10 | // +----------------------------------------------------------------------
11 |
12 | define('THINK_VERSION', '5.0.10');
13 | define('THINK_START_TIME', microtime(true));
14 | define('THINK_START_MEM', memory_get_usage());
15 | define('EXT', '.php');
16 | define('DS', DIRECTORY_SEPARATOR);
17 | defined('THINK_PATH') or define('THINK_PATH', __DIR__ . DS);
18 | define('LIB_PATH', THINK_PATH . 'library' . DS);
19 | define('CORE_PATH', LIB_PATH . 'think' . DS);
20 | define('TRAIT_PATH', LIB_PATH . 'traits' . DS);
21 | defined('APP_PATH') or define('APP_PATH', dirname($_SERVER['SCRIPT_FILENAME']) . DS);
22 | defined('ROOT_PATH') or define('ROOT_PATH', dirname(realpath(APP_PATH)) . DS);
23 | defined('EXTEND_PATH') or define('EXTEND_PATH', ROOT_PATH . 'extend' . DS);
24 | defined('VENDOR_PATH') or define('VENDOR_PATH', ROOT_PATH . 'vendor' . DS);
25 | defined('RUNTIME_PATH') or define('RUNTIME_PATH', ROOT_PATH . 'runtime' . DS);
26 | defined('LOG_PATH') or define('LOG_PATH', RUNTIME_PATH . 'log' . DS);
27 | defined('CACHE_PATH') or define('CACHE_PATH', RUNTIME_PATH . 'cache' . DS);
28 | defined('TEMP_PATH') or define('TEMP_PATH', RUNTIME_PATH . 'temp' . DS);
29 | defined('CONF_PATH') or define('CONF_PATH', APP_PATH); // 配置文件目录
30 | defined('CONF_EXT') or define('CONF_EXT', EXT); // 配置文件后缀
31 | defined('ENV_PREFIX') or define('ENV_PREFIX', 'PHP_'); // 环境变量的配置前缀
32 |
33 | // 环境常量
34 | define('IS_CLI', PHP_SAPI == 'cli' ? true : false);
35 | define('IS_WIN', strpos(PHP_OS, 'WIN') !== false);
36 |
37 | // 载入Loader类
38 | require CORE_PATH . 'Loader.php';
39 |
40 | // 加载环境变量配置文件
41 | if (is_file(ROOT_PATH . '.env')) {
42 | $env = parse_ini_file(ROOT_PATH . '.env', true);
43 | foreach ($env as $key => $val) {
44 | $name = ENV_PREFIX . strtoupper($key);
45 | if (is_array($val)) {
46 | foreach ($val as $k => $v) {
47 | $item = $name . '_' . strtoupper($k);
48 | putenv("$item=$v");
49 | }
50 | } else {
51 | putenv("$name=$val");
52 | }
53 | }
54 | }
55 |
56 | // 注册自动加载
57 | \think\Loader::register();
58 |
59 | // 注册错误和异常处理机制
60 | \think\Error::register();
61 |
62 | // 加载惯例配置文件
63 | \think\Config::set(include THINK_PATH . 'convention' . EXT);
64 |
--------------------------------------------------------------------------------
/example/thinkphp/library/think/response/View.php:
--------------------------------------------------------------------------------
1 |
10 | // +----------------------------------------------------------------------
11 |
12 | namespace think\response;
13 |
14 | use think\Config;
15 | use think\Response;
16 | use think\View as ViewTemplate;
17 |
18 | class View extends Response
19 | {
20 | // 输出参数
21 | protected $options = [];
22 | protected $vars = [];
23 | protected $replace = [];
24 | protected $contentType = 'text/html';
25 |
26 | /**
27 | * 处理数据
28 | * @access protected
29 | * @param mixed $data 要处理的数据
30 | * @return mixed
31 | */
32 | protected function output($data)
33 | {
34 | // 渲染模板输出
35 | return ViewTemplate::instance(Config::get('template'), Config::get('view_replace_str'))
36 | ->fetch($data, $this->vars, $this->replace);
37 | }
38 |
39 | /**
40 | * 获取视图变量
41 | * @access public
42 | * @param string $name 模板变量
43 | * @return mixed
44 | */
45 | public function getVars($name = null)
46 | {
47 | if (is_null($name)) {
48 | return $this->vars;
49 | } else {
50 | return isset($this->vars[$name]) ? $this->vars[$name] : null;
51 | }
52 | }
53 |
54 | /**
55 | * 模板变量赋值
56 | * @access public
57 | * @param mixed $name 变量名
58 | * @param mixed $value 变量值
59 | * @return $this
60 | */
61 | public function assign($name, $value = '')
62 | {
63 | if (is_array($name)) {
64 | $this->vars = array_merge($this->vars, $name);
65 | return $this;
66 | } else {
67 | $this->vars[$name] = $value;
68 | }
69 | return $this;
70 | }
71 |
72 | /**
73 | * 视图内容替换
74 | * @access public
75 | * @param string|array $content 被替换内容(支持批量替换)
76 | * @param string $replace 替换内容
77 | * @return $this
78 | */
79 | public function replace($content, $replace = '')
80 | {
81 | if (is_array($content)) {
82 | $this->replace = array_merge($this->replace, $content);
83 | } else {
84 | $this->replace[$content] = $replace;
85 | }
86 | return $this;
87 | }
88 |
89 | }
90 |
--------------------------------------------------------------------------------
/example/thinkphp/library/think/console/command/optimize/Route.php:
--------------------------------------------------------------------------------
1 |
10 | // +----------------------------------------------------------------------
11 | namespace think\console\command\optimize;
12 |
13 | use think\console\Command;
14 | use think\console\Input;
15 | use think\console\Output;
16 |
17 | class Route extends Command
18 | {
19 | /** @var Output */
20 | protected $output;
21 |
22 | protected function configure()
23 | {
24 | $this->setName('optimize:route')
25 | ->setDescription('Build route cache.');
26 | }
27 |
28 | protected function execute(Input $input, Output $output)
29 | {
30 | file_put_contents(RUNTIME_PATH . 'route.php', $this->buildRouteCache());
31 | $output->writeln('Succeed!');
32 | }
33 |
34 | protected function buildRouteCache()
35 | {
36 | $files = \think\Config::get('route_config_file');
37 | foreach ($files as $file) {
38 | if (is_file(CONF_PATH . $file . CONF_EXT)) {
39 | $config = include CONF_PATH . $file . CONF_EXT;
40 | if (is_array($config)) {
41 | \think\Route::import($config);
42 | }
43 | }
44 | }
45 | $rules = \think\Route::rules(true);
46 | array_walk_recursive($rules, [$this, 'buildClosure']);
47 | $content = 'getStartLine();
58 | $endLine = $reflection->getEndLine();
59 | $file = $reflection->getFileName();
60 | $item = file($file);
61 | $content = '';
62 | for ($i = $startLine - 1; $i <= $endLine - 1; $i++) {
63 | $content .= $item[$i];
64 | }
65 | $start = strpos($content, 'function');
66 | $end = strrpos($content, '}');
67 | $value = '[__start__' . substr($content, $start, $end - $start + 1) . '__end__]';
68 | }
69 | }
70 | }
71 |
--------------------------------------------------------------------------------
/example/thinkphp/library/think/model/Collection.php:
--------------------------------------------------------------------------------
1 |
10 | // +----------------------------------------------------------------------
11 |
12 | namespace think\model;
13 |
14 | use think\Collection as BaseCollection;
15 | use think\Model;
16 |
17 | class Collection extends BaseCollection
18 | {
19 | /**
20 | * 返回数组中指定的一列
21 | * @param string $column_key
22 | * @param string|null $index_key
23 | * @return array
24 | */
25 | public function column($column_key, $index_key = null)
26 | {
27 | if (function_exists('array_column')) {
28 | return array_column($this->toArray(), $column_key, $index_key);
29 | }
30 | return parent::column($column_key, $index_key);
31 | }
32 |
33 | /**
34 | * 延迟预载入关联查询
35 | * @access public
36 | * @param mixed $relation 关联
37 | * @return $this
38 | */
39 | public function load($relation)
40 | {
41 | $item = current($this->items);
42 | $item->eagerlyResultSet($this->items, $relation);
43 | return $this;
44 | }
45 |
46 | /**
47 | * 设置需要隐藏的输出属性
48 | * @access public
49 | * @param array $hidden 属性列表
50 | * @param bool $override 是否覆盖
51 | * @return $this
52 | */
53 | public function hidden($hidden = [], $override = false)
54 | {
55 | $this->each(function ($model) use ($hidden, $override) {
56 | /** @var Model $model */
57 | $model->hidden($hidden, $override);
58 | });
59 | return $this;
60 | }
61 |
62 | /**
63 | * 设置需要输出的属性
64 | * @param array $visible
65 | * @param bool $override 是否覆盖
66 | * @return $this
67 | */
68 | public function visible($visible = [], $override = false)
69 | {
70 | $this->each(function ($model) use ($visible, $override) {
71 | /** @var Model $model */
72 | $model->visible($visible, $override);
73 | });
74 | return $this;
75 | }
76 |
77 | /**
78 | * 设置需要追加的输出属性
79 | * @access public
80 | * @param array $append 属性列表
81 | * @param bool $override 是否覆盖
82 | * @return $this
83 | */
84 | public function append($append = [], $override = false)
85 | {
86 | $this->each(function ($model) use ($append, $override) {
87 | /** @var Model $model */
88 | $model->append($append, $override);
89 | });
90 | return $this;
91 | }
92 |
93 | }
94 |
--------------------------------------------------------------------------------
/example/thinkphp/library/think/response/Redirect.php:
--------------------------------------------------------------------------------
1 |
10 | // +----------------------------------------------------------------------
11 |
12 | namespace think\response;
13 |
14 | use think\Request;
15 | use think\Response;
16 | use think\Session;
17 | use think\Url;
18 |
19 | class Redirect extends Response
20 | {
21 |
22 | protected $options = [];
23 |
24 | // URL参数
25 | protected $params = [];
26 |
27 | public function __construct($data = '', $code = 302, array $header = [], array $options = [])
28 | {
29 | parent::__construct($data, $code, $header, $options);
30 | $this->cacheControl('no-cache,must-revalidate');
31 | }
32 |
33 | /**
34 | * 处理数据
35 | * @access protected
36 | * @param mixed $data 要处理的数据
37 | * @return mixed
38 | */
39 | protected function output($data)
40 | {
41 | $this->header['Location'] = $this->getTargetUrl();
42 | return;
43 | }
44 |
45 | /**
46 | * 重定向传值(通过Session)
47 | * @access protected
48 | * @param string|array $name 变量名或者数组
49 | * @param mixed $value 值
50 | * @return $this
51 | */
52 | public function with($name, $value = null)
53 | {
54 | if (is_array($name)) {
55 | foreach ($name as $key => $val) {
56 | Session::flash($key, $val);
57 | }
58 | } else {
59 | Session::flash($name, $value);
60 | }
61 | return $this;
62 | }
63 |
64 | /**
65 | * 获取跳转地址
66 | * @return string
67 | */
68 | public function getTargetUrl()
69 | {
70 | return strpos($this->data, '://') ? $this->data : Url::build($this->data, $this->params);
71 | }
72 |
73 | public function params($params = [])
74 | {
75 | $this->params = $params;
76 | return $this;
77 | }
78 |
79 | /**
80 | * 记住当前url后跳转
81 | * @return $this
82 | */
83 | public function remember()
84 | {
85 | Session::set('redirect_url', Request::instance()->url());
86 | return $this;
87 | }
88 |
89 | /**
90 | * 跳转到上次记住的url
91 | * @return $this
92 | */
93 | public function restore()
94 | {
95 | if (Session::has('redirect_url')) {
96 | $this->data = Session::get('redirect_url');
97 | Session::delete('redirect_url');
98 | }
99 | return $this;
100 | }
101 | }
102 |
--------------------------------------------------------------------------------
/example/thinkphp/CONTRIBUTING.md:
--------------------------------------------------------------------------------
1 | 如何贡献我的源代码
2 | ===
3 |
4 | 此文档介绍了 ThinkPHP 团队的组成以及运转机制,您提交的代码将给 ThinkPHP 项目带来什么好处,以及如何才能加入我们的行列。
5 |
6 | ## 通过 Github 贡献代码
7 |
8 | ThinkPHP 目前使用 Git 来控制程序版本,如果你想为 ThinkPHP 贡献源代码,请先大致了解 Git 的使用方法。我们目前把项目托管在 GitHub 上,任何 GitHub 用户都可以向我们贡献代码。
9 |
10 | 参与的方式很简单,`fork`一份 ThinkPHP 的代码到你的仓库中,修改后提交,并向我们发起`pull request`申请,我们会及时对代码进行审查并处理你的申请并。审查通过后,你的代码将被`merge`进我们的仓库中,这样你就会自动出现在贡献者名单里了,非常方便。
11 |
12 | 我们希望你贡献的代码符合:
13 |
14 | * ThinkPHP 的编码规范
15 | * 适当的注释,能让其他人读懂
16 | * 遵循 Apache2 开源协议
17 |
18 | **如果想要了解更多细节或有任何疑问,请继续阅读下面的内容**
19 |
20 | ### 注意事项
21 |
22 | * 本项目代码格式化标准选用 [**PSR-2**](http://www.kancloud.cn/thinkphp/php-fig-psr/3141);
23 | * 类名和类文件名遵循 [**PSR-4**](http://www.kancloud.cn/thinkphp/php-fig-psr/3144);
24 | * 对于 Issues 的处理,请使用诸如 `fix #xxx(Issue ID)` 的 commit title 直接关闭 issue。
25 | * 系统会自动在 PHP 5.4 5.5 5.6 7.0 和 HHVM 上测试修改,其中 HHVM 下的测试容许报错,请确保你的修改符合 PHP 5.4 ~ 5.6 和 PHP 7.0 的语法规范;
26 | * 管理员不会合并造成 CI faild 的修改,若出现 CI faild 请检查自己的源代码或修改相应的[单元测试文件](tests);
27 |
28 | ## GitHub Issue
29 |
30 | GitHub 提供了 Issue 功能,该功能可以用于:
31 |
32 | * 提出 bug
33 | * 提出功能改进
34 | * 反馈使用体验
35 |
36 | 该功能不应该用于:
37 |
38 | * 提出修改意见(涉及代码署名和修订追溯问题)
39 | * 不友善的言论
40 |
41 | ## 快速修改
42 |
43 | **GitHub 提供了快速编辑文件的功能**
44 |
45 | 1. 登录 GitHub 帐号;
46 | 2. 浏览项目文件,找到要进行修改的文件;
47 | 3. 点击右上角铅笔图标进行修改;
48 | 4. 填写 `Commit changes` 相关内容(Title 必填);
49 | 5. 提交修改,等待 CI 验证和管理员合并。
50 |
51 | **若您需要一次提交大量修改,请继续阅读下面的内容**
52 |
53 | ## 完整流程
54 |
55 | 1. `fork`本项目;
56 | 2. 克隆(`clone`)你 `fork` 的项目到本地;
57 | 3. 新建分支(`branch`)并检出(`checkout`)新分支;
58 | 4. 添加本项目到你的本地 git 仓库作为上游(`upstream`);
59 | 5. 进行修改,若你的修改包含方法或函数的增减,请记得修改[单元测试文件](tests);
60 | 6. 变基(衍合 `rebase`)你的分支到上游 master 分支;
61 | 7. `push` 你的本地仓库到 GitHub;
62 | 8. 提交 `pull request`;
63 | 9. 等待 CI 验证(若不通过则重复 5~7,GitHub 会自动更新你的 `pull request`);
64 | 10. 等待管理员处理,并及时 `rebase` 你的分支到上游 master 分支(若上游 master 分支有修改)。
65 |
66 | *若有必要,可以 `git push -f` 强行推送 rebase 后的分支到自己的 `fork`*
67 |
68 | *绝对不可以使用 `git push -f` 强行推送修改到上游*
69 |
70 | ### 注意事项
71 |
72 | * 若对上述流程有任何不清楚的地方,请查阅 GIT 教程,如 [这个](http://backlogtool.com/git-guide/cn/);
73 | * 对于代码**不同方面**的修改,请在自己 `fork` 的项目中**创建不同的分支**(原因参见`完整流程`第9条备注部分);
74 | * 变基及交互式变基操作参见 [Git 交互式变基](http://pakchoi.me/2015/03/17/git-interactive-rebase/)
75 |
76 | ## 推荐资源
77 |
78 | ### 开发环境
79 |
80 | * XAMPP for Windows 5.5.x
81 | * WampServer (for Windows)
82 | * upupw Apache PHP5.4 ( for Windows)
83 |
84 | 或自行安装
85 |
86 | - Apache / Nginx
87 | - PHP 5.4 ~ 5.6
88 | - MySQL / MariaDB
89 |
90 | *Windows 用户推荐添加 PHP bin 目录到 PATH,方便使用 composer*
91 |
92 | *Linux 用户自行配置环境, Mac 用户推荐使用内置 Apache 配合 Homebrew 安装 PHP 和 MariaDB*
93 |
94 | ### 编辑器
95 |
96 | Sublime Text 3 + phpfmt 插件
97 |
98 | phpfmt 插件参数
99 |
100 | ```json
101 | {
102 | "autocomplete": true,
103 | "enable_auto_align": true,
104 | "format_on_save": true,
105 | "indent_with_space": true,
106 | "psr1_naming": false,
107 | "psr2": true,
108 | "version": 4
109 | }
110 | ```
111 |
112 | 或其他 编辑器 / IDE 配合 PSR2 自动格式化工具
113 |
114 | ### Git GUI
115 |
116 | * SourceTree
117 | * GitHub Desktop
118 |
119 | 或其他 Git 图形界面客户端
120 |
--------------------------------------------------------------------------------
/example/thinkphp/library/think/console/output/formatter/Stack.php:
--------------------------------------------------------------------------------
1 |
10 | // +----------------------------------------------------------------------
11 |
12 | namespace think\console\output\formatter;
13 |
14 | class Stack
15 | {
16 |
17 | /**
18 | * @var Style[]
19 | */
20 | private $styles;
21 |
22 | /**
23 | * @var Style
24 | */
25 | private $emptyStyle;
26 |
27 | /**
28 | * 构造方法
29 | * @param Style|null $emptyStyle
30 | */
31 | public function __construct(Style $emptyStyle = null)
32 | {
33 | $this->emptyStyle = $emptyStyle ?: new Style();
34 | $this->reset();
35 | }
36 |
37 | /**
38 | * 重置堆栈
39 | */
40 | public function reset()
41 | {
42 | $this->styles = [];
43 | }
44 |
45 | /**
46 | * 推一个样式进入堆栈
47 | * @param Style $style
48 | */
49 | public function push(Style $style)
50 | {
51 | $this->styles[] = $style;
52 | }
53 |
54 | /**
55 | * 从堆栈中弹出一个样式
56 | * @param Style|null $style
57 | * @return Style
58 | * @throws \InvalidArgumentException
59 | */
60 | public function pop(Style $style = null)
61 | {
62 | if (empty($this->styles)) {
63 | return $this->emptyStyle;
64 | }
65 |
66 | if (null === $style) {
67 | return array_pop($this->styles);
68 | }
69 |
70 | /**
71 | * @var int $index
72 | * @var Style $stackedStyle
73 | */
74 | foreach (array_reverse($this->styles, true) as $index => $stackedStyle) {
75 | if ($style->apply('') === $stackedStyle->apply('')) {
76 | $this->styles = array_slice($this->styles, 0, $index);
77 |
78 | return $stackedStyle;
79 | }
80 | }
81 |
82 | throw new \InvalidArgumentException('Incorrectly nested style tag found.');
83 | }
84 |
85 | /**
86 | * 计算堆栈的当前样式。
87 | * @return Style
88 | */
89 | public function getCurrent()
90 | {
91 | if (empty($this->styles)) {
92 | return $this->emptyStyle;
93 | }
94 |
95 | return $this->styles[count($this->styles) - 1];
96 | }
97 |
98 | /**
99 | * @param Style $emptyStyle
100 | * @return Stack
101 | */
102 | public function setEmptyStyle(Style $emptyStyle)
103 | {
104 | $this->emptyStyle = $emptyStyle;
105 |
106 | return $this;
107 | }
108 |
109 | /**
110 | * @return Style
111 | */
112 | public function getEmptyStyle()
113 | {
114 | return $this->emptyStyle;
115 | }
116 | }
117 |
--------------------------------------------------------------------------------
/example/thinkphp/library/think/response/Xml.php:
--------------------------------------------------------------------------------
1 |
10 | // +----------------------------------------------------------------------
11 |
12 | namespace think\response;
13 |
14 | use think\Response;
15 |
16 | class Xml extends Response
17 | {
18 | // 输出参数
19 | protected $options = [
20 | // 根节点名
21 | 'root_node' => 'think',
22 | // 根节点属性
23 | 'root_attr' => '',
24 | //数字索引的子节点名
25 | 'item_node' => 'item',
26 | // 数字索引子节点key转换的属性名
27 | 'item_key' => 'id',
28 | // 数据编码
29 | 'encoding' => 'utf-8',
30 | ];
31 |
32 | protected $contentType = 'text/xml';
33 |
34 | /**
35 | * 处理数据
36 | * @access protected
37 | * @param mixed $data 要处理的数据
38 | * @return mixed
39 | */
40 | protected function output($data)
41 | {
42 | // XML数据转换
43 | return $this->xmlEncode($data, $this->options['root_node'], $this->options['item_node'], $this->options['root_attr'], $this->options['item_key'], $this->options['encoding']);
44 | }
45 |
46 | /**
47 | * XML编码
48 | * @param mixed $data 数据
49 | * @param string $root 根节点名
50 | * @param string $item 数字索引的子节点名
51 | * @param string $attr 根节点属性
52 | * @param string $id 数字索引子节点key转换的属性名
53 | * @param string $encoding 数据编码
54 | * @return string
55 | */
56 | protected function xmlEncode($data, $root, $item, $attr, $id, $encoding)
57 | {
58 | if (is_array($attr)) {
59 | $array = [];
60 | foreach ($attr as $key => $value) {
61 | $array[] = "{$key}=\"{$value}\"";
62 | }
63 | $attr = implode(' ', $array);
64 | }
65 | $attr = trim($attr);
66 | $attr = empty($attr) ? '' : " {$attr}";
67 | $xml = "";
68 | $xml .= "<{$root}{$attr}>";
69 | $xml .= $this->dataToXml($data, $item, $id);
70 | $xml .= "{$root}>";
71 | return $xml;
72 | }
73 |
74 | /**
75 | * 数据XML编码
76 | * @param mixed $data 数据
77 | * @param string $item 数字索引时的节点名称
78 | * @param string $id 数字索引key转换为的属性名
79 | * @return string
80 | */
81 | protected function dataToXml($data, $item, $id)
82 | {
83 | $xml = $attr = '';
84 | foreach ($data as $key => $val) {
85 | if (is_numeric($key)) {
86 | $id && $attr = " {$id}=\"{$key}\"";
87 | $key = $item;
88 | }
89 | $xml .= "<{$key}{$attr}>";
90 | $xml .= (is_array($val) || is_object($val)) ? $this->dataToXml($val, $item, $id) : $val;
91 | $xml .= "{$key}>";
92 | }
93 | return $xml;
94 | }
95 | }
96 |
--------------------------------------------------------------------------------
/ppython/Ppython.php:
--------------------------------------------------------------------------------
1 |
10 | // +----------------------------------------------------------------------
11 |
12 | // 核心中文语言包
13 | return [
14 | // 系统错误提示
15 | 'Undefined variable' => '未定义变量',
16 | 'Undefined index' => '未定义数组索引',
17 | 'Undefined offset' => '未定义数组下标',
18 | 'Parse error' => '语法解析错误',
19 | 'Type error' => '类型错误',
20 | 'Fatal error' => '致命错误',
21 | 'syntax error' => '语法错误',
22 |
23 | // 框架核心错误提示
24 | 'dispatch type not support' => '不支持的调度类型',
25 | 'method param miss' => '方法参数错误',
26 | 'method not exists' => '方法不存在',
27 | 'module not exists' => '模块不存在',
28 | 'controller not exists' => '控制器不存在',
29 | 'class not exists' => '类不存在',
30 | 'property not exists' => '类的属性不存在',
31 | 'template not exists' => '模板文件不存在',
32 | 'illegal controller name' => '非法的控制器名称',
33 | 'illegal action name' => '非法的操作名称',
34 | 'url suffix deny' => '禁止的URL后缀访问',
35 | 'Route Not Found' => '当前访问路由未定义',
36 | 'Undefined db type' => '未定义数据库类型',
37 | 'variable type error' => '变量类型错误',
38 | 'PSR-4 error' => 'PSR-4 规范错误',
39 | 'not support total' => '简洁模式下不能获取数据总数',
40 | 'not support last' => '简洁模式下不能获取最后一页',
41 | 'error session handler' => '错误的SESSION处理器类',
42 | 'not allow php tag' => '模板不允许使用PHP语法',
43 | 'not support' => '不支持',
44 | 'redisd master' => 'Redisd 主服务器错误',
45 | 'redisd slave' => 'Redisd 从服务器错误',
46 | 'must run at sae' => '必须在SAE运行',
47 | 'memcache init error' => '未开通Memcache服务,请在SAE管理平台初始化Memcache服务',
48 | 'KVDB init error' => '没有初始化KVDB,请在SAE管理平台初始化KVDB服务',
49 | 'fields not exists' => '数据表字段不存在',
50 | 'where express error' => '查询表达式错误',
51 | 'no data to update' => '没有任何数据需要更新',
52 | 'miss data to insert' => '缺少需要写入的数据',
53 | 'miss complex primary data' => '缺少复合主键数据',
54 | 'miss update condition' => '缺少更新条件',
55 | 'model data Not Found' => '模型数据不存在',
56 | 'table data not Found' => '表数据不存在',
57 | 'delete without condition' => '没有条件不会执行删除操作',
58 | 'miss relation data' => '缺少关联表数据',
59 | 'tag attr must' => '模板标签属性必须',
60 | 'tag error' => '模板标签错误',
61 | 'cache write error' => '缓存写入失败',
62 | 'sae mc write error' => 'SAE mc 写入错误',
63 | 'route name not exists' => '路由标识不存在(或参数不够)',
64 | 'invalid request' => '非法请求',
65 | 'bind attr has exists' => '模型的属性已经存在',
66 | 'relation data not exists' => '关联数据不存在',
67 | 'relation not support' => '关联不支持',
68 | ];
69 |
--------------------------------------------------------------------------------
/example/thinkphp/library/think/db/connector/Sqlite.php:
--------------------------------------------------------------------------------
1 |
10 | // +----------------------------------------------------------------------
11 |
12 | namespace think\db\connector;
13 |
14 | use PDO;
15 | use think\db\Connection;
16 |
17 | /**
18 | * Sqlite数据库驱动
19 | */
20 | class Sqlite extends Connection
21 | {
22 |
23 | protected $builder = '\\think\\db\\builder\\Sqlite';
24 |
25 | /**
26 | * 解析pdo连接的dsn信息
27 | * @access protected
28 | * @param array $config 连接信息
29 | * @return string
30 | */
31 | protected function parseDsn($config)
32 | {
33 | $dsn = 'sqlite:' . $config['database'];
34 | return $dsn;
35 | }
36 |
37 | /**
38 | * 取得数据表的字段信息
39 | * @access public
40 | * @param string $tableName
41 | * @return array
42 | */
43 | public function getFields($tableName)
44 | {
45 | list($tableName) = explode(' ', $tableName);
46 | $sql = 'PRAGMA table_info( ' . $tableName . ' )';
47 |
48 | $pdo = $this->query($sql, [], false, true);
49 | $result = $pdo->fetchAll(PDO::FETCH_ASSOC);
50 | $info = [];
51 | if ($result) {
52 | foreach ($result as $key => $val) {
53 | $val = array_change_key_case($val);
54 | $info[$val['name']] = [
55 | 'name' => $val['name'],
56 | 'type' => $val['type'],
57 | 'notnull' => 1 === $val['notnull'],
58 | 'default' => $val['dflt_value'],
59 | 'primary' => '1' == $val['pk'],
60 | 'autoinc' => '1' == $val['pk'],
61 | ];
62 | }
63 | }
64 | return $this->fieldCase($info);
65 | }
66 |
67 | /**
68 | * 取得数据库的表信息
69 | * @access public
70 | * @param string $dbName
71 | * @return array
72 | */
73 | public function getTables($dbName = '')
74 | {
75 |
76 | $sql = "SELECT name FROM sqlite_master WHERE type='table' "
77 | . "UNION ALL SELECT name FROM sqlite_temp_master "
78 | . "WHERE type='table' ORDER BY name";
79 |
80 | $pdo = $this->query($sql, [], false, true);
81 | $result = $pdo->fetchAll(PDO::FETCH_ASSOC);
82 | $info = [];
83 | foreach ($result as $key => $val) {
84 | $info[$key] = current($val);
85 | }
86 | return $info;
87 | }
88 |
89 | /**
90 | * SQL性能分析
91 | * @access protected
92 | * @param string $sql
93 | * @return array
94 | */
95 | protected function getExplain($sql)
96 | {
97 | return [];
98 | }
99 |
100 | protected function supportSavepoint()
101 | {
102 | return true;
103 | }
104 | }
105 |
--------------------------------------------------------------------------------
/example/thinkphp/library/think/console/input/Argument.php:
--------------------------------------------------------------------------------
1 |
10 | // +----------------------------------------------------------------------
11 |
12 | namespace think\console\input;
13 |
14 | class Argument
15 | {
16 |
17 | const REQUIRED = 1;
18 | const OPTIONAL = 2;
19 | const IS_ARRAY = 4;
20 |
21 | private $name;
22 | private $mode;
23 | private $default;
24 | private $description;
25 |
26 | /**
27 | * 构造方法
28 | * @param string $name 参数名
29 | * @param int $mode 参数类型: self::REQUIRED 或者 self::OPTIONAL
30 | * @param string $description 描述
31 | * @param mixed $default 默认值 (仅 self::OPTIONAL 类型有效)
32 | * @throws \InvalidArgumentException
33 | */
34 | public function __construct($name, $mode = null, $description = '', $default = null)
35 | {
36 | if (null === $mode) {
37 | $mode = self::OPTIONAL;
38 | } elseif (!is_int($mode) || $mode > 7 || $mode < 1) {
39 | throw new \InvalidArgumentException(sprintf('Argument mode "%s" is not valid.', $mode));
40 | }
41 |
42 | $this->name = $name;
43 | $this->mode = $mode;
44 | $this->description = $description;
45 |
46 | $this->setDefault($default);
47 | }
48 |
49 | /**
50 | * 获取参数名
51 | * @return string
52 | */
53 | public function getName()
54 | {
55 | return $this->name;
56 | }
57 |
58 | /**
59 | * 是否必须
60 | * @return bool
61 | */
62 | public function isRequired()
63 | {
64 | return self::REQUIRED === (self::REQUIRED & $this->mode);
65 | }
66 |
67 | /**
68 | * 该参数是否接受数组
69 | * @return bool
70 | */
71 | public function isArray()
72 | {
73 | return self::IS_ARRAY === (self::IS_ARRAY & $this->mode);
74 | }
75 |
76 | /**
77 | * 设置默认值
78 | * @param mixed $default 默认值
79 | * @throws \LogicException
80 | */
81 | public function setDefault($default = null)
82 | {
83 | if (self::REQUIRED === $this->mode && null !== $default) {
84 | throw new \LogicException('Cannot set a default value except for InputArgument::OPTIONAL mode.');
85 | }
86 |
87 | if ($this->isArray()) {
88 | if (null === $default) {
89 | $default = [];
90 | } elseif (!is_array($default)) {
91 | throw new \LogicException('A default value for an array argument must be an array.');
92 | }
93 | }
94 |
95 | $this->default = $default;
96 | }
97 |
98 | /**
99 | * 获取默认值
100 | * @return mixed
101 | */
102 | public function getDefault()
103 | {
104 | return $this->default;
105 | }
106 |
107 | /**
108 | * 获取描述
109 | * @return string
110 | */
111 | public function getDescription()
112 | {
113 | return $this->description;
114 | }
115 | }
116 |
--------------------------------------------------------------------------------
/example/thinkphp/library/think/db/connector/Pgsql.php:
--------------------------------------------------------------------------------
1 |
10 | // +----------------------------------------------------------------------
11 |
12 | namespace think\db\connector;
13 |
14 | use PDO;
15 | use think\db\Connection;
16 |
17 | /**
18 | * Pgsql数据库驱动
19 | */
20 | class Pgsql extends Connection
21 | {
22 | protected $builder = '\\think\\db\\builder\\Pgsql';
23 |
24 | /**
25 | * 解析pdo连接的dsn信息
26 | * @access protected
27 | * @param array $config 连接信息
28 | * @return string
29 | */
30 | protected function parseDsn($config)
31 | {
32 | $dsn = 'pgsql:dbname=' . $config['database'] . ';host=' . $config['hostname'];
33 | if (!empty($config['hostport'])) {
34 | $dsn .= ';port=' . $config['hostport'];
35 | }
36 | return $dsn;
37 | }
38 |
39 | /**
40 | * 取得数据表的字段信息
41 | * @access public
42 | * @param string $tableName
43 | * @return array
44 | */
45 | public function getFields($tableName)
46 | {
47 |
48 | list($tableName) = explode(' ', $tableName);
49 | $sql = 'select fields_name as "field",fields_type as "type",fields_not_null as "null",fields_key_name as "key",fields_default as "default",fields_default as "extra" from table_msg(\'' . $tableName . '\');';
50 |
51 | $pdo = $this->query($sql, [], false, true);
52 | $result = $pdo->fetchAll(PDO::FETCH_ASSOC);
53 | $info = [];
54 | if ($result) {
55 | foreach ($result as $key => $val) {
56 | $val = array_change_key_case($val);
57 | $info[$val['field']] = [
58 | 'name' => $val['field'],
59 | 'type' => $val['type'],
60 | 'notnull' => (bool) ('' !== $val['null']),
61 | 'default' => $val['default'],
62 | 'primary' => !empty($val['key']),
63 | 'autoinc' => (0 === strpos($val['extra'], 'nextval(')),
64 | ];
65 | }
66 | }
67 | return $this->fieldCase($info);
68 | }
69 |
70 | /**
71 | * 取得数据库的表信息
72 | * @access public
73 | * @param string $dbName
74 | * @return array
75 | */
76 | public function getTables($dbName = '')
77 | {
78 | $sql = "select tablename as Tables_in_test from pg_tables where schemaname ='public'";
79 | $pdo = $this->query($sql, [], false, true);
80 | $result = $pdo->fetchAll(PDO::FETCH_ASSOC);
81 | $info = [];
82 | foreach ($result as $key => $val) {
83 | $info[$key] = current($val);
84 | }
85 | return $info;
86 | }
87 |
88 | /**
89 | * SQL性能分析
90 | * @access protected
91 | * @param string $sql
92 | * @return array
93 | */
94 | protected function getExplain($sql)
95 | {
96 | return [];
97 | }
98 |
99 | protected function supportSavepoint()
100 | {
101 | return true;
102 | }
103 | }
104 |
--------------------------------------------------------------------------------
/example/thinkphp/library/think/console/command/optimize/Config.php:
--------------------------------------------------------------------------------
1 |
10 | // +----------------------------------------------------------------------
11 | namespace think\console\command\optimize;
12 |
13 | use think\Config as ThinkConfig;
14 | use think\console\Command;
15 | use think\console\Input;
16 | use think\console\input\Argument;
17 | use think\console\Output;
18 |
19 | class Config extends Command
20 | {
21 | /** @var Output */
22 | protected $output;
23 |
24 | protected function configure()
25 | {
26 | $this->setName('optimize:config')
27 | ->addArgument('module', Argument::OPTIONAL, 'Build module config cache .')
28 | ->setDescription('Build config and common file cache.');
29 | }
30 |
31 | protected function execute(Input $input, Output $output)
32 | {
33 | if ($input->hasArgument('module')) {
34 | $module = $input->getArgument('module') . DS;
35 | } else {
36 | $module = '';
37 | }
38 |
39 | $content = 'buildCacheContent($module);
40 |
41 | if (!is_dir(RUNTIME_PATH . $module)) {
42 | @mkdir(RUNTIME_PATH . $module, 0755, true);
43 | }
44 |
45 | file_put_contents(RUNTIME_PATH . $module . 'init' . EXT, $content);
46 |
47 | $output->writeln('Succeed!');
48 | }
49 |
50 | protected function buildCacheContent($module)
51 | {
52 | $content = '';
53 | $path = realpath(APP_PATH . $module) . DS;
54 |
55 | if ($module) {
56 | // 加载模块配置
57 | $config = ThinkConfig::load(CONF_PATH . $module . 'config' . CONF_EXT);
58 |
59 | // 读取数据库配置文件
60 | $filename = CONF_PATH . $module . 'database' . CONF_EXT;
61 | ThinkConfig::load($filename, 'database');
62 |
63 | // 加载应用状态配置
64 | if ($config['app_status']) {
65 | $config = ThinkConfig::load(CONF_PATH . $module . $config['app_status'] . CONF_EXT);
66 | }
67 | // 读取扩展配置文件
68 | if (is_dir(CONF_PATH . $module . 'extra')) {
69 | $dir = CONF_PATH . $module . 'extra';
70 | $files = scandir($dir);
71 | foreach ($files as $file) {
72 | if (strpos($file, CONF_EXT)) {
73 | $filename = $dir . DS . $file;
74 | ThinkConfig::load($filename, pathinfo($file, PATHINFO_FILENAME));
75 | }
76 | }
77 | }
78 | }
79 |
80 | // 加载行为扩展文件
81 | if (is_file(CONF_PATH . $module . 'tags' . EXT)) {
82 | $content .= '\think\Hook::import(' . (var_export(include CONF_PATH . $module . 'tags' . EXT, true)) . ');' . PHP_EOL;
83 | }
84 |
85 | // 加载公共文件
86 | if (is_file($path . 'common' . EXT)) {
87 | $content .= substr(php_strip_whitespace($path . 'common' . EXT), 5) . PHP_EOL;
88 | }
89 |
90 | $content .= '\think\Config::set(' . var_export(ThinkConfig::get(), true) . ');';
91 | return $content;
92 | }
93 | }
94 |
--------------------------------------------------------------------------------
/example/thinkphp/library/think/controller/Rest.php:
--------------------------------------------------------------------------------
1 |
10 | // +----------------------------------------------------------------------
11 |
12 | namespace think\controller;
13 |
14 | use think\App;
15 | use think\Request;
16 | use think\Response;
17 |
18 | abstract class Rest
19 | {
20 |
21 | protected $method; // 当前请求类型
22 | protected $type; // 当前资源类型
23 | // 输出类型
24 | protected $restMethodList = 'get|post|put|delete';
25 | protected $restDefaultMethod = 'get';
26 | protected $restTypeList = 'html|xml|json|rss';
27 | protected $restDefaultType = 'html';
28 | protected $restOutputType = [ // REST允许输出的资源类型列表
29 | 'xml' => 'application/xml',
30 | 'json' => 'application/json',
31 | 'html' => 'text/html',
32 | ];
33 |
34 | /**
35 | * 构造函数 取得模板对象实例
36 | * @access public
37 | */
38 | public function __construct()
39 | {
40 | // 资源类型检测
41 | $request = Request::instance();
42 | $ext = $request->ext();
43 | if ('' == $ext) {
44 | // 自动检测资源类型
45 | $this->type = $request->type();
46 | } elseif (!preg_match('/\(' . $this->restTypeList . '\)$/i', $ext)) {
47 | // 资源类型非法 则用默认资源类型访问
48 | $this->type = $this->restDefaultType;
49 | } else {
50 | $this->type = $ext;
51 | }
52 | // 请求方式检测
53 | $method = strtolower($request->method());
54 | if (false === stripos($this->restMethodList, $method)) {
55 | // 请求方式非法 则用默认请求方法
56 | $method = $this->restDefaultMethod;
57 | }
58 | $this->method = $method;
59 | }
60 |
61 | /**
62 | * REST 调用
63 | * @access public
64 | * @param string $method 方法名
65 | * @return mixed
66 | * @throws \Exception
67 | */
68 | public function _empty($method)
69 | {
70 | if (method_exists($this, $method . '_' . $this->method . '_' . $this->type)) {
71 | // RESTFul方法支持
72 | $fun = $method . '_' . $this->method . '_' . $this->type;
73 | } elseif ($this->method == $this->restDefaultMethod && method_exists($this, $method . '_' . $this->type)) {
74 | $fun = $method . '_' . $this->type;
75 | } elseif ($this->type == $this->restDefaultType && method_exists($this, $method . '_' . $this->method)) {
76 | $fun = $method . '_' . $this->method;
77 | }
78 | if (isset($fun)) {
79 | return App::invokeMethod([$this, $fun]);
80 | } else {
81 | // 抛出异常
82 | throw new \Exception('error action :' . $method);
83 | }
84 | }
85 |
86 | /**
87 | * 输出返回数据
88 | * @access protected
89 | * @param mixed $data 要返回的数据
90 | * @param String $type 返回类型 JSON XML
91 | * @param integer $code HTTP状态码
92 | * @return Response
93 | */
94 | protected function response($data, $type = 'json', $code = 200)
95 | {
96 | return Response::create($data, $type)->code($code);
97 | }
98 |
99 | }
100 |
--------------------------------------------------------------------------------
/example/thinkphp/library/think/console/command/Make.php:
--------------------------------------------------------------------------------
1 |
10 | // +----------------------------------------------------------------------
11 |
12 | namespace think\console\command;
13 |
14 | use think\App;
15 | use think\Config;
16 | use think\console\Command;
17 | use think\console\Input;
18 | use think\console\input\Argument;
19 | use think\console\Output;
20 |
21 | abstract class Make extends Command
22 | {
23 |
24 | protected $type;
25 |
26 | abstract protected function getStub();
27 |
28 | protected function configure()
29 | {
30 | $this->addArgument('name', Argument::REQUIRED, "The name of the class");
31 | }
32 |
33 | protected function execute(Input $input, Output $output)
34 | {
35 |
36 | $name = trim($input->getArgument('name'));
37 |
38 | $classname = $this->getClassName($name);
39 |
40 | $pathname = $this->getPathName($classname);
41 |
42 | if (is_file($pathname)) {
43 | $output->writeln('' . $this->type . ' already exists!');
44 | return false;
45 | }
46 |
47 | if (!is_dir(dirname($pathname))) {
48 | mkdir(strtolower(dirname($pathname)), 0755, true);
49 | }
50 |
51 | file_put_contents($pathname, $this->buildClass($classname));
52 |
53 | $output->writeln('' . $this->type . ' created successfully.');
54 |
55 | }
56 |
57 | protected function buildClass($name)
58 | {
59 | $stub = file_get_contents($this->getStub());
60 |
61 | $namespace = trim(implode('\\', array_slice(explode('\\', $name), 0, -1)), '\\');
62 |
63 | $class = str_replace($namespace . '\\', '', $name);
64 |
65 | return str_replace(['{%className%}', '{%namespace%}', '{%app_namespace%}'], [
66 | $class,
67 | $namespace,
68 | App::$namespace,
69 | ], $stub);
70 |
71 | }
72 |
73 | protected function getPathName($name)
74 | {
75 | $name = str_replace(App::$namespace . '\\', '', $name);
76 |
77 | return APP_PATH . str_replace('\\', '/', $name) . '.php';
78 | }
79 |
80 | protected function getClassName($name)
81 | {
82 | $appNamespace = App::$namespace;
83 |
84 | if (strpos($name, $appNamespace . '\\') === 0) {
85 | return $name;
86 | }
87 |
88 | if (Config::get('app_multi_module')) {
89 | if (strpos($name, '/')) {
90 | list($module, $name) = explode('/', $name, 2);
91 | } else {
92 | $module = 'common';
93 | }
94 | } else {
95 | $module = null;
96 | }
97 |
98 | if (strpos($name, '/') !== false) {
99 | $name = str_replace('/', '\\', $name);
100 | }
101 |
102 | return $this->getNamespace($appNamespace, $module) . '\\' . $name;
103 | }
104 |
105 | protected function getNamespace($appNamespace, $module)
106 | {
107 | return $module ? ($appNamespace . '\\' . $module) : $appNamespace;
108 | }
109 |
110 | }
111 |
--------------------------------------------------------------------------------
/example/thinkphp/library/think/model/Relation.php:
--------------------------------------------------------------------------------
1 |
10 | // +----------------------------------------------------------------------
11 |
12 | namespace think\model;
13 |
14 | use think\db\Query;
15 | use think\Exception;
16 | use think\Model;
17 |
18 | /**
19 | * Class Relation
20 | * @package think\model
21 | *
22 | * @mixin Query
23 | */
24 | abstract class Relation
25 | {
26 | // 父模型对象
27 | protected $parent;
28 | /** @var Model 当前关联的模型类 */
29 | protected $model;
30 | /** @var Query 关联模型查询对象 */
31 | protected $query;
32 | // 关联表外键
33 | protected $foreignKey;
34 | // 关联表主键
35 | protected $localKey;
36 | // 基础查询
37 | protected $baseQuery;
38 |
39 | /**
40 | * 获取关联的所属模型
41 | * @access public
42 | * @return Model
43 | */
44 | public function getParent()
45 | {
46 | return $this->parent;
47 | }
48 |
49 | /**
50 | * 获取当前的关联模型类
51 | * @access public
52 | * @return string
53 | */
54 | public function getModel()
55 | {
56 | return $this->model;
57 | }
58 |
59 | /**
60 | * 获取关联的查询对象
61 | * @access public
62 | * @return Query
63 | */
64 | public function getQuery()
65 | {
66 | return $this->query;
67 | }
68 |
69 | /**
70 | * 封装关联数据集
71 | * @access public
72 | * @param array $resultSet 数据集
73 | * @return mixed
74 | */
75 | protected function resultSetBuild($resultSet)
76 | {
77 | return (new $this->model)->toCollection($resultSet);
78 | }
79 |
80 | protected function getQueryFields($model)
81 | {
82 | $fields = $this->query->getOptions('field');
83 | return $this->getRelationQueryFields($fields, $model);
84 | }
85 |
86 | protected function getRelationQueryFields($fields, $model)
87 | {
88 | if ($fields) {
89 |
90 | if (is_string($fields)) {
91 | $fields = explode(',', $fields);
92 | }
93 |
94 | foreach ($fields as &$field) {
95 | if (false === strpos($field, '.')) {
96 | $field = $model . '.' . $field;
97 | }
98 | }
99 | } else {
100 | $fields = $model . '.*';
101 | }
102 |
103 | return $fields;
104 | }
105 |
106 | /**
107 | * 执行基础查询(仅执行一次)
108 | * @access protected
109 | * @return void
110 | */
111 | abstract protected function baseQuery();
112 |
113 | public function __call($method, $args)
114 | {
115 | if ($this->query) {
116 | // 执行基础查询
117 | $this->baseQuery();
118 |
119 | $result = call_user_func_array([$this->query, $method], $args);
120 | if ($result instanceof Query) {
121 | return $this;
122 | } else {
123 | $this->baseQuery = false;
124 | return $result;
125 | }
126 | } else {
127 | throw new Exception('method not exists:' . __CLASS__ . '->' . $method);
128 | }
129 | }
130 | }
131 |
--------------------------------------------------------------------------------
/example/thinkphp/README.md:
--------------------------------------------------------------------------------
1 | ThinkPHP 5.0
2 | ===============
3 |
4 | [](https://styleci.io/repos/48530411)
5 | [](https://travis-ci.org/top-think/framework)
6 | [](http://codecov.io/github/github/top-think/framework?branch=master)
7 | [](https://packagist.org/packages/topthink/framework)
8 | [](https://packagist.org/packages/topthink/framework)
9 | [](https://packagist.org/packages/topthink/framework)
10 | [](https://packagist.org/packages/topthink/framework)
11 |
12 | ThinkPHP5在保持快速开发和大道至简的核心理念不变的同时,PHP版本要求提升到5.4,优化核心,减少依赖,基于全新的架构思想和命名空间实现,是ThinkPHP突破原有框架思路的颠覆之作,其主要特性包括:
13 |
14 | + 基于命名空间和众多PHP新特性
15 | + 核心功能组件化
16 | + 强化路由功能
17 | + 更灵活的控制器
18 | + 重构的模型和数据库类
19 | + 配置文件可分离
20 | + 重写的自动验证和完成
21 | + 简化扩展机制
22 | + API支持完善
23 | + 改进的Log类
24 | + 命令行访问支持
25 | + REST支持
26 | + 引导文件支持
27 | + 方便的自动生成定义
28 | + 真正惰性加载
29 | + 分布式环境支持
30 | + 支持Composer
31 | + 支持MongoDb
32 |
33 | > ThinkPHP5的运行环境要求PHP5.4以上。
34 |
35 | 详细开发文档参考 [ThinkPHP5完全开发手册](http://www.kancloud.cn/manual/thinkphp5) 以及[ThinkPHP5入门系列教程](http://www.kancloud.cn/special/thinkphp5_quickstart)
36 |
37 | ## 目录结构
38 |
39 | 初始的目录结构如下:
40 |
41 | ~~~
42 | www WEB部署目录(或者子目录)
43 | ├─application 应用目录
44 | │ ├─common 公共模块目录(可以更改)
45 | │ ├─module_name 模块目录
46 | │ │ ├─config.php 模块配置文件
47 | │ │ ├─common.php 模块函数文件
48 | │ │ ├─controller 控制器目录
49 | │ │ ├─model 模型目录
50 | │ │ ├─view 视图目录
51 | │ │ └─ ... 更多类库目录
52 | │ │
53 | │ ├─command.php 命令行工具配置文件
54 | │ ├─common.php 公共函数文件
55 | │ ├─config.php 公共配置文件
56 | │ ├─route.php 路由配置文件
57 | │ ├─tags.php 应用行为扩展定义文件
58 | │ └─database.php 数据库配置文件
59 | │
60 | ├─public WEB目录(对外访问目录)
61 | │ ├─index.php 入口文件
62 | │ ├─router.php 快速测试文件
63 | │ └─.htaccess 用于apache的重写
64 | │
65 | ├─thinkphp 框架系统目录
66 | │ ├─lang 语言文件目录
67 | │ ├─library 框架类库目录
68 | │ │ ├─think Think类库包目录
69 | │ │ └─traits 系统Trait目录
70 | │ │
71 | │ ├─tpl 系统模板目录
72 | │ ├─base.php 基础定义文件
73 | │ ├─console.php 控制台入口文件
74 | │ ├─convention.php 框架惯例配置文件
75 | │ ├─helper.php 助手函数文件
76 | │ ├─phpunit.xml phpunit配置文件
77 | │ └─start.php 框架入口文件
78 | │
79 | ├─extend 扩展类库目录
80 | ├─runtime 应用的运行时目录(可写,可定制)
81 | ├─vendor 第三方类库目录(Composer依赖库)
82 | ├─build.php 自动生成定义文件(参考)
83 | ├─composer.json composer 定义文件
84 | ├─LICENSE.txt 授权说明文件
85 | ├─README.md README 文件
86 | ├─think 命令行入口文件
87 | ~~~
88 |
89 | > router.php用于php自带webserver支持,可用于快速测试
90 | > 切换到public目录后,启动命令:php -S localhost:8888 router.php
91 | > 上面的目录结构和名称是可以改变的,这取决于你的入口文件和配置参数。
92 |
93 | ## 命名规范
94 |
95 | ThinkPHP5的命名规范遵循PSR-2规范以及PSR-4自动加载规范。
96 |
97 | ## 参与开发
98 | 注册并登录 Github 帐号, fork 本项目并进行改动。
99 |
100 | 更多细节参阅 [CONTRIBUTING.md](CONTRIBUTING.md)
101 |
102 | ## 版权信息
103 |
104 | ThinkPHP遵循Apache2开源协议发布,并提供免费使用。
105 |
106 | 本项目包含的第三方源码和二进制文件之版权信息另行标注。
107 |
108 | 版权所有Copyright © 2006-2016 by ThinkPHP (http://thinkphp.cn)
109 |
110 | All rights reserved。
111 |
112 | ThinkPHP® 商标和著作权所有者为上海顶想信息科技有限公司。
113 |
114 | 更多细节参阅 [LICENSE.txt](LICENSE.txt)
115 |
--------------------------------------------------------------------------------
/example/thinkphp/library/think/session/driver/Memcache.php:
--------------------------------------------------------------------------------
1 |
10 | // +----------------------------------------------------------------------
11 |
12 | namespace think\session\driver;
13 |
14 | use SessionHandler;
15 | use think\Exception;
16 |
17 | class Memcache extends SessionHandler
18 | {
19 | protected $handler = null;
20 | protected $config = [
21 | 'host' => '127.0.0.1', // memcache主机
22 | 'port' => 11211, // memcache端口
23 | 'expire' => 3600, // session有效期
24 | 'timeout' => 0, // 连接超时时间(单位:毫秒)
25 | 'persistent' => true, // 长连接
26 | 'session_name' => '', // memcache key前缀
27 | ];
28 |
29 | public function __construct($config = [])
30 | {
31 | $this->config = array_merge($this->config, $config);
32 | }
33 |
34 | /**
35 | * 打开Session
36 | * @access public
37 | * @param string $savePath
38 | * @param mixed $sessName
39 | */
40 | public function open($savePath, $sessName)
41 | {
42 | // 检测php环境
43 | if (!extension_loaded('memcache')) {
44 | throw new Exception('not support:memcache');
45 | }
46 | $this->handler = new \Memcache;
47 | // 支持集群
48 | $hosts = explode(',', $this->config['host']);
49 | $ports = explode(',', $this->config['port']);
50 | if (empty($ports[0])) {
51 | $ports[0] = 11211;
52 | }
53 | // 建立连接
54 | foreach ((array) $hosts as $i => $host) {
55 | $port = isset($ports[$i]) ? $ports[$i] : $ports[0];
56 | $this->config['timeout'] > 0 ?
57 | $this->handler->addServer($host, $port, $this->config['persistent'], 1, $this->config['timeout']) :
58 | $this->handler->addServer($host, $port, $this->config['persistent'], 1);
59 | }
60 | return true;
61 | }
62 |
63 | /**
64 | * 关闭Session
65 | * @access public
66 | */
67 | public function close()
68 | {
69 | $this->gc(ini_get('session.gc_maxlifetime'));
70 | $this->handler->close();
71 | $this->handler = null;
72 | return true;
73 | }
74 |
75 | /**
76 | * 读取Session
77 | * @access public
78 | * @param string $sessID
79 | */
80 | public function read($sessID)
81 | {
82 | return (string) $this->handler->get($this->config['session_name'] . $sessID);
83 | }
84 |
85 | /**
86 | * 写入Session
87 | * @access public
88 | * @param string $sessID
89 | * @param String $sessData
90 | * @return bool
91 | */
92 | public function write($sessID, $sessData)
93 | {
94 | return $this->handler->set($this->config['session_name'] . $sessID, $sessData, 0, $this->config['expire']);
95 | }
96 |
97 | /**
98 | * 删除Session
99 | * @access public
100 | * @param string $sessID
101 | * @return bool
102 | */
103 | public function destroy($sessID)
104 | {
105 | return $this->handler->delete($this->config['session_name'] . $sessID);
106 | }
107 |
108 | /**
109 | * Session 垃圾回收
110 | * @access public
111 | * @param string $sessMaxLifeTime
112 | * @return true
113 | */
114 | public function gc($sessMaxLifeTime)
115 | {
116 | return true;
117 | }
118 | }
119 |
--------------------------------------------------------------------------------
/example/thinkphp/library/think/Error.php:
--------------------------------------------------------------------------------
1 |
10 | // +----------------------------------------------------------------------
11 |
12 | namespace think;
13 |
14 | use think\console\Output as ConsoleOutput;
15 | use think\exception\ErrorException;
16 | use think\exception\Handle;
17 | use think\exception\ThrowableError;
18 |
19 | class Error
20 | {
21 | /**
22 | * 注册异常处理
23 | * @return void
24 | */
25 | public static function register()
26 | {
27 | error_reporting(E_ALL);
28 | set_error_handler([__CLASS__, 'appError']);
29 | set_exception_handler([__CLASS__, 'appException']);
30 | register_shutdown_function([__CLASS__, 'appShutdown']);
31 | }
32 |
33 | /**
34 | * Exception Handler
35 | * @param \Exception|\Throwable $e
36 | */
37 | public static function appException($e)
38 | {
39 | if (!$e instanceof \Exception) {
40 | $e = new ThrowableError($e);
41 | }
42 |
43 | self::getExceptionHandler()->report($e);
44 | if (IS_CLI) {
45 | self::getExceptionHandler()->renderForConsole(new ConsoleOutput, $e);
46 | } else {
47 | self::getExceptionHandler()->render($e)->send();
48 | }
49 | }
50 |
51 | /**
52 | * Error Handler
53 | * @param integer $errno 错误编号
54 | * @param integer $errstr 详细错误信息
55 | * @param string $errfile 出错的文件
56 | * @param integer $errline 出错行号
57 | * @param array $errcontext
58 | * @throws ErrorException
59 | */
60 | public static function appError($errno, $errstr, $errfile = '', $errline = 0, $errcontext = [])
61 | {
62 | $exception = new ErrorException($errno, $errstr, $errfile, $errline, $errcontext);
63 | if (error_reporting() & $errno) {
64 | // 将错误信息托管至 think\exception\ErrorException
65 | throw $exception;
66 | } else {
67 | self::getExceptionHandler()->report($exception);
68 | }
69 | }
70 |
71 | /**
72 | * Shutdown Handler
73 | */
74 | public static function appShutdown()
75 | {
76 | if (!is_null($error = error_get_last()) && self::isFatal($error['type'])) {
77 | // 将错误信息托管至think\ErrorException
78 | $exception = new ErrorException($error['type'], $error['message'], $error['file'], $error['line']);
79 |
80 | self::appException($exception);
81 | }
82 |
83 | // 写入日志
84 | Log::save();
85 | }
86 |
87 | /**
88 | * 确定错误类型是否致命
89 | *
90 | * @param int $type
91 | * @return bool
92 | */
93 | protected static function isFatal($type)
94 | {
95 | return in_array($type, [E_ERROR, E_CORE_ERROR, E_COMPILE_ERROR, E_PARSE]);
96 | }
97 |
98 | /**
99 | * Get an instance of the exception handler.
100 | *
101 | * @return Handle
102 | */
103 | public static function getExceptionHandler()
104 | {
105 | static $handle;
106 | if (!$handle) {
107 | // 异常处理handle
108 | $class = Config::get('exception_handle');
109 | if ($class && class_exists($class) && is_subclass_of($class, "\\think\\exception\\Handle")) {
110 | $handle = new $class;
111 | } else {
112 | $handle = new Handle;
113 | }
114 | }
115 | return $handle;
116 | }
117 | }
118 |
--------------------------------------------------------------------------------
/example/thinkphp/library/think/session/driver/Redis.php:
--------------------------------------------------------------------------------
1 |
10 | // +----------------------------------------------------------------------
11 |
12 | namespace think\session\driver;
13 |
14 | use SessionHandler;
15 | use think\Exception;
16 |
17 | class Redis extends SessionHandler
18 | {
19 | /** @var \Redis */
20 | protected $handler = null;
21 | protected $config = [
22 | 'host' => '127.0.0.1', // redis主机
23 | 'port' => 6379, // redis端口
24 | 'password' => '', // 密码
25 | 'select' => 0, // 操作库
26 | 'expire' => 3600, // 有效期(秒)
27 | 'timeout' => 0, // 超时时间(秒)
28 | 'persistent' => true, // 是否长连接
29 | 'session_name' => '', // sessionkey前缀
30 | ];
31 |
32 | public function __construct($config = [])
33 | {
34 | $this->config = array_merge($this->config, $config);
35 | }
36 |
37 | /**
38 | * 打开Session
39 | * @access public
40 | * @param string $savePath
41 | * @param mixed $sessName
42 | * @return bool
43 | * @throws Exception
44 | */
45 | public function open($savePath, $sessName)
46 | {
47 | // 检测php环境
48 | if (!extension_loaded('redis')) {
49 | throw new Exception('not support:redis');
50 | }
51 | $this->handler = new \Redis;
52 |
53 | // 建立连接
54 | $func = $this->config['persistent'] ? 'pconnect' : 'connect';
55 | $this->handler->$func($this->config['host'], $this->config['port'], $this->config['timeout']);
56 |
57 | if ('' != $this->config['password']) {
58 | $this->handler->auth($this->config['password']);
59 | }
60 |
61 | if (0 != $this->config['select']) {
62 | $this->handler->select($this->config['select']);
63 | }
64 |
65 | return true;
66 | }
67 |
68 | /**
69 | * 关闭Session
70 | * @access public
71 | */
72 | public function close()
73 | {
74 | $this->gc(ini_get('session.gc_maxlifetime'));
75 | $this->handler->close();
76 | $this->handler = null;
77 | return true;
78 | }
79 |
80 | /**
81 | * 读取Session
82 | * @access public
83 | * @param string $sessID
84 | * @return string
85 | */
86 | public function read($sessID)
87 | {
88 | return (string) $this->handler->get($this->config['session_name'] . $sessID);
89 | }
90 |
91 | /**
92 | * 写入Session
93 | * @access public
94 | * @param string $sessID
95 | * @param String $sessData
96 | * @return bool
97 | */
98 | public function write($sessID, $sessData)
99 | {
100 | if ($this->config['expire'] > 0) {
101 | return $this->handler->setex($this->config['session_name'] . $sessID, $this->config['expire'], $sessData);
102 | } else {
103 | return $this->handler->set($this->config['session_name'] . $sessID, $sessData);
104 | }
105 | }
106 |
107 | /**
108 | * 删除Session
109 | * @access public
110 | * @param string $sessID
111 | * @return bool
112 | */
113 | public function destroy($sessID)
114 | {
115 | return $this->handler->delete($this->config['session_name'] . $sessID) > 0;
116 | }
117 |
118 | /**
119 | * Session 垃圾回收
120 | * @access public
121 | * @param string $sessMaxLifeTime
122 | * @return bool
123 | */
124 | public function gc($sessMaxLifeTime)
125 | {
126 | return true;
127 | }
128 | }
129 |
--------------------------------------------------------------------------------
/example/thinkphp/library/think/session/driver/Memcached.php:
--------------------------------------------------------------------------------
1 |
10 | // +----------------------------------------------------------------------
11 |
12 | namespace think\session\driver;
13 |
14 | use SessionHandler;
15 | use think\Exception;
16 |
17 | class Memcached extends SessionHandler
18 | {
19 | protected $handler = null;
20 | protected $config = [
21 | 'host' => '127.0.0.1', // memcache主机
22 | 'port' => 11211, // memcache端口
23 | 'expire' => 3600, // session有效期
24 | 'timeout' => 0, // 连接超时时间(单位:毫秒)
25 | 'session_name' => '', // memcache key前缀
26 | 'username' => '', //账号
27 | 'password' => '', //密码
28 | ];
29 |
30 | public function __construct($config = [])
31 | {
32 | $this->config = array_merge($this->config, $config);
33 | }
34 |
35 | /**
36 | * 打开Session
37 | * @access public
38 | * @param string $savePath
39 | * @param mixed $sessName
40 | */
41 | public function open($savePath, $sessName)
42 | {
43 | // 检测php环境
44 | if (!extension_loaded('memcached')) {
45 | throw new Exception('not support:memcached');
46 | }
47 | $this->handler = new \Memcached;
48 | // 设置连接超时时间(单位:毫秒)
49 | if ($this->config['timeout'] > 0) {
50 | $this->handler->setOption(\Memcached::OPT_CONNECT_TIMEOUT, $this->config['timeout']);
51 | }
52 | // 支持集群
53 | $hosts = explode(',', $this->config['host']);
54 | $ports = explode(',', $this->config['port']);
55 | if (empty($ports[0])) {
56 | $ports[0] = 11211;
57 | }
58 | // 建立连接
59 | $servers = [];
60 | foreach ((array) $hosts as $i => $host) {
61 | $servers[] = [$host, (isset($ports[$i]) ? $ports[$i] : $ports[0]), 1];
62 | }
63 | $this->handler->addServers($servers);
64 | if ('' != $this->config['username']) {
65 | $this->handler->setOption(\Memcached::OPT_BINARY_PROTOCOL, true);
66 | $this->handler->setSaslAuthData($this->config['username'], $this->config['password']);
67 | }
68 | return true;
69 | }
70 |
71 | /**
72 | * 关闭Session
73 | * @access public
74 | */
75 | public function close()
76 | {
77 | $this->gc(ini_get('session.gc_maxlifetime'));
78 | $this->handler->quit();
79 | $this->handler = null;
80 | return true;
81 | }
82 |
83 | /**
84 | * 读取Session
85 | * @access public
86 | * @param string $sessID
87 | */
88 | public function read($sessID)
89 | {
90 | return (string) $this->handler->get($this->config['session_name'] . $sessID);
91 | }
92 |
93 | /**
94 | * 写入Session
95 | * @access public
96 | * @param string $sessID
97 | * @param String $sessData
98 | * @return bool
99 | */
100 | public function write($sessID, $sessData)
101 | {
102 | return $this->handler->set($this->config['session_name'] . $sessID, $sessData, $this->config['expire']);
103 | }
104 |
105 | /**
106 | * 删除Session
107 | * @access public
108 | * @param string $sessID
109 | * @return bool
110 | */
111 | public function destroy($sessID)
112 | {
113 | return $this->handler->delete($this->config['session_name'] . $sessID);
114 | }
115 |
116 | /**
117 | * Session 垃圾回收
118 | * @access public
119 | * @param string $sessMaxLifeTime
120 | * @return true
121 | */
122 | public function gc($sessMaxLifeTime)
123 | {
124 | return true;
125 | }
126 | }
127 |
--------------------------------------------------------------------------------
/example/thinkphp/library/think/db/connector/pgsql.sql:
--------------------------------------------------------------------------------
1 | CREATE OR REPLACE FUNCTION pgsql_type(a_type varchar) RETURNS varchar AS
2 | $BODY$
3 | DECLARE
4 | v_type varchar;
5 | BEGIN
6 | IF a_type='int8' THEN
7 | v_type:='bigint';
8 | ELSIF a_type='int4' THEN
9 | v_type:='integer';
10 | ELSIF a_type='int2' THEN
11 | v_type:='smallint';
12 | ELSIF a_type='bpchar' THEN
13 | v_type:='char';
14 | ELSE
15 | v_type:=a_type;
16 | END IF;
17 | RETURN v_type;
18 | END;
19 | $BODY$
20 | LANGUAGE PLPGSQL;
21 |
22 | CREATE TYPE "public"."tablestruct" AS (
23 | "fields_key_name" varchar(100),
24 | "fields_name" VARCHAR(200),
25 | "fields_type" VARCHAR(20),
26 | "fields_length" BIGINT,
27 | "fields_not_null" VARCHAR(10),
28 | "fields_default" VARCHAR(500),
29 | "fields_comment" VARCHAR(1000)
30 | );
31 |
32 | CREATE OR REPLACE FUNCTION "public"."table_msg" (a_schema_name varchar, a_table_name varchar) RETURNS SETOF "public"."tablestruct" AS
33 | $body$
34 | DECLARE
35 | v_ret tablestruct;
36 | v_oid oid;
37 | v_sql varchar;
38 | v_rec RECORD;
39 | v_key varchar;
40 | BEGIN
41 | SELECT
42 | pg_class.oid INTO v_oid
43 | FROM
44 | pg_class
45 | INNER JOIN pg_namespace ON (pg_class.relnamespace = pg_namespace.oid AND lower(pg_namespace.nspname) = a_schema_name)
46 | WHERE
47 | pg_class.relname=a_table_name;
48 | IF NOT FOUND THEN
49 | RETURN;
50 | END IF;
51 |
52 | v_sql='
53 | SELECT
54 | pg_attribute.attname AS fields_name,
55 | pg_attribute.attnum AS fields_index,
56 | pgsql_type(pg_type.typname::varchar) AS fields_type,
57 | pg_attribute.atttypmod-4 as fields_length,
58 | CASE WHEN pg_attribute.attnotnull THEN ''not null''
59 | ELSE ''''
60 | END AS fields_not_null,
61 | pg_attrdef.adsrc AS fields_default,
62 | pg_description.description AS fields_comment
63 | FROM
64 | pg_attribute
65 | INNER JOIN pg_class ON pg_attribute.attrelid = pg_class.oid
66 | INNER JOIN pg_type ON pg_attribute.atttypid = pg_type.oid
67 | LEFT OUTER JOIN pg_attrdef ON pg_attrdef.adrelid = pg_class.oid AND pg_attrdef.adnum = pg_attribute.attnum
68 | LEFT OUTER JOIN pg_description ON pg_description.objoid = pg_class.oid AND pg_description.objsubid = pg_attribute.attnum
69 | WHERE
70 | pg_attribute.attnum > 0
71 | AND attisdropped <> ''t''
72 | AND pg_class.oid = ' || v_oid || '
73 | ORDER BY pg_attribute.attnum' ;
74 |
75 | FOR v_rec IN EXECUTE v_sql LOOP
76 | v_ret.fields_name=v_rec.fields_name;
77 | v_ret.fields_type=v_rec.fields_type;
78 | IF v_rec.fields_length > 0 THEN
79 | v_ret.fields_length:=v_rec.fields_length;
80 | ELSE
81 | v_ret.fields_length:=NULL;
82 | END IF;
83 | v_ret.fields_not_null=v_rec.fields_not_null;
84 | v_ret.fields_default=v_rec.fields_default;
85 | v_ret.fields_comment=v_rec.fields_comment;
86 | SELECT constraint_name INTO v_key FROM information_schema.key_column_usage WHERE table_schema=a_schema_name AND table_name=a_table_name AND column_name=v_rec.fields_name;
87 | IF FOUND THEN
88 | v_ret.fields_key_name=v_key;
89 | ELSE
90 | v_ret.fields_key_name='';
91 | END IF;
92 | RETURN NEXT v_ret;
93 | END LOOP;
94 | RETURN ;
95 | END;
96 | $body$
97 | LANGUAGE 'plpgsql' VOLATILE CALLED ON NULL INPUT SECURITY INVOKER;
98 |
99 | COMMENT ON FUNCTION "public"."table_msg"(a_schema_name varchar, a_table_name varchar)
100 | IS '获得表信息';
101 |
102 | ---重载一个函数
103 | CREATE OR REPLACE FUNCTION "public"."table_msg" (a_table_name varchar) RETURNS SETOF "public"."tablestruct" AS
104 | $body$
105 | DECLARE
106 | v_ret tablestruct;
107 | BEGIN
108 | FOR v_ret IN SELECT * FROM table_msg('public',a_table_name) LOOP
109 | RETURN NEXT v_ret;
110 | END LOOP;
111 | RETURN;
112 | END;
113 | $body$
114 | LANGUAGE 'plpgsql' VOLATILE CALLED ON NULL INPUT SECURITY INVOKER;
115 |
116 | COMMENT ON FUNCTION "public"."table_msg"(a_table_name varchar)
117 | IS '获得表信息';
--------------------------------------------------------------------------------
/example/thinkphp/library/think/db/connector/Sqlsrv.php:
--------------------------------------------------------------------------------
1 |
10 | // +----------------------------------------------------------------------
11 |
12 | namespace think\db\connector;
13 |
14 | use PDO;
15 | use think\db\Connection;
16 |
17 | /**
18 | * Sqlsrv数据库驱动
19 | */
20 | class Sqlsrv extends Connection
21 | {
22 | // PDO连接参数
23 | protected $params = [
24 | PDO::ATTR_CASE => PDO::CASE_NATURAL,
25 | PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
26 | PDO::ATTR_STRINGIFY_FETCHES => false,
27 | ];
28 | protected $builder = '\\think\\db\\builder\\Sqlsrv';
29 | /**
30 | * 解析pdo连接的dsn信息
31 | * @access protected
32 | * @param array $config 连接信息
33 | * @return string
34 | */
35 | protected function parseDsn($config)
36 | {
37 | $dsn = 'sqlsrv:Database=' . $config['database'] . ';Server=' . $config['hostname'];
38 | if (!empty($config['hostport'])) {
39 | $dsn .= ',' . $config['hostport'];
40 | }
41 | return $dsn;
42 | }
43 |
44 | /**
45 | * 取得数据表的字段信息
46 | * @access public
47 | * @param string $tableName
48 | * @return array
49 | */
50 | public function getFields($tableName)
51 | {
52 | list($tableName) = explode(' ', $tableName);
53 | $sql = "SELECT column_name, data_type, column_default, is_nullable
54 | FROM information_schema.tables AS t
55 | JOIN information_schema.columns AS c
56 | ON t.table_catalog = c.table_catalog
57 | AND t.table_schema = c.table_schema
58 | AND t.table_name = c.table_name
59 | WHERE t.table_name = '$tableName'";
60 |
61 | $pdo = $this->query($sql, [], false, true);
62 | $result = $pdo->fetchAll(PDO::FETCH_ASSOC);
63 | $info = [];
64 | if ($result) {
65 | foreach ($result as $key => $val) {
66 | $val = array_change_key_case($val);
67 | $info[$val['column_name']] = [
68 | 'name' => $val['column_name'],
69 | 'type' => $val['data_type'],
70 | 'notnull' => (bool) ('' === $val['is_nullable']), // not null is empty, null is yes
71 | 'default' => $val['column_default'],
72 | 'primary' => false,
73 | 'autoinc' => false,
74 | ];
75 | }
76 | }
77 | $sql = "SELECT column_name FROM information_schema.key_column_usage WHERE table_name='$tableName'";
78 | // 调试开始
79 | $this->debug(true);
80 | $pdo = $this->linkID->query($sql);
81 | // 调试结束
82 | $this->debug(false, $sql);
83 | $result = $pdo->fetch(PDO::FETCH_ASSOC);
84 | if ($result) {
85 | $info[$result['column_name']]['primary'] = true;
86 | }
87 | return $this->fieldCase($info);
88 | }
89 |
90 | /**
91 | * 取得数据表的字段信息
92 | * @access public
93 | * @param string $dbName
94 | * @return array
95 | */
96 | public function getTables($dbName = '')
97 | {
98 | $sql = "SELECT TABLE_NAME
99 | FROM INFORMATION_SCHEMA.TABLES
100 | WHERE TABLE_TYPE = 'BASE TABLE'
101 | ";
102 |
103 | $pdo = $this->query($sql, [], false, true);
104 | $result = $pdo->fetchAll(PDO::FETCH_ASSOC);
105 | $info = [];
106 | foreach ($result as $key => $val) {
107 | $info[$key] = current($val);
108 | }
109 | return $info;
110 | }
111 |
112 | /**
113 | * SQL性能分析
114 | * @access protected
115 | * @param string $sql
116 | * @return array
117 | */
118 | protected function getExplain($sql)
119 | {
120 | return [];
121 | }
122 | }
123 |
--------------------------------------------------------------------------------
/example/thinkphp/library/think/db/connector/Mysql.php:
--------------------------------------------------------------------------------
1 |
10 | // +----------------------------------------------------------------------
11 |
12 | namespace think\db\connector;
13 |
14 | use PDO;
15 | use think\db\Connection;
16 | use think\Log;
17 |
18 | /**
19 | * mysql数据库驱动
20 | */
21 | class Mysql extends Connection
22 | {
23 |
24 | protected $builder = '\\think\\db\\builder\\Mysql';
25 |
26 | /**
27 | * 解析pdo连接的dsn信息
28 | * @access protected
29 | * @param array $config 连接信息
30 | * @return string
31 | */
32 | protected function parseDsn($config)
33 | {
34 | $dsn = 'mysql:dbname=' . $config['database'] . ';host=' . $config['hostname'];
35 | if (!empty($config['hostport'])) {
36 | $dsn .= ';port=' . $config['hostport'];
37 | } elseif (!empty($config['socket'])) {
38 | $dsn .= ';unix_socket=' . $config['socket'];
39 | }
40 | if (!empty($config['charset'])) {
41 | $dsn .= ';charset=' . $config['charset'];
42 | }
43 | return $dsn;
44 | }
45 |
46 | /**
47 | * 取得数据表的字段信息
48 | * @access public
49 | * @param string $tableName
50 | * @return array
51 | */
52 | public function getFields($tableName)
53 | {
54 | list($tableName) = explode(' ', $tableName);
55 | if (false === strpos($tableName, '`')) {
56 | if (strpos($tableName, '.')) {
57 | $tableName = str_replace('.', '`.`', $tableName);
58 | }
59 | $tableName = '`' . $tableName . '`';
60 | }
61 | $sql = 'SHOW COLUMNS FROM ' . $tableName;
62 | $pdo = $this->query($sql, [], false, true);
63 | $result = $pdo->fetchAll(PDO::FETCH_ASSOC);
64 | $info = [];
65 | if ($result) {
66 | foreach ($result as $key => $val) {
67 | $val = array_change_key_case($val);
68 | $info[$val['field']] = [
69 | 'name' => $val['field'],
70 | 'type' => $val['type'],
71 | 'notnull' => (bool) ('' === $val['null']), // not null is empty, null is yes
72 | 'default' => $val['default'],
73 | 'primary' => (strtolower($val['key']) == 'pri'),
74 | 'autoinc' => (strtolower($val['extra']) == 'auto_increment'),
75 | ];
76 | }
77 | }
78 | return $this->fieldCase($info);
79 | }
80 |
81 | /**
82 | * 取得数据库的表信息
83 | * @access public
84 | * @param string $dbName
85 | * @return array
86 | */
87 | public function getTables($dbName = '')
88 | {
89 | $sql = !empty($dbName) ? 'SHOW TABLES FROM ' . $dbName : 'SHOW TABLES ';
90 | $pdo = $this->query($sql, [], false, true);
91 | $result = $pdo->fetchAll(PDO::FETCH_ASSOC);
92 | $info = [];
93 | foreach ($result as $key => $val) {
94 | $info[$key] = current($val);
95 | }
96 | return $info;
97 | }
98 |
99 | /**
100 | * SQL性能分析
101 | * @access protected
102 | * @param string $sql
103 | * @return array
104 | */
105 | protected function getExplain($sql)
106 | {
107 | $pdo = $this->linkID->query("EXPLAIN " . $sql);
108 | $result = $pdo->fetch(PDO::FETCH_ASSOC);
109 | $result = array_change_key_case($result);
110 | if (isset($result['extra'])) {
111 | if (strpos($result['extra'], 'filesort') || strpos($result['extra'], 'temporary')) {
112 | Log::record('SQL:' . $this->queryStr . '[' . $result['extra'] . ']', 'warn');
113 | }
114 | }
115 | return $result;
116 | }
117 |
118 | protected function supportSavepoint()
119 | {
120 | return true;
121 | }
122 |
123 | }
124 |
--------------------------------------------------------------------------------
/example/thinkphp/library/think/db/builder/Sqlsrv.php:
--------------------------------------------------------------------------------
1 |
10 | // +----------------------------------------------------------------------
11 |
12 | namespace think\db\builder;
13 |
14 | use think\db\Builder;
15 |
16 | /**
17 | * Sqlsrv数据库驱动
18 | */
19 | class Sqlsrv extends Builder
20 | {
21 | protected $selectSql = 'SELECT T1.* FROM (SELECT thinkphp.*, ROW_NUMBER() OVER (%ORDER%) AS ROW_NUMBER FROM (SELECT %DISTINCT% %FIELD% FROM %TABLE%%JOIN%%WHERE%%GROUP%%HAVING%) AS thinkphp) AS T1 %LIMIT%%COMMENT%';
22 | protected $selectInsertSql = 'SELECT %DISTINCT% %FIELD% FROM %TABLE%%JOIN%%WHERE%%GROUP%%HAVING%';
23 | protected $updateSql = 'UPDATE %TABLE% SET %SET% FROM %TABLE% %JOIN% %WHERE% %LIMIT% %LOCK%%COMMENT%';
24 | protected $deleteSql = 'DELETE FROM %TABLE% %USING% FROM %TABLE% %JOIN% %WHERE% %LIMIT% %LOCK%%COMMENT%';
25 |
26 | /**
27 | * order分析
28 | * @access protected
29 | * @param mixed $order
30 | * @param array $options
31 | * @return string
32 | */
33 | protected function parseOrder($order, $options = [])
34 | {
35 | if (is_array($order)) {
36 | $array = [];
37 | foreach ($order as $key => $val) {
38 | if (is_numeric($key)) {
39 | if (false === strpos($val, '(')) {
40 | $array[] = $this->parseKey($val, $options);
41 | } elseif ('[rand]' == $val) {
42 | $array[] = $this->parseRand();
43 | }
44 | } else {
45 | $sort = in_array(strtolower(trim($val)), ['asc', 'desc']) ? ' ' . $val : '';
46 | $array[] = $this->parseKey($key, $options) . ' ' . $sort;
47 | }
48 | }
49 | $order = implode(',', $array);
50 | }
51 | return !empty($order) ? ' ORDER BY ' . $order : ' ORDER BY rand()';
52 | }
53 |
54 | /**
55 | * 随机排序
56 | * @access protected
57 | * @return string
58 | */
59 | protected function parseRand()
60 | {
61 | return 'rand()';
62 | }
63 |
64 | /**
65 | * 字段和表名处理
66 | * @access protected
67 | * @param string $key
68 | * @param array $options
69 | * @return string
70 | */
71 | protected function parseKey($key, $options = [])
72 | {
73 | $key = trim($key);
74 | if (strpos($key, '.') && !preg_match('/[,\'\"\(\)\[\s]/', $key)) {
75 | list($table, $key) = explode('.', $key, 2);
76 | if ('__TABLE__' == $table) {
77 | $table = $this->query->getTable();
78 | }
79 | if (isset($options['alias'][$table])) {
80 | $table = $options['alias'][$table];
81 | }
82 | }
83 | if (!is_numeric($key) && !preg_match('/[,\'\"\*\(\)\[.\s]/', $key)) {
84 | $key = '[' . $key . ']';
85 | }
86 | if (isset($table)) {
87 | $key = '[' . $table . '].' . $key;
88 | }
89 | return $key;
90 | }
91 |
92 | /**
93 | * limit
94 | * @access protected
95 | * @param mixed $limit
96 | * @return string
97 | */
98 | protected function parseLimit($limit)
99 | {
100 | if (empty($limit)) {
101 | return '';
102 | }
103 |
104 | $limit = explode(',', $limit);
105 | if (count($limit) > 1) {
106 | $limitStr = '(T1.ROW_NUMBER BETWEEN ' . $limit[0] . ' + 1 AND ' . $limit[0] . ' + ' . $limit[1] . ')';
107 | } else {
108 | $limitStr = '(T1.ROW_NUMBER BETWEEN 1 AND ' . $limit[0] . ")";
109 | }
110 | return 'WHERE ' . $limitStr;
111 | }
112 |
113 | public function selectInsert($fields, $table, $options)
114 | {
115 | $this->selectSql = $this->selectInsertSql;
116 | return parent::selectInsert($fields, $table, $options);
117 | }
118 |
119 | }
120 |
--------------------------------------------------------------------------------
/example/thinkphp/library/think/cache/driver/Wincache.php:
--------------------------------------------------------------------------------
1 |
10 | // +----------------------------------------------------------------------
11 |
12 | namespace think\cache\driver;
13 |
14 | use think\cache\Driver;
15 |
16 | /**
17 | * Wincache缓存驱动
18 | * @author liu21st
19 | */
20 | class Wincache extends Driver
21 | {
22 | protected $options = [
23 | 'prefix' => '',
24 | 'expire' => 0,
25 | ];
26 |
27 | /**
28 | * 构造函数
29 | * @param array $options 缓存参数
30 | * @throws \BadFunctionCallException
31 | * @access public
32 | */
33 | public function __construct($options = [])
34 | {
35 | if (!function_exists('wincache_ucache_info')) {
36 | throw new \BadFunctionCallException('not support: WinCache');
37 | }
38 | if (!empty($options)) {
39 | $this->options = array_merge($this->options, $options);
40 | }
41 | }
42 |
43 | /**
44 | * 判断缓存
45 | * @access public
46 | * @param string $name 缓存变量名
47 | * @return bool
48 | */
49 | public function has($name)
50 | {
51 | $key = $this->getCacheKey($name);
52 | return wincache_ucache_exists($key);
53 | }
54 |
55 | /**
56 | * 读取缓存
57 | * @access public
58 | * @param string $name 缓存变量名
59 | * @param mixed $default 默认值
60 | * @return mixed
61 | */
62 | public function get($name, $default = false)
63 | {
64 | $key = $this->getCacheKey($name);
65 | return wincache_ucache_exists($key) ? wincache_ucache_get($key) : $default;
66 | }
67 |
68 | /**
69 | * 写入缓存
70 | * @access public
71 | * @param string $name 缓存变量名
72 | * @param mixed $value 存储数据
73 | * @param integer $expire 有效时间(秒)
74 | * @return boolean
75 | */
76 | public function set($name, $value, $expire = null)
77 | {
78 | if (is_null($expire)) {
79 | $expire = $this->options['expire'];
80 | }
81 | $key = $this->getCacheKey($name);
82 | if ($this->tag && !$this->has($name)) {
83 | $first = true;
84 | }
85 | if (wincache_ucache_set($key, $value, $expire)) {
86 | isset($first) && $this->setTagItem($key);
87 | return true;
88 | }
89 | return false;
90 | }
91 |
92 | /**
93 | * 自增缓存(针对数值缓存)
94 | * @access public
95 | * @param string $name 缓存变量名
96 | * @param int $step 步长
97 | * @return false|int
98 | */
99 | public function inc($name, $step = 1)
100 | {
101 | $key = $this->getCacheKey($name);
102 | return wincache_ucache_inc($key, $step);
103 | }
104 |
105 | /**
106 | * 自减缓存(针对数值缓存)
107 | * @access public
108 | * @param string $name 缓存变量名
109 | * @param int $step 步长
110 | * @return false|int
111 | */
112 | public function dec($name, $step = 1)
113 | {
114 | $key = $this->getCacheKey($name);
115 | return wincache_ucache_dec($key, $step);
116 | }
117 |
118 | /**
119 | * 删除缓存
120 | * @access public
121 | * @param string $name 缓存变量名
122 | * @return boolean
123 | */
124 | public function rm($name)
125 | {
126 | return wincache_ucache_delete($this->getCacheKey($name));
127 | }
128 |
129 | /**
130 | * 清除缓存
131 | * @access public
132 | * @param string $tag 标签名
133 | * @return boolean
134 | */
135 | public function clear($tag = null)
136 | {
137 | if ($tag) {
138 | $keys = $this->getTagItem($tag);
139 | foreach ($keys as $key) {
140 | wincache_ucache_delete($key);
141 | }
142 | $this->rm('tag_' . md5($tag));
143 | return true;
144 | } else {
145 | return wincache_ucache_clear();
146 | }
147 | }
148 |
149 | }
150 |
--------------------------------------------------------------------------------
/example/thinkphp/library/traits/model/SoftDelete.php:
--------------------------------------------------------------------------------
1 | getDeleteTimeField();
18 | if (!empty($this->data[$field])) {
19 | return true;
20 | }
21 | return false;
22 | }
23 |
24 | /**
25 | * 查询软删除数据
26 | * @access public
27 | * @return Query
28 | */
29 | public static function withTrashed()
30 | {
31 | $model = new static();
32 | $field = $model->getDeleteTimeField(true);
33 | return $model->getQuery();
34 | }
35 |
36 | /**
37 | * 只查询软删除数据
38 | * @access public
39 | * @return Query
40 | */
41 | public static function onlyTrashed()
42 | {
43 | $model = new static();
44 | $field = $model->getDeleteTimeField(true);
45 | return $model->getQuery()
46 | ->useSoftDelete($field, ['not null', '']);
47 | }
48 |
49 | /**
50 | * 删除当前的记录
51 | * @access public
52 | * @param bool $force 是否强制删除
53 | * @return integer
54 | */
55 | public function delete($force = false)
56 | {
57 | if (false === $this->trigger('before_delete', $this)) {
58 | return false;
59 | }
60 | $name = $this->getDeleteTimeField();
61 | if (!$force) {
62 | // 软删除
63 | $this->data[$name] = $this->autoWriteTimestamp($name);
64 | $result = $this->isUpdate()->save();
65 | } else {
66 | $result = $this->getQuery()->delete($this->data);
67 | }
68 |
69 | $this->trigger('after_delete', $this);
70 | return $result;
71 | }
72 |
73 | /**
74 | * 删除记录
75 | * @access public
76 | * @param mixed $data 主键列表 支持闭包查询条件
77 | * @param bool $force 是否强制删除
78 | * @return integer 成功删除的记录数
79 | */
80 | public static function destroy($data, $force = false)
81 | {
82 | // 包含软删除数据
83 | $query = self::withTrashed();
84 | if (is_array($data) && key($data) !== 0) {
85 | $query->where($data);
86 | $data = null;
87 | } elseif ($data instanceof \Closure) {
88 | call_user_func_array($data, [ & $query]);
89 | $data = null;
90 | } elseif (is_null($data)) {
91 | return 0;
92 | }
93 |
94 | $resultSet = $query->select($data);
95 | $count = 0;
96 | if ($resultSet) {
97 | foreach ($resultSet as $data) {
98 | $result = $data->delete($force);
99 | $count += $result;
100 | }
101 | }
102 | return $count;
103 | }
104 |
105 | /**
106 | * 恢复被软删除的记录
107 | * @access public
108 | * @param array $where 更新条件
109 | * @return integer
110 | */
111 | public function restore($where = [])
112 | {
113 | $name = $this->getDeleteTimeField();
114 | if (empty($where)) {
115 | $pk = $this->getPk();
116 | $where[$pk] = $this->getData($pk);
117 | }
118 | // 恢复删除
119 | return $this->getQuery()
120 | ->useSoftDelete($name, ['not null', ''])
121 | ->where($where)
122 | ->update([$name => null]);
123 | }
124 |
125 | /**
126 | * 查询默认不包含软删除数据
127 | * @access protected
128 | * @param Query $query 查询对象
129 | * @return void
130 | */
131 | protected function base($query)
132 | {
133 | $field = $this->getDeleteTimeField(true);
134 | $query->useSoftDelete($field);
135 | }
136 |
137 | /**
138 | * 获取软删除字段
139 | * @access public
140 | * @param bool $read 是否查询操作 写操作的时候会自动去掉表别名
141 | * @return string
142 | */
143 | protected function getDeleteTimeField($read = false)
144 | {
145 | $field = property_exists($this, 'deleteTime') && isset($this->deleteTime) ? $this->deleteTime : 'delete_time';
146 | if (!strpos($field, '.')) {
147 | $field = '__TABLE__.' . $field;
148 | }
149 | if (!$read && strpos($field, '.')) {
150 | $array = explode('.', $field);
151 | $field = array_pop($array);
152 | }
153 | return $field;
154 | }
155 | }
156 |
--------------------------------------------------------------------------------
/example/thinkphp/library/think/console/output/descriptor/Console.php:
--------------------------------------------------------------------------------
1 |
10 | // +----------------------------------------------------------------------
11 |
12 | namespace think\console\output\descriptor;
13 |
14 | use think\Console as ThinkConsole;
15 | use think\console\Command;
16 |
17 | class Console
18 | {
19 |
20 | const GLOBAL_NAMESPACE = '_global';
21 |
22 | /**
23 | * @var ThinkConsole
24 | */
25 | private $console;
26 |
27 | /**
28 | * @var null|string
29 | */
30 | private $namespace;
31 |
32 | /**
33 | * @var array
34 | */
35 | private $namespaces;
36 |
37 | /**
38 | * @var Command[]
39 | */
40 | private $commands;
41 |
42 | /**
43 | * @var Command[]
44 | */
45 | private $aliases;
46 |
47 | /**
48 | * 构造方法
49 | * @param ThinkConsole $console
50 | * @param string|null $namespace
51 | */
52 | public function __construct(ThinkConsole $console, $namespace = null)
53 | {
54 | $this->console = $console;
55 | $this->namespace = $namespace;
56 | }
57 |
58 | /**
59 | * @return array
60 | */
61 | public function getNamespaces()
62 | {
63 | if (null === $this->namespaces) {
64 | $this->inspectConsole();
65 | }
66 |
67 | return $this->namespaces;
68 | }
69 |
70 | /**
71 | * @return Command[]
72 | */
73 | public function getCommands()
74 | {
75 | if (null === $this->commands) {
76 | $this->inspectConsole();
77 | }
78 |
79 | return $this->commands;
80 | }
81 |
82 | /**
83 | * @param string $name
84 | * @return Command
85 | * @throws \InvalidArgumentException
86 | */
87 | public function getCommand($name)
88 | {
89 | if (!isset($this->commands[$name]) && !isset($this->aliases[$name])) {
90 | throw new \InvalidArgumentException(sprintf('Command %s does not exist.', $name));
91 | }
92 |
93 | return isset($this->commands[$name]) ? $this->commands[$name] : $this->aliases[$name];
94 | }
95 |
96 | private function inspectConsole()
97 | {
98 | $this->commands = [];
99 | $this->namespaces = [];
100 |
101 | $all = $this->console->all($this->namespace ? $this->console->findNamespace($this->namespace) : null);
102 | foreach ($this->sortCommands($all) as $namespace => $commands) {
103 | $names = [];
104 |
105 | /** @var Command $command */
106 | foreach ($commands as $name => $command) {
107 | if (!$command->getName()) {
108 | continue;
109 | }
110 |
111 | if ($command->getName() === $name) {
112 | $this->commands[$name] = $command;
113 | } else {
114 | $this->aliases[$name] = $command;
115 | }
116 |
117 | $names[] = $name;
118 | }
119 |
120 | $this->namespaces[$namespace] = ['id' => $namespace, 'commands' => $names];
121 | }
122 | }
123 |
124 | /**
125 | * @param array $commands
126 | * @return array
127 | */
128 | private function sortCommands(array $commands)
129 | {
130 | $namespacedCommands = [];
131 | foreach ($commands as $name => $command) {
132 | $key = $this->console->extractNamespace($name, 1);
133 | if (!$key) {
134 | $key = self::GLOBAL_NAMESPACE;
135 | }
136 |
137 | $namespacedCommands[$key][$name] = $command;
138 | }
139 | ksort($namespacedCommands);
140 |
141 | foreach ($namespacedCommands as &$commandsSet) {
142 | ksort($commandsSet);
143 | }
144 | // unset reference to keep scope clear
145 | unset($commandsSet);
146 |
147 | return $namespacedCommands;
148 | }
149 | }
150 |
--------------------------------------------------------------------------------