Driver.php 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. <?php
  2. namespace app\common\library\printer;
  3. use app\common\exception\BaseException;
  4. use app\common\enum\PrinterType as PrinterTypeEnum;
  5. /**
  6. * 小票打印机驱动
  7. * Class driver
  8. * @package app\common\library\printer
  9. */
  10. class Driver
  11. {
  12. private $printer; // 当前打印机
  13. private $engine; // 当前打印机引擎类
  14. /** @var array $engineList 打印机引擎列表 */
  15. private static $engineList = [
  16. PrinterTypeEnum::FEI_E_YUN => 'Feie',
  17. PrinterTypeEnum::PRINT_CENTER => 'PrintCenter',
  18. ];
  19. /**
  20. * 构造方法
  21. * Driver constructor.
  22. * @param $printer
  23. * @throws BaseException
  24. */
  25. public function __construct($printer)
  26. {
  27. // 当前打印机
  28. $this->printer = $printer;
  29. // 实例化当前打印机引擎
  30. $this->engine = $this->getEngineClass();
  31. }
  32. /**
  33. * 执行打印请求
  34. * @param $content
  35. * @return bool
  36. */
  37. public function printTicket($content)
  38. {
  39. return $this->engine->printTicket($content);
  40. }
  41. /**
  42. * 获取错误信息
  43. * @return mixed
  44. */
  45. public function getError()
  46. {
  47. return $this->engine->getError();
  48. }
  49. /**
  50. * 获取当前的打印机引擎类
  51. * @return mixed
  52. * @throws BaseException
  53. */
  54. private function getEngineClass()
  55. {
  56. $engineName = self::$engineList[$this->printer['printer_type']['value']];
  57. $classSpace = __NAMESPACE__ . "\\engine\\{$engineName}";
  58. if (!class_exists($classSpace)) {
  59. throw new BaseException("未找到打印机引擎类: {$engineName}");
  60. }
  61. return new $classSpace($this->printer['printer_config'], $this->printer['print_times']);
  62. }
  63. }