1 : <?php
2 :
3 : /**
4 : * \Midi\Reporting\HtmlPostProcessor
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 \SplFileObject;
15 :
16 : /**
17 : * Handles post processing for HTML formatters
18 : *
19 : * @package Midi
20 : * @subpackage Reporting
21 : * @since 1.0
22 : */
23 1 : class HtmlPostProcessor implements PostProcessor {
24 :
25 : /**
26 : * @var array
27 : */
28 : private $params;
29 :
30 : /**
31 : * Constructor
32 : *
33 : * @since 1.0
34 : */
35 : public function __construct() {
36 4 : $this->params = array(
37 4 : 'target' => null,
38 4 : 'items' => 0,
39 : 'multi_file' => false
40 4 : );
41 4 : }
42 :
43 : /**
44 : * @since 1.0
45 : *
46 : * @param string $key
47 : * @param string $value
48 : */
49 : public function setParameter($key, $value) {
50 1 : if (array_key_exists($key, $this->params)) {
51 1 : $this->params[$key] = $value;
52 1 : }
53 1 : }
54 :
55 : /**
56 : * Opens and returns a file object suitable for writing
57 : *
58 : * @since 1.0
59 : *
60 : * @param string $file Path to the file to open
61 : * @return SplFileObject
62 : */
63 : public function createFileObject($file) {
64 1 : return new SplFileObject($file, 'w');
65 : }
66 :
67 : /**
68 : * @since 1.0
69 : * @uses createFileObject()
70 : * @uses copyFile()
71 : */
72 : public function execute() {
73 1 : $target = $this->params['target'];
74 1 : $numItems = $this->params['items'];
75 1 : $isMultiFile = (bool)$this->params['multi_file'];
76 :
77 1 : $dir = dirname(__FILE__) . '/_html';
78 :
79 1 : $javascript = $dir . ($isMultiFile ? '/midiparser-multi.js' : '/midiparser.js');
80 1 : $data = preg_replace('/\{numItems\}/', $numItems, file_get_contents($javascript));
81 :
82 1 : $this->createFileObject($target . '/midiparser.js')->fwrite($data, strlen($data));
83 1 : unset($data);
84 :
85 : $files = array(
86 1 : '/midiparser.css',
87 1 : '/row.png',
88 1 : '/arrow_left.png',
89 1 : '/arrow_right.png',
90 : '/loading.gif'
91 1 : );
92 :
93 1 : foreach ($files as $file) {
94 1 : $this->copyFile($dir . $file, $target . $file);
95 1 : }
96 1 : }
97 :
98 : //@codeCoverageIgnoreStart
99 : /**
100 : * Copies a file
101 : *
102 : * @since 1.0
103 : *
104 : * @param string $source
105 : * @param string $target
106 : * @ignore
107 : */
108 : protected function copyFile($source, $target) {
109 0 : copy($source, $target);
110 0 : }
111 : //@codeCoverageIgnoreEnd
112 :
113 : }
114 :
|