Skip to content
This repository was archived by the owner on Aug 22, 2023. It is now read-only.

Draft notes after a first review of the full code #3

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1179,6 +1179,7 @@ class Message extends Model

### Authentication

// Could be simplified if we get rid of the custom PasswordResetServiceProvider
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we don't need the custom provider, I'm all for it.

If you want to use Laravel's native Auth functionality, register this included service provider:

```php
Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
],
"license": "MIT",
"require": {
"ext-mongodb": "*",
"illuminate/support": "^10.0",
"illuminate/container": "^10.0",
"illuminate/database": "^10.0",
Expand Down
2 changes: 1 addition & 1 deletion phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
</testsuite>
</testsuites>
<php>
<env name="MONGODB_URI" value="mongodb://mongodb/" />
<env name="MONGODB_URI" value="mongodb://127.0.0.1:27017/?serverSelectionTimeoutMS=100"/>
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

May be necessary for the CI.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe it was CI that used the mongodb name, but in that case I'd rather have a specific file for CI and have a version that works with a normal setup for local testing.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

<env name="MONGODB_DATABASE" value="unittest"/>
<env name="MYSQL_HOST" value="mysql"/>
<env name="MYSQL_PORT" value="3306"/>
Expand Down
2 changes: 2 additions & 0 deletions src/Auth/DatabaseTokenRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ protected function getPayload($email, $token)
return [
'email' => $email,
'token' => $this->hasher->make($token),
// why is it necessary ? vs "new Carbon" in parent class
'created_at' => new UTCDateTime(Date::now()->format('Uv')),
];
}
Expand All @@ -27,6 +28,7 @@ protected function getPayload($email, $token)
*/
protected function tokenExpired($createdAt)
{
// Could be removed if Carbon was natively suported?
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the writes are handled through the Model class, we could indeed get rid of it once we support Carbon. Otherwise, we're forced to handle this here. I'm not too familiar with this logic and haven't touched this part yet ("it just works"), but happy to simplify things where possible.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$createdAt = $this->convertDateTime($createdAt);

return parent::tokenExpired($createdAt);
Expand Down
13 changes: 11 additions & 2 deletions src/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,8 @@ public function getSchemaBuilder()
*
* @return Database
*/
public function getMongoDB()
// Should be named getDatabase ?
public function getDatabase()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I don't like getMongoDB as a name. We can introduce getDatabase and deprecate the getMongoDB method in 4.0 for later removal.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

{
return $this->db;
}
Expand All @@ -132,7 +133,7 @@ public function getMongoClient()
*/
public function getDatabaseName()
{
return $this->getMongoDB()->getDatabaseName();
return $this->getDatabase()->getDatabaseName();
}

/**
Expand All @@ -147,6 +148,7 @@ public function getDatabaseName()
protected function getDefaultDatabaseName(string $dsn, array $config): string
{
if (empty($config['database'])) {
// Use parse_url() to get the database name from dsn
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes.

if (preg_match('/^mongodb(?:[+]srv)?:\\/\\/.+\\/([^?&]+)/s', $dsn, $matches)) {
$config['database'] = $matches[1];
} else {
Expand Down Expand Up @@ -195,6 +197,8 @@ protected function createConnection($dsn, array $config, array $options): Client
*/
public function disconnect()
{
// No need to explicitly disconnect?
// What appens if a reference to the connection is kept somewhere?
Comment on lines +200 to +201
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe this was done in an attempt to duplicate behaviour to relational databases. However, unless the client was created with disabled client persistence (which it isn't), this will keep connections open due to the underlying libmongoc client object being persisted for future use in the process. The drivers API also doesn't support disconnecting a client object. That said, this is probably the closest we'll get to disconnecting the database connection, so I'm fine keeping it as long as we document the caveats.

unset($this->connection);
}

Expand Down Expand Up @@ -260,6 +264,9 @@ protected function getDsn(array $config): string
/**
* @inheritdoc
*/
// Should be @internal because the parent method is protected
// and this method is use in Collection::__call
// Otherwise reimplement the method in Collection
Comment on lines +267 to +269
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. I'm wondering why Collection uses this - does it store some instrumentation logs somewhere?

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

public function getElapsedTime($start)
{
return parent::getElapsedTime($start);
Expand Down Expand Up @@ -302,6 +309,8 @@ protected function getDefaultSchemaGrammar()
*
* @param \MongoDB\Database $db
*/
// 1 more reason to rename getMongoDB to getDatabase, for consistency with the setter
// this is not used in the codebase, use-case may be challenged as it allows to by-pass the connection
Comment on lines +312 to +313
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I'm very much in favour of removing this method or at least marking it as @internal. The side effects of this are quite large.

public function setDatabase(\MongoDB\Database $db)
{
$this->db = $db;
Expand Down
7 changes: 5 additions & 2 deletions src/Eloquent/EmbedsRelations.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Jenssegers\Mongodb\Relations\EmbedsMany;
use Jenssegers\Mongodb\Relations\EmbedsOne;

// Documentation ?
trait EmbedsRelations
{
/**
Expand All @@ -23,7 +24,8 @@ protected function embedsMany($related, $localKey = null, $foreignKey = null, $r
// the calling method's name and use that as the relationship name as most
// of the time this will be what we desire to use for the relationships.
if ($relation === null) {
[, $caller] = debug_backtrace(false);
// Black magic. The call may be optimized to get only the method name.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#11

[, $caller] = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2);

$relation = $caller['function'];
}
Expand Down Expand Up @@ -58,7 +60,8 @@ protected function embedsOne($related, $localKey = null, $foreignKey = null, $re
// the calling method's name and use that as the relationship name as most
// of the time this will be what we desire to use for the relationships.
if ($relation === null) {
[, $caller] = debug_backtrace(false);
// Black magic. The call may be optimized to get only the method name.
[, $caller] = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2);

$relation = $caller['function'];
}
Expand Down
5 changes: 3 additions & 2 deletions src/Eloquent/HybridRelations.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Jenssegers\Mongodb\Relations\MorphMany;
use Jenssegers\Mongodb\Relations\MorphTo;

// Documentation ?
trait HybridRelations
{
/**
Expand Down Expand Up @@ -134,7 +135,7 @@ public function belongsTo($related, $foreignKey = null, $otherKey = null, $relat
// the calling method's name and use that as the relationship name as most
// of the time this will be what we desire to use for the relationships.
if ($relation === null) {
[$current, $caller] = debug_backtrace(false, 2);
[, $caller] = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2);

$relation = $caller['function'];
}
Expand Down Expand Up @@ -178,7 +179,7 @@ public function morphTo($name = null, $type = null, $id = null, $ownerKey = null
// since that is most likely the name of the polymorphic interface. We can
// use that to get both the class and foreign key that will be utilized.
if ($name === null) {
[$current, $caller] = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2);
[, $caller] = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2);

$name = $caller['function'];
}
Expand Down
7 changes: 6 additions & 1 deletion src/Eloquent/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Jenssegers\Mongodb\Eloquent;

use Carbon\CarbonInterface;
use function array_key_exists;
use DateTimeInterface;
use function explode;
Expand Down Expand Up @@ -86,6 +87,8 @@ public function getQualifiedKeyName()

/**
* @inheritdoc
*
* @return UTCDateTime
*/
public function fromDateTime($value)
{
Expand All @@ -104,6 +107,8 @@ public function fromDateTime($value)

/**
* @inheritdoc
*
* @param UTCDateTime|CarbonInterface|DateTimeInterface|int|string $value
*/
protected function asDateTime($value)
{
Expand Down Expand Up @@ -151,7 +156,7 @@ public function getTable()
public function getAttribute($key)
{
if (! $key) {
return;
return null;
}

// Dot notation support.
Expand Down
Loading