Skip to content

Commit 58c785e

Browse files
committed
added Nette PhpGenerator
0 parents  commit 58c785e

13 files changed

+798
-0
lines changed

src/PhpGenerator/ClassType.php

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
<?php
2+
3+
/**
4+
* This file is part of the Nette Framework (http://nette.org)
5+
*
6+
* Copyright (c) 2004, 2011 David Grudl (http://davidgrudl.com)
7+
*
8+
* For the full copyright and license information, please view
9+
* the file license.txt that was distributed with this source code.
10+
*/
11+
12+
namespace Nette\Utils\PhpGenerator;
13+
14+
use Nette;
15+
16+
17+
18+
/**
19+
* Class/Interface/Trait description.
20+
*
21+
* @author David Grudl
22+
*
23+
* @method ClassType setName(string $name)
24+
* @method ClassType setType(string $type)
25+
* @method ClassType setFinal(bool $on)
26+
* @method ClassType setAbstract(bool $on)
27+
* @method ClassType addExtend(string $class)
28+
* @method ClassType addImplement(string $interface)
29+
* @method ClassType addTrait(string $trait)
30+
* @method ClassType addDocument(string $doc)
31+
*/
32+
class ClassType extends Nette\Object
33+
{
34+
/** @var string */
35+
public $name;
36+
37+
/** @var string class|interface|trait */
38+
public $type = 'class';
39+
40+
/** @var bool */
41+
public $final;
42+
43+
/** @var bool */
44+
public $abstract;
45+
46+
/** @var array of string */
47+
public $extends = array();
48+
49+
/** @var array of string */
50+
public $implements = array();
51+
52+
/** @var array of string */
53+
public $traits = array();
54+
55+
/** @var array of string */
56+
public $documents = array();
57+
58+
/** @var array of name => value */
59+
public $consts = array();
60+
61+
/** @var array of name => Property */
62+
public $properties = array();
63+
64+
/** @var array of name => Method */
65+
public $methods = array();
66+
67+
68+
public function __construct($name)
69+
{
70+
$this->name = $name;
71+
}
72+
73+
74+
75+
/** @return ClassType */
76+
public function addConst($name, $value)
77+
{
78+
$this->consts[$name] = $value;
79+
return $this;
80+
}
81+
82+
83+
84+
/** @return Property */
85+
public function addProperty($name, $value = NULL)
86+
{
87+
$property = new Property;
88+
return $this->properties[$name] = $property->setName($name)->setValue($value);
89+
}
90+
91+
92+
93+
/** @return Method */
94+
public function addMethod($name)
95+
{
96+
$method = new Method;
97+
if ($this->type === 'interface') {
98+
$method->setVisibility('')->setBody(FALSE);
99+
} else {
100+
$method->setVisibility('public');
101+
}
102+
return $this->methods[$name] = $method->setName($name);
103+
}
104+
105+
106+
107+
public function __call($name, $args)
108+
{
109+
return Nette\ObjectMixin::callProperty($this, $name, $args);
110+
}
111+
112+
113+
114+
/** @return string PHP code */
115+
public function __toString()
116+
{
117+
$consts = array();
118+
foreach ($this->consts as $name => $value) {
119+
$consts[] = "const $name = " . Helpers::dump($value) . ";\n";
120+
}
121+
$properties = array();
122+
foreach ($this->properties as $property) {
123+
$properties[] = ($property->documents ? str_replace("\n", "\n * ", "/**\n" . implode("\n", (array) $property->documents)) . "\n */\n" : '')
124+
. $property->visibility . ' $' . $property->name
125+
. ($property->value === NULL ? '' : ' = ' . Helpers::dump($property->value))
126+
. ";\n";
127+
}
128+
return Nette\Utils\Strings::normalize(
129+
($this->documents ? str_replace("\n", "\n * ", "/**\n" . implode("\n", (array) $this->documents)) . "\n */\n" : '')
130+
. ($this->abstract ? 'abstract ' : '')
131+
. ($this->final ? 'final ' : '')
132+
. $this->type . ' '
133+
. $this->name . ' '
134+
. ($this->extends ? 'extends ' . implode(', ', (array) $this->extends) . ' ' : '')
135+
. ($this->implements ? 'implements ' . implode(', ', (array) $this->implements) . ' ' : '')
136+
. "\n{\n\n"
137+
. Nette\Utils\Strings::indent(
138+
($this->traits ? "use " . implode(', ', (array) $this->traits) . ";\n\n" : '')
139+
. ($this->consts ? implode('', $consts) . "\n" : '')
140+
. ($this->properties ? implode("\n", $properties) . "\n" : '')
141+
. implode("\n\n\n", $this->methods), 1)
142+
. "\n\n}") . "\n";
143+
}
144+
145+
}

src/PhpGenerator/Helpers.php

Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
<?php
2+
3+
/**
4+
* This file is part of the Nette Framework (http://nette.org)
5+
*
6+
* Copyright (c) 2004, 2011 David Grudl (http://davidgrudl.com)
7+
*
8+
* For the full copyright and license information, please view
9+
* the file license.txt that was distributed with this source code.
10+
*/
11+
12+
namespace Nette\Utils\PhpGenerator;
13+
14+
use Nette;
15+
16+
17+
18+
/**
19+
* PHP code generator utils.
20+
*
21+
* @author David Grudl
22+
*/
23+
class Helpers
24+
{
25+
const PHP_IDENT = '[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*';
26+
27+
28+
/**
29+
* Returns a PHP representation of a variable.
30+
* @return string
31+
*/
32+
public static function dump($var)
33+
{
34+
return self::_dump($var);
35+
}
36+
37+
38+
39+
/**
40+
* Returns a PHP representation of a object member.
41+
* @return string
42+
*/
43+
public static function dumpMember($var)
44+
{
45+
return preg_match('#^' . self::PHP_IDENT . '$#', $var) ? $var : '{' . self::_dump($var) . '}';
46+
}
47+
48+
49+
50+
private static function _dump(&$var, $level = 0)
51+
{
52+
if ($var instanceof PhpLiteral) {
53+
return $var->value;
54+
55+
} elseif (is_float($var)) {
56+
$var = var_export($var, TRUE);
57+
return strpos($var, '.') === FALSE ? $var . '.0' : $var;
58+
59+
} elseif (is_bool($var)) {
60+
return $var ? 'TRUE' : 'FALSE';
61+
62+
} elseif (is_string($var) && (preg_match('#[^\x09\x20-\x7E\xA0-\x{10FFFF}]#u', $var) || preg_last_error())) {
63+
static $table;
64+
if ($table === NULL) {
65+
foreach (range("\x00", "\xFF") as $ch) {
66+
$table[$ch] = ord($ch) < 32 || ord($ch) >= 127
67+
? '\\x' . str_pad(dechex(ord($ch)), 2, '0', STR_PAD_LEFT)
68+
: $ch;
69+
}
70+
$table["\r"] = '\r';
71+
$table["\n"] = '\n';
72+
$table["\t"] = '\t';
73+
$table['$'] = '\\$';
74+
$table['\\'] = '\\\\';
75+
$table['"'] = '\\"';
76+
}
77+
return '"' . strtr($var, $table) . '"';
78+
79+
} elseif (is_array($var)) {
80+
$s = '';
81+
$space = str_repeat("\t", $level);
82+
83+
static $marker;
84+
if ($marker === NULL) {
85+
$marker = uniqid("\x00", TRUE);
86+
}
87+
if (empty($var)) {
88+
89+
} elseif ($level > 50 || isset($var[$marker])) {
90+
throw new Nette\InvalidArgumentException('Nesting level too deep or recursive dependency.');
91+
92+
} else {
93+
$s .= "\n";
94+
$var[$marker] = TRUE;
95+
$counter = 0;
96+
foreach ($var as $k => &$v) {
97+
if ($k !== $marker) {
98+
$s .= "$space\t" . ($k === $counter ? '' : self::_dump($k) . " => ") . self::_dump($v, $level + 1) . ",\n";
99+
$counter = is_int($k) ? $k + 1 : $counter;
100+
}
101+
}
102+
unset($var[$marker]);
103+
$s .= $space;
104+
}
105+
return "array($s)";
106+
107+
} elseif (is_object($var)) {
108+
$arr = (array) $var;
109+
$s = '';
110+
$space = str_repeat("\t", $level);
111+
112+
static $list = array();
113+
if (empty($arr)) {
114+
115+
} elseif ($level > 50 || in_array($var, $list, TRUE)) {
116+
throw new Nette\InvalidArgumentException('Nesting level too deep or recursive dependency.');
117+
118+
} else {
119+
$s .= "\n";
120+
$list[] = $var;
121+
foreach ($arr as $k => &$v) {
122+
if ($k[0] === "\x00") {
123+
$k = substr($k, strrpos($k, "\x00") + 1);
124+
}
125+
$s .= "$space\t" . self::_dump($k) . " => " . self::_dump($v, $level + 1) . ",\n";
126+
}
127+
array_pop($list);
128+
$s .= $space;
129+
}
130+
return get_class($var) === 'stdClass'
131+
? "(object) array($s)"
132+
: get_class($var) . "::__set_state(array($s))";
133+
134+
} else {
135+
return var_export($var, TRUE);
136+
}
137+
}
138+
139+
140+
141+
/** @return string */
142+
public static function generate($statement)
143+
{
144+
$args = func_get_args();
145+
unset($args[0]);
146+
foreach ($args as $key => $arg) {
147+
$args[$key] = self::_dump($arg);
148+
}
149+
if (strpos($statement, '?') === FALSE) {
150+
return $statement .= '(' . implode(', ', $args) . ');';
151+
}
152+
153+
$a = strpos($statement, '?');
154+
$i = 1;
155+
while ($a !== FALSE) {
156+
$statement = substr_replace($statement, $args[$i], $a, 1);
157+
$a = strpos($statement, '?', $a + strlen($args[$i]));
158+
$i++;
159+
}
160+
return $statement . ';';
161+
}
162+
163+
}

0 commit comments

Comments
 (0)