22
22
use Doctrine \Common \Annotations \AnnotationReader ,
23
23
Doctrine \Common \Annotations \AnnotationRegistry ,
24
24
Doctrine \Common \Annotations \Reader ,
25
+ Doctrine \Common \Persistence \Mapping \ClassMetadata ,
26
+ Doctrine \Common \Persistence \Mapping \Driver \AnnotationDriver as AbstractAnnotationDriver ,
25
27
Doctrine \ODM \MongoDB \Events ,
26
28
Doctrine \ODM \MongoDB \Mapping \Annotations as ODM ,
27
29
Doctrine \ODM \MongoDB \Mapping \ClassMetadataInfo ,
36
38
* @author Jonathan H. Wage <jonwage@gmail.com>
37
39
* @author Roman Borschel <roman@code-factory.org>
38
40
*/
39
- class AnnotationDriver implements Driver
41
+ class AnnotationDriver extends AbstractAnnotationDriver
40
42
{
41
- /**
42
- * Document annotation classes, ordered by precedence.
43
- */
44
- static private $ documentAnnotationClasses = array (
45
- 'Doctrine \\ODM \\MongoDB \\Mapping \\Annotations \\Document ' ,
46
- 'Doctrine \\ODM \\MongoDB \\Mapping \\Annotations \\MappedSuperclass ' ,
47
- 'Doctrine \\ODM \\MongoDB \\Mapping \\Annotations \\EmbeddedDocument ' ,
43
+ protected $ entityAnnotationClasses = array (
44
+ 'Doctrine \\ODM \\MongoDB \\Mapping \\Annotations \\Document ' => 1 ,
45
+ 'Doctrine \\ODM \\MongoDB \\Mapping \\Annotations \\MappedSuperclass ' => 2 ,
46
+ 'Doctrine \\ODM \\MongoDB \\Mapping \\Annotations \\EmbeddedDocument ' => 3 ,
48
47
);
49
48
50
- /**
51
- * The annotation reader.
52
- *
53
- * @var Reader
54
- */
55
- private $ reader ;
56
-
57
- /**
58
- * The paths where to look for mapping files.
59
- *
60
- * @var array
61
- */
62
- private $ paths = array ();
63
-
64
- /**
65
- * The file extension of mapping documents.
66
- *
67
- * @var string
68
- */
69
- private $ fileExtension = '.php ' ;
70
-
71
- /**
72
- * @param array
73
- */
74
- private $ classNames ;
75
-
76
49
/**
77
50
* Registers annotation classes to the common registry.
78
51
*
@@ -83,56 +56,12 @@ public static function registerAnnotationClasses()
83
56
AnnotationRegistry::registerFile (__DIR__ . '/../Annotations/DoctrineAnnotations.php ' );
84
57
}
85
58
86
- /**
87
- * Initializes a new AnnotationDriver that uses the given Reader for reading
88
- * docblock annotations.
89
- *
90
- * @param $reader Reader The annotation reader to use.
91
- * @param string|array $paths One or multiple paths where mapping classes can be found.
92
- */
93
- public function __construct (Reader $ reader , $ paths = null )
94
- {
95
- $ this ->reader = $ reader ;
96
- if ($ paths ) {
97
- $ this ->addPaths ((array ) $ paths );
98
- }
99
- }
100
-
101
- /**
102
- * Append lookup paths to metadata driver.
103
- *
104
- * @param array $paths
105
- */
106
- public function addPaths (array $ paths )
107
- {
108
- $ this ->paths = array_unique (array_merge ($ this ->paths , $ paths ));
109
- }
110
-
111
- /**
112
- * Retrieve the defined metadata lookup paths.
113
- *
114
- * @return array
115
- */
116
- public function getPaths ()
117
- {
118
- return $ this ->paths ;
119
- }
120
-
121
- /**
122
- * Retrieve the current annotation reader
123
- *
124
- * @return AnnotationReader
125
- */
126
- public function getReader ()
127
- {
128
- return $ this ->reader ;
129
- }
130
-
131
59
/**
132
60
* {@inheritdoc}
133
61
*/
134
- public function loadMetadataForClass ($ className , ClassMetadataInfo $ class )
62
+ public function loadMetadataForClass ($ className , ClassMetadata $ class )
135
63
{
64
+ /** @var $class ClassMetadataInfo */
136
65
$ reflClass = $ class ->getReflectionClass ();
137
66
138
67
$ documentAnnots = array ();
@@ -288,80 +217,6 @@ private function addIndex(ClassMetadataInfo $class, $index, array $keys = array(
288
217
$ class ->addIndex ($ keys , $ options );
289
218
}
290
219
291
- /**
292
- * Whether the class with the specified name is transient. Only non-transient
293
- * classes, that is entities and mapped superclasses, should have their metadata loaded.
294
- * A class is non-transient if it is annotated with either (at)Entity or
295
- * (at)MappedSuperclass in the class doc block.
296
- *
297
- * @param string $className
298
- * @return boolean
299
- */
300
- public function isTransient ($ className )
301
- {
302
- $ classAnnotations = $ this ->reader ->getClassAnnotations (new \ReflectionClass ($ className ));
303
-
304
- foreach ($ classAnnotations as $ annot ) {
305
- if ($ annot instanceof ODM \AbstractDocument) {
306
- return false ;
307
- }
308
- }
309
-
310
- return true ;
311
- }
312
-
313
- /**
314
- * {@inheritDoc}
315
- */
316
- public function getAllClassNames ()
317
- {
318
- if ($ this ->classNames !== null ) {
319
- return $ this ->classNames ;
320
- }
321
-
322
- if ( ! $ this ->paths ) {
323
- throw MongoDBException::pathRequired ();
324
- }
325
-
326
- $ classes = array ();
327
- $ includedFiles = array ();
328
-
329
- foreach ($ this ->paths as $ path ) {
330
- if ( ! is_dir ($ path )) {
331
- throw MongoDBException::fileMappingDriversRequireConfiguredDirectoryPath ();
332
- }
333
-
334
- $ iterator = new \RecursiveIteratorIterator (
335
- new \RecursiveDirectoryIterator ($ path ),
336
- \RecursiveIteratorIterator::LEAVES_ONLY
337
- );
338
-
339
- foreach ($ iterator as $ file ) {
340
- if (($ fileName = $ file ->getBasename ($ this ->fileExtension )) == $ file ->getBasename ()) {
341
- continue ;
342
- }
343
-
344
- $ sourceFile = realpath ($ file ->getPathName ());
345
- require_once $ sourceFile ;
346
- $ includedFiles [] = $ sourceFile ;
347
- }
348
- }
349
-
350
- $ declared = get_declared_classes ();
351
-
352
- foreach ($ declared as $ className ) {
353
- $ rc = new \ReflectionClass ($ className );
354
- $ sourceFile = $ rc ->getFileName ();
355
- if (in_array ($ sourceFile , $ includedFiles ) && ! $ this ->isTransient ($ className )) {
356
- $ classes [] = $ className ;
357
- }
358
- }
359
-
360
- $ this ->classNames = $ classes ;
361
-
362
- return $ classes ;
363
- }
364
-
365
220
/**
366
221
* Factory method for the Annotation Driver
367
222
*
0 commit comments