Factory.Class.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. <?php
  2. namespace Mall\Framework;
  3. use Mall\Framework\Cache\Redis;
  4. use Mall\Framework\Core\Cache;
  5. use Mall\Framework\Core\Config;
  6. use Mall\Framework\Core\Cookie;
  7. use Mall\Framework\Core\Db;
  8. use Mall\Framework\Core\File;
  9. use Mall\Framework\Core\RedisQueue;
  10. use Mall\Framework\Core\Request;
  11. use Mall\Framework\Core\View;
  12. use Mall\Framework\Core\Session;
  13. use Mall\Framework\Core\SendMail;
  14. use Mall\Framework\Core\SendSms;
  15. use Mall\Framework\Core\Swoole;
  16. use Mall\Framework\Core\Aes;
  17. use Mall\Framework\Core\Search;
  18. use Mall\Framework\Core\BaseImg;
  19. use Mall\Framework\Core\VerificationCode;
  20. use Mall\Framework\Core\Logs;
  21. abstract class Factory
  22. {
  23. /**
  24. * @param array $options
  25. * @return Redis
  26. * @throws \Exception
  27. */
  28. public static function cache($options = [])
  29. {
  30. $cacheConfig = self::config()->get('cache');
  31. $options = $options ?: $cacheConfig['default'];
  32. if (!$options) {
  33. throw new \Exception('The Factory::cache need default cache config');
  34. } else if (is_string($options)) {
  35. $optionsData = $cacheConfig[$options];
  36. }
  37. if (!is_array($optionsData)) {
  38. throw new \Exception('config file cache node '.$options.' is error');
  39. }
  40. return Cache::getInstance($optionsData);
  41. }
  42. public static function config()
  43. {
  44. return Config::getInstance();
  45. }
  46. public static function logs($logPath='', $logSaveFileApp='', $logSystem = '')
  47. {
  48. return Logs::getInstance($logPath, $logSaveFileApp, $logSystem);
  49. }
  50. public static function cookie($options = [])
  51. {
  52. $options = $options ?: self::config()->get('cookie');
  53. if (!$options || !is_array($options)) {
  54. throw new \Exception('The Factory::cookie need default cookie config');
  55. }
  56. return Cookie::getInstance($options);
  57. }
  58. public static function request()
  59. {
  60. return Request::getInstance();
  61. }
  62. public static function view($options = [])
  63. {
  64. $options = $options ?: self::config()->get('smarty');
  65. if (!$options || !is_array($options)) {
  66. throw new \Exception('The Factory::view need default smarty config');
  67. }
  68. return View::getInstance($options);
  69. }
  70. /**
  71. * 另外一种直接修改php.ini 配置方式,配置项如下:
  72. * session.save_handler = Redis
  73. * session.save_path = “tcp://192.168.5.114:6379?auth=password&database=3”
  74. */
  75. public static function session($options = [])
  76. {
  77. $options = $options ?: self::config()->get('session');
  78. if (!$options || !is_array($options)) {
  79. throw new \Exception('The Factory::session need default session config');
  80. }
  81. foreach ($options as $k => &$v) {
  82. if (empty($v)) {
  83. unset($options[$k]);
  84. }
  85. if ($k == 'save_path') {
  86. $options[$k] = urldecode($options[$k]);
  87. }
  88. }
  89. Session::setOptions($options);
  90. return Session::getInstance();
  91. }
  92. public static function sendmail()
  93. {
  94. return SendMail::getInstance();
  95. }
  96. /**
  97. * 发送短信消息
  98. *
  99. * @return mixed
  100. */
  101. public static function sendSms()
  102. {
  103. return SendSms::getInstance();
  104. }
  105. public static function db($options = 'default')
  106. {
  107. $options = $options ?: self::config()->get('db');
  108. if (!$options) {
  109. throw new \Exception('The Factory::db need default db config');
  110. } else if (is_string($options)) {
  111. $dbconfig = self::config()->get('db');
  112. $options = $dbconfig[$options];
  113. }
  114. if (!is_array($options)) {
  115. throw new \Exception('The Factory::db config is error');
  116. }
  117. return Db::getInstance($options);
  118. }
  119. public static function swoole($options = [], $driver = 'Client')
  120. {
  121. if (!is_array($options) || empty($options)) {
  122. throw new \Exception('Need default Swoole ' . $driver . ' config');
  123. }
  124. return Swoole::getInstance($options, $driver);
  125. }
  126. public static function aes($encryptKey = '')
  127. {
  128. return aes::getInstance($encryptKey);
  129. }
  130. public static function search($options = 'default')
  131. {
  132. $options = $options ?: self::config()->get('search');
  133. if (!$options) {
  134. throw new \Exception('The Factory::search need default search config');
  135. } else if (is_string($options)) {
  136. $dbconfig = self::config()->get('search');
  137. $options = isset($dbconfig[$options]) ? $dbconfig[$options]: $dbconfig['default'];
  138. }
  139. if (!is_array($options)) {
  140. throw new \Exception('The Factory::search config is error');
  141. }
  142. $search = new Search($options);
  143. return $search->getConnection();
  144. }
  145. /**
  146. * @return File
  147. */
  148. public static function baseImg()
  149. {
  150. return BaseImg::getInstance();
  151. }
  152. /**
  153. * @param string $filename
  154. * @param string $mode
  155. * @return File
  156. */
  157. public static function file($filename = '', $mode = 'r')
  158. {
  159. return File::getInstance($filename, $mode);
  160. }
  161. public static function redisQueue(Redis $redis, $queueName = '')
  162. {
  163. return RedisQueue::getInstance($redis, $queueName);
  164. }
  165. public static function verificationCode()
  166. {
  167. return VerificationCode::getInstance();
  168. }
  169. }