Skip to content

Commit

Permalink
add mongodb odm
Browse files Browse the repository at this point in the history
  • Loading branch information
dominikzogg committed Oct 2, 2020
1 parent da8d193 commit 6f11f65
Show file tree
Hide file tree
Showing 28 changed files with 1,118 additions and 2 deletions.
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ dist: bionic
language: php

services:
- mongodb
- mysql

addons:
Expand Down Expand Up @@ -33,6 +34,7 @@ before_script:
- (phpenv config-rm xdebug.ini || exit 0)
- pecl install pcov
- echo 'Europe/Zurich' | sudo tee /etc/timezone
- echo 'extension=mongodb.so' >> ~/.phpenv/versions/$(phpenv version-name)/etc/php.ini
- echo 'date.timezone = "Europe/Zurich"' >> ~/.phpenv/versions/$(phpenv version-name)/etc/conf.d/travis.ini
- composer self-update -q
- composer global require hirak/prestissimo
Expand Down
7 changes: 5 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,10 @@
"chubbyphp/chubbyphp-mock": "^1.4.5",
"doctrine/dbal": "^2.10.3",
"doctrine/orm": "^2.7.3",
"doctrine/mongodb-odm": "^2.1.2",
"infection/infection": "^0.15.3|^0.16.2",
"mavimo/phpstan-junit": "^0.3",
"mongodb/mongodb": "^1.6",
"mongodb/mongodb": "^1.5.2",
"php-coveralls/php-coveralls": "^2.2",
"phploc/phploc": "^5.0|^6.0.2",
"phpstan/extension-installer": "^1.0.4",
Expand All @@ -43,7 +44,9 @@
},
"conflict": {
"doctrine/dbal": "<2.10.3,>=3.0",
"doctrine/orm": "<2.7.3,>=3.0"
"doctrine/mongodb-odm": "<2.1.2,>=3.0",
"doctrine/orm": "<2.7.3,>=3.0",
"mongodb/mongodb": "<1.5.2,>=2.0"
},
"autoload": {
"psr-4": { "Chubbyphp\\Laminas\\Config\\Doctrine\\": "src/" }
Expand Down
65 changes: 65 additions & 0 deletions src/ODM/MongoDB/Tools/Console/Command/DocumentManagerCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

declare(strict_types=1);

namespace Chubbyphp\Laminas\Config\Doctrine\ODM\MongoDB\Tools\Console\Command;

use Doctrine\ODM\MongoDB\DocumentManager;
use Doctrine\ODM\MongoDB\Tools\Console\Helper\DocumentManagerHelper;
use Psr\Container\ContainerInterface;
use Psr\Container\NotFoundExceptionInterface;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Helper\HelperSet;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

final class DocumentManagerCommand extends Command
{
/**
* @var Command
*/
private $command;

/**
* @var ContainerInterface
*/
private $container;

public function __construct(Command $command, ContainerInterface $container)
{
$this->command = $command;
$this->container = $container;

parent::__construct();
}

protected function configure(): void
{
$this->setName($this->command->getName());
$this->setAliases($this->command->getAliases());
$this->setDescription($this->command->getDescription());
$this->setHelp($this->command->getHelp());
$this->setDefinition($this->command->getDefinition());

$this->addOption('dm', null, InputOption::VALUE_OPTIONAL, 'The document manager to use for this command');
}

protected function execute(InputInterface $input, OutputInterface $output): int
{
/** @var string $documentManagerName */
$documentManagerName = $input->getOption('dm');

try {
$documentManager = $this->container->get(DocumentManager::class.$documentManagerName);
} catch (NotFoundExceptionInterface $serviceNotFoundException) {
throw new \InvalidArgumentException(sprintf('Missing document manager with name "%s"', $documentManagerName));
}

$this->command->setHelperSet(new HelperSet([
'dm' => new DocumentManagerHelper($documentManager),
]));

return (int) $this->command->execute($input, $output);
}
}
38 changes: 38 additions & 0 deletions src/ServiceFactory/ODM/MongoDB/ConfigurationFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

declare(strict_types=1);

namespace Chubbyphp\Laminas\Config\Doctrine\ServiceFactory\ODM\MongoDB;

use Chubbyphp\Laminas\Config\Factory\AbstractFactory;
use Doctrine\ODM\MongoDB\Configuration;
use Psr\Container\ContainerInterface;

final class ConfigurationFactory extends AbstractFactory
{
public function __invoke(ContainerInterface $container): Configuration
{
$config = $this->resolveConfig($container->get('config')['doctrine']['mongodbOdm']['configuration'] ?? []);

$configuration = new Configuration();

$this->callAdders($container, $configuration, $config);
$this->callSetters($container, $configuration, $config);

return $configuration;
}

/**
* @param array<string, mixed> $config
*/
private function callAdders(ContainerInterface $container, Configuration $configuration, array &$config): void
{
$filters = $this->resolveValue($container, $config['filters'] ?? []);

unset($config['filters']);

foreach ($filters as $filter) {
$configuration->addFilter($filter['name'], $filter['className'], $filter['parameters']);
}
}
}
31 changes: 31 additions & 0 deletions src/ServiceFactory/ODM/MongoDB/DocumentManagerFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

declare(strict_types=1);

namespace Chubbyphp\Laminas\Config\Doctrine\ServiceFactory\ODM\MongoDB;

use Chubbyphp\Laminas\Config\Doctrine\ServiceFactory\Common\EventManagerFactory;
use Chubbyphp\Laminas\Config\Doctrine\ServiceFactory\MongoDB\ClientFactory;
use Chubbyphp\Laminas\Config\Factory\AbstractFactory;
use Doctrine\Common\EventManager;
use Doctrine\ODM\MongoDB\Configuration;
use Doctrine\ODM\MongoDB\DocumentManager;
use MongoDB\Client;
use Psr\Container\ContainerInterface;

final class DocumentManagerFactory extends AbstractFactory
{
public function __invoke(ContainerInterface $container): DocumentManager
{
/** @var Client $client */
$client = $this->resolveDependency($container, Client::class, ClientFactory::class);

/** @var Configuration $configuration */
$configuration = $this->resolveDependency($container, Configuration::class, ConfigurationFactory::class);

/** @var EventManager $eventManager */
$eventManager = $this->resolveDependency($container, EventManager::class, EventManagerFactory::class);

return DocumentManager::create($client, $configuration, $eventManager);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

namespace Chubbyphp\Laminas\Config\Doctrine\ServiceFactory\ODM\MongoDB\Tools\Console\Command\ClearCache;

use Chubbyphp\Laminas\Config\Doctrine\ODM\MongoDB\Tools\Console\Command\DocumentManagerCommand;
use Doctrine\ODM\MongoDB\Tools\Console\Command\ClearCache\MetadataCommand;
use Psr\Container\ContainerInterface;

final class MetadataCommandFactory
{
public function __invoke(ContainerInterface $container): DocumentManagerCommand
{
return new DocumentManagerCommand(new MetadataCommand(), $container);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

namespace Chubbyphp\Laminas\Config\Doctrine\ServiceFactory\ODM\MongoDB\Tools\Console\Command;

use Chubbyphp\Laminas\Config\Doctrine\ODM\MongoDB\Tools\Console\Command\DocumentManagerCommand;
use Doctrine\ODM\MongoDB\Tools\Console\Command\GenerateHydratorsCommand;
use Psr\Container\ContainerInterface;

final class GenerateHydratorsCommandFactory
{
public function __invoke(ContainerInterface $container): DocumentManagerCommand
{
return new DocumentManagerCommand(new GenerateHydratorsCommand(), $container);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

namespace Chubbyphp\Laminas\Config\Doctrine\ServiceFactory\ODM\MongoDB\Tools\Console\Command;

use Chubbyphp\Laminas\Config\Doctrine\ODM\MongoDB\Tools\Console\Command\DocumentManagerCommand;
use Doctrine\ODM\MongoDB\Tools\Console\Command\GeneratePersistentCollectionsCommand;
use Psr\Container\ContainerInterface;

final class GeneratePersistentCollectionsCommandFactory
{
public function __invoke(ContainerInterface $container): DocumentManagerCommand
{
return new DocumentManagerCommand(new GeneratePersistentCollectionsCommand(), $container);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

namespace Chubbyphp\Laminas\Config\Doctrine\ServiceFactory\ODM\MongoDB\Tools\Console\Command;

use Chubbyphp\Laminas\Config\Doctrine\ODM\MongoDB\Tools\Console\Command\DocumentManagerCommand;
use Doctrine\ODM\MongoDB\Tools\Console\Command\GenerateProxiesCommand;
use Psr\Container\ContainerInterface;

final class GenerateProxiesCommandFactory
{
public function __invoke(ContainerInterface $container): DocumentManagerCommand
{
return new DocumentManagerCommand(new GenerateProxiesCommand(), $container);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

namespace Chubbyphp\Laminas\Config\Doctrine\ServiceFactory\ODM\MongoDB\Tools\Console\Command;

use Chubbyphp\Laminas\Config\Doctrine\ODM\MongoDB\Tools\Console\Command\DocumentManagerCommand;
use Doctrine\ODM\MongoDB\Tools\Console\Command\QueryCommand;
use Psr\Container\ContainerInterface;

final class QueryCommandFactory
{
public function __invoke(ContainerInterface $container): DocumentManagerCommand
{
return new DocumentManagerCommand(new QueryCommand(), $container);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

namespace Chubbyphp\Laminas\Config\Doctrine\ServiceFactory\ODM\MongoDB\Tools\Console\Command\Schema;

use Chubbyphp\Laminas\Config\Doctrine\ODM\MongoDB\Tools\Console\Command\DocumentManagerCommand;
use Doctrine\ODM\MongoDB\Tools\Console\Command\Schema\CreateCommand;
use Psr\Container\ContainerInterface;

final class CreateCommandFactory
{
public function __invoke(ContainerInterface $container): DocumentManagerCommand
{
return new DocumentManagerCommand(new CreateCommand(), $container);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

namespace Chubbyphp\Laminas\Config\Doctrine\ServiceFactory\ODM\MongoDB\Tools\Console\Command\Schema;

use Chubbyphp\Laminas\Config\Doctrine\ODM\MongoDB\Tools\Console\Command\DocumentManagerCommand;
use Doctrine\ODM\MongoDB\Tools\Console\Command\Schema\DropCommand;
use Psr\Container\ContainerInterface;

final class DropCommandFactory
{
public function __invoke(ContainerInterface $container): DocumentManagerCommand
{
return new DocumentManagerCommand(new DropCommand(), $container);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

namespace Chubbyphp\Laminas\Config\Doctrine\ServiceFactory\ODM\MongoDB\Tools\Console\Command\Schema;

use Chubbyphp\Laminas\Config\Doctrine\ODM\MongoDB\Tools\Console\Command\DocumentManagerCommand;
use Doctrine\ODM\MongoDB\Tools\Console\Command\Schema\ShardCommand;
use Psr\Container\ContainerInterface;

final class ShardCommandFactory
{
public function __invoke(ContainerInterface $container): DocumentManagerCommand
{
return new DocumentManagerCommand(new ShardCommand(), $container);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

namespace Chubbyphp\Laminas\Config\Doctrine\ServiceFactory\ODM\MongoDB\Tools\Console\Command\Schema;

use Chubbyphp\Laminas\Config\Doctrine\ODM\MongoDB\Tools\Console\Command\DocumentManagerCommand;
use Doctrine\ODM\MongoDB\Tools\Console\Command\Schema\UpdateCommand;
use Psr\Container\ContainerInterface;

final class UpdateCommandFactory
{
public function __invoke(ContainerInterface $container): DocumentManagerCommand
{
return new DocumentManagerCommand(new UpdateCommand(), $container);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

namespace Chubbyphp\Laminas\Config\Doctrine\ServiceFactory\ODM\MongoDB\Tools\Console\Command\Schema;

use Chubbyphp\Laminas\Config\Doctrine\ODM\MongoDB\Tools\Console\Command\DocumentManagerCommand;
use Doctrine\ODM\MongoDB\Tools\Console\Command\Schema\ValidateCommand;
use Psr\Container\ContainerInterface;

final class ValidateCommandFactory
{
public function __invoke(ContainerInterface $container): DocumentManagerCommand
{
return new DocumentManagerCommand(new ValidateCommand(), $container);
}
}
Loading

0 comments on commit 6f11f65

Please sign in to comment.