Skip to content

Commit

Permalink
allow only document manager on cli
Browse files Browse the repository at this point in the history
  • Loading branch information
juliangut committed Jul 27, 2016
1 parent 1c74fa1 commit 47c243b
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 52 deletions.
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
],
"require": {
"php": ">=5.5",
"slim/slim": "^3.0"
"slim/slim": "^3.0",
"doctrine/orm": "~2.4"
},
"require-dev": {
"phpunit/phpunit": "~4.5",
Expand All @@ -29,7 +30,6 @@
"doctrine/mongodb-odm": "^1.0"
},
"suggest": {
"doctrine/orm": "~2.4",
"doctrine/mongodb-odm": "^1.0"
},
"autoload": {
Expand Down
69 changes: 40 additions & 29 deletions src/CLIApplicationBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,14 @@

namespace Jgut\Slim\Doctrine;

use Doctrine\DBAL\Tools\Console\Helper\ConnectionHelper;
use Doctrine\ODM\MongoDB\DocumentManager;
use Doctrine\ODM\MongoDB\Tools\Console\Command\ClearCache\MetadataCommand;
use Doctrine\ODM\MongoDB\Tools\Console\Command\GenerateDocumentsCommand;
use Doctrine\ODM\MongoDB\Tools\Console\Command\GenerateHydratorsCommand;
use Doctrine\ODM\MongoDB\Tools\Console\Command\GenerateProxiesCommand;
use Doctrine\ODM\MongoDB\Tools\Console\Command\GenerateRepositoriesCommand;
use Doctrine\ODM\MongoDB\Tools\Console\Command\QueryCommand;
use Doctrine\ODM\MongoDB\Tools\Console\Command\Schema\CreateCommand;
use Doctrine\ODM\MongoDB\Tools\Console\Command\Schema\DropCommand;
use Doctrine\ODM\MongoDB\Tools\Console\Command\Schema\UpdateCommand;
use Doctrine\ODM\MongoDB\Tools\Console\Command as ODMCommand;
use Doctrine\ODM\MongoDB\Tools\Console\Helper\DocumentManagerHelper;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\Tools\Console\ConsoleRunner;
use Doctrine\ORM\Tools\Console\Helper\EntityManagerHelper;
use Symfony\Component\Console\Helper\HelperSet;

/**
* Doctrine CLI application builder.
Expand All @@ -31,8 +26,8 @@ class CLIApplicationBuilder
/**
* Create a Doctrine CLI application.
*
* @param array|\Doctrine\ORM\EntityManager $entityManager
* @param array|\Doctrine\ODM\MongoDB\DocumentManager|null $documentManager
* @param array|EntityManager|null $entityManager
* @param array|DocumentManager|null $documentManager
*
* @throws \Doctrine\DBAL\DBALException
* @throws \Doctrine\ORM\ORMException
Expand All @@ -42,29 +37,45 @@ class CLIApplicationBuilder
*
* @return \Symfony\Component\Console\Application
*/
public static function build($entityManager, $documentManager = null)
public static function build($entityManager = null, $documentManager = null)
{
$entityManager = self::getEntityManager($entityManager);
if ($entityManager === null && $documentManager === null) {
throw new \InvalidArgumentException('At least one of EntityManager or DocumentManager must be provided');
}

$helperSet = ConsoleRunner::createHelperSet($entityManager);
$application = ConsoleRunner::createApplication($helperSet);
if ($entityManager !== null) {
$entityManager = static::getEntityManager($entityManager);
}

if ($documentManager !== null) {
$documentManager = self::getDocumentManager($documentManager);
$documentManager = static::getDocumentManager($documentManager);
}

$helperSet = new HelperSet;

if ($entityManager instanceof EntityManager) {
$helperSet->set(new ConnectionHelper($entityManager->getConnection()), 'db');
$helperSet->set(new EntityManagerHelper($entityManager), 'em');
}

if ($documentManager instanceof DocumentManager) {
$helperSet->set(new DocumentManagerHelper($documentManager), 'dm');
}

$application = ConsoleRunner::createApplication($helperSet);

if ($documentManager instanceof DocumentManager) {
$application->addCommands(
[
new GenerateDocumentsCommand,
new GenerateHydratorsCommand,
new GenerateProxiesCommand,
new GenerateRepositoriesCommand,
new QueryCommand,
new MetadataCommand,
new CreateCommand,
new DropCommand,
new UpdateCommand,
new ODMCommand\GenerateDocumentsCommand,
new ODMCommand\GenerateHydratorsCommand,
new ODMCommand\GenerateProxiesCommand,
new ODMCommand\GenerateRepositoriesCommand,
new ODMCommand\QueryCommand,
new ODMCommand\ClearCache\MetadataCommand,
new ODMCommand\Schema\CreateCommand,
new ODMCommand\Schema\DropCommand,
new ODMCommand\Schema\UpdateCommand,
]
);
}
Expand All @@ -75,14 +86,14 @@ public static function build($entityManager, $documentManager = null)
/**
* Retrieve entity manager.
*
* @param array|\Doctrine\ORM\EntityManager $entityManager
* @param array|EntityManager $entityManager
*
* @throws \Doctrine\DBAL\DBALException
* @throws \Doctrine\ORM\ORMException
* @throws \InvalidArgumentException
* @throws \RuntimeException
*
* @return \Doctrine\ORM\EntityManager
* @return EntityManager
*/
protected static function getEntityManager($entityManager)
{
Expand All @@ -100,12 +111,12 @@ protected static function getEntityManager($entityManager)
/**
* Retrieve document manager.
*
* @param array|\Doctrine\ODM\MongoDB\DocumentManager $documentManager
* @param array|DocumentManager $documentManager
*
* @throws \InvalidArgumentException
* @throws \RuntimeException
*
* @return \Doctrine\ODM\MongoDB\DocumentManager
* @return DocumentManager
*/
protected static function getDocumentManager($documentManager)
{
Expand Down
36 changes: 15 additions & 21 deletions tests/Doctrine/CLIApplicationBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,20 @@ class CLIApplicationBuilderTest extends \PHPUnit_Framework_TestCase
/**
* @expectedException \InvalidArgumentException
*/
public function testNoEntityManager()
public function testNoManagers()
{
CLIApplicationBuilder::build();
}

/**
* @expectedException \InvalidArgumentException
*/
public function testBadEntityManagers()
{
CLIApplicationBuilder::build('');
}

public function testOnlyEntityManager()
public function testEntityManager()
{
$entityOptions = [
'connection' => [
Expand All @@ -37,41 +45,27 @@ public function testOnlyEntityManager()
$application = CLIApplicationBuilder::build($entityOptions);

self::assertInstanceOf('Symfony\Component\Console\Application', $application);
self::assertTrue($application->has('orm:schema-tool:create'));
}

/**
* @expectedException \InvalidArgumentException
*/
public function testNoDocumentManager()
public function testBadDocumentManagers()
{
$entityOptions = [
'connection' => [
'driver' => 'pdo_sqlite',
'memory' => true,
],
'annotation_paths' => sys_get_temp_dir(),
];

CLIApplicationBuilder::build($entityOptions, '');
CLIApplicationBuilder::build(null, '');
}

public function testBothManagers()
public function testDocumentManagers()
{
$entityOptions = [
'connection' => [
'driver' => 'pdo_sqlite',
'memory' => true,
],
'annotation_paths' => sys_get_temp_dir(),
];
$documentOptions = [
'connection' => [
'server' => 'mongodb://localhost:27017',
],
'annotation_paths' => sys_get_temp_dir(),
];

$application = CLIApplicationBuilder::build($entityOptions, $documentOptions);
$application = CLIApplicationBuilder::build(null, $documentOptions);

self::assertInstanceOf('Symfony\Component\Console\Application', $application);
self::assertTrue($application->has('odm:generate:documents'));
Expand Down

0 comments on commit 47c243b

Please sign in to comment.