1: <?php
2:
3: 4: 5: 6:
7:
8: namespace Nette\PhpGenerator;
9:
10: use Nette;
11:
12:
13: 14: 15: 16: 17:
18: class Helpers
19: {
20: const PHP_IDENT = '[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*';
21: const MAX_DEPTH = 50;
22:
23:
24: 25: 26: 27:
28: public static function dump($var)
29: {
30: return self::_dump($var);
31: }
32:
33:
34: private static function _dump(& $var, $level = 0)
35: {
36: if ($var instanceof PhpLiteral) {
37: return (string) $var;
38:
39: } elseif (is_float($var)) {
40: $var = var_export($var, TRUE);
41: return strpos($var, '.') === FALSE ? $var . '.0' : $var;
42:
43: } elseif (is_bool($var)) {
44: return $var ? 'TRUE' : 'FALSE';
45:
46: } elseif (is_string($var) && (preg_match('#[^\x09\x20-\x7E\xA0-\x{10FFFF}]#u', $var) || preg_last_error())) {
47: static $table;
48: if ($table === NULL) {
49: foreach (array_merge(range("\x00", "\x1F"), range("\x7F", "\xFF")) as $ch) {
50: $table[$ch] = '\x' . str_pad(dechex(ord($ch)), 2, '0', STR_PAD_LEFT);
51: }
52: $table['\\'] = '\\\\';
53: $table["\r"] = '\r';
54: $table["\n"] = '\n';
55: $table["\t"] = '\t';
56: $table['$'] = '\$';
57: $table['"'] = '\"';
58: }
59: return '"' . strtr($var, $table) . '"';
60:
61: } elseif (is_array($var)) {
62: $space = str_repeat("\t", $level);
63:
64: static $marker;
65: if ($marker === NULL) {
66: $marker = uniqid("\x00", TRUE);
67: }
68: if (empty($var)) {
69: $out = '';
70:
71: } elseif ($level > self::MAX_DEPTH || isset($var[$marker])) {
72: throw new Nette\InvalidArgumentException('Nesting level too deep or recursive dependency.');
73:
74: } else {
75: $out = '';
76: $outAlt = "\n$space";
77: $var[$marker] = TRUE;
78: $counter = 0;
79: foreach ($var as $k => & $v) {
80: if ($k !== $marker) {
81: $item = ($k === $counter ? '' : self::_dump($k, $level + 1) . ' => ') . self::_dump($v, $level + 1);
82: $counter = is_int($k) ? max($k + 1, $counter) : $counter;
83: $out .= ($out === '' ? '' : ', ') . $item;
84: $outAlt .= "\t$item,\n$space";
85: }
86: }
87: unset($var[$marker]);
88: }
89: return 'array(' . (strpos($out, "\n") === FALSE && strlen($out) < 40 ? $out : $outAlt) . ')';
90:
91: } elseif ($var instanceof \Serializable) {
92: $var = serialize($var);
93: return 'unserialize(' . self::_dump($var, $level) . ')';
94:
95: } elseif (is_object($var)) {
96: $arr = (array) $var;
97: $space = str_repeat("\t", $level);
98: $class = get_class($var);
99:
100: static $list = array();
101: if ($level > self::MAX_DEPTH || in_array($var, $list, TRUE)) {
102: throw new Nette\InvalidArgumentException('Nesting level too deep or recursive dependency.');
103:
104: } else {
105: $out = "\n";
106: $list[] = $var;
107: if (method_exists($var, '__sleep')) {
108: foreach ($var->__sleep() as $v) {
109: $props[$v] = $props["\x00*\x00$v"] = $props["\x00$class\x00$v"] = TRUE;
110: }
111: }
112: foreach ($arr as $k => & $v) {
113: if (!isset($props) || isset($props[$k])) {
114: $out .= "$space\t" . self::_dump($k, $level + 1) . ' => ' . self::_dump($v, $level + 1) . ",\n";
115: }
116: }
117: array_pop($list);
118: $out .= $space;
119: }
120: return $class === 'stdClass'
121: ? "(object) array($out)"
122: : __CLASS__ . "::createObject('$class', array($out))";
123:
124: } elseif (is_resource($var)) {
125: throw new Nette\InvalidArgumentException('Cannot dump resource.');
126:
127: } else {
128: return var_export($var, TRUE);
129: }
130: }
131:
132:
133: 134: 135: 136:
137: public static function format($statement)
138: {
139: $args = func_get_args();
140: return self::formatArgs(array_shift($args), $args);
141: }
142:
143:
144: 145: 146: 147:
148: public static function formatArgs($statement, array $args)
149: {
150: $a = strpos($statement, '?');
151: while ($a !== FALSE) {
152: if (!$args) {
153: throw new Nette\InvalidArgumentException('Insufficient number of arguments.');
154: }
155: $arg = array_shift($args);
156: if (substr($statement, $a + 1, 1) === '*') {
157: if (!is_array($arg)) {
158: throw new Nette\InvalidArgumentException('Argument must be an array.');
159: }
160: $arg = implode(', ', array_map(array(__CLASS__, 'dump'), $arg));
161: $statement = substr_replace($statement, $arg, $a, 2);
162:
163: } else {
164: $arg = substr($statement, $a - 1, 1) === '$' || in_array(substr($statement, $a - 2, 2), array('->', '::'), TRUE)
165: ? self::formatMember($arg) : self::_dump($arg);
166: $statement = substr_replace($statement, $arg, $a, 1);
167: }
168: $a = strpos($statement, '?', $a + strlen($arg));
169: }
170: return $statement;
171: }
172:
173:
174: 175: 176: 177:
178: public static function formatMember($name)
179: {
180: return $name instanceof PhpLiteral || !self::isIdentifier($name)
181: ? '{' . self::_dump($name) . '}'
182: : $name;
183: }
184:
185:
186: 187: 188:
189: public static function isIdentifier($value)
190: {
191: return is_string($value) && preg_match('#^' . self::PHP_IDENT . '\z#', $value);
192: }
193:
194:
195:
196: public static function createObject($class, array $props)
197: {
198: return unserialize('O' . substr(serialize((string) $class), 1, -1) . substr(serialize($props), 1));
199: }
200:
201: }
202: