Skip to content

Commit

Permalink
First commit.
Browse files Browse the repository at this point in the history
  • Loading branch information
nilov committed May 3, 2017
0 parents commit 6ad6505
Show file tree
Hide file tree
Showing 9 changed files with 272 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/.idea

48 changes: 48 additions & 0 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

/*
* This file is part of the "GlavwebMongoDBBundle" package.
*
* (c) Andrey Nilov <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Glavweb\MongoDBBundle\DependencyInjection;

use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\ConfigurationInterface;

/**
* Class Configuration
*
* This is the class that validates and merges configuration from your app/config files.
*
* To learn more see {@link http://symfony.com/doc/current/cookbook/bundles/configuration.html}
*
* @package Glavweb\MongoDBBundle\DependencyInjection
* @author Andrey Nilov <[email protected]>
*/
class Configuration implements ConfigurationInterface
{
/**
* {@inheritdoc}
*/
public function getConfigTreeBuilder()
{
$treeBuilder = new TreeBuilder();
$rootNode = $treeBuilder->root('glavweb_mongo_db');

$rootNode
->children()
->scalarNode('host')->isRequired()->cannotBeEmpty()->end()
->scalarNode('dbname')->isRequired()->cannotBeEmpty()->end()
->scalarNode('user')->isRequired()->cannotBeEmpty()->end()
->scalarNode('password')->isRequired()->cannotBeEmpty()->end()
->end()
;

return $treeBuilder;
}
}
47 changes: 47 additions & 0 deletions DependencyInjection/GlavwebMongoDBExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

/*
* This file is part of the "GlavwebMongoDBBundle" package.
*
* (c) Andrey Nilov <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Glavweb\MongoDBBundle\DependencyInjection;

use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
use Symfony\Component\DependencyInjection\Loader;

/**
* Class GlavwebMongoDBExtension
*
* This is the class that loads and manages your bundle configuration.
*
* @link http://symfony.com/doc/current/cookbook/bundles/extension.html
*
* @package Glavweb\MongoDBBundle\DependencyInjection
* @author Andrey Nilov <[email protected]>
*/
class GlavwebMongoDBExtension extends Extension
{
/**
* {@inheritdoc}
*/
public function load(array $configs, ContainerBuilder $container)
{
$configuration = new Configuration();
$config = $this->processConfiguration($configuration, $configs);

$container->setParameter('mongodb.host', $config['host']);
$container->setParameter('mongodb.dbname', $config['dbname']);
$container->setParameter('mongodb.user', $config['user']);
$container->setParameter('mongodb.password', $config['password']);

$loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
$loader->load('services.yml');
}
}
23 changes: 23 additions & 0 deletions GlavwebMongoDBBundle.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

/*
* This file is part of the "GlavwebMongoDBBundle" package.
*
* (c) Andrey Nilov <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Glavweb\MongoDBBundle;

use Symfony\Component\HttpKernel\Bundle\Bundle;

/**
* Class GlavwebMongoDBBundle
*
* @package Glavweb\MongoDBBundle
* @author Andrey Nilov <[email protected]>
*/
class GlavwebMongoDBBundle extends Bundle
{}
22 changes: 22 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
The MIT License (MIT)

Copyright (c) 2017 GLAVWEB

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

38 changes: 38 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
Installation
============

### Get the bundle using composer

Add GlavwebMongoDBBundle by running this command from the terminal at the root of
your Symfony project:

```bash
php composer.phar require glavweb/mongodb-bundle
```

### Enable the bundle

To start using the bundle, register the bundle in your application's kernel class:

```php
// app/AppKernel.php
public function registerBundles()
{
$bundles = array(
// ...
new Glavweb\MongoDBBundle\GlavwebMongoDBBundle(),
// ...
);
}
```
### Configure the bundle


```yaml

glavweb_mongo_db:
host: "%mongodb_host%"
dbname: "%mongodb_dbname%"
user: "%mongodb_user%"
password: "%mongodb_password%"
```
66 changes: 66 additions & 0 deletions Registry.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

/*
* This file is part of the "GlavwebMongoDBBundle" package.
*
* (c) Andrey Nilov <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Glavweb\MongoDBBundle;

/**
* Class Registry
*
* @package Glavweb\MongoDBBundle
* @author Andrey Nilov <[email protected]>
*/
class Registry
{
/**
* @var string
*/
private $dbname;

/**
* @var \MongoDB\Client
*/
private $client;

/**
* Registry constructor.
*
* @param string $host
* @param string $dbname
* @param string $username
* @param string $password
*/
public function __construct(string $host, string $dbname, string $username, string $password)
{
$this->dbname = $dbname;

$uri = sprintf('mongodb://%s/', $host);
$this->client = new \MongoDB\Client($uri, [
'username' => $username,
'password' => $password,
]);
}

/**
* @return \MongoDB\Client
*/
public function getClient()
{
return $this->client;
}

/**
* @return \MongoDB\Database
*/
public function getDatabase()
{
return $this->getClient()->selectDatabase($this->dbname);
}
}
4 changes: 4 additions & 0 deletions Resources/config/services.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
services:
glavweb_mongodb:
class: Glavweb\MongoDBBundle\Registry
arguments: ["%mongodb.host%", "%mongodb.dbname%", "%mongodb.user%", "%mongodb.password%"]
22 changes: 22 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"name": "glavweb/mongodb-bundle",
"description": "Symfony GlavwebMongoDBBundle.",
"authors": [
{
"name": "Andrey Nilov",
"email": "[email protected]",
"homepage": "http://glavweb.ru"
},
{
"name": "Glavweb Community",
"homepage": "https://github.com/glavweb/GlavwebMongoDBBundle/contributors"
}
],
"require": {
"php": ">=7.1",
"symfony/framework-bundle": "~2.3|~3.0"
},
"autoload": {
"psr-4": { "Glavweb\\MongoDBBundle\\": "" }
}
}

0 comments on commit 6ad6505

Please sign in to comment.