PageRenderTime 49ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/Nette/Annotations.php

https://github.com/DocX/nette
PHP | 167 lines | 89 code | 27 blank | 51 comment | 7 complexity | f63337e80f8f96f798dd9d29f1a15d28 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. /**
  3. * Nette Framework
  4. *
  5. * Copyright (c) 2004, 2009 David Grudl (http://davidgrudl.com)
  6. *
  7. * This source file is subject to the "Nette license" that is bundled
  8. * with this package in the file license.txt.
  9. *
  10. * For more information please see http://nettephp.com
  11. *
  12. * @copyright Copyright (c) 2004, 2009 David Grudl
  13. * @license http://nettephp.com/license Nette license
  14. * @link http://nettephp.com
  15. * @category Nette
  16. * @package Nette
  17. */
  18. /*namespace Nette;*/
  19. /**
  20. * Annotations support for PHP.
  21. *
  22. * @author David Grudl
  23. * @copyright Copyright (c) 2004, 2009 David Grudl
  24. * @package Nette
  25. */
  26. final class Annotations
  27. {
  28. /** @var array */
  29. static private $cache = array();
  30. /**
  31. * Static class - cannot be instantiated.
  32. */
  33. final public function __construct()
  34. {
  35. throw new /*\*/LogicException("Cannot instantiate static class " . get_class($this));
  36. }
  37. /**
  38. * Has class/method/property specified annotation?
  39. * @param \ReflectionClass|\ReflectionMethod|\ReflectionProperty
  40. * @param string annotation name
  41. * @return bool
  42. */
  43. public static function has(/*\*/Reflector $r, $name)
  44. {
  45. $cache = & self::init($r);
  46. return !empty($cache[$name]);
  47. }
  48. /**
  49. * Returns an annotation value.
  50. * @param \ReflectionClass|\ReflectionMethod|\ReflectionProperty
  51. * @param string annotation name
  52. * @return array
  53. */
  54. public static function get(/*\*/Reflector $r, $name)
  55. {
  56. $cache = & self::init($r);
  57. return isset($cache[$name]) ? end($cache[$name]) : NULL;
  58. }
  59. /**
  60. * Returns all annotations.
  61. * @param \ReflectionClass|\ReflectionMethod|\ReflectionProperty
  62. * @param string annotation name
  63. * @return array
  64. */
  65. public static function getAll(/*\*/Reflector $r, $name = NULL)
  66. {
  67. $cache = & self::init($r);
  68. if ($name === NULL) {
  69. return $cache;
  70. } elseif (isset($cache[$name])) {
  71. return $cache[$name];
  72. } else {
  73. return array();
  74. }
  75. }
  76. /**
  77. * Parses and caches annotations.
  78. * @param \ReflectionClass|\ReflectionMethod|\ReflectionProperty
  79. * @return array
  80. */
  81. public static function & init(/*\*/Reflector $r)
  82. {
  83. $cache = & self::$cache[$r->getName()];
  84. if ($r instanceof /*\*/ReflectionClass) {
  85. $cache = & $cache[''];
  86. } elseif ($r instanceof /*\*/ReflectionMethod) {
  87. $cache = & $cache[$r->getDeclaringClass()->getName()];
  88. } else {
  89. $cache = & $cache['$' . $r->getDeclaringClass()->getName()];
  90. }
  91. if ($cache !== NULL) {
  92. return $cache;
  93. }
  94. preg_match_all('#@([a-zA-Z0-9_]+)(?:\(((?>[^\'")]+|\'[^\']*\'|"[^"]*")*)\))?#', $r->getDocComment(), $matches, PREG_SET_ORDER);
  95. $cache = array();
  96. foreach ($matches as $match)
  97. {
  98. if (isset($match[2])) {
  99. preg_match_all('#[,\s](?>([a-zA-Z0-9_]+)\s*=\s*)?([^\'",\s][^,]*|\'[^\']*\'|"[^"]*")#', ',' . $match[2], $matches, PREG_SET_ORDER);
  100. $items = array();
  101. $key = '';
  102. $val = TRUE;
  103. foreach ($matches as $m) {
  104. list(, $key, $val) = $m;
  105. if ($val[0] === "'" || $val[0] === '"') {
  106. $val = substr($val, 1, -1);
  107. } elseif (strcasecmp($val, 'true') === 0) {
  108. $val = TRUE;
  109. } elseif (strcasecmp($val, 'false') === 0) {
  110. $val = FALSE;
  111. } elseif (strcasecmp($val, 'null') === 0) {
  112. $val = NULL;
  113. } elseif (is_numeric($val)) {
  114. $val = 1 * $val;
  115. }
  116. if ($key === '') {
  117. $items[] = $val;
  118. } else {
  119. $items[$key] = $val;
  120. }
  121. }
  122. $items = count($items) < 2 && $key === '' ? $val : new /*\*/ArrayObject($items, /*\*/ArrayObject::ARRAY_AS_PROPS);
  123. } else {
  124. $items = TRUE;
  125. }
  126. $cache[$match[1]][] = $items;
  127. }
  128. return $cache;
  129. }
  130. }