Skip to content

Commit

Permalink
refactor console commands to delegates
Browse files Browse the repository at this point in the history
  • Loading branch information
fprochazka committed May 27, 2017
1 parent b6944c3 commit 5ce1e1b
Show file tree
Hide file tree
Showing 15 changed files with 364 additions and 377 deletions.
9 changes: 8 additions & 1 deletion src/Kdyby/Doctrine/Console/CommandHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,17 @@ public static function setApplicationEntityManager(ContainerHelper $containerHel
{
/** @var \Kdyby\Doctrine\EntityManager $em */
$em = $containerHelper->getByType('Kdyby\Doctrine\Registry')->getManager($emName);
/** @var \Symfony\Component\Console\Helper\HelperSet $helperSet */
$helperSet = $containerHelper->getHelperSet();
$helperSet->set(new ConnectionHelper($em->getConnection()), 'db');
$helperSet->set(new EntityManagerHelper($em), 'em');
}

public static function setApplicationConnection(ContainerHelper $containerHelper, $connName)
{
/** @var \Kdyby\Doctrine\EntityManager $db */
$connection = $containerHelper->getByType('Kdyby\Doctrine\Registry')->getConnection($connName);
$helperSet = $containerHelper->getHelperSet();
$helperSet->set(new ConnectionHelper($connection), 'db');
}

}
85 changes: 85 additions & 0 deletions src/Kdyby/Doctrine/Console/DbalDelegateCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?php

/**
* This file is part of the Kdyby (http://www.kdyby.org)
*
* Copyright (c) 2008 Filip Procházka ([email protected])
*
* For the full copyright and license information, please view the file license.txt that was distributed with this source code.
*/

namespace Kdyby\Doctrine\Console;

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

/**
* Command Delegate.
*
* @author Fabio B. Silva <[email protected]>
* @author Filip Procházka <[email protected]>
*/
abstract class DbalDelegateCommand extends Command
{
/**
* @var \Symfony\Component\Console\Command\Command
*/
protected $command;

/**
* @return \Symfony\Component\Console\Command\Command
*/
abstract protected function createCommand();

/**
* @param string $connectionName
* @return \Symfony\Component\Console\Command\Command
*/
protected function wrapCommand($connectionName)
{
CommandHelper::setApplicationConnection($this->getHelper('container'), $connectionName);
$this->command->setApplication($this->getApplication());
return $this->command;
}

/**
* {@inheritDoc}
*/
protected function configure()
{
$this->command = $this->createCommand();

$this->setName($this->command->getName());
$this->setHelp($this->command->getHelp());
$this->setDefinition($this->command->getDefinition());
$this->setDescription($this->command->getDescription());

$this->addOption('connection', NULL, InputOption::VALUE_OPTIONAL, 'The connection to use for this command');
}

/**
* {@inheritDoc}
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
return $this->wrapCommand($input->getOption('connection'))->execute($input, $output);
}

/**
* {@inheritDoc}
*/
protected function interact(InputInterface $input, OutputInterface $output)
{
$this->wrapCommand($input->getOption('connection'))->interact($input, $output);
}

/**
* {@inheritDoc}
*/
protected function initialize(InputInterface $input, OutputInterface $output)
{
$this->wrapCommand($input->getOption('connection'))->initialize($input, $output);
}
}
85 changes: 85 additions & 0 deletions src/Kdyby/Doctrine/Console/OrmDelegateCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?php

/**
* This file is part of the Kdyby (http://www.kdyby.org)
*
* Copyright (c) 2008 Filip Procházka ([email protected])
*
* For the full copyright and license information, please view the file license.txt that was distributed with this source code.
*/

namespace Kdyby\Doctrine\Console;

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

