LogicTrait.php 2.4 KB

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