4
4
5
5
namespace MongoDB \CodeGenerator ;
6
6
7
- use MongoDB \BSON \Decimal128 ;
8
- use MongoDB \BSON \Document ;
9
- use MongoDB \BSON \Int64 ;
10
- use MongoDB \BSON \PackedArray ;
11
- use MongoDB \BSON \Serializable ;
12
- use MongoDB \BSON \Timestamp ;
13
- use MongoDB \BSON \Type ;
14
- use MongoDB \Builder \Expression \ArrayFieldPath ;
15
- use MongoDB \Builder \Expression \FieldPath ;
16
- use MongoDB \Builder \Expression \ResolvesToArray ;
17
- use MongoDB \Builder \Expression \ResolvesToObject ;
18
7
use MongoDB \Builder \Pipeline ;
19
8
use MongoDB \Builder \Stage ;
20
- use MongoDB \Builder \Type \AccumulatorInterface ;
21
- use MongoDB \Builder \Type \ExpressionInterface ;
22
- use MongoDB \Builder \Type \FieldQueryInterface ;
23
- use MongoDB \Builder \Type \Optional ;
24
- use MongoDB \Builder \Type \QueryInterface ;
25
- use MongoDB \Builder \Type \Sort ;
26
9
use MongoDB \Builder \Type \StageInterface ;
27
10
use MongoDB \CodeGenerator \Definition \GeneratorDefinition ;
28
- use MongoDB \Model \BSONArray ;
29
11
use Nette \PhpGenerator \ClassType ;
30
12
use Nette \PhpGenerator \Method ;
31
13
use Nette \PhpGenerator \Parameter ;
14
+ use Nette \PhpGenerator \PhpFile ;
32
15
use Nette \PhpGenerator \PhpNamespace ;
33
16
use Nette \PhpGenerator \TraitType ;
34
17
use ReflectionClass ;
35
- use stdClass ;
36
18
37
19
use function array_key_last ;
38
20
use function array_map ;
39
21
use function assert ;
22
+ use function file_get_contents ;
40
23
use function implode ;
41
24
use function sprintf ;
42
25
@@ -59,29 +42,10 @@ private function createFluentFactoryClass(GeneratorDefinition $definition): PhpN
59
42
$ namespace = new PhpNamespace ($ definition ->namespace );
60
43
$ class = $ namespace ->addClass ('FluentFactory ' );
61
44
45
+ $ namespace ->addUse (self ::FACTORY_CLASS );
62
46
$ namespace ->addUse (StageInterface::class);
63
- $ namespace ->addUse (FieldQueryInterface::class);
64
- $ namespace ->addUse (Pipeline::class);
65
- $ namespace ->addUse (Decimal128::class);
66
- $ namespace ->addUse (Document::class);
67
- $ namespace ->addUse (Int64::class);
68
- $ namespace ->addUse (PackedArray::class);
69
- $ namespace ->addUse (Serializable::class);
70
- $ namespace ->addUse (Timestamp::class);
71
- $ namespace ->addUse (Type::class);
72
- $ namespace ->addUse (ArrayFieldPath::class);
73
- $ namespace ->addUse (FieldPath::class);
74
- $ namespace ->addUse (ResolvesToArray::class);
75
- $ namespace ->addUse (ResolvesToObject::class);
76
47
$ namespace ->addUse (Pipeline::class);
77
- $ namespace ->addUse (AccumulatorInterface::class);
78
- $ namespace ->addUse (ExpressionInterface::class);
79
- $ namespace ->addUse (Optional::class);
80
- $ namespace ->addUse (QueryInterface::class);
81
- $ namespace ->addUse (Sort::class);
82
- $ namespace ->addUse (BSONArray::class);
83
- $ namespace ->addUse (stdClass::class);
84
- $ namespace ->addUse (self ::FACTORY_CLASS );
48
+
85
49
$ class ->addProperty ('pipeline ' )
86
50
->setType ('array ' )
87
51
->setComment ('@var list<StageInterface> ' )
@@ -92,6 +56,7 @@ private function createFluentFactoryClass(GeneratorDefinition $definition): PhpN
92
56
return new Pipeline(...$this->pipeline);
93
57
PHP);
94
58
59
+ $ this ->addUsesFrom (self ::FACTORY_CLASS , $ namespace );
95
60
$ staticFactory = ClassType::from (self ::FACTORY_CLASS );
96
61
assert ($ staticFactory instanceof ClassType);
97
62
@@ -106,6 +71,7 @@ private function createFluentFactoryClass(GeneratorDefinition $definition): PhpN
106
71
107
72
// Import the other methods provided by the generated trait
108
73
foreach ($ staticFactory ->getTraits () as $ trait ) {
74
+ $ this ->addUsesFrom ($ trait ->getName (), $ namespace );
109
75
$ staticFactory = TraitType::from ($ trait ->getName ());
110
76
assert ($ staticFactory instanceof TraitType);
111
77
foreach ($ staticFactory ->getMethods () as $ method ) {
@@ -156,4 +122,15 @@ private function addMethod(Method $factoryMethod, ClassType $class): void
156
122
implode (', ' , $ args ),
157
123
));
158
124
}
125
+
126
+ private static function addUsesFrom (string $ class , PhpNamespace $ namespace ): void
127
+ {
128
+ $ file = PhpFile::fromCode (file_get_contents ((new ReflectionClass ($ class ))->getFileName ()));
129
+
130
+ foreach ($ file ->getNamespaces () as $ ns ) {
131
+ foreach ($ ns ->getUses () as $ use ) {
132
+ $ namespace ->addUse ($ use );
133
+ }
134
+ }
135
+ }
159
136
}
0 commit comments