|
| 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 |
0 commit comments