/**
* Command Delegate.
*
* @author Fabio B. Silva <[email protected]>
* @author Filip Procházka <[email protected]>
*/
abstract class OrmDelegateCommand extends Command
{
/**
* @var \Symfony\Component\Console\Command\Command
*/
protected $command;

/**
* @return \Symfony\Component\Console\Command\Command
*/
abstract protected function createCommand();

/**
* @param string $entityManagerName
* @return \Symfony\Component\Console\Command\Command
*/
protected function wrapCommand($entityManagerName)
{
CommandHelper::setApplicationEntityManager($this->getHelper('container'), $entityManagerName);
$this->command->setApplication($this->getApplication());
return $this->command;
}

/**
* {@inheritDoc}
*/
protected function configure()
{
$this->command = $this->createCommand();

$this->setName($this->command->getName());
$this->setHelp($this->command->getHelp());
$this->setDefinition($this->command->getDefinition());
$this->setDescription($this->command->getDescription());

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

/**
* {@inheritDoc}
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
return $this->wrapCommand($input->getOption('em'))->execute($input, $output);
}

/**
* {@inheritDoc}
*/
protected function interact(InputInterface $input, OutputInterface $output)
{
$this->wrapCommand($input->getOption('em'))->interact($input, $output);
}

/**
* {@inheritDoc}
*/
protected function initialize(InputInterface $input, OutputInterface $output)
{
$this->wrapCommand($input->getOption('em'))->initialize($input, $output);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,16 @@
* For the full copyright and license information, please view the file license.txt that was distributed with this source code.
*/

namespace Kdyby\Doctrine\Console;
namespace Kdyby\Doctrine\Console\Proxy;

use Doctrine;
use Kdyby\Doctrine\Console\OrmDelegateCommand;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;



/**
* @author Tomas Jacik <[email protected]>
*/
class ConvertMappingCommand extends Doctrine\ORM\Tools\Console\Command\ConvertMappingCommand
class ConvertMappingCommand extends OrmDelegateCommand
{

/**
Expand All @@ -29,35 +26,21 @@ class ConvertMappingCommand extends Doctrine\ORM\Tools\Console\Command\ConvertMa
*/
public $cacheCleaner;



public function __construct()
{
parent::__construct();
}



/**
* {@inheritDoc}
*/
protected function configure()
{
parent::configure();
$this->addOption('em', NULL, InputOption::VALUE_OPTIONAL, 'The entity manager to use for this command');
}



protected function initialize(InputInterface $input, OutputInterface $output)
{
parent::initialize($input, $output);

if ($input->getOption('em')) {
CommandHelper::setApplicationEntityManager($this->getHelper('container'), $input->getOption('em'));
}

$this->cacheCleaner->invalidate();
}

protected function createCommand()
{
return new \Doctrine\ORM\Tools\Console\Command\ConvertMappingCommand();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,16 @@
* For the full copyright and license information, please view the file license.txt that was distributed with this source code.
*/

namespace Kdyby\Doctrine\Console;
namespace Kdyby\Doctrine\Console\Proxy;

use Doctrine;
use Kdyby\Doctrine\Console\OrmDelegateCommand;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;



/**
* @author Tomas Jacik <[email protected]>
*/
class GenerateEntitiesCommand extends Doctrine\ORM\Tools\Console\Command\GenerateEntitiesCommand
class GenerateEntitiesCommand extends OrmDelegateCommand
{

/**
Expand All @@ -29,38 +26,21 @@ class GenerateEntitiesCommand extends Doctrine\ORM\Tools\Console\Command\Generat
*/
public $cacheCleaner;



public function __construct()
{
parent::__construct();
}



/**
* {@inheritDoc}
*/
protected function configure()
{
parent::configure();
$this->addOption('em', NULL, InputOption::VALUE_OPTIONAL, 'The entity manager to use for this command');
}



/**
* {@inheritDoc}
*/
protected function initialize(InputInterface $input, OutputInterface $output)
{
parent::initialize($input, $output);

if ($input->getOption('em')) {
CommandHelper::setApplicationEntityManager($this->getHelper('container'), $input->getOption('em'));
}

$this->cacheCleaner->invalidate();
}

protected function createCommand()
{
return new \Doctrine\ORM\Tools\Console\Command\GenerateEntitiesCommand();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,16 @@
* For the full copyright and license information, please view the file license.txt that was distributed with this source code.
*/

namespace Kdyby\Doctrine\Console;
namespace Kdyby\Doctrine\Console\Proxy;

use Doctrine;
use Kdyby\Doctrine\Console\OrmDelegateCommand;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;



/**
* @author Filip Procházka <[email protected]>
*/
class GenerateProxiesCommand extends Doctrine\ORM\Tools\Console\Command\GenerateProxiesCommand
class GenerateProxiesCommand extends OrmDelegateCommand
{

/**
Expand All @@ -29,38 +26,21 @@ class GenerateProxiesCommand extends Doctrine\ORM\Tools\Console\Command\Generate
*/
public $cacheCleaner;



public function __construct()
{
parent::__construct();
}



/**
* {@inheritDoc}
*/
protected function configure()
{
parent::configure();
$this->addOption('em', NULL, InputOption::VALUE_OPTIONAL, 'The entity manager to use for this command');
}



/**
* {@inheritDoc}
*/
protected function initialize(InputInterface $input, OutputInterface $output)
{
parent::initialize($input, $output);

if ($input->getOption('em')) {
CommandHelper::setApplicationEntityManager($this->getHelper('container'), $input->getOption('em'));
}

$this->cacheCleaner->invalidate();
}

protected function createCommand()
{
return new \Doctrine\ORM\Tools\Console\Command\GenerateProxiesCommand();
}

}
Loading

0 comments on commit 5ce1e1b

Please sign in to comment.