Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
L
laravel-mongodb
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
sinan
laravel-mongodb
Commits
22a9e3be
Unverified
Commit
22a9e3be
authored
Feb 26, 2020
by
Stas
Committed by
GitHub
Feb 26, 2020
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #1971 from divine/refactor_and_fix_db_selection
[fix] default database detection from dsn
parents
50e75d6a
f2ce3d4f
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
40 additions
and
13 deletions
+40
-13
Connection.php
src/Jenssegers/Mongodb/Connection.php
+25
-13
ConnectionTest.php
tests/ConnectionTest.php
+9
-0
TestCase.php
tests/TestCase.php
+1
-0
database.php
tests/config/database.php
+5
-0
No files found.
src/Jenssegers/Mongodb/Connection.php
View file @
22a9e3be
...
@@ -4,6 +4,7 @@ namespace Jenssegers\Mongodb;
...
@@ -4,6 +4,7 @@ namespace Jenssegers\Mongodb;
use
Illuminate\Database\Connection
as
BaseConnection
;
use
Illuminate\Database\Connection
as
BaseConnection
;
use
Illuminate\Support\Arr
;
use
Illuminate\Support\Arr
;
use
InvalidArgumentException
;
use
MongoDB\Client
;
use
MongoDB\Client
;
class
Connection
extends
BaseConnection
class
Connection
extends
BaseConnection
...
@@ -37,8 +38,11 @@ class Connection extends BaseConnection
...
@@ -37,8 +38,11 @@ class Connection extends BaseConnection
// Create the connection
// Create the connection
$this
->
connection
=
$this
->
createConnection
(
$dsn
,
$config
,
$options
);
$this
->
connection
=
$this
->
createConnection
(
$dsn
,
$config
,
$options
);
// Get default database name
$default_db
=
$this
->
getDefaultDatabaseName
(
$dsn
,
$config
);
// Select database
// Select database
$this
->
db
=
$this
->
connection
->
selectDatabase
(
$
this
->
getDatabaseDsn
(
$dsn
,
$config
[
'database'
])
);
$this
->
db
=
$this
->
connection
->
selectDatabase
(
$
default_db
);
$this
->
useDefaultPostProcessor
();
$this
->
useDefaultPostProcessor
();
...
@@ -114,6 +118,26 @@ class Connection extends BaseConnection
...
@@ -114,6 +118,26 @@ class Connection extends BaseConnection
return
$this
->
getMongoDB
()
->
getDatabaseName
();
return
$this
->
getMongoDB
()
->
getDatabaseName
();
}
}
/**
* Get the name of the default database based on db config or try to detect it from dsn
* @param string $dsn
* @param array $config
* @return string
* @throws InvalidArgumentException
*/
protected
function
getDefaultDatabaseName
(
$dsn
,
$config
)
{
if
(
empty
(
$config
[
'database'
]))
{
if
(
preg_match
(
'/^mongodb:\\/\\/.+\\/([^?&]+)/s'
,
$dsn
,
$matches
))
{
$config
[
'database'
]
=
$matches
[
1
];
}
else
{
throw
new
InvalidArgumentException
(
"Database is not properly configured."
);
}
}
return
$config
[
'database'
];
}
/**
/**
* Create a new MongoDB connection.
* Create a new MongoDB connection.
* @param string $dsn
* @param string $dsn
...
@@ -191,18 +215,6 @@ class Connection extends BaseConnection
...
@@ -191,18 +215,6 @@ class Connection extends BaseConnection
return
'mongodb://'
.
implode
(
','
,
$hosts
)
.
(
$auth_database
?
'/'
.
$auth_database
:
''
);
return
'mongodb://'
.
implode
(
','
,
$hosts
)
.
(
$auth_database
?
'/'
.
$auth_database
:
''
);
}
}
/**
* Get database name from DSN string, if there is no database in DSN path - returns back $database argument.
* @param string $dsn
* @param $database
* @return string
*/
protected
function
getDatabaseDsn
(
$dsn
,
$database
)
{
$dsnDatabase
=
trim
(
parse_url
(
$dsn
,
PHP_URL_PATH
),
'/'
);
return
trim
(
$dsnDatabase
)
?
$dsnDatabase
:
$database
;
}
/**
/**
* Create a DSN string from a configuration.
* Create a DSN string from a configuration.
* @param array $config
* @param array $config
...
...
tests/ConnectionTest.php
View file @
22a9e3be
...
@@ -32,6 +32,15 @@ class ConnectionTest extends TestCase
...
@@ -32,6 +32,15 @@ class ConnectionTest extends TestCase
$this
->
assertInstanceOf
(
\MongoDB\Client
::
class
,
$connection
->
getMongoClient
());
$this
->
assertInstanceOf
(
\MongoDB\Client
::
class
,
$connection
->
getMongoClient
());
}
}
public
function
testDsnDb
()
{
$connection
=
DB
::
connection
(
'dsn_mongodb_db'
);
$this
->
assertInstanceOf
(
\MongoDB\Database
::
class
,
$connection
->
getMongoDB
());
$connection
=
DB
::
connection
(
'dsn_mongodb_db'
);
$this
->
assertInstanceOf
(
\MongoDB\Client
::
class
,
$connection
->
getMongoClient
());
}
public
function
testCollection
()
public
function
testCollection
()
{
{
$collection
=
DB
::
connection
(
'mongodb'
)
->
getCollection
(
'unittest'
);
$collection
=
DB
::
connection
(
'mongodb'
)
->
getCollection
(
'unittest'
);
...
...
tests/TestCase.php
View file @
22a9e3be
...
@@ -53,6 +53,7 @@ class TestCase extends Orchestra\Testbench\TestCase
...
@@ -53,6 +53,7 @@ class TestCase extends Orchestra\Testbench\TestCase
$app
[
'config'
]
->
set
(
'database.connections.mongodb'
,
$config
[
'connections'
][
'mongodb'
]);
$app
[
'config'
]
->
set
(
'database.connections.mongodb'
,
$config
[
'connections'
][
'mongodb'
]);
$app
[
'config'
]
->
set
(
'database.connections.mongodb2'
,
$config
[
'connections'
][
'mongodb'
]);
$app
[
'config'
]
->
set
(
'database.connections.mongodb2'
,
$config
[
'connections'
][
'mongodb'
]);
$app
[
'config'
]
->
set
(
'database.connections.dsn_mongodb'
,
$config
[
'connections'
][
'dsn_mongodb'
]);
$app
[
'config'
]
->
set
(
'database.connections.dsn_mongodb'
,
$config
[
'connections'
][
'dsn_mongodb'
]);
$app
[
'config'
]
->
set
(
'database.connections.dsn_mongodb_db'
,
$config
[
'connections'
][
'dsn_mongodb_db'
]);
$app
[
'config'
]
->
set
(
'auth.model'
,
'User'
);
$app
[
'config'
]
->
set
(
'auth.model'
,
'User'
);
$app
[
'config'
]
->
set
(
'auth.providers.users.model'
,
'User'
);
$app
[
'config'
]
->
set
(
'auth.providers.users.model'
,
'User'
);
...
...
tests/config/database.php
View file @
22a9e3be
...
@@ -21,6 +21,11 @@ return [
...
@@ -21,6 +21,11 @@ return [
'database'
=>
env
(
'MONGO_DATABASE'
,
'unittest'
),
'database'
=>
env
(
'MONGO_DATABASE'
,
'unittest'
),
],
],
'dsn_mongodb_db'
=>
[
'driver'
=>
'mongodb'
,
'dsn'
=>
"mongodb://
$mongoHost
:
$mongoPort
/"
.
env
(
'MONGO_DATABASE'
,
'unittest'
),
],
'mysql'
=>
[
'mysql'
=>
[
'driver'
=>
'mysql'
,
'driver'
=>
'mysql'
,
'host'
=>
env
(
'MYSQL_HOST'
,
'mysql'
),
'host'
=>
env
(
'MYSQL_HOST'
,
'mysql'
),
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment