33use Closure ;
44use Illuminate \Database \Connection ;
55
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 ;
714
815 /**
916 * The MongoCollection object for this blueprint.
@@ -12,6 +19,13 @@ class Blueprint {
1219 */
1320 protected $ collection ;
1421
22+ /**
23+ * Fluent columns
24+ *
25+ * @var array
26+ */
27+ protected $ columns = array ();
28+
1529 /**
1630 * Create a new schema blueprint.
1731 *
@@ -21,6 +35,7 @@ class Blueprint {
2135 */
2236 public function __construct (Connection $ connection , $ collection )
2337 {
38+ $ this ->connection = $ connection ;
2439 $ this ->collection = $ connection ->getCollection ($ collection );
2540 }
2641
@@ -31,11 +46,26 @@ public function __construct(Connection $connection, $collection)
3146 * @param array $options
3247 * @return bool
3348 */
34- public function index ($ columns , $ options = array ())
49+ public function index ($ columns = null , $ options = array ())
3550 {
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+ }
3765
38- return (1 == (int ) $ result ['ok ' ]);
66+ $ this ->collection ->ensureIndex ($ columns , $ options );
67+
68+ return $ this ;
3969 }
4070
4171 /**
@@ -44,11 +74,16 @@ public function index($columns, $options = array())
4474 * @param string|array $columns
4575 * @return bool
4676 */
47- public function dropIndex ($ columns )
77+ public function dropIndex ($ columns = null )
4878 {
49- $ result = $ this ->collection ->deleteIndex ($ columns );
79+ $ columns = $ this ->fluent ($ columns );
80+
81+ foreach ($ columns as $ column )
82+ {
83+ $ this ->collection ->deleteIndex ($ column );
84+ }
5085
51- return ( 1 == ( int ) $ result [ ' ok ' ]) ;
86+ return $ this ;
5287 }
5388
5489 /**
@@ -57,44 +92,70 @@ public function dropIndex($columns)
5792 * @param string|array $columns
5893 * @return bool
5994 */
60- public function unique ($ columns )
95+ public function unique ($ columns = null )
6196 {
62- return $ this ->index ($ columns , array ('unique ' => true ));
97+ $ columns = $ this ->fluent ($ columns );
98+ $ this ->index ($ columns , array ('unique ' => true ));
99+
100+ return $ this ;
63101 }
64102
65103 /**
66104 * Specify a non blocking index for the collection.
67- *
105+ *
68106 * @param string|array $columns
69107 * @return bool
70108 */
71- public function background ($ columns )
109+ public function background ($ columns = null )
72110 {
73- return $ this ->index ($ columns , array ('background ' => true ));
111+ $ columns = $ this ->fluent ($ columns );
112+ $ this ->index ($ columns , array ('background ' => true ));
113+
114+ return $ this ;
74115 }
75116
76117 /**
77118 * Specify a sparse index for the collection.
78- *
119+ *
79120 * @param string|array $columns
80121 * @return bool
81122 */
82- public function sparse ($ columns )
123+ public function sparse ($ columns = null )
83124 {
84- return $ this ->index ($ columns , array ('sparse ' => true ));
125+ $ columns = $ this ->fluent ($ columns );
126+ $ this ->index ($ columns , array ('sparse ' => true ));
127+
128+ return $ this ;
85129 }
86130
87131 /**
88132 * Specify the number of seconds after wich a document should be considered expired based,
89133 * on the given single-field index containing a date.
90- *
134+ *
91135 * @param string|array $columns
92136 * @param int $seconds
93137 * @return bool
94138 */
95139 public function expire ($ columns , $ seconds )
96140 {
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 );
98159 }
99160
100161 /**
@@ -104,9 +165,43 @@ public function expire($columns, $seconds)
104165 */
105166 public function drop ()
106167 {
107- $ result = $ this ->collection ->drop ();
168+ $ this ->collection ->drop ();
169+ }
108170
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+ }
110205 }
111206
112207}
0 commit comments