Download the PHP package corllete/silex-mongodb-provider without Composer
On this page you can find all versions of the php package corllete/silex-mongodb-provider. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package silex-mongodb-provider
silex-mongodb-provider
Silex MongoDB Service Provider - integrates Mongo PHP Library with Silex 2.x
Requirements
- PHP 5.6+ || 7.0+
- php-mongodb (ext-mongodb)
- Silex 2.x
Installation
Configuration & Usage
Connection uri defaults to mongodb://localhost:27017
. For additional resources about configuration/setup options and library usage, take a look at the Resources section.
Zero Configuration
Container setup
NOTE!
I omit
use Corllete\SilexMongoDB\Provider\MongoDBServiceProvider;
line in all of the examples below for a sake of simplicity.
Usage
Provide some options
Container setup
Usage
Multiple Connections
Container setup
WARNING!
You have to apply
rawurlencode()
username and password when used directly in the uri string
Usage
Explicit set default connection via connection name reference
Container setup
Usage
Explicit set default connection - connection label
Container setup
Usage
Assemble uri
Container setup
Few things to keep in mind here:
host
andport
are required in this configuration scenario, no defaults fallback- if you provide
username
,password
is required database
is optional in all cases
Container namespace
By default, this service provider is registered in mongodb.*
and mongodbs.*
container namespace. While reserved by the core parameters and core services namespace is something everyone must live with, I don't feel occupying namespace (e.g. mongodb
) from thrid party service providers is a good practice. Exactly this bad feeling made me implement feature (that I personally call) service provider namespace
. The logic behind it is extremely simple - you provide single and multi namespace values (following the logic of core DoctrineServiceProvider
which takes db
and dbs
namespaces) to the service provider constructor as respectively first and second argument.
It's a 'behind the scenes' feature, you may or may not use it but most important - you have a choice.
Example - occupy db
and dbs
namespaces - given there is no registered DoctrineServiceProvider
, no space for RDBMS!
Usage
Example - override only single connection namespace to db
Usage
MongoDB Client service factory
If you, for any reason (unknown to me!) decide you need to manually create \MongoDB\Client
instance, you may use the registered with MongoDBServiceProvider
factory callable.
Factory callable accepts one argument - array of connection configuration options, as they are passed via mongodb.options
to the service provider. If no configuration options are passed, $app['mongodb.default_options']
will be used (and you may override it!).
Configuration options, service parameters and services reference
Configuration options
The following describes the options passed to the register()
container method (see examples section).
Single connection options
mongodb
is the single connection configuration namespace, it may be overridden (see Namespace section). If this happens, you have to substitute it in the examples below.
-
mongodb.default_options
(array
) - options used for zero configuration (or the factory service) in same format asmongodb.options
. You may override it directly in your bootstrap code to change default behaviour. The default value of this option: mongodb.options
(array
) - same format asmongodb.default_options
, set your single connection options - more details abouturi
format, uri and driver options in MongoDB PHP library \MongoDB\Client documentation and Official MongoDBconection uri
documentation.
Multi connection options
mongodbs
is the multi connection configuration namespace, it may be overridden (see Namespace section). If this happens, you have to substitute it in the examples below.
mongodbs.options
(array
) - array ofmongodb.options
in formatLABEL => mongodb.options
. More details in the examples section.mongodbs.default
(string
) - connection label, defined inmongodbs.options
to be set as default (thus retrieved with$app['mongodb']
).
Services
Retrievable in your code via $app['SERVICE_NAME']
;
mongodb
(\MongoDB\Client
) - default mongodb client. The name of this service depends on the single connection configuration namespace.mongodbs
(Pimple\Container
) - contains all mongodb clients registered with the service provider. Retrieve them by label. The name of this service depends on the multi connection configuration namespace.
Internal
mongodb.factory
(callable
) -\MongoDB\Client
factory service. The name of this service depends on the single connection configuration namespace.mongodb.options.init
(callable
) - used internally to one-time prepare the configuration array. The name of this service depends on the single connection configuration namespace.
Parameters
Retrieve or override in your code via $app['PARAMETER_NAME']
;
mongodb.default_options
(array
) - set to override zero configuration setup & mongodb client factory default behaviour. More details inConfiguration options
section. The name of this parameter depends on the single connection configuration namespace.
Tests
Run phpunit
test after composer install
You may also print the coverage
Gotchas
s
is important! Be sure not to mess upmongodb.options
andmongodbs.options
- It seems your service provider configuration is not found? In most cases you did the following - you have changed the service provider namespace, but you have provided the default namespace configuration options (e.g.
mongodb.options
insteadyourNamespace.options
) - If you see
\LogicException
after you call the mongodb client in your code, check your service provider configuration - most probably you have provided bothmongodb.options
andmongodbs.options
configuration parameters (which is clearly illegal). - You have set 'default' connection in multi connection setup, but it is NOT the default one! Well, hardly, but you may double check if you are using both methods of setting default connection - connection labeled 'default' and
mongodbs.default = connection_name
. Connection label has higher precedence, so in the previous scenario, 'default' labeled connection will be the one returned by$app['mongodb']
and$app['mongodbs']['default']
Contribute
Fork me and open a Pull Request. Please don't provide code contribution without test coverage. Single commit Pull Request preffered.
Resources
- Official MongoDB PHP library documentation
- Official MongoDB documentation
- Official MongoDB PHP extension (the new
mongodb
driver) documentation - MongoDB\Client - PHP library documentation
- MongoDB Connection - official MongoDB documentation
- Silex Micro-framework
- Google - everything else!
License
This package is licensed under MIT License
(c) Corllete