LogicTrait.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <?php
  2. /**
  3. * Created by CRMEB.
  4. * User: 136327134@qq.com
  5. * Date: 2019/4/9 16:50
  6. */
  7. namespace app\core\traits;
  8. trait LogicTrait
  9. {
  10. protected $items = [];
  11. /*
  12. * 魔术方法 对不可访问或不存在的属性调用
  13. *
  14. * */
  15. public function __isset($name)
  16. {
  17. }
  18. /*
  19. * 魔术方法 对不可访问或不存在的属性进行unset时被调用
  20. * */
  21. public function __unset($name)
  22. {
  23. }
  24. /*
  25. * 静态方法调用
  26. * @param string $method 调用方法
  27. * @param mixed $args 参数
  28. * */
  29. public static function __callStatic($method,$args)
  30. {
  31. }
  32. /*
  33. * 执行本类的方法
  34. * @param string $carryoutname 方法名
  35. * @return boolean
  36. * */
  37. public static function CarryOut($carryoutname)
  38. {
  39. $methords = get_class_methods(self::class);
  40. if(!in_array($carryoutname,$methords)) return false;
  41. try{
  42. return (new self)->$carryoutname();
  43. }catch (\Exception $e){
  44. return false;
  45. }
  46. }
  47. /*
  48. * 配置参数
  49. *
  50. * */
  51. protected function setConfig(array $config=[])
  52. {
  53. foreach ($config as $key => $value) {
  54. $this->set($this->items,$key, $value);
  55. }
  56. }
  57. /*
  58. * 设置参数
  59. * @param array $array
  60. * @param string $key
  61. * @param string $value
  62. * */
  63. protected function set(&$array, $key, $value)
  64. {
  65. if (is_null($key)) return $array = $value;
  66. $keys = explode('.', $key);
  67. while (count($keys) > 1) {
  68. $key = array_shift($keys);
  69. if (!isset($array[$key]) || !is_array($array[$key])) {
  70. $array[$key] = [];
  71. }
  72. $array = &$array[$key];
  73. }
  74. $array[array_shift($keys)] = $value;
  75. return $array;
  76. }
  77. /*
  78. * 实例化类
  79. *
  80. * */
  81. protected function registerProviders()
  82. {
  83. foreach ($this->providers as $key=>$provider)
  84. {
  85. $this->register(new $provider(),$key);
  86. }
  87. }
  88. /*
  89. * 获取类内配置信息
  90. * @param object $pimple
  91. * @return this
  92. * */
  93. protected function register($pimple,$key)
  94. {
  95. $response=$pimple->register($this->items);
  96. if(is_array($response)) {
  97. list($key,$provider)=$response;
  98. $this->$key= $provider;
  99. }else if(is_string($key)){
  100. $this->$key= $pimple;
  101. }
  102. return $this;
  103. }
  104. /*
  105. * 实例化本类
  106. * @param array $config
  107. * @return this
  108. * */
  109. public static function instance($config=[])
  110. {
  111. $that=new self();
  112. $that->setConfig($config);
  113. $that->registerProviders();
  114. return $that;
  115. }
  116. }