Driver.php 1.8 KB

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