Skip to content

Commit 89d91ee

Browse files
committed
deprecate DummyPrintConnector in favour of MemoryPrintConnector (a rename w/ backwards compatibility)
1 parent ce85cb2 commit 89d91ee

9 files changed

Lines changed: 109 additions & 80 deletions

File tree

doc/FAQ.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ The purpose of this driver it to generate binary ESC/POS code, which is understo
1010

1111
If the printer understands ESC/POS, and you know how to get raw binary data to it, then yes. Otherwise, no.
1212

13-
The [list of printers that are known to work](https://github.com/mike42/escpos-php/blob/development/README.md#printers) is crowd-sourced. We appreciate it when developers try out the driver, then [file information on the bug tracker](https://github.com/mike42/escpos-php/issues/new) with some information about which features worked on their model of printer.
13+
The [list of printers that are known to work](https://github.com/mike42/escpos-php/blob/development/README.md#printers) is crowdsourced. We appreciate it when developers try out the driver, then [file information on the bug tracker](https://github.com/mike42/escpos-php/issues/new) with some information about which features worked on their model of printer.
1414

1515
To see how well your printer works, first check that it supports ESC/POS, then begin by attempting to send the text "Hello World" to your printer on the command-line, from the computer that will run PHP.
1616

@@ -24,7 +24,7 @@ If you encounter garbage output when you try to print images or special characte
2424

2525
## I have a printer that does not understand ESC/POS. Can I use this driver?
2626

27-
No. The purpose of this driver it to generate binary ESC/POS code. If your printer doesn't understand that, then this code wont be much use to you.
27+
No. The purpose of this driver it to generate binary ESC/POS code. If your printer doesn't understand that, then this code won't be much use to you.
2828

2929
Some printers do have an emulation mode for ESC/POS. The vendor docs will tell if this is the case, and how to enable it.
3030

@@ -46,13 +46,13 @@ The connectors are-
4646

4747
- `FilePrintConnector` and `NetworkPrintConnector` directly use files or network sockets.
4848
- `WindowsPrintConnector` and `CupsPrintConnector` tie in with Windows and Unix system printing.
49-
- `DummyPrintConnector` does not connect to a real printer, and can be used to save ESC/POS receipts to a database, for example.
49+
- `MemoryPrintConnector` does not connect to a real printer, and can be used capture ESC/POS receipts for testing or audit, for example.
5050

51-
At this point, you might find that the way you would like to print is not supported by escpos-php. You can post your printing command as a feature request on the issue tracker.
51+
You might find that the way you would like to print is not supported by escpos-php. You can post your printing command as a feature request on the issue tracker.
5252

5353
Lastly, you may run in to the final common trap:
5454

55-
4. Your PHP is not running with the same sort of permissions as your login account. Again, no amount of PHP code can fix this. For example, on LAMP, your `www-data` user needs to be in the `lp` group, while on WAMP, `Local Service` account may run in to problems. SELinux and firewalls are also worth a look.
55+
4. Your PHP script is not running with the same sort of permissions as your login account. Again, it's not possible for the PHP code itself to fix this. For example, on LAMP, your `www-data` user needs to be in the `lp` group, while on WAMP, `Local Service` account may run in to problems. SELinux and firewalls are also worth a look.
5656

5757
When printing fails, you can expect a PHP Exception that explains what went wrong. They are all clues:
5858

@@ -66,7 +66,7 @@ Please file a bug if you think that there is a specific situation which escpos-p
6666

6767
## Can I print over the network?
6868

69-
Certainly, as long as your printer is available over the network.
69+
Certainly, as long as your printer is available over the network!
7070

7171
- `NetworkPrintConnector` will speak directly to an Ethernet-connected printer on port 9100.
7272

@@ -75,7 +75,7 @@ For USB or Serial printers, you need to install the printer on a computer and th
7575
- `WindowsPrintConnector` will connect to Windows shared printers from Windows or Linux (Linux users will need Samba).
7676
- `CupsPrintConnector` will connect to CUPS-shared printers from Linux or Mac.
7777

78-
Always start by testing your shared printer setup outside of escpos-php. The examples linked to in the README are commented with some example commands to get you started. Typically, networks, firewalls and permissions need to be set up.
78+
Always start by testing your shared printer setup outside of escpos-php. The examples linked to in the README are commented with some example commands to get you started. Typically, networks, firewalls and permissions all need to be set up.
7979

8080
Once you have a working command to send text to your printer (from the PHP server), you are ready to use escpos-php.
8181

@@ -127,7 +127,7 @@ First, ensure you have the Imagick plugin loaded. The driver will avoid a slower
127127

128128
Next, connect over a faster interface. Serial printers have a low bit-rate, and the printer spends a lot of time waiting for data. If you have USB or Ethernet, then use it (note: storing graphics to the printer memory is not currently implemented).
129129

130-
Lastly, the printer will go faster if you use less pixels. Since images are two-dimensional, scaling down by 50% removes 75% of the pixels. The driver can then print at a half the density, so that your lower resolution image appears the same size when printed.
130+
Lastly, images are sent uncompressed in ESC/POS, and you can speed up the transfer if you use fewer pixels. Images are two-dimensional, and scaling down by 50% removes 75% of the pixels. The driver has an option to print at half the density, so that the lower resolution image appears the same size when printed.
131131

132132
## How can I get the status of the printer?
133133

example/specific/235-get-data.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@
66
*/
77
require __DIR__ . '/../../vendor/autoload.php';
88
use Mike42\Escpos\Printer;
9-
use Mike42\Escpos\PrintConnectors\DummyPrintConnector;
9+
use Mike42\Escpos\PrintConnectors\MemoryPrintConnector;
1010
use Mike42\Escpos\CapabilityProfile;
1111

1212
// Make sure you load a Star print connector or you may get gibberish.
13-
$connector = new DummyPrintConnector();
13+
$connector = new MemoryPrintConnector();
1414
$profile = CapabilityProfile::load("TSP600");
1515
$printer = new Printer($connector);
1616
$printer -> text("Hello world!\n");

src/Mike42/Escpos/PrintConnectors/DummyPrintConnector.php

Lines changed: 6 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -17,65 +17,12 @@
1717

1818
/**
1919
* Print connector that writes to nowhere, but allows the user to retrieve the
20-
* buffered data. Used for testing.
20+
* buffered data.
21+
*
22+
* @deprecated Renamed to {@see MemoryPrintConnector}. This subclass is retained
23+
* for backwards compatibility and will be removed in a future major version.
24+
* Use MemoryPrintConnector in new code.
2125
*/
22-
final class DummyPrintConnector implements PrintConnector
26+
class DummyPrintConnector extends MemoryPrintConnector
2327
{
24-
/**
25-
* @var array $buffer
26-
* Buffer of accumilated data.
27-
*/
28-
private $buffer;
29-
30-
/**
31-
* @var string data which the printer will provide on next read
32-
*/
33-
private $readData;
34-
35-
/**
36-
* Create new print connector
37-
*/
38-
public function __construct()
39-
{
40-
$this -> buffer = [];
41-
}
42-
43-
public function clear()
44-
{
45-
$this -> buffer = [];
46-
}
47-
48-
public function __destruct()
49-
{
50-
if ($this -> buffer !== null) {
51-
trigger_error("Print connector was not finalized. Did you forget to close the printer?", E_USER_NOTICE);
52-
}
53-
}
54-
55-
public function finalize(): void
56-
{
57-
$this -> buffer = null;
58-
}
59-
60-
/**
61-
* @return string Get the accumulated data that has been sent to this buffer.
62-
*/
63-
public function getData()
64-
{
65-
return implode($this -> buffer);
66-
}
67-
68-
/**
69-
* {@inheritDoc}
70-
* @see PrintConnector::read()
71-
*/
72-
public function read(int $len): bool|string
73-
{
74-
return $len >= strlen($this -> readData) ? $this -> readData : substr($this -> readData, 0, $len);
75-
}
76-
77-
public function write(string $data): void
78-
{
79-
$this -> buffer[] = $data;
80-
}
8128
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
<?php
2+
3+
/**
4+
* This file is part of escpos-php: PHP receipt printer library for use with
5+
* ESC/POS-compatible thermal and impact printers.
6+
*
7+
* Copyright (c) 2014-2026 Michael Billington < michael.billington@gmail.com >,
8+
* incorporating modifications by others. See CONTRIBUTORS.md for a full list.
9+
*
10+
* This software is distributed under the terms of the MIT license. See LICENSE.md
11+
* for details.
12+
*/
13+
14+
declare(strict_types=1);
15+
16+
namespace Mike42\Escpos\PrintConnectors;
17+
18+
/**
19+
* Print connector that writes to nowhere, but allows the user to retrieve the
20+
* buffered data. Useful for capturing raw ESC/POS output in memory - for
21+
* inspection, for sending over your own transport, or for testing.
22+
*/
23+
class MemoryPrintConnector implements PrintConnector
24+
{
25+
/**
26+
* @var array $buffer
27+
* Buffer of accumilated data.
28+
*/
29+
private $buffer;
30+
31+
/**
32+
* @var string data which the printer will provide on next read
33+
*/
34+
private $readData;
35+
36+
/**
37+
* Create new print connector
38+
*/
39+
public function __construct()
40+
{
41+
$this -> buffer = [];
42+
}
43+
44+
public function clear()
45+
{
46+
$this -> buffer = [];
47+
}
48+
49+
public function __destruct()
50+
{
51+
if ($this -> buffer !== null) {
52+
trigger_error("Print connector was not finalized. Did you forget to close the printer?", E_USER_NOTICE);
53+
}
54+
}
55+
56+
public function finalize(): void
57+
{
58+
$this -> buffer = null;
59+
}
60+
61+
/**
62+
* @return string Get the accumulated data that has been sent to this buffer.
63+
*/
64+
public function getData()
65+
{
66+
return implode($this -> buffer);
67+
}
68+
69+
/**
70+
* {@inheritDoc}
71+
* @see PrintConnector::read()
72+
*/
73+
public function read(int $len): bool|string
74+
{
75+
return $len >= strlen($this -> readData) ? $this -> readData : substr($this -> readData, 0, $len);
76+
}
77+
78+
public function write(string $data): void
79+
{
80+
$this -> buffer[] = $data;
81+
}
82+
}

test/unit/AuresCustomerDisplayTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22

33
use Mike42\Escpos\Devices\AuresCustomerDisplay;
4-
use Mike42\Escpos\PrintConnectors\DummyPrintConnector;
4+
use Mike42\Escpos\PrintConnectors\MemoryPrintConnector;
55
use Mike42\Escpos\CapabilityProfile;
66

77
class AuresCustomerDisplayTest extends PHPUnit\Framework\TestCase
@@ -12,7 +12,7 @@ class AuresCustomerDisplayTest extends PHPUnit\Framework\TestCase
1212
protected function setUp(): void
1313
{
1414
/* Print to nowhere- for testing which inputs are accepted */
15-
$this -> outputConnector = new DummyPrintConnector();
15+
$this -> outputConnector = new MemoryPrintConnector();
1616
$profile = CapabilityProfile::load('OCD-300');
1717
$this -> printer = new AuresCustomerDisplay($this -> outputConnector, $profile);
1818
}

test/unit/EscposPrintBufferTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
* - http://clagnut.com/blog/2380/ (mirrored from the English Wikipedia)
1111
*/
1212
use Mike42\Escpos\Printer;
13-
use Mike42\Escpos\PrintConnectors\DummyPrintConnector;
13+
use Mike42\Escpos\PrintConnectors\MemoryPrintConnector;
1414

1515
class EscposPrintBufferTest extends PHPUnit\Framework\TestCase
1616
{
@@ -19,7 +19,7 @@ class EscposPrintBufferTest extends PHPUnit\Framework\TestCase
1919

2020
protected function setUp(): void
2121
{
22-
$this -> outputConnector = new DummyPrintConnector();
22+
$this -> outputConnector = new MemoryPrintConnector();
2323
$printer = new Printer($this -> outputConnector);
2424
$this -> buffer = $printer -> getPrintBuffer();
2525
}

test/unit/EscposTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22
use Mike42\Escpos\Printer;
3-
use Mike42\Escpos\PrintConnectors\DummyPrintConnector;
3+
use Mike42\Escpos\PrintConnectors\MemoryPrintConnector;
44
use Mike42\Escpos\EscposImage;
55
use Mike42\Escpos\CapabilityProfile;
66

@@ -12,7 +12,7 @@ class EscposTest extends PHPUnit\Framework\TestCase
1212
protected function setUp(): void
1313
{
1414
/* Print to nowhere- for testing which inputs are accepted */
15-
$this -> outputConnector = new DummyPrintConnector();
15+
$this -> outputConnector = new MemoryPrintConnector();
1616
$this -> printer = new Printer($this -> outputConnector);
1717
}
1818

test/unit/Experimental/Unifont/UnifontPrintBufferTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace Mike42\Escpos\Experimental\Unifont;
44

5-
use Mike42\Escpos\PrintConnectors\DummyPrintConnector;
5+
use Mike42\Escpos\PrintConnectors\MemoryPrintConnector;
66
use Mike42\Escpos\Printer;
77
use PHPUnit\Framework\TestCase;
88

@@ -13,7 +13,7 @@ class UnifontPrintBufferTest extends TestCase
1313

1414
protected function setUp(): void
1515
{
16-
$this -> outputConnector = new DummyPrintConnector();
16+
$this -> outputConnector = new MemoryPrintConnector();
1717
$this -> printer = new Printer($this -> outputConnector);
1818
$filename = tempnam(sys_get_temp_dir(), "escpos-php-");
1919
$glyphs = [

test/unit/MultiplePrintConnectorTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
use Mike42\Escpos\PrintConnectors\DummyPrintConnector;
3+
use Mike42\Escpos\PrintConnectors\MemoryPrintConnector;
44
use Mike42\Escpos\PrintConnectors\MultiplePrintConnector;
55
use Mike42\Escpos\Printer;
66

@@ -9,8 +9,8 @@ class MultiplePrintConnectorTest extends PHPUnit\Framework\TestCase
99
public function testOnePrinter()
1010
{
1111
// Set up connector which goes to multiple printers
12-
$kitchenPrinter = new DummyPrintConnector();
13-
$barPrinter = new DummyPrintConnector();
12+
$kitchenPrinter = new MemoryPrintConnector();
13+
$barPrinter = new MemoryPrintConnector();
1414
$connector = new MultiplePrintConnector($kitchenPrinter, $barPrinter);
1515
// Print something
1616
$printer = new Printer($connector);

0 commit comments

Comments
 (0)