Skip to content

Commit 7855bb6

Browse files
authored
Merge ef2e4e9 into 4496d41
2 parents 4496d41 + ef2e4e9 commit 7855bb6

File tree

10 files changed

+311
-122
lines changed

10 files changed

+311
-122
lines changed

.gitignore

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
1+
# IDE files
12
/.idea/
2-
/vendor/
33

4+
# Generated files
5+
6+
## MkDocs
7+
/site/
8+
9+
## Composer
10+
/vendor/
411
composer.lock

README.md

Lines changed: 2 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -16,112 +16,15 @@ mongodb_watchdog | Store logger (watchdog) messages in MongoDB.
1616

1717
As its name implies, this release is currently alpha-level only. Use at your own risk.
1818

19-
Documentation is available on [Github pages]
19+
The complete documentation is available on [Github pages].
2020

2121
[Github pages]: https://fgm.github.io/mongodb/
2222

2323

24-
INSTALLATION
25-
============
26-
27-
The MongoDB module and sub-modules need some amount of configuration before they
28-
will properly work. This guide assumes that :
29-
30-
* a [MongoDB][download] 3.0 or later instance is already installed, configured, and available to connect to from the Drupal instance.
31-
* the site will be running [Drupal][drupal] 8.1.x or 8.2.x.
32-
* the [mongodb][mongodb] (not [mongo][mongo]) PHP extension version 1.1.7 or later is installed and configured.
33-
34-
[download]: https://www.mongodb.org/downloads
35-
[drupal]: https://www.drupal.org/project/drupal
36-
[php]: http://php.net/downloads.php
37-
[mongo]: http://php.net/mongo
38-
[mongodb]: http://php.net/mongodb
39-
40-
If MongoDB is installed on localhost, you may view the web admin interface:
41-
42-
http://localhost:28017/
43-
44-
* Download the module package, as per [Installing contributed modules (Drupal 8)][install]
45-
* Copy the relevant section from the `example.settings.local.php` to your
46-
`settings.local.php` file if you use one, or `settings.php` otherwise, and
47-
adapt it to match your MongoDB settings.
48-
* At the root of your site, add a composer requiment:
49-
50-
composer require mongodb/mongodb "^1.0.0"
51-
52-
* Enable the `mongodb` module. You now have access to the MongoDB services and Drush commands.
53-
54-
[install]: https://www.drupal.org/documentation/install/modules-themes/modules-8
55-
56-
57-
CONFIGURATION
58-
=============
59-
mongodb_watchdog
60-
----------------
61-
62-
The module uses a separate database, using the `logger` alias in MongoDB
63-
settings. Do NOT point that alias to the same database as `default`, because the
64-
module drops the logger database when uninstalling, which would drop all your
65-
other data with it.
66-
67-
* `mongodb.watchdog.items`: the maximum item limit on the capped collection used
68-
by the module. If not defined, it defaults to 10000. The actual (size-based)
69-
limit is derived from this variable, assuming 1 kiB per watchdog entry.
70-
* `mongodb.watchdog.limit`: the maximum severity level (0 to 7, per RFC 5424) to save
71-
into watchdog. Errors below this level (with a higher numerical level) will be
72-
ignored by the module. If not defined, all events will saved.
73-
* `mongodb.watchdog.items_per_page`: the maximum number of events displayed on
74-
the event details page.
75-
* `mongodb_watchdog.request_tracking`: if true, enable the per-request event
76-
tracking. If not defined, it defaults to false because its cost is not
77-
entirely negligible.
78-
* `mongodb_watchdog.requests`: if request tracking is enabled, this setting
79-
defined the maximum requests limit on the capped collection used by the
80-
module. If not defined, it defaults to 100000. The actual (size-based) limit
81-
is derived from this variable, assuming 1 kiB per tracker entry.
82-
83-
See [Drupal\Core\Logger\RfcLogLevel][levels] and [Psr\Log\LogLevel][levelnames]
84-
for further information about severity levels.
85-
86-
[levels]: https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Logger%21RfcLogLevel.php/class/RfcLogLevel/8.2.x
87-
[levelnames]: https://api.drupal.org/api/drupal/vendor%21psr%21log%21Psr%21Log%21LogLevel.php/class/LogLevel/8.2.x
88-
89-
90-
DATABASES / COLLECTIONS
91-
=======================
92-
93-
Module | Database alias | Collection(s) | Information
94-
----------------------|----------------|--------------------|-------------------------------
95-
`mongodb` | `default` | (none) | Checks alias/client consistency
96-
`mongodb_watchdog` | `logger` | `watchdog` | Event types
97-
↑ | ↑ | `watchdog_*` | Capped collections for events
98-
↑ | ↑ | `watchdog_tracker` | Capped collection for requests
99-
100-
Earlier versions used to support a collection aliasing mechanism. With this
101-
version generalizing dedicated databases per module, this is no longer needed
102-
and the associated machinery has been removed.
103-
104-
105-
DEVELOPMENT
106-
===========
107-
108-
To write your own custom modules on top of MongoDB while avoiding compatibility
109-
issues with this suite, you probably want to write on top of the `mongodb`
110-
module, using the `mongodb.client_factory` and/or `mongodb.database_factory`
111-
services instead of accessing the PHP library or MongoDB extension directly.
112-
113-
114-
CONTRIBUTING
115-
============
116-
117-
Starting with 8.x-2.0-alpha1, use the drupal.org issue queue for issue
118-
discussion, but send pull requests on Github rather than drupal.org patches.
119-
120-
12124
LEGAL
12225
=====
12326

