3
3
use Closure ;
4
4
use Illuminate \Database \Connection ;
5
5
6
- class Blueprint {
6
+ class Blueprint extends \Illuminate \Database \Schema \Blueprint {
7
+
8
+ /**
9
+ * The MongoConnection object for this blueprint.
10
+ *
11
+ * @var MongoConnection
12
+ */
13
+ protected $ connection ;
7
14
8
15
/**
9
16
* The MongoCollection object for this blueprint.
@@ -12,6 +19,13 @@ class Blueprint {
12
19
*/
13
20
protected $ collection ;
14
21
22
+ /**
23
+ * Fluent columns
24
+ *
25
+ * @var array
26
+ */
27
+ protected $ columns = array ();
28
+
15
29
/**
16
30
* Create a new schema blueprint.
17
31
*
@@ -21,6 +35,7 @@ class Blueprint {
21
35
*/
22
36
public function __construct (Connection $ connection , $ collection )
23
37
{
38
+ $ this ->connection = $ connection ;
24
39
$ this ->collection = $ connection ->getCollection ($ collection );
25
40
}
26
41
@@ -31,11 +46,26 @@ public function __construct(Connection $connection, $collection)
31
46
* @param array $options
32
47
* @return bool
33
48
*/
34
- public function index ($ columns , $ options = array ())
49
+ public function index ($ columns = null , $ options = array ())
35
50
{
36
- $ result = $ this ->collection ->ensureIndex ($ columns , $ options );
51
+ $ columns = $ this ->fluent ($ columns );
52
+
53
+ // Columns are passed as a default array
54
+ if (is_array ($ columns ) && is_int (key ($ columns )))
55
+ {
56
+ // Transform the columns to the required array format
57
+ $ transform = array ();
58
+ foreach ($ columns as $ column )
59
+ {
60
+ $ transform [$ column ] = 1 ;
61
+ }
62
+
63
+ $ columns = $ transform ;
64
+ }
37
65
38
- return (1 == (int ) $ result ['ok ' ]);
66
+ $ this ->collection ->ensureIndex ($ columns , $ options );
67
+
68
+ return $ this ;
39
69
}
40
70
41
71
/**
@@ -44,11 +74,16 @@ public function index($columns, $options = array())
44
74
* @param string|array $columns
45
75
* @return bool
46
76
*/
47
- public function dropIndex ($ columns )
77
+ public function dropIndex ($ columns = null )
48
78
{
49
- $ result = $ this ->collection ->deleteIndex ($ columns );
79
+ $ columns = $ this ->fluent ($ columns );
80
+
81
+ foreach ($ columns as $ column )
82
+ {
83
+ $ this ->collection ->deleteIndex ($ column );
84
+ }
50
85
51
- return ( 1 == ( int ) $ result [ ' ok ' ]) ;
86
+ return $ this ;
52
87
}
53
88
54
89
/**
@@ -57,44 +92,70 @@ public function dropIndex($columns)
57
92
* @param string|array $columns
58
93
* @return bool
59
94
*/
60
- public function unique ($ columns )
95
+ public function unique ($ columns = null )
61
96
{
62
- return $ this ->index ($ columns , array ('unique ' => true ));
97
+ $ columns = $ this ->fluent ($ columns );
98
+ $ this ->index ($ columns , array ('unique ' => true ));
99
+
100
+ return $ this ;
63
101
}
64
102
65
103
/**
66
104
* Specify a non blocking index for the collection.
67
- *
105
+ *
68
106
* @param string|array $columns
69
107
* @return bool
70
108
*/
71
- public function background ($ columns )
109
+ public function background ($ columns = null )
72
110
{
73
- return $ this ->index ($ columns , array ('background ' => true ));
111
+ $ columns = $ this ->fluent ($ columns );
112
+ $ this ->index ($ columns , array ('background ' => true ));
113
+
114
+ return $ this ;
74
115
}
75
116
76
117
/**
77
118
* Specify a sparse index for the collection.
78
- *
119
+ *
79
120
* @param string|array $columns
80
121
* @return bool
81
122
*/
82
- public function sparse ($ columns )
123
+ public function sparse ($ columns = null )
83
124
{
84
- return $ this ->index ($ columns , array ('sparse ' => true ));
125
+ $ columns = $ this ->fluent ($ columns );
126
+ $ this ->index ($ columns , array ('sparse ' => true ));
127
+
128
+ return $ this ;
85
129
}
86
130
87
131
/**
88
132
* Specify the number of seconds after wich a document should be considered expired based,
89
133
* on the given single-field index containing a date.
90
- *
134
+ *
91
135
* @param string|array $columns
92
136
* @param int $seconds
93
137
* @return bool
94
138
*/
95
139
public function expire ($ columns , $ seconds )
96
140
{
97
- return $ this ->index ($ columns , array ('expireAfterSeconds ' => $ seconds ));
141
+ $ columns = $ this ->fluent ($ columns );
142
+ $ this ->index ($ columns , array ('expireAfterSeconds ' => $ seconds ));
143
+
144
+ return $ this ;
145
+ }
146
+
147
+ /**
148
+ * Indicate that the table needs to be created.
149
+ *
150
+ * @return bool
151
+ */
152
+ public function create ()
153
+ {
154
+ $ collection = $ this ->collection ->getName ();
155
+
156
+ // Ensure the collection is created
157
+ $ db = $ this ->connection ->getMongoDB ();
158
+ $ db ->createCollection ($ collection );
98
159
}
99
160
100
161
/**
@@ -104,9 +165,43 @@ public function expire($columns, $seconds)
104
165
*/
105
166
public function drop ()
106
167
{
107
- $ result = $ this ->collection ->drop ();
168
+ $ this ->collection ->drop ();
169
+ }
108
170
109
- return (1 == (int ) $ result ['ok ' ]);
171
+ /**
172
+ * Add a new column to the blueprint.
173
+ *
174
+ * @param string $type
175
+ * @param string $name
176
+ * @param array $parameters
177
+ * @return Blueprint
178
+ */
179
+ protected function addColumn ($ type , $ name , array $ parameters = array ())
180
+ {
181
+ $ this ->fluent ($ name );
182
+ return $ this ;
183
+ }
184
+
185
+ /**
186
+ * Allow fluent columns
187
+ *
188
+ * @param string|array $columns
189
+ * @return string|array
190
+ */
191
+ protected function fluent ($ columns = null )
192
+ {
193
+ if (is_null ($ columns ))
194
+ {
195
+ return $ this ->columns ;
196
+ }
197
+ else if (is_string ($ columns ))
198
+ {
199
+ return $ this ->columns = array ($ columns );
200
+ }
201
+ else
202
+ {
203
+ return $ this ->columns = $ columns ;
204
+ }
110
205
}
111
206
112
207
}
0 commit comments