Skip to content

Commit 199d9a9

Browse files
committed
fix
1 parent 71cf30a commit 199d9a9

File tree

4 files changed

+201
-0
lines changed

4 files changed

+201
-0
lines changed

Framework/Base/BaseObject.php

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: zhjx
5+
* Date: 2018/11/5
6+
* Time: 11:30
7+
*/
8+
9+
namespace Framework\Base;
10+
11+
12+
use Framework\Framework;
13+
14+
abstract class BaseObject implements Objects
15+
{
16+
17+
public function __construct($config = [])
18+
{
19+
if (!empty($config)) {
20+
Framework::configure($this, $config);
21+
}
22+
$this->init();
23+
}
24+
25+
public static function className()
26+
{
27+
return get_called_class();
28+
}
29+
30+
public function init(){
31+
32+
}
33+
34+
public function __get($name)
35+
{
36+
// TODO: Implement __get() method.
37+
}
38+
39+
public function __set($name, $value)
40+
{
41+
$setter = 'set' . $name;
42+
if (method_exists($this, $setter)) {
43+
$this->$setter($value);
44+
} elseif (method_exists($this, 'get' . $name)) {
45+
throw new Exception('Setting read-only property: ' . get_class($this) . '::' . $name);
46+
} else {
47+
throw new Exception('Setting unknown property: ' . get_class($this) . '::' . $name);
48+
}
49+
}
50+
51+
public function __isset($name)
52+
{
53+
// TODO: Implement __isset() method.
54+
}
55+
}

Framework/Base/Objects.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: zhjx
5+
* Date: 2018/11/5
6+
* Time: 11:24
7+
*/
8+
9+
namespace Framework\Base;
10+
11+
12+
interface Objects
13+
{
14+
}

Framework/Tool/Log.php

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: Administrator
5+
* Date: 2018/11/22
6+
* Time: 16:07
7+
*/
8+
9+
namespace Framework\Tool;
10+
11+
use Framework\Tool\Tool;
12+
13+
class Log
14+
{
15+
use \Framework\Traits\SingletonTrait;
16+
const INFO = 0;
17+
const NOTICE = 1;
18+
const TRACE = 2;
19+
const ERROR = 3;
20+
public $level = self::INFO;
21+
22+
static $levelInfo = [
23+
self::INFO => 'INFO',
24+
self::NOTICE => 'NOTICE',
25+
self::TRACE => 'TRACE',
26+
self::ERROR => 'ERROR',
27+
];
28+
29+
30+
public $config = [
31+
'is_display' => false,
32+
'level' => self::INFO,
33+
'log_file' => 'Log.log'
34+
];
35+
36+
public function setConfig($config)
37+
{
38+
$this->config = array_merge($this->config, $config);
39+
}
40+
41+
public function setLevel($level)
42+
{
43+
if (isset($level) && in_array($level, array_keys(self::$levelInfo))) {
44+
$this->level = $level;
45+
}
46+
47+
}
48+
49+
public static function Conversion($level = '')
50+
{
51+
if (!$level) {
52+
$level = self::INFO;
53+
return self::$levelInfo[$level];
54+
}
55+
$level = strtoupper($level);
56+
if (in_array($level, self::$levelInfo)) {
57+
$tmp = array_flip(self::$levelInfo);
58+
return $tmp[$level];
59+
}
60+
$level = self::INFO;
61+
return self::$levelInfo[$level];
62+
63+
}
64+
65+
function put($msg, $level = '')
66+
{
67+
$level = self::Conversion($level);
68+
if ($this->config['is_display']) {
69+
echo $this->format($msg, $level);
70+
} else { //写入到日志里面
71+
$log_dir = $this->config['log_dir']; //日志目录
72+
if (!is_dir($log_dir)) {
73+
$is_dir = Tool::createDir($log_dir);
74+
if (!$is_dir) {
75+
throw new Exception("目录{$log_dir}创建失败!!");
76+
}
77+
}
78+
if (!$msg) {
79+
return;
80+
}
81+
$logFilePath = $log_dir . DIRECTORY_SEPARATOR . date("Y_m_d_") . $this->config['log_file'];
82+
$msg = $this->format($msg, $level);
83+
$msg = $msg . "\r\n";
84+
file_put_contents($logFilePath, $msg, FILE_APPEND);
85+
}
86+
}
87+
88+
public function format($msg, $level = '')
89+
{
90+
if (!$level) {
91+
$level = $this->level;
92+
}
93+
$dateFormatStr = "[Y-m-d H:i:s]";
94+
return date($dateFormatStr) . "\t" . self::$levelInfo[$level] . "\t" . $msg;
95+
}
96+
}

Server/Task/ProcessAsyncTask.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: hdeng
5+
* Date: 2018/11/29
6+
* Time: 9:21
7+
*/
8+
9+
namespace Server\Task;
10+
11+
12+
class ProcessAsyncTask
13+
{
14+
public function onPipeMessage($processObjectData, $processObjectParams)
15+
{ print_r($processObjectData);
16+
print_r($processObjectParams);
17+
list($classNameSpacePath, $method) = $processObjectData;
18+
$classObject = new $classNameSpacePath();
19+
$controllerInstance = new \ReflectionClass($classNameSpacePath);
20+
if ($controllerInstance->hasMethod($method)) {
21+
$classMethod = new \ReflectionMethod($classNameSpacePath, $method);
22+
if ($classMethod->isPublic()) {
23+
try {
24+
$classObject->$method(...$processObjectParams);
25+
} catch (\Exception $e) {
26+
throw new \Exception($e->getMessage(), 2);
27+
} catch (\Throwable $t) {
28+
29+
throw new \Exception($t->getMessage(), 1);
30+
}
31+
} else {
32+
throw new \Exception('class method ' . $method . ' is static or private, protected property, can not be object call!', 1);
33+
}
34+
}
35+
}
36+
}

0 commit comments

Comments
 (0)