Annotation.php 9.0 KB

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