DuckPhp\Ext\StrictCheck

简介

用于 严格使用 Db 等情况。使得在调试状态下,不能在 Controller 里 使用 M::Db();等 不能在 service 里调用 service

选项

  1. 'namespace' => '',
  2. 'namespace_controller' => '',
  3. 'namespace_service' => '',
  4. 'namespace_model' => '',
  5. 'controller_base_class' => '',
  6. 'is_debug' => true,
  7. 'app_class' => null,

扩充方法

方法

public function init($options=[], $context=null) public function checkStrictComponent($component_name, $trace_level, $parent_classes_to_skip=[]) public function checkStrictModel($trace_level) public function checkStrictService($service_class, $trace_level) protected function getCallerByLevel($level, $parent_classes_to_skip=[]) protected function checkEnv(): bool

详解

没文档,先看单元覆盖测试吧。

  1. public function __construct()
  2. public function init(array $options, object $context = null)
  3. protected function initContext($options = [], $context = null)
  4. public static function CheckStrictDB()
  5. public function getCallerByLevel($level, $parent_classes_to_skip = [])
  6. public function checkEnv(): bool
  7. public function checkStrictComponent($component_name, $trace_level, $parent_classes_to_skip = [])
  8. public function checkStrictModel($trace_level)
  9. public function checkStrictService($service_class, $trace_level)

这个例子禁止了Controller 里调用 DB ,禁止调用 Model

  1. class StrictCheckTestMain extends BaseController
  2. {
  3. public function index()
  4. {
  5. }
  6. public function foo()
  7. {
  8. try{
  9. DuckPhp::DB()->fetch("select 1+1 as t");
  10. }catch(\Throwable $ex){
  11. echo "111111111111".$ex->getMessage().PHP_EOL;
  12. }
  13. try{
  14. M::DB()->fetch("select 1+1 as t");
  15. }catch(\Throwable $ex){
  16. echo "2222222222222222222 Catch S::DB ".$ex->getMessage().PHP_EOL;
  17. }
  18. try{
  19. (new t)->foo();
  20. }catch(\Throwable $ex){
  21. echo "33333333333333333333333".$ex->getMessage().PHP_EOL;
  22. }
  23. try{
  24. FakeModel::G()->foo();
  25. }catch(\Throwable $ex){
  26. echo "4444444444444444444444444".$ex->getMessage().PHP_EOL;
  27. }
  28. try{
  29. FakeService::G()->callService();
  30. }catch(\Throwable $ex){
  31. echo "55555555555555555555555555555FakeService::G()->callService()".$ex->getMessage().PHP_EOL;
  32. }
  33. try{
  34. FakeService::G()->modelCallService();
  35. }catch(\Throwable $ex){
  36. echo "sssssssss modelCallService sssssssssssssssssss".$ex->getMessage().PHP_EOL;
  37. }
  38. try{
  39. FakeService::G()->callDB();
  40. }catch(\Throwable $ex){
  41. echo "sssssssss modelCallService sssssssssssssssssss".$ex->getMessage().PHP_EOL;
  42. }
  43. try{
  44. DuckPhp::DB()->fetch("select 1+1 as t");
  45. }catch(\Throwable $ex){
  46. echo "zzzzzzzzzzzzz".$ex->getMessage().PHP_EOL;
  47. }
  48. try{
  49. M::DB()->fetch("select 1+1 as t");
  50. }catch(\Throwable $ex){
  51. echo "zzzzzzzzzzzzz Catch S::DB ".$ex->getMessage().PHP_EOL;
  52. }
  53. try{
  54. (new BaseController2)->foo();
  55. }catch(\Throwable $ex){
  56. echo "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa".$ex->getMessage().PHP_EOL;
  57. }
  58. FakeService::G()->normal();
  59. echo "============================\n";
  60. FakeBatchService::G()->foo();
  61. }
  62. }