1 : <?php
2 :
3 : /**
4 : * \Midi\Reporting\FilePrinter
5 : *
6 : * @package Midi
7 : * @subpackage Reporting
8 : * @copyright © 2009 Tommy Montgomery <http://phpmidiparser.com/>
9 : * @since 1.0
10 : */
11 :
12 : namespace Midi\Reporting;
13 :
14 : use \Midi\Parsing\Parser;
15 : use \Midi\MidiException;
16 : use \SplFileObject;
17 :
18 : /**
19 : * Printer that redirects its output to a file
20 : *
21 : * @package Midi
22 : * @subpackage Reporting
23 : * @since 1.0
24 : */
25 : class FilePrinter extends Printer {
26 :
27 : /**
28 : * @var SplFileObject
29 : */
30 : protected $file;
31 :
32 : /**
33 : * @var int
34 : */
35 : protected $bytesWritten;
36 :
37 : /**
38 : * @since 1.0
39 : *
40 : * @param Formatter $formatter
41 : * @param Parser $parser
42 : */
43 : public function __construct(Formatter $formatter, Parser $parser) {
44 4 : parent::__construct($formatter, $parser);
45 4 : $this->file = null;
46 4 : $this->bytesWritten = 0;
47 4 : }
48 :
49 : /**
50 : * Opens and returns a file object suitable for writing
51 : *
52 : * @since 1.0
53 : *
54 : * @param string $file Path to the file to open
55 : * @param bool $binary Whether to open the file in binary mode
56 : * @return SplFileObject
57 : */
58 : public function createFileObject($file, $binary = false) {
59 2 : $mode = 'w' . ($binary ? 'b' : '');
60 2 : return new SplFileObject($file, $mode);
61 : }
62 :
63 : /**
64 : * @since 1.0
65 : *
66 : * @param string $data
67 : */
68 : protected function printData($data) {
69 2 : if ($this->file === null) {
70 1 : throw new MidiException('Target not set');
71 : }
72 :
73 1 : $this->file->fwrite($data, strlen($data));
74 1 : $this->bytesWritten += strlen($data);
75 1 : }
76 :
77 : }
78 :
|