-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 6ad6505
Showing
9 changed files
with
272 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
/.idea | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
{} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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%" | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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%"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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\\": "" } | ||
} | ||
} |