提交 9cafa2fa 编写于 作者: Y yuenblue

Merge branch 'master' of gitcode.net:yuencczzy/phpdemo

......@@ -10,6 +10,8 @@ composer create-project thinkcmf/thinkcmf=~6.0 demothinkcmf6
composer create-project thinkcmf/thinkcmf=~8.0 demothinkcmf8
ln -s /usr/local/etc/php/8.2/ php82
/usr/local/opt/mysql@5.7/bin/mysqld_safe --datadir\=/usr/local/var/mysql
./vendor/bin/phpunit -dxdebug.mode=debug -dxdebug.start_with_request=yes -dxdebug.client_host=localhost -dxdebug.client_port=9003 tests/DemoTest.php
php -dxdebug.mode=debug -dxdebug.start_with_request=yes -dxdebug.client_host=localhost -dxdebug.client_port=9003 index.php
php ../phpunit-10.5.13.phar tests/DemoTest.php
......
<?php
// class Outer {
// // 外部类的成员变量和方法
// class Inner {
// // 内部类的成员变量和方法
// }
// }
// class Outer{
// public $aa=null;
// function __construct()
// {
// $this->aa='aaaa';
// }
// function say(){
// print $this->aa;
// }
// class Inner{
// }
// }
// $outer = new Outer();
// $outer->say();
\ No newline at end of file
<?php
namespace app;
enum UserState:int{
case Aa=0;
case Bb=1;
case Cc=2;
public function label(): string{
return match ($this) {
static::Aa => "啊啊啊Aa",
static::Bb => "哦哦哦Bb",
static::Cc => "uuuCc",
};
}
}
class User{
function say() {
print "hello";
print UserState::Aa->label();
}
}
\ No newline at end of file
......@@ -2,11 +2,17 @@
use PHPUnit\Framework\TestCase;
use app\User;
use app\UserState;
use Monolog\Level;
use Monolog\Logger;
use Monolog\Handler\StreamHandler;
use PgSql\Lob;
use function PHPUnit\Framework\assertTrue;
class DemoTest extends TestCase
{
function tearDown(): void
......@@ -16,12 +22,19 @@ class DemoTest extends TestCase
public function testAaa(){
$user=new User();
$user->say();
print UserState::Cc->name;
$aa =11;
}
function testInnerClass(){
}
function testLog() {
// create a log channel
$log = new Logger('name');
$log->pushHandler(new StreamHandler('php://stdout', Level::Warning));
$log->pushHandler(new StreamHandler('php://stdout'),Level::Warning);
// add records to the log
$log->warning('Foo');
......
APP_DEBUG = true [APP] DEFAULT_TIMEZONE = Asia/Shanghai [DATABASE] TYPE = mysql HOSTNAME = 127.0.0.1 DATABASE = test USERNAME = username PASSWORD = password HOSTPORT = 3306 CHARSET = utf8 DEBUG = true [LANG] default_lang = zh-cn
\ No newline at end of file
APP_DEBUG = false
[APP]
DEFAULT_TIMEZONE = Asia/Shanghai
[DATABASE]
TYPE = mysql
HOSTNAME = 127.0.0.1
DATABASE = test
USERNAME = username
PASSWORD = password
HOSTPORT = 3306
CHARSET = utf8
DEBUG = true
[LANG]
default_lang = zh-cn
\ No newline at end of file
<?php
declare (strict_types = 1);
declare(strict_types=1);
namespace app;
......@@ -9,6 +10,7 @@ use Monolog\Handler\StreamHandler;
use think\facade\Db;
use think\Service;
use think\facade\Event;
/**
* 应用服务类
*/
......@@ -31,30 +33,29 @@ class AppService extends Service
// echo "??????";
// });
// Event::subscribe("app\subscribe\Mysql");
$logger_sql=new Logger("logger_sql");
$handler=new StreamHandler('php://stdout', Logger::WARNING);
$dateFormat = "Y-m-d, H:i:s";
$output = "%datetime% > %level_name% > %message%\n";
// $output = "%datetime% > %level_name% > %message% %context% %extra%\n";
$formatter = new LineFormatter($output, $dateFormat);
$handler->setFormatter($formatter);
$logger_sql->pushHandler($handler);
if (env("APP_DEBUG")==true) {
$logger_sql = new Logger("logger_sql");
$handler = new StreamHandler('php://stdout', Logger::WARNING);
$dateFormat = "Y-m-d, H:i:s";
$output = "%datetime% > %level_name% > %message%\n";
// $output = "%datetime% > %level_name% > %message% %context% %extra%\n";
$formatter = new LineFormatter($output, $dateFormat);
$handler->setFormatter($formatter);
$logger_sql->pushHandler($handler);
$this->app->bind('logger_sql',$logger_sql);
Db::listen(function($sql, $runtime, $master) {
// 进行监听处理
// file_put_contents("php://stdout","==============={$sql}===========\r\n");
/**
* @var Logger
*/
$log = $this->app->logger_sql;
$master=$master?$master:'default';
$log->warning("{$sql} Take {$runtime} By {$master}");
});
$this->app->bind('logger_sql', $logger_sql);
Db::listen(function ($sql, $runtime, $master) {
// 进行监听处理
// file_put_contents("php://stdout","==============={$sql}===========\r\n");
/**
* @var Logger
*/
$log = $this->app->logger_sql;
$master = $master ? $master : 'default';
$log->warning("{$sql} Take {$runtime} By {$master}");
});
}
}
public function boot()
......
<?php
namespace app\model;
use think\Model;
enum UserState:int{
case Inactive=0;
case Activated=1;
case Frozen=2;
public function label(): string{
return match ($this) {
static::Inactive => "未激活",
static::Activated => "已激活",
static::Frozen => "已冻结",
};
}
}
final class User extends Model
{
......
......@@ -2,6 +2,7 @@
namespace think;
use app\model\User;
use app\model\UserState;
use PHPUnit\Framework\TestCase;
use think\facade\Cache;
use think\facade\Config;
......@@ -38,11 +39,16 @@ class DemoTp6 extends TestCase{
$cc=env("database.HOSTPORT");
var_dump($bb,$cc);
}
function test_select_user() {
$user = User::find(3);
print User::class;
print UserState::from($user->status)->label();
}
function test_createData() {
$user =new User();
$user->username='aaaa';
$user->password='bbbb';
$user->status=UserState::Activated->value;
$user->save();
}
function test_modal() {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册