Push.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?php
  2. namespace addons\baidupush\library;
  3. use addons\baidupush\library\push\Driver;
  4. /**
  5. * Push操作类
  6. */
  7. class Push
  8. {
  9. /**
  10. * @var array Push的实例
  11. */
  12. public static $instance = [];
  13. /**
  14. * @var object 操作句柄
  15. */
  16. public static $handler;
  17. /**
  18. * 连接Push驱动
  19. * @access public
  20. * @param array $options 配置数组
  21. * @param bool|string $name Push连接标识 true 强制重新初始化
  22. * @return Driver
  23. */
  24. public static function connect(array $options = [], $name = false)
  25. {
  26. $type = !empty($options['type']) ? $options['type'] : 'normal';
  27. $config = get_addon_config('baidupush');
  28. $type = strtolower($type);
  29. $options = array_merge($options, isset($config[$type]) ? $config[$type] : []);
  30. if (false === $name) {
  31. $name = md5(serialize($options));
  32. }
  33. if (true === $name || !isset(self::$instance[$name])) {
  34. $class = false === strpos($type, '\\') ?
  35. '\\addons\\baidupush\\library\\push\\driver\\' . ucwords($type) :
  36. $type;
  37. if (true === $name) {
  38. return new $class($options);
  39. }
  40. self::$instance[$name] = new $class($options);
  41. }
  42. return self::$instance[$name];
  43. }
  44. /**
  45. * 自动初始化Push
  46. * @access public
  47. * @param array $options 配置数组
  48. * @return Driver
  49. */
  50. public static function init(array $options = [])
  51. {
  52. if (is_null(self::$handler)) {
  53. self::$handler = self::connect($options);
  54. }
  55. return self::$handler;
  56. }
  57. /**
  58. * 推送实时链接
  59. * @access public
  60. * @param array $urls URL数组
  61. * @return bool
  62. */
  63. public static function realtime($urls)
  64. {
  65. return self::init()->realtime($urls);
  66. }
  67. /**
  68. * 推送历史链接
  69. * @access public
  70. * @param array $urls URL数组
  71. * @return bool
  72. */
  73. public static function history($urls)
  74. {
  75. return self::init()->history($urls);
  76. }
  77. /**
  78. * 删除链接
  79. * @access public
  80. * @param array $urls URL数组
  81. * @return mixed
  82. */
  83. public static function delete($urls)
  84. {
  85. return self::init()->delete($urls);
  86. }
  87. }