1
|
|
|
<?php |
2
|
|
|
namespace PhpBoot; |
3
|
|
|
use PhpBoot\DB\DB; |
4
|
|
|
use PhpBoot\ORM\ModelWithClass; |
5
|
|
|
use PhpBoot\ORM\ModelWithObject; |
6
|
|
|
use PhpBoot\Utils\Logger; |
7
|
|
|
|
8
|
|
|
if (! function_exists('PhpBoot\abort')) { |
9
|
|
|
/** |
10
|
|
|
* 抛出异常, 并记录日志 |
11
|
|
|
* @param string|\Exception $error |
12
|
|
|
* @param array $context |
13
|
|
|
* @param string $level "error"|"warning"|"info"|"debug"|null |
14
|
|
|
* @throws \Exception |
15
|
|
|
*/ |
16
|
|
|
function abort($error = '', $context=[], $level='warning') |
17
|
|
|
{ |
18
|
9 |
|
if(is_object($context)){ |
19
|
|
|
$context = get_object_vars($context); |
20
|
|
|
} |
21
|
9 |
|
if($error instanceof \Exception){ |
22
|
8 |
|
$e = $error; |
23
|
8 |
|
$message = "exception '".get_class($error)."' with message {$error->getMessage()}"; |
24
|
8 |
|
}else{ |
25
|
1 |
|
$e = new \RuntimeException($error); |
26
|
1 |
|
$message = $error; |
27
|
|
|
} |
28
|
9 |
|
$trace = $e->getTrace(); |
29
|
|
|
|
30
|
9 |
|
if($e->getFile() == __FILE__){ |
31
|
1 |
|
$file = $trace[0]['file']; |
32
|
1 |
|
$line = $trace[0]['line']; |
33
|
1 |
|
}else{ |
34
|
8 |
|
$file = $e->getFile(); |
35
|
8 |
|
$line = $e->getLine(); |
36
|
|
|
} |
37
|
9 |
|
if($level){ |
38
|
9 |
|
Logger::$level($message, $context +['@file'=>$file, '@line'=>$line]); |
39
|
9 |
|
} |
40
|
9 |
|
throw $e; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
if (!function_exists('PhpBoot\model')) { |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @param DB $db |
49
|
|
|
* @param @param object |
50
|
|
|
* @return ModelWithObject |
51
|
|
|
*/ |
52
|
|
|
function model(DB $db, $entity) |
53
|
|
|
{ |
54
|
4 |
|
return $db->getApp()->make(ModelWithObject::class, ['db'=>$db, 'entity'=>$entity]); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @param DB $db |
59
|
|
|
* @param @param string $entity |
60
|
|
|
* @return ModelWithClass |
61
|
|
|
*/ |
62
|
|
|
function models(DB $db, $entity) |
63
|
|
|
{ |
64
|
6 |
|
if(is_object($entity)){ |
65
|
|
|
return $db->getApp()->make(ModelWithObject::class, ['db'=>$db, 'entity'=>$entity]); |
66
|
|
|
}else{ |
67
|
6 |
|
return $db->getApp()->make(ModelWithClass::class, ['db'=>$db, 'entityName'=>$entity]); |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
} |