LogicTrait.php 2.7 KB

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