1<?php
2
3/**
4 Honestly: this is evil code to come by a problem in the cssmgr.php class of mpdf that breaks generating PDFs with large CSS files
5 It comes from: http://stackoverflow.com/questions/137006/redefine-class-methods-or-class
6*/
7class CSSMgrPatch {
8
9    private $_code;
10
11    public function __construct($include_file = null) {
12        if ($include_file) {
13            $this->includeCode($include_file);
14        }
15    }
16
17    public function setCode($code) {
18        $this->_code = $code;
19    }
20
21    public function includeCode($path) {
22
23        $fp = fopen($path, 'r');
24        if ( !$fp ) { return; }
25        $contents = fread($fp, filesize($path));
26        $contents = str_replace('<?php', '', $contents);
27        $contents = str_replace('?>', '', $contents);
28        fclose($fp);
29
30        $this->setCode($contents);
31    }
32
33    public function redefineFunction($new_function) {
34
35        preg_match('/function (.+)\(/', $new_function, $aryMatches);
36        $func_name = trim($aryMatches[1]);
37
38        if (preg_match('/(function ' . $func_name . '[\w\W\n]+?)(function)/s', $this->_code, $aryMatches)) {
39
40            $search_code = $aryMatches[1];
41
42            $new_code = str_replace($search_code, $new_function . "\n\n", $this->_code);
43            $new_code = str_replace("function cssmgr", "function __construct", $new_code);
44
45            $this->setCode($new_code);
46
47            return true;
48
49        } else {
50
51            return false;
52
53        }
54    }
55
56    public function getCode() {
57        return $this->_code;
58    }
59}
60