Annotation.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. <?php
  2. namespace Gedmo\Tree\Mapping\Driver;
  3. use Gedmo\Mapping\Driver\AnnotationDriverInterface,
  4. Gedmo\Exception\InvalidMappingException,
  5. Gedmo\Tree\Mapping\Validator;
  6. /**
  7. * This is an annotation mapping driver for Tree
  8. * behavioral extension. Used for extraction of extended
  9. * metadata from Annotations specificaly for Tree
  10. * extension.
  11. *
  12. * @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
  13. * @package Gedmo.Tree.Mapping.Driver
  14. * @subpackage Annotation
  15. * @link http://www.gediminasm.org
  16. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  17. */
  18. class Annotation implements AnnotationDriverInterface
  19. {
  20. /**
  21. * Annotation to define the tree type
  22. */
  23. const TREE = 'Gedmo\\Mapping\\Annotation\\Tree';
  24. /**
  25. * Annotation to mark field as one which will store left value
  26. */
  27. const LEFT = 'Gedmo\\Mapping\\Annotation\\TreeLeft';
  28. /**
  29. * Annotation to mark field as one which will store right value
  30. */
  31. const RIGHT = 'Gedmo\\Mapping\\Annotation\\TreeRight';
  32. /**
  33. * Annotation to mark relative parent field
  34. */
  35. const PARENT = 'Gedmo\\Mapping\\Annotation\\TreeParent';
  36. /**
  37. * Annotation to mark node level
  38. */
  39. const LEVEL = 'Gedmo\\Mapping\\Annotation\\TreeLevel';
  40. /**
  41. * Annotation to mark field as tree root
  42. */
  43. const ROOT = 'Gedmo\\Mapping\\Annotation\\TreeRoot';
  44. /**
  45. * Annotation to specify closure tree class
  46. */
  47. const CLOSURE = 'Gedmo\\Mapping\\Annotation\\TreeClosure';
  48. /**
  49. * Annotation to specify path class
  50. */
  51. const PATH = 'Gedmo\\Mapping\\Annotation\\TreePath';
  52. /**
  53. * Annotation to specify path source class
  54. */
  55. const PATH_SOURCE = 'Gedmo\\Mapping\\Annotation\\TreePathSource';
  56. /**
  57. * Annotation to mark the field to be used to hold the lock time
  58. */
  59. const LOCK_TIME = 'Gedmo\\Mapping\\Annotation\\TreeLockTime';
  60. /**
  61. * List of tree strategies available
  62. *
  63. * @var array
  64. */
  65. private $strategies = array(
  66. 'nested',
  67. 'closure',
  68. 'materializedPath'
  69. );
  70. /**
  71. * Annotation reader instance
  72. *
  73. * @var object
  74. */
  75. private $reader;
  76. /**
  77. * original driver if it is available
  78. */
  79. protected $_originalDriver = null;
  80. /**
  81. * {@inheritDoc}
  82. */
  83. public function setAnnotationReader($reader)
  84. {
  85. $this->reader = $reader;
  86. }
  87. /**
  88. * {@inheritDoc}
  89. */
  90. public function readExtendedMetadata($meta, array &$config)
  91. {
  92. $class = $meta->getReflectionClass();
  93. $validator = new Validator();
  94. if (!$class) {
  95. // based on recent doctrine 2.3.0-DEV maybe will be fixed in some way
  96. // this happens when running annotation driver in combination with
  97. // static reflection services. This is not the nicest fix
  98. $class = new \ReflectionClass($meta->name);
  99. }
  100. // class annotations
  101. if ($annot = $this->reader->getClassAnnotation($class, self::TREE)) {
  102. if (!in_array($annot->type, $this->strategies)) {
  103. throw new InvalidMappingException("Tree type: {$annot->type} is not available.");
  104. }
  105. $config['strategy'] = $annot->type;
  106. $config['activate_locking'] = $annot->activateLocking;
  107. $config['locking_timeout'] = (int) $annot->lockingTimeout;
  108. if ($config['locking_timeout'] < 1) {
  109. throw new InvalidMappingException("Tree Locking Timeout must be at least of 1 second.");
  110. }
  111. }
  112. if ($annot = $this->reader->getClassAnnotation($class, self::CLOSURE)) {
  113. if (!class_exists($annot->class)) {
  114. throw new InvalidMappingException("Tree closure class: {$annot->class} does not exist.");
  115. }
  116. $config['closure'] = $annot->class;
  117. }
  118. // property annotations
  119. foreach ($class->getProperties() as $property) {
  120. if ($meta->isMappedSuperclass && !$property->isPrivate() ||
  121. $meta->isInheritedField($property->name) ||
  122. isset($meta->associationMappings[$property->name]['inherited'])
  123. ) {
  124. continue;
  125. }
  126. // left
  127. if ($this->reader->getPropertyAnnotation($property, self::LEFT)) {
  128. $field = $property->getName();
  129. if (!$meta->hasField($field)) {
  130. throw new InvalidMappingException("Unable to find 'left' - [{$field}] as mapped property in entity - {$meta->name}");
  131. }
  132. if (!$validator->isValidField($meta, $field)) {
  133. throw new InvalidMappingException("Tree left field - [{$field}] type is not valid and must be 'integer' in class - {$meta->name}");
  134. }
  135. $config['left'] = $field;
  136. }
  137. // right
  138. if ($this->reader->getPropertyAnnotation($property, self::RIGHT)) {
  139. $field = $property->getName();
  140. if (!$meta->hasField($field)) {
  141. throw new InvalidMappingException("Unable to find 'right' - [{$field}] as mapped property in entity - {$meta->name}");
  142. }
  143. if (!$validator->isValidField($meta, $field)) {
  144. throw new InvalidMappingException("Tree right field - [{$field}] type is not valid and must be 'integer' in class - {$meta->name}");
  145. }
  146. $config['right'] = $field;
  147. }
  148. // ancestor/parent
  149. if ($this->reader->getPropertyAnnotation($property, self::PARENT)) {
  150. $field = $property->getName();
  151. if (!$meta->isSingleValuedAssociation($field)) {
  152. throw new InvalidMappingException("Unable to find ancestor/parent child relation through ancestor field - [{$field}] in class - {$meta->name}");
  153. }
  154. $config['parent'] = $field;
  155. }
  156. // root
  157. if ($this->reader->getPropertyAnnotation($property, self::ROOT)) {
  158. $field = $property->getName();
  159. if (!$meta->hasField($field)) {
  160. throw new InvalidMappingException("Unable to find 'root' - [{$field}] as mapped property in entity - {$meta->name}");
  161. }
  162. if (!$validator->isValidFieldForRoot($meta, $field)) {
  163. throw new InvalidMappingException("Tree root field - [{$field}] type is not valid and must be any of the 'integer' types or 'string' in class - {$meta->name}");
  164. }
  165. $config['root'] = $field;
  166. }
  167. // level
  168. if ($this->reader->getPropertyAnnotation($property, self::LEVEL)) {
  169. $field = $property->getName();
  170. if (!$meta->hasField($field)) {
  171. throw new InvalidMappingException("Unable to find 'level' - [{$field}] as mapped property in entity - {$meta->name}");
  172. }
  173. if (!$validator->isValidField($meta, $field)) {
  174. throw new InvalidMappingException("Tree level field - [{$field}] type is not valid and must be 'integer' in class - {$meta->name}");
  175. }
  176. $config['level'] = $field;
  177. }
  178. // path
  179. if ($pathAnnotation = $this->reader->getPropertyAnnotation($property, self::PATH)) {
  180. $field = $property->getName();
  181. if (!$meta->hasField($field)) {
  182. throw new InvalidMappingException("Unable to find 'path' - [{$field}] as mapped property in entity - {$meta->name}");
  183. }
  184. if (!$validator->isValidFieldForPath($meta, $field)) {
  185. throw new InvalidMappingException("Tree Path field - [{$field}] type is not valid. It must be string or text in class - {$meta->name}");
  186. }
  187. if (strlen($pathAnnotation->separator) > 1) {
  188. throw new InvalidMappingException("Tree Path field - [{$field}] Separator {$pathAnnotation->separator} is invalid. It must be only one character long.");
  189. }
  190. $config['path'] = $field;
  191. $config['path_separator'] = $pathAnnotation->separator;
  192. }
  193. // path source
  194. if ($this->reader->getPropertyAnnotation($property, self::PATH_SOURCE)) {
  195. $field = $property->getName();
  196. if (!$meta->hasField($field)) {
  197. throw new InvalidMappingException("Unable to find 'path_source' - [{$field}] as mapped property in entity - {$meta->name}");
  198. }
  199. if (!$validator->isValidFieldForPathSource($meta, $field)) {
  200. throw new InvalidMappingException("Tree PathSource field - [{$field}] type is not valid. It can be any of the integer variants, double, float or string in class - {$meta->name}");
  201. }
  202. $config['path_source'] = $field;
  203. }
  204. // lock time
  205. if ($this->reader->getPropertyAnnotation($property, self::LOCK_TIME)) {
  206. $field = $property->getName();
  207. if (!$meta->hasField($field)) {
  208. throw new InvalidMappingException("Unable to find 'lock_time' - [{$field}] as mapped property in entity - {$meta->name}");
  209. }
  210. if (!$validator->isValidFieldForLockTime($meta, $field)) {
  211. throw new InvalidMappingException("Tree PathSource field - [{$field}] type is not valid. It must be \"date\" in class - {$meta->name}");
  212. }
  213. $config['lock_time'] = $field;
  214. }
  215. }
  216. if (isset($config['activate_locking']) && $config['activate_locking'] && !isset($config['lock_time'])) {
  217. throw new InvalidMappingException("You need to map a date field as the tree lock time field to activate locking support.");
  218. }
  219. if (!$meta->isMappedSuperclass && $config) {
  220. if (isset($config['strategy'])) {
  221. if (is_array($meta->identifier) && count($meta->identifier) > 1) {
  222. throw new InvalidMappingException("Tree does not support composite identifiers in class - {$meta->name}");
  223. }
  224. $method = 'validate' . ucfirst($config['strategy']) . 'TreeMetadata';
  225. $validator->$method($meta, $config);
  226. } else {
  227. throw new InvalidMappingException("Cannot find Tree type for class: {$meta->name}");
  228. }
  229. }
  230. }
  231. /**
  232. * Passes in the mapping read by original driver
  233. *
  234. * @param $driver
  235. * @return void
  236. */
  237. public function setOriginalDriver($driver)
  238. {
  239. $this->_originalDriver = $driver;
  240. }
  241. }