/libraries/rokcommon/RokCommon/Annotation/Addendum.php

https://bitbucket.org/pastor399/newcastleunifc · PHP · 173 lines · 83 code · 11 blank · 79 comment · 16 complexity · 89d475e84de11d757138cc9e98045f10 MD5 · raw file

  1. <?php
  2. /**
  3. * @version $Id: Addendum.php 53534 2012-06-06 18:21:34Z btowles $
  4. * @author RocketTheme http://www.rockettheme.com
  5. * @copyright Copyright (C) 2007 - ${copyright_year} RocketTheme, LLC
  6. * @license http://www.gnu.org/licenses/gpl-2.0.html GNU/GPLv2 only
  7. *
  8. * Based on Addendum
  9. * Original Copyright below
  10. *
  11. * RokCommon_Annotation_Addendum PHP Reflection Annotations
  12. * http://code.google.com/p/addendum/
  13. *
  14. * Copyright (C) 2006-2009 Jan "johno Suchal <johno@jsmf.net>
  15. * This library is free software; you can redistribute it and/or
  16. * modify it under the terms of the GNU Lesser General Public
  17. * License as published by the Free Software Foundation; either
  18. * version 2.1 of the License, or (at your option) any later version.
  19. * This library is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  22. * Lesser General Public License for more details.
  23. * You should have received a copy of the GNU Lesser General Public
  24. * License along with this library; if not, write to the Free Software
  25. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  26. **/
  27. defined('ROKCOMMON') or die('Restricted access');
  28. /**
  29. *
  30. */
  31. class RokCommon_Annotation_Addendum
  32. {
  33. /**
  34. * @var
  35. */
  36. private static $rawMode;
  37. /**
  38. * @var
  39. */
  40. private static $ignore;
  41. /**
  42. * @var array
  43. */
  44. private static $classnames = array();
  45. /**
  46. * @var bool
  47. */
  48. private static $annotations = false;
  49. /**
  50. * @static
  51. *
  52. * @param $reflection
  53. *
  54. * @return bool
  55. */
  56. public static function getDocComment($reflection)
  57. {
  58. if (self::checkRawDocCommentParsingNeeded()) {
  59. $docComment = new RokCommon_Reflection_DocComment();
  60. return $docComment->get($reflection);
  61. } else {
  62. return $reflection->getDocComment();
  63. }
  64. }
  65. /** Raw mode test */
  66. private static function checkRawDocCommentParsingNeeded()
  67. {
  68. if (self::$rawMode === null) {
  69. $reflection = new ReflectionClass('RokCommon_Annotation_Addendum');
  70. $method = $reflection->getMethod('checkRawDocCommentParsingNeeded');
  71. self::setRawMode($method->getDocComment() === false);
  72. }
  73. return self::$rawMode;
  74. }
  75. /**
  76. * @static
  77. *
  78. * @param bool $enabled
  79. */
  80. public static function setRawMode($enabled = true)
  81. {
  82. self::$rawMode = $enabled;
  83. }
  84. /**
  85. * @static
  86. *
  87. */
  88. public static function resetIgnoredAnnotations()
  89. {
  90. self::$ignore = array();
  91. }
  92. /**
  93. * @static
  94. *
  95. * @param $class
  96. *
  97. * @return bool
  98. */
  99. public static function ignores($class)
  100. {
  101. return isset(self::$ignore[$class]);
  102. }
  103. /**
  104. * @static
  105. *
  106. */
  107. public static function ignore()
  108. {
  109. foreach (func_get_args() as $class) {
  110. self::$ignore[$class] = true;
  111. }
  112. }
  113. /**
  114. * @static
  115. *
  116. * @param $class
  117. *
  118. * @return null
  119. */
  120. public static function resolveClassName($class)
  121. {
  122. if (isset(self::$classnames[$class])) return self::$classnames[$class];
  123. $matching = array();
  124. foreach (self::getDeclaredAnnotations() as $declared) {
  125. if ($declared == $class) {
  126. $matching[] = $declared;
  127. } else {
  128. $pos = strrpos($declared, "_$class");
  129. if ($pos !== false && ($pos + strlen($class) == strlen($declared) - 1)) {
  130. $matching[] = $declared;
  131. }
  132. }
  133. }
  134. $result = null;
  135. switch (count($matching)) {
  136. case 0:
  137. $result = $class;
  138. break;
  139. case 1:
  140. $result = $matching[0];
  141. break;
  142. default:
  143. trigger_error("Cannot resolve class name for '$class'. Possible matches: " . join(', ', $matching), E_USER_ERROR);
  144. }
  145. self::$classnames[$class] = $result;
  146. return $result;
  147. }
  148. /**
  149. * @static
  150. * @return bool
  151. */
  152. private static function getDeclaredAnnotations()
  153. {
  154. if (!self::$annotations) {
  155. self::$annotations = array();
  156. foreach (get_declared_classes() as $class) {
  157. if (is_subclass_of($class, 'RokCommon_Annotation') || $class == 'RokCommon_Annotation') self::$annotations[] = $class;
  158. }
  159. }
  160. return self::$annotations;
  161. }
  162. }