I have implemented code for parsing annotation:
/**
* @Route(path="sample \n test",code,value,boolean,test)
* @access(code=false)
* @sample as asdad asd
* asd
*/
function sample()
{
}
$refelection = new ReflectionFunction("sample");
$pattern = "/@(\w+?)(.*?)/U";
preg_match_all($pattern, $refelection->getDocComment(), $matches);
$matches = array_combine($matches[1], $matches[2]);
foreach ($matches as $key => $value)
{
$params = array();
$token = token_get_all("<?php " . trim($value) . "?>");
if (substr($value, 0, 1) !== "(" || substr($value, -2, -1) !== ")")
{
continue;
}
echo $key;
$limit = count($token) - 2;
for ($i = 2; $i < $limit; $i++)
{
if (array_key_exists($i + 1, $token) && $token[$i + 1] == "=")
{
if (!is_array($token[$i + 2]))
{
die("invalid");
}
$params[$token[$i][1]] = $token[$i + 2][1];
$i+=3;
}
else
{
if (!is_array($token[$i + 2]))
{
die("invalid");
}
$params[$token[$i][1]] = NULL;
$i+=1;
}
if ($token[$i] !== "," && $token[$i] !== ")")
{
die("invalid");
}
}
var_dump($params);
}
Please tell me if there are any cons, limitation or bug in this code or any alternatives.
Last annotation in doc comment is ignore, as you can see.