Skip to content

Instantly share code, notes, and snippets.

@sasezaki
Created October 30, 2013 16:01
Show Gist options
  • Save sasezaki/7235215 to your computer and use it in GitHub Desktop.
Save sasezaki/7235215 to your computer and use it in GitHub Desktop.
zf2-5384
diff --git a/library/Zend/Code/Scanner/PropertyScanner.php b/library/Zend/Code/Scanner/PropertyScanner.php
index 0ba37f6..0d7a49f 100644
--- a/library/Zend/Code/Scanner/PropertyScanner.php
+++ b/library/Zend/Code/Scanner/PropertyScanner.php
@@ -226,6 +226,7 @@ class PropertyScanner implements ScannerInterface
SCANNER_TOP:
$token = current($tokens);
+ $index = key($tokens);
if (!is_string($token)) {
list($tokenType, $tokenContent, $tokenLine) = $token;
@@ -261,10 +262,19 @@ class PropertyScanner implements ScannerInterface
default:
if ($this->name !== null && trim($tokenContent) !== '') {
+ if (!$this->value) {
+ //var_dump(array_slice($tokens, $index, null));
+ $this->value = new PropertyValue(array_slice($tokens, $index));
+ //$this->value = new PropertyValue();
+ goto SCANNER_END;
+ }
+ $this->value->add($token);
+
+ /**
$this->value .= (is_string($token)) ? $token : $tokenContent;
if (substr($this->value, 0, 1) === '"' || substr($this->value, 0, 1) === "'") {
$this->value = substr($this->value, 1, -1); // Remove quotes
- }
+ }*/
}
goto SCANNER_CONTINUE;
}
@@ -282,3 +292,26 @@ class PropertyScanner implements ScannerInterface
$this->isScanned = true;
}
}
+
+class PropertyValue
+{
+ private $tokens = array();
+ public function __construct($tokens)
+ {
+ $this->tokens = $tokens;
+ }
+
+ /**
+ public function add($token)
+ {
+ $this->tokens[] = $token;
+ }*/
+
+ /**
+ * Evaluate token value
+ */
+ public function export()
+ {
+ return $this->tokens;
+ }
+}
<?php
use Zend\Loader\StandardAutoloader;
use Zend\Code\Scanner\DirectoryScanner;
require 'library/Zend/Loader/StandardAutoloader.php';
$loader = new StandardAutoloader(array('autoregister_zf' => true));
$loader->register();
$class = <<<'CLASS'
<?php
class Foo
{
private $array = array(1, "2", array(3), 5);
}
CLASS;
$tokenScanner = new Zend\Code\Scanner\TokenArrayScanner(token_get_all($class));
$fooClass = $tokenScanner->getClass('Foo');
foreach ($fooClass->getProperties() as $property) {
var_export($property->getValue()->export());
}
<?php
/**
$tokens = array (
0 =>
array (
0 => 364,
1 => 'array',
2 => 6,
),
1 =>
array (
0 => 306,
1 => '1',
2 => 6,
),
2 =>
array (
0 => 306,
1 => '2',
2 => 6,
),
);
$tokens2 = array (
0 =>
array (
0 => 364,
1 => 'array',
2 => 4,
),
1 =>
array (
0 => 306,
1 => '1',
2 => 4,
),
2 =>
array (
0 => 306,
1 => '2',
2 => 4,
),
3 =>
array (
0 => 364,
1 => 'array',
2 => 4,
),
4 =>
array (
0 => 306,
1 => '3',
2 => 4,
),
5 =>
array (
0 => 306,
1 => '4',
2 => 4,
),
);
$tokens3 =
array (
0 =>
array (
0 => 364,
1 => 'array',
2 => 4,
),
1 =>
array (
0 => 306,
1 => '1',
2 => 4,
),
2 =>
array (
0 => 316,
1 => '"2"',
2 => 4,
),
3 =>
array (
0 => 364,
1 => 'array',
2 => 4,
),
4 =>
array (
0 => 306,
1 => '3',
2 => 4,
),
5 =>
array (
0 => 306,
1 => '5',
2 => 4,
),
);
*/
class PropertyValue {
private $tokens_count;
private $index = 0;
public function __construct($tokens)
{
$this->tokens = $tokens;
$this->tokens_count = count($tokens);
}
function token_value($tokens) {
/**
$values = array();
foreach ($tokens as $i => $token) {
if ($token[0] == T_ARRAY) {
$values[$this->index++] = $this->token_value(array_slice($tokens, $this->index, $this->tokens_count - $this->index, true));
++$this->index;
} elseif (T_NUM_STRING) {
$values[] = $token[1];
++$this->index;
}
}
return $values;
*/
$values = array();
do {
$index = key($this->tokens);
if (current($tokens)[0] === T_ARRAY) {
$values[] = $this->token_value(array_slice($tokens, $index + 1, $this->tokens_count - $index, true));
} else {
$ret = current($tokens);
if ($ret) $values[] = $ret;
next($tokens);
next($this->tokens);
}
} while ($index < $this->tokens_count && current($this->tokens));
return $values;
/**
do {
if ($tokens[$this->index][0] == T_ARRAY) {
++$this->index;
$array_values[] = $this->token_value(array_slice($tokens, $this->index, $this->tokens_count - $this->index, true));
return $array_values;
} else {
var_dump($tokens);
$token = current($tokens);
//++$this->index;
return $token;
}
} while ($this->index < $this->tokens_count);
*/
}
}
$tokens = include 'toknes.php';
$pv = new PropertyValue($tokens);
var_export($pv->token_value($tokens));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment