Skip to content

Commit 2d8449e

Browse files
committed
Tweaks
1 parent a6bc2f5 commit 2d8449e

File tree

5 files changed

+77
-42
lines changed

5 files changed

+77
-42
lines changed

composer.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@
1111
"license" : "MIT",
1212
"require": {
1313
"php": ">=5.3.0",
14-
"illuminate/support": "4.0.x",
15-
"illuminate/database": "4.0.x",
16-
"illuminate/events": "4.0.x"
14+
"illuminate/support": "4.1.x",
15+
"illuminate/database": "4.1.x",
16+
"illuminate/events": "4.1.x"
1717
},
1818
"autoload": {
1919
"psr-0": {

src/Jenssegers/Mongodb/Builder.php

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,9 @@ class Builder extends \Illuminate\Database\Query\Builder {
3333
* @param Connection $connection
3434
* @return void
3535
*/
36-
public function __construct(Connection $connection, $collection = null)
36+
public function __construct(Connection $connection)
3737
{
3838
$this->connection = $connection;
39-
40-
// Set optional collection
41-
if (!is_null($collection))
42-
{
43-
$this->from($collection);
44-
}
4539
}
4640

4741
/**
@@ -528,4 +522,4 @@ public function newQuery()
528522
return new Builder($this->connection);
529523
}
530524

531-
}
525+
}

src/Jenssegers/Mongodb/Connection.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?php namespace Jenssegers\Mongodb;
22

3+
use Jenssegers\Mongodb\Builder as QueryBuilder;
34
use MongoClient;
45

56
class Connection extends \Illuminate\Database\Connection {
@@ -39,6 +40,30 @@ public function __construct(array $config)
3940
$this->db = $this->connection->{$config['database']};
4041
}
4142

43+
/**
44+
* Return a new QueryBuilder for a collection
45+
*
46+
* @param string $collection
47+
* @return QueryBuilder
48+
*/
49+
public function collection($collection)
50+
{
51+
$query = new QueryBuilder($this);
52+
53+
return $query->from($collection);
54+
}
55+
56+
/**
57+
* Begin a fluent query against a database table.
58+
*
59+
* @param string $table
60+
* @return QueryBuilder
61+
*/
62+
public function table($table)
63+
{
64+
return $this->collection($table);
65+
}
66+
4267
/**
4368
* Get a MongoDB collection.
4469
*
@@ -92,4 +117,16 @@ protected function getDsn(array $config)
92117
return $dsn;
93118
}
94119

120+
/**
121+
* Dynamically pass methods to the connection.
122+
*
123+
* @param string $method
124+
* @param array $parameters
125+
* @return mixed
126+
*/
127+
public function __call($method, $parameters)
128+
{
129+
return call_user_func_array(array($this->connection, $method), $parameters);
130+
}
131+
95132
}

src/Jenssegers/Mongodb/DatabaseManager.php

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public function __construct($app)
4949
* Get a database connection instance.
5050
*
5151
* @param string $name
52-
* @return \Illuminate\Database\Connection
52+
* @return Connection
5353
*/
5454
public function connection($name = null)
5555
{
@@ -72,7 +72,7 @@ public function connection($name = null)
7272
* Reconnect to the given database.
7373
*
7474
* @param string $name
75-
* @return \Illuminate\Database\Connection
75+
* @return Connection
7676
*/
7777
public function reconnect($name = null)
7878
{
@@ -85,7 +85,7 @@ public function reconnect($name = null)
8585
* Make the database connection instance.
8686
*
8787
* @param string $name
88-
* @return \Illuminate\Database\Connection
88+
* @return Connection
8989
*/
9090
protected function makeConnection($name)
9191
{
@@ -102,18 +102,26 @@ protected function makeConnection($name)
102102
/**
103103
* Prepare the database connection instance.
104104
*
105-
* @param \Illuminate\Database\Connection $connection
106-
* @return \Illuminate\Database\Connection
105+
* @param Connection $connection
106+
* @return Connection
107107
*/
108108
protected function prepare(Connection $connection)
109109
{
110110
$connection->setEventDispatcher($this->app['events']);
111111

112+
// The database connection can also utilize a cache manager instance when cache
113+
// functionality is used on queries, which provides an expressive interface
114+
// to caching both fluent queries and Eloquent queries that are executed.
115+
$app = $this->app;
116+
117+
$connection->setCacheManager(function() use ($app)
118+
{
119+
return $app['cache'];
120+
});
121+
112122
// We will setup a Closure to resolve the paginator instance on the connection
113123
// since the Paginator isn't sued on every request and needs quite a few of
114124
// our dependencies. It'll be more efficient to lazily resolve instances.
115-
$app = $this->app;
116-
117125
$connection->setPaginator(function() use ($app)
118126
{
119127
return $app['paginator'];
@@ -167,25 +175,15 @@ public function setDefaultConnection($name)
167175
}
168176

169177
/**
170-
* Return a new QueryBuilder for a collection
171-
*
172-
* @param string $collection
173-
* @return QueryBuilder
174-
*/
175-
public function collection($collection)
176-
{
177-
return new QueryBuilder($this->connection(), $collection);
178-
}
179-
180-
/**
181-
* Return a new QueryBuilder for a collection
178+
* Dynamically pass methods to the default connection.
182179
*
183-
* @param string $collection
184-
* @return QueryBuilder
180+
* @param string $method
181+
* @param array $parameters
182+
* @return mixed
185183
*/
186-
public function __get($collection)
184+
public function __call($method, $parameters)
187185
{
188-
return $this->collection($collection);
186+
return call_user_func_array(array($this->connection(), $method), $parameters);
189187
}
190188

191189
}

tests/ConnectionTest.php

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,16 @@
55

66
class ConnectionTest extends PHPUnit_Framework_TestCase {
77

8-
private $collection;
98
private $connection;
109

1110
public function setUp()
1211
{
1312
include('tests/app.php');
1413
$this->connection = new Connection($app['config']['database.connections']['mongodb']);
15-
$this->collection = $this->connection->getCollection('unittest');
1614
}
1715

1816
public function tearDown()
1917
{
20-
if ($this->collection)
21-
{
22-
$this->collection->drop();
23-
}
2418
}
2519

2620
public function testDb()
@@ -31,8 +25,20 @@ public function testDb()
3125

3226
public function testCollection()
3327
{
34-
$this->assertInstanceOf('MongoCollection', $this->collection);
35-
$this->assertEquals('unittest', $this->collection->getName());
28+
$collection = $this->connection->getCollection('unittest');
29+
$this->assertInstanceOf('MongoCollection', $collection);
30+
31+
$collection = $this->connection->collection('unittests');
32+
$this->assertInstanceOf('Jenssegers\Mongodb\Builder', $collection);
33+
34+
$collection = $this->connection->table('unittests');
35+
$this->assertInstanceOf('Jenssegers\Mongodb\Builder', $collection);
36+
}
37+
38+
public function testDynamic()
39+
{
40+
$dbs = $this->connection->listDBs();
41+
$this->assertTrue(is_array($dbs));
3642
}
3743

3844
}

0 commit comments

Comments
 (0)