124-
Like any Drupal module, this package is licensed under the [General Public
27+
Like any Drupal module, this package is licensed under the [General Public
12528
License, version 2.0](https://www.gnu.org/licenses/old-licenses/gpl-2.0.en.html) or later.
12629

12730
* Drupal is a registered trademark of Dries Buytaert.

docs/about.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
This is the about page.

docs/bespoke.md

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
# Bespoke code
2+
3+
Beyond the simple use cases covered by this standard package, most uses of
4+
MongoDB in Drupal projects appear in enteprise-class bespoke developments. Until
5+
this version, this usually meant totally custom code, built either straight from
6+
the legacy `mongo` extension, or on top of the Doctrine ODM for MongoDB,
7+
suffering from a total lack of integration with the underlying core Drupal CMS.
8+
9+
Starting with 8.x-2.0, such one-off code can be developed on top of the base
10+
`mongodb` module: unlike earlier releases, 8.x-2.x uses the PHP-standard
11+
connection methods and options, without deviation, adding only a thin layer of
12+
Drupal adaptation on top of the standard `mongodb` [extension] and
13+
[PHP library].
14+
15+
[extension]: http://php.net/mongodb
16+
[PHP library]: http://mongodb.github.io/mongo-php-library/
17+
18+
19+
## Example
20+
21+
The familiar Drupal alias mechanism for databases is available to provide easy,
22+
string-referenced access to `Client` and `Database` instances through the
23+
package-provided `ClientFactory` and `DatabaseFactory` services respectively.
24+
25+
Most such code is likely to be service based, so here is an example of a service
26+
`bar` in module `foo`, using a custom `foo-database` aliased as `foodb`, to keep
27+
its storage separate from the main database used by the package modules, and its
28+
logic independent of other Drupal modules.
29+
30+
31+
### Per-environment settings
32+
33+
The site local settings file includes the alias definition, binding it to the
34+
actual database credentials, allowing for per-environment configuration:
35+
36+
<?php
37+
// settings.local.php
38+
$settings['mongodb'] = [
39+
'clients' => [
40+
// Client alias => constructor parameters.
41+
'default' => [
42+
'uri' => 'mongodb://localhost:27017',
43+
'uriOptions' => [],
44+
'driverOptions' => [],
45+
],
46+
],
47+
'databases' => [
48+
// Collection alias => [ client_alias, collection_name ]
49+
'default' => ['default', 'drupal'],
50+
'logger' => ['default', 'logger'],
51+
'foodb' => ['default', 'foo-database'],
52+
],
53+
];
54+
55+
With such a configuration, the `foodb` alias is available to all MongoDB-using
56+
modules in the site, possibly pointing to different databases depending on the
57+
environment (development, staging, production...).
58+
59+
60+
### Service-based module adapter
61+
62+
The `foo.services.yml` service file for the bespoke `foo.module` can then
63+
reference `foodb` to access the database with a constant alias, regardless
64+
of the environment:
65+
66+
// modules/custom/Foo/foo.services.yml
67+
services:
68+
foo.storage:
69+
class: 'MongoDB\Database'
70+
factory: ['@mongodb.database_factory', 'get']
71+
arguments: ['foodb']
72+
73+
foo.bar:
74+
class: 'Drupal\foo\Bar'
75+
arguments: ['@foo.storage', '$logger.channel.foo']
76+
77+
This allows services in the module to access the database in both function code
78+
for Drupal hooks, and OO code for component-level logic without having to be
79+
environment-aware.
80+
81+
If the `mongodb_watchdog` module is enabled, the logger passed to the
82+
application will be a standard PSR-3 logger writing to MongoDB without the
83+
application having to know anything about it, but still providing a standard
84+
Drupal UI to examine the application logs.
85+
86+
87+
### Component logic
88+
89+
Finally the component application logic can use the services without receiving
90+
any Drupal-specific dependency. In this example, we can simply assume the
91+
service code is located within the module itself, for simplicity:
92+
93+
<?php
94+
// modules/custom/Foo/src/Bar.php
95+
use MongoDb\Database;
96+
use Psr\Log\LoggerInterface;
97+
98+
public function __construct(Database $database, LoggerInterface $logger) {
99+
$this->database = $database;
100+
$this->logger = $logger;
101+
}
102+
103+
public function baz() {
104+
// Perform some business logic using $this->database.
105+
// Log it using $this->logger.
106+
}
107+
108+
Having the code only receive standard services (like a PSR-3 logger) or
109+
[PHP library] classes allows it to be written as an agnostic component that can
110+
be brought in using Composer and shared with non-Drupal code. This is often
111+
useful in bespoke projects, which tend to combine Drupal with other parts of the
112+
application written in Laravel or Symfony standard edition, since the code has
113+
no Drupal-specific dependency.
114+
115+
116+
### Tests
117+
118+
The `mongodb` module provides a `MongoDbTestBase` base test class allowing
119+
kernel-based integration tests, as described on the [tests] page.
120+
121+
[tests]: /tests

docs/index.md

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
# MongoDB suite for Drupal 8
2+
3+
The MongoDB&reg; suite for Drupal&reg; 8 is a set of modules enabling the
4+
storage of various types of data on a Drupal site, in addition to, or as a
5+
replacement for the standard SQL storage used by Drupal.
6+
7+
It comprises several Drupal modules, each implementing a specific functionality.
8+
With the exception of the base "driver" `mongodb` module, upon which all others
9+
depend because it provides the standardized connection service to Drupal, all
10+
the modules are independent of each other except where indicated.
11+
12+
The `mongodb` module is not just the basis for this package, but also is
13+
designed to ease the development of bespoke code for end-user projects,
14+
providing Drupal-integrated Symfony services for Client and Database with a
15+
familiar alias-based selection, like the SQL database drivers do.
16+
17+
## Modules
18+
### Working
19+
20+
Module | In a word | Information
21+
------------------------|-----------|-------------------------------------------
22+
[mongodb] | driver | MongoDB Client and Database services, [tests] base
23+
[mongodb_watchdog] | logger | PSR-3 compliant logger with a built-in UI
24+
25+
[mongodb]: /mongodb
26+
[mongodb_watchdog]: /mongodb_watchdog
27+
[tests]: /tests
28+
29+
### Not yet ported
30+
31+
These modules exist for earlier Drupal versions, but have not been ported to
32+
Drupal 8 yet. Some of them might never be ported because they are no longer
33+
relevant in Drupal 8
34+
35+
Module | Information
36+
------------------------|-------------------------------------------------------
37+
`mongodb_block` | Store Drupal blocks in MongoDB
38+
`mongodb_block_ui` | Provide a UI to manager blocks stored in MongoDB. Depends on `mongodb_block`.
39+
`mongodb_cache` | Store cache information in MongoDB
40+
`mongodb_field_storage` | Store entities and their fields in MongoDB
41+
`mongodb_migrate` | Migrate SQL entities/fields to MongoDB. Depends on `mongodb_field_storage`.
42+
`mongodb_queue` | Implement a MongoDB backend for Queue API
43+
`mongodb_session` | Store session information in MongoDB
44+
45+
### Planned
46+
47+
These modules have no direct equivalent in earlier versions, but their
48+
development is being considered.
49+
50+
Module | Information
51+
-----------------|-------------------------------------------------------
52+
`mongodb_debug` | Provide low-level debug information. A D7 version exists on [mongodb_logger] but depends on the legacy `mongo` PHP extension. Futures versions will need the 1.4 version of the `mongodb` extension which implements the MongoDB APM specification.
53+
54+
[mongodb_logger]: https://github.com/FGM/mongodb_logger/
55+
56+
## Project layout
57+
58+
mkdocs.yml # The configuration file.
59+
docs/
60+
index.md # The documentation homepage.
61+
... # Other markdown pages, images and other files.
62+
63+
## Legal information
64+
65+
* This suite of modules is licensed under the General Public License, version 2 or later (GPL-2.0+).
66+
* MongoDB is a registered trademark of MongoDB Inc.
67+
* Drupal is a registered trademark of Dries Buytaert.

docs/modules/mongodb.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Driver: `mongodb`
2+
3+
The `mongodb` module is the main module in the suite, providing a thin Drupal
4+
adapter for the standard MongoDB PHP library, in the form of two factory
5+
services and a base test class.
6+
7+
## Factories
8+
9+
The basic idea of these factory services is to provide a way to create
10+
instances of the standard `Client` and `Database` classes from a simple alias
11+
string, taking all properties initialized during these creations from the
12+
Drupal standard `Settings` object.
13+
14+
This allows code to be unaware of the execution environment, referring to
15+
database or client by a functional alias, while the specifics of accessing
16+
the relevant `mongod`/`mongos` and database are left to per-environment
17+
settings,
18+
19+
* `\Drupal\mongodb\ClientFactory`:
20+
* `__construct()`: takes a single `Settings` parameter. This is normally
21+
invoked by the DIC, to which the class is exposed as
22+
`mongodb.client_factory`.
23+
* `get(string $alias): MongoDb\Client`; returns a `Client` instance matching
24+
the value defined in Drupal settings for `$alias`.

docs/modules/mongodb_watchdog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Logger: `mongodb_watchdog`

0 commit comments

Comments
 (0)