├── .gitignore ├── src └── Dflydev │ └── Pimple │ └── Provider │ └── DoctrineCommands │ ├── Adapter │ ├── Cilex │ │ └── DoctrineCommandsServiceProvider.php │ └── Silex │ │ └── DoctrineCommandsServiceProvider.php │ ├── DoctrineCommandsServiceProvider.php │ └── Command │ ├── Command.php │ ├── Proxy │ ├── CommandHelper.php │ └── UpdateSchemaCommand.php │ ├── CreateDatabaseCommand.php │ └── DropDatabaseCommand.php └── composer.json /.gitignore: -------------------------------------------------------------------------------- 1 | /vendor/ 2 | composer.lock 3 | -------------------------------------------------------------------------------- /src/Dflydev/Pimple/Provider/DoctrineCommands/Adapter/Cilex/DoctrineCommandsServiceProvider.php: -------------------------------------------------------------------------------- 1 | 22 | */ 23 | class DoctrineCommandsServiceProvider implements ServiceProviderInterface 24 | { 25 | /** 26 | * {@inheritdoc} 27 | */ 28 | public function register(Application $app) 29 | { 30 | $serviceProvider = new BaseDoctrineCommandsServiceProvider; 31 | $serviceProvider->register($app); 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /src/Dflydev/Pimple/Provider/DoctrineCommands/Adapter/Silex/DoctrineCommandsServiceProvider.php: -------------------------------------------------------------------------------- 1 | 22 | */ 23 | class DoctrineCommandsServiceProvider implements ServiceProviderInterface 24 | { 25 | /** 26 | * {@inheritdoc} 27 | */ 28 | public function boot(Application $app) 29 | { 30 | } 31 | 32 | /** 33 | * {@inheritdoc} 34 | */ 35 | public function register(Application $app) 36 | { 37 | $serviceProvider = new BaseDoctrineCommandsServiceProvider; 38 | $serviceProvider->register($app); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "dflydev/doctrine-commands-service-provider", 3 | "description": "Doctrine Commands Service Provider", 4 | "keywords": ["silex", "doctrine", "commands", "console", "orm"], 5 | "license": "MIT", 6 | "authors": [ 7 | { 8 | "name": "Dragonfly Development Inc.", 9 | "email": "info@dflydev.com", 10 | "homepage": "http://dflydev.com" 11 | }, 12 | { 13 | "name": "Beau Simensen", 14 | "email": "beau@dflydev.com", 15 | "homepage": "http://beausimensen.com" 16 | } 17 | ], 18 | "require": { 19 | "php": ">=5.3.3", 20 | "doctrine/dbal": "~2.3", 21 | "pimple/pimple": "1.*@dev" 22 | }, 23 | "suggest": { 24 | "cilex/console-service-provider": "1.*", 25 | "dflydev/doctrine-orm-service-provider": "1.*", 26 | "doctrine/migrations": "*", 27 | "doctrine/orm": "~2.3" 28 | }, 29 | "autoload": { 30 | "psr-0": { 31 | "Dflydev\\Pimple\\Provider\\DoctrineCommands": "src" 32 | } 33 | }, 34 | "extra": { 35 | "branch-alias": { 36 | "dev-master": "1.0-dev" 37 | } 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /src/Dflydev/Pimple/Provider/DoctrineCommands/DoctrineCommandsServiceProvider.php: -------------------------------------------------------------------------------- 1 | 20 | */ 21 | class DoctrineCommandsServiceProvider 22 | { 23 | /** 24 | * {@inheritdoc} 25 | */ 26 | public function register(Pimple $c) 27 | { 28 | $c['console'] = $c->share($c->extend('console', function($console, $c) { 29 | foreach (array( 30 | new Command\CreateDatabaseCommand, 31 | new Command\DropDatabaseCommand, 32 | ) as $command) { 33 | $console->add($command); 34 | } 35 | 36 | if (class_exists('Doctrine\ORM\Tools\Console\Command\SchemaTool\UpdateCommand')) { 37 | foreach (array( 38 | new Command\Proxy\UpdateSchemaCommand, 39 | ) as $command) { 40 | $console->add($command); 41 | } 42 | } 43 | 44 | return $console; 45 | })); 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /src/Dflydev/Pimple/Provider/DoctrineCommands/Command/Command.php: -------------------------------------------------------------------------------- 1 | 9 | * (c) Doctrine Project, Benjamin Eberlei 10 | * (c) Dragonfly Development, Inc. 11 | * 12 | * For the full copyright and license information, please view the LICENSE 13 | * file that was distributed with this source code. 14 | */ 15 | 16 | namespace Dflydev\Pimple\Provider\DoctrineCommands\Command; 17 | 18 | use Symfony\Component\Console\Command\Command as BaseCommand; 19 | 20 | /** 21 | * Base class for Doctrine console commands to extend from. 22 | * 23 | * @author Fabien Potencier 24 | * @author Beau Simensen 25 | */ 26 | abstract class Command extends BaseCommand 27 | { 28 | protected $app; 29 | 30 | /** 31 | * Get a Doctrine Entity Manager by name. 32 | * 33 | * @param string $name 34 | * 35 | * @return \Doctrine\ORM\EntityManager 36 | */ 37 | protected function getEntityManager($name = null) 38 | { 39 | $c = $this->getApplication()->getContainer(); 40 | 41 | if (null === $name) { 42 | return $c['orm.em']; 43 | } 44 | 45 | return $c['orm.ems'][$name]; 46 | } 47 | 48 | /** 49 | * Get a Doctrine DBAL connection by name. 50 | * 51 | * @param string $name 52 | * 53 | * @return \Doctrine\DBAL\Connection 54 | */ 55 | protected function getDoctrineConnection($name = null) 56 | { 57 | $c = $this->getApplication()->getContainer(); 58 | 59 | if (null === $name) { 60 | return $c['db']; 61 | } 62 | 63 | return $c['dbs'][$name]; 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /src/Dflydev/Pimple/Provider/DoctrineCommands/Command/Proxy/CommandHelper.php: -------------------------------------------------------------------------------- 1 | 9 | * (c) Doctrine Project, Benjamin Eberlei 10 | * (c) Dragonfly Development, Inc. 11 | * 12 | * For the full copyright and license information, please view the LICENSE 13 | * file that was distributed with this source code. 14 | */ 15 | 16 | namespace Dflydev\Pimple\Provider\DoctrineCommands\Command\Proxy; 17 | 18 | use Doctrine\DBAL\Tools\Console\Helper\ConnectionHelper; 19 | use Doctrine\ORM\Tools\Console\Helper\EntityManagerHelper; 20 | 21 | /** 22 | * Provides some helper and convenience methods to configure doctrine commands 23 | * in the context of silex and multiple connections/entity managers. 24 | * 25 | * @author Fabien Potencier 26 | * @author Beau Simensen 27 | */ 28 | abstract class CommandHelper 29 | { 30 | /** 31 | * Convenience method to push the helper sets of a given entity manager into the application. 32 | * 33 | * @param Application $application Application 34 | * @param string $emName Entity Manager name 35 | */ 36 | static public function setApplicationEntityManager($application, $emName) 37 | { 38 | $c = $application->getContainer(); 39 | 40 | if ($emName) { 41 | $em = $c['orm.ems'][$emName]; 42 | } else { 43 | $em = $c['orm.em']; 44 | } 45 | 46 | $helperSet = $application->getHelperSet(); 47 | $helperSet->set(new ConnectionHelper($em->getConnection()), 'db'); 48 | $helperSet->set(new EntityManagerHelper($em), 'em'); 49 | } 50 | 51 | /** 52 | * Convenience method to push the helper sets of a given connection into the application. 53 | * 54 | * @param Application $application Application 55 | * @param string $connName Connection name 56 | */ 57 | static public function setApplicationConnection($application, $connName) 58 | { 59 | $c = $application->getContainer(); 60 | 61 | if ($emName) { 62 | $em = $c['orm.ems'][$emName]; 63 | } else { 64 | $em = $c['orm.em']; 65 | } 66 | 67 | $helperSet = $application->getHelperSet(); 68 | $helperSet->set(new ConnectionHelper($connection), 'db'); 69 | } 70 | } 71 | -------------------------------------------------------------------------------- /src/Dflydev/Pimple/Provider/DoctrineCommands/Command/Proxy/UpdateSchemaCommand.php: -------------------------------------------------------------------------------- 1 | 9 | * (c) Doctrine Project, Benjamin Eberlei 10 | * (c) Dragonfly Development, Inc. 11 | * 12 | * For the full copyright and license information, please view the LICENSE 13 | * file that was distributed with this source code. 14 | */ 15 | 16 | namespace Dflydev\Pimple\Provider\DoctrineCommands\Command\Proxy; 17 | 18 | use Symfony\Component\Console\Input\InputOption; 19 | use Symfony\Component\Console\Input\InputInterface; 20 | use Symfony\Component\Console\Output\OutputInterface; 21 | use Symfony\Component\Console\Output\Output; 22 | use Doctrine\ORM\Tools\Console\Command\SchemaTool\UpdateCommand; 23 | 24 | /** 25 | * Command to generate the SQL needed to update the database schema to match 26 | * the current mapping information. 27 | * 28 | * @author Fabien Potencier 29 | * @author Jonathan H. Wage 30 | * @author Beau Simensen 31 | */ 32 | class UpdateSchemaCommand extends UpdateCommand 33 | { 34 | /** 35 | * {@inheritdoc} 36 | */ 37 | protected function configure() 38 | { 39 | parent::configure(); 40 | 41 | $this 42 | ->setName('doctrine:schema:update') 43 | ->setDescription('Executes (or dumps) the SQL needed to update the database schema to match the current mapping metadata') 44 | ->addOption('em', null, InputOption::VALUE_OPTIONAL, 'The entity manager to use for this command') 45 | ->setHelp(<<doctrine:schema:update command generates the SQL needed to 47 | synchronize the database schema with the current mapping metadata of the 48 | default entity manager. 49 | 50 | For example, if you add metadata for a new column to an entity, this command 51 | would generate and output the SQL needed to add the new column to the database: 52 | 53 | php app/console doctrine:schema:update --dump-sql 54 | 55 | Alternatively, you can execute the generated queries: 56 | 57 | php app/console doctrine:schema:update --force 58 | 59 | You can also update the database schema for a specific entity manager: 60 | 61 | php app/console doctrine:schema:update --em=default 62 | EOT 63 | ); 64 | } 65 | 66 | 67 | /** 68 | * {@inheritdoc} 69 | */ 70 | protected function execute(InputInterface $input, OutputInterface $output) 71 | { 72 | CommandHelper::setApplicationEntityManager($this->getApplication(), $input->getOption('em')); 73 | 74 | parent::execute($input, $output); 75 | } 76 | } 77 | -------------------------------------------------------------------------------- /src/Dflydev/Pimple/Provider/DoctrineCommands/Command/CreateDatabaseCommand.php: -------------------------------------------------------------------------------- 1 | 9 | * (c) Doctrine Project, Benjamin Eberlei 10 | * (c) Dragonfly Development, Inc. 11 | * 12 | * For the full copyright and license information, please view the LICENSE 13 | * file that was distributed with this source code. 14 | */ 15 | 16 | namespace Dflydev\Pimple\Provider\DoctrineCommands\Command; 17 | 18 | use Doctrine\DBAL\DriverManager; 19 | use Symfony\Component\Console\Input\InputInterface; 20 | use Symfony\Component\Console\Input\InputOption; 21 | use Symfony\Component\Console\Output\OutputInterface; 22 | 23 | /** 24 | * Database tool allows you to easily drop and create your configured databases. 25 | * 26 | * @author Fabien Potencier 27 | * @author Jonathan H. Wage 28 | * @author Beau Simensen 29 | */ 30 | class CreateDatabaseCommand extends Command 31 | { 32 | /** 33 | * {@inheritdoc} 34 | */ 35 | protected function configure() 36 | { 37 | $this 38 | ->setName('doctrine:database:create') 39 | ->setDescription('Creates the configured databases') 40 | ->addOption('connection', null, InputOption::VALUE_OPTIONAL, 'The connection to use for this command') 41 | ->setHelp(<<doctrine:database:create command creates the default 43 | connections database: 44 | 45 | console doctrine:database:create 46 | 47 | You can also optionally specify the name of a connection to create the 48 | database for: 49 | 50 | console doctrine:database:create --connection=default 51 | EOT 52 | ); 53 | } 54 | 55 | /** 56 | * {@inheritdoc} 57 | */ 58 | protected function execute(InputInterface $input, OutputInterface $output) 59 | { 60 | $connection = $this->getDoctrineConnection($input->getOption('connection')); 61 | 62 | $params = $connection->getParams(); 63 | $name = isset($params['path']) ? $params['path'] : $params['dbname']; 64 | 65 | unset($params['dbname']); 66 | 67 | $tmpConnection = DriverManager::getConnection($params); 68 | 69 | // Only quote if we don't have a path 70 | if (!isset($params['path'])) { 71 | $name = $tmpConnection->getDatabasePlatform()->quoteSingleIdentifier($name); 72 | } 73 | 74 | $error = false; 75 | try { 76 | $tmpConnection->getSchemaManager()->createDatabase($name); 77 | $output->writeln(sprintf('Created database for connection named %s', $name)); 78 | } catch (\Exception $e) { 79 | $output->writeln(sprintf('Could not create database for connection named %s', $name)); 80 | $output->writeln(sprintf('%s', $e->getMessage())); 81 | $error = true; 82 | } 83 | 84 | $tmpConnection->close(); 85 | 86 | return $error ? 1 : 0; 87 | } 88 | } 89 | -------------------------------------------------------------------------------- /src/Dflydev/Pimple/Provider/DoctrineCommands/Command/DropDatabaseCommand.php: -------------------------------------------------------------------------------- 1 | 9 | * (c) Doctrine Project, Benjamin Eberlei 10 | * (c) Dragonfly Development, Inc. 11 | * 12 | * For the full copyright and license information, please view the LICENSE 13 | * file that was distributed with this source code. 14 | */ 15 | 16 | namespace Dflydev\Pimple\Provider\DoctrineCommands\Command; 17 | 18 | use Doctrine\DBAL\DriverManager; 19 | use Symfony\Component\Console\Input\InputInterface; 20 | use Symfony\Component\Console\Input\InputOption; 21 | use Symfony\Component\Console\Output\OutputInterface; 22 | 23 | /** 24 | * Database tool allows you to easily drop and create your configured databases. 25 | * 26 | * @author Fabien Potencier 27 | * @author Jonathan H. Wage 28 | * @author Beau Simensen 29 | */ 30 | class DropDatabaseCommand extends Command 31 | { 32 | const RETURN_CODE_NOT_DROP = 1; 33 | 34 | const RETURN_CODE_NO_FORCE = 2; 35 | 36 | /** 37 | * {@inheritdoc} 38 | */ 39 | protected function configure() 40 | { 41 | $this 42 | ->setName('doctrine:database:drop') 43 | ->setDescription('Drops the configured databases') 44 | ->addOption('connection', null, InputOption::VALUE_OPTIONAL, 'The connection to use for this command') 45 | ->addOption('force', null, InputOption::VALUE_NONE, 'Set this parameter to execute this action') 46 | ->setHelp(<<doctrine:database:drop command drops the default connections 48 | database: 49 | 50 | console doctrine:database:drop 51 | 52 | The --force parameter has to be used to actually drop the database. 53 | 54 | You can also optionally specify the name of a connection to drop the database 55 | for: 56 | 57 | console doctrine:database:drop --connection=default 58 | 59 | Be careful: All data in a given database will be lost when executing 60 | this command. 61 | EOT 62 | ); 63 | } 64 | 65 | /** 66 | * {@inheritdoc} 67 | */ 68 | protected function execute(InputInterface $input, OutputInterface $output) 69 | { 70 | $connection = $this->getDoctrineConnection($input->getOption('connection')); 71 | 72 | $params = $connection->getParams(); 73 | 74 | $name = isset($params['path']) ? $params['path'] : (isset($params['dbname']) ? $params['dbname'] : false); 75 | 76 | if (!$name) { 77 | throw new \InvalidArgumentException("Connection does not contain a 'path' or 'dbname' parameter and cannot be dropped."); 78 | } 79 | 80 | if ($input->getOption('force')) { 81 | // Only quote if we don't have a path 82 | if (!isset($params['path'])) { 83 | $name = $connection->getDatabasePlatform()->quoteSingleIdentifier($name); 84 | } 85 | 86 | try { 87 | $connection->getSchemaManager()->dropDatabase($name); 88 | $output->writeln(sprintf('Dropped database for connection named %s', $name)); 89 | } catch (\Exception $e) { 90 | $output->writeln(sprintf('Could not drop database for connection named %s', $name)); 91 | $output->writeln(sprintf('%s', $e->getMessage())); 92 | 93 | return self::RETURN_CODE_NOT_DROP; 94 | } 95 | } else { 96 | $output->writeln('ATTENTION: This operation should not be executed in a production environment.'); 97 | $output->writeln(''); 98 | $output->writeln(sprintf('Would drop the database named %s.', $name)); 99 | $output->writeln('Please run the operation with --force to execute'); 100 | $output->writeln('All data will be lost!'); 101 | 102 | return self::RETURN_CODE_NO_FORCE; 103 | } 104 | } 105 | } 106 | --------------------------------------------------------------------------------