Initial commit

This commit is contained in:
2020-10-07 10:37:15 +02:00
commit ce5f440392
28157 changed files with 4429172 additions and 0 deletions

View File

@@ -0,0 +1,114 @@
<?php
namespace Doctrine\Bundle\DoctrineBundle\Command;
use Doctrine\DBAL\DriverManager;
use Exception;
use InvalidArgumentException;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
/**
* Database tool allows you to easily create your configured databases.
*
* @final
*/
class CreateDatabaseDoctrineCommand extends DoctrineCommand
{
/**
* {@inheritDoc}
*/
protected function configure()
{
$this
->setName('doctrine:database:create')
->setDescription('Creates the configured database')
->addOption('shard', null, InputOption::VALUE_REQUIRED, 'The shard connection to use for this command')
->addOption('connection', null, InputOption::VALUE_OPTIONAL, 'The connection to use for this command')
->addOption('if-not-exists', null, InputOption::VALUE_NONE, 'Don\'t trigger an error, when the database already exists')
->setHelp(<<<EOT
The <info>%command.name%</info> command creates the default connections database:
<info>php %command.full_name%</info>
You can also optionally specify the name of a connection to create the database for:
<info>php %command.full_name% --connection=default</info>
EOT
);
}
/**
* {@inheritDoc}
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$connectionName = $input->getOption('connection');
if (empty($connectionName)) {
$connectionName = $this->getDoctrine()->getDefaultConnectionName();
}
$connection = $this->getDoctrineConnection($connectionName);
$ifNotExists = $input->getOption('if-not-exists');
$params = $connection->getParams();
if (isset($params['master'])) {
$params = $params['master'];
}
// Cannot inject `shard` option in parent::getDoctrineConnection
// cause it will try to connect to a non-existing database
if (isset($params['shards'])) {
$shards = $params['shards'];
// Default select global
$params = array_merge($params, $params['global']);
unset($params['global']['dbname'], $params['global']['path'], $params['global']['url']);
if ($input->getOption('shard')) {
foreach ($shards as $i => $shard) {
if ($shard['id'] === (int) $input->getOption('shard')) {
// Select sharded database
$params = array_merge($params, $shard);
unset($params['shards'][$i]['dbname'], $params['shards'][$i]['path'], $params['shards'][$i]['url'], $params['id']);
break;
}
}
}
}
$hasPath = isset($params['path']);
$name = $hasPath ? $params['path'] : (isset($params['dbname']) ? $params['dbname'] : false);
if (! $name) {
throw new InvalidArgumentException("Connection does not contain a 'path' or 'dbname' parameter and cannot be created.");
}
// Need to get rid of _every_ occurrence of dbname from connection configuration and we have already extracted all relevant info from url
unset($params['dbname'], $params['path'], $params['url']);
$tmpConnection = DriverManager::getConnection($params);
$tmpConnection->connect($input->getOption('shard'));
$shouldNotCreateDatabase = $ifNotExists && in_array($name, $tmpConnection->getSchemaManager()->listDatabases());
// Only quote if we don't have a path
if (! $hasPath) {
$name = $tmpConnection->getDatabasePlatform()->quoteSingleIdentifier($name);
}
$error = false;
try {
if ($shouldNotCreateDatabase) {
$output->writeln(sprintf('<info>Database <comment>%s</comment> for connection named <comment>%s</comment> already exists. Skipped.</info>', $name, $connectionName));
} else {
$tmpConnection->getSchemaManager()->createDatabase($name);
$output->writeln(sprintf('<info>Created database <comment>%s</comment> for connection named <comment>%s</comment></info>', $name, $connectionName));
}
} catch (Exception $e) {
$output->writeln(sprintf('<error>Could not create database <comment>%s</comment> for connection named <comment>%s</comment></error>', $name, $connectionName));
$output->writeln(sprintf('<error>%s</error>', $e->getMessage()));
$error = true;
}
$tmpConnection->close();
return $error ? 1 : 0;
}
}

View File

@@ -0,0 +1,123 @@
<?php
namespace Doctrine\Bundle\DoctrineBundle\Command;
use Doctrine\Common\Persistence\ManagerRegistry;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Sharding\PoolingShardConnection;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\Tools\EntityGenerator;
use LogicException;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Base class for Doctrine console commands to extend from.
*
* @internal
*/
abstract class DoctrineCommand extends Command
{
/** @var ManagerRegistry|null */
private $doctrine;
/** @var ContainerInterface|null */
private $container;
public function __construct(ManagerRegistry $doctrine = null)
{
parent::__construct();
$this->doctrine = $doctrine;
}
/**
* @deprecated
*/
public function setContainer(ContainerInterface $container = null)
{
$this->container = $container;
}
/**
* @deprecated
*
* @return ContainerInterface
*
* @throws LogicException
*/
protected function getContainer()
{
if ($this->container === null) {
$application = $this->getApplication();
if ($application === null) {
throw new LogicException('The container cannot be retrieved as the application instance is not yet set.');
}
$this->container = $application->getKernel()->getContainer();
}
return $this->container;
}
/**
* get a doctrine entity generator
*
* @return EntityGenerator
*/
protected function getEntityGenerator()
{
$entityGenerator = new EntityGenerator();
$entityGenerator->setGenerateAnnotations(false);
$entityGenerator->setGenerateStubMethods(true);
$entityGenerator->setRegenerateEntityIfExists(false);
$entityGenerator->setUpdateEntityIfExists(true);
$entityGenerator->setNumSpaces(4);
$entityGenerator->setAnnotationPrefix('ORM\\');
return $entityGenerator;
}
/**
* Get a doctrine entity manager by symfony name.
*
* @param string $name
* @param int|null $shardId
*
* @return EntityManager
*/
protected function getEntityManager($name, $shardId = null)
{
$manager = $this->getDoctrine()->getManager($name);
if ($shardId) {
if (! $manager->getConnection() instanceof PoolingShardConnection) {
throw new LogicException(sprintf("Connection of EntityManager '%s' must implement shards configuration.", $name));
}
$manager->getConnection()->connect($shardId);
}
return $manager;
}
/**
* Get a doctrine dbal connection by symfony name.
*
* @param string $name
*
* @return Connection
*/
protected function getDoctrineConnection($name)
{
return $this->getDoctrine()->getConnection($name);
}
/**
* @return ManagerRegistry
*/
protected function getDoctrine()
{
return $this->doctrine ?: $this->doctrine = $this->getContainer()->get('doctrine');
}
}

View File

@@ -0,0 +1,126 @@
<?php
namespace Doctrine\Bundle\DoctrineBundle\Command;
use Doctrine\DBAL\DriverManager;
use Exception;
use InvalidArgumentException;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
/**
* Database tool allows you to easily drop your configured databases.
*
* @final
*/
class DropDatabaseDoctrineCommand extends DoctrineCommand
{
const RETURN_CODE_NOT_DROP = 1;
const RETURN_CODE_NO_FORCE = 2;
/**
* {@inheritDoc}
*/
protected function configure()
{
$this
->setName('doctrine:database:drop')
->setDescription('Drops the configured database')
->addOption('shard', null, InputOption::VALUE_REQUIRED, 'The shard connection to use for this command')
->addOption('connection', null, InputOption::VALUE_OPTIONAL, 'The connection to use for this command')
->addOption('if-exists', null, InputOption::VALUE_NONE, 'Don\'t trigger an error, when the database doesn\'t exist')
->addOption('force', null, InputOption::VALUE_NONE, 'Set this parameter to execute this action')
->setHelp(<<<EOT
The <info>%command.name%</info> command drops the default connections database:
<info>php %command.full_name%</info>
The <info>--force</info> parameter has to be used to actually drop the database.
You can also optionally specify the name of a connection to drop the database for:
<info>php %command.full_name% --connection=default</info>
<error>Be careful: All data in a given database will be lost when executing this command.</error>
EOT
);
}
/**
* {@inheritDoc}
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$connectionName = $input->getOption('connection');
if (empty($connectionName)) {
$connectionName = $this->getDoctrine()->getDefaultConnectionName();
}
$connection = $this->getDoctrineConnection($connectionName);
$ifExists = $input->getOption('if-exists');
$params = $connection->getParams();
if (isset($params['master'])) {
$params = $params['master'];
}
if (isset($params['shards'])) {
$shards = $params['shards'];
// Default select global
$params = array_merge($params, $params['global']);
if ($input->getOption('shard')) {
foreach ($shards as $shard) {
if ($shard['id'] === (int) $input->getOption('shard')) {
// Select sharded database
$params = $shard;
unset($params['id']);
break;
}
}
}
}
$name = isset($params['path']) ? $params['path'] : (isset($params['dbname']) ? $params['dbname'] : false);
if (! $name) {
throw new InvalidArgumentException("Connection does not contain a 'path' or 'dbname' parameter and cannot be dropped.");
}
unset($params['dbname'], $params['url']);
if (! $input->getOption('force')) {
$output->writeln('<error>ATTENTION:</error> This operation should not be executed in a production environment.');
$output->writeln('');
$output->writeln(sprintf('<info>Would drop the database <comment>%s</comment> for connection named <comment>%s</comment>.</info>', $name, $connectionName));
$output->writeln('Please run the operation with --force to execute');
$output->writeln('<error>All data will be lost!</error>');
return self::RETURN_CODE_NO_FORCE;
}
// Reopen connection without database name set
// as some vendors do not allow dropping the database connected to.
$connection->close();
$connection = DriverManager::getConnection($params);
$shouldDropDatabase = ! $ifExists || in_array($name, $connection->getSchemaManager()->listDatabases());
// Only quote if we don't have a path
if (! isset($params['path'])) {
$name = $connection->getDatabasePlatform()->quoteSingleIdentifier($name);
}
try {
if ($shouldDropDatabase) {
$connection->getSchemaManager()->dropDatabase($name);
$output->writeln(sprintf('<info>Dropped database <comment>%s</comment> for connection named <comment>%s</comment></info>', $name, $connectionName));
} else {
$output->writeln(sprintf('<info>Database <comment>%s</comment> for connection named <comment>%s</comment> doesn\'t exist. Skipped.</info>', $name, $connectionName));
}
} catch (Exception $e) {
$output->writeln(sprintf('<error>Could not drop database <comment>%s</comment> for connection named <comment>%s</comment></error>', $name, $connectionName));
$output->writeln(sprintf('<error>%s</error>', $e->getMessage()));
return self::RETURN_CODE_NOT_DROP;
}
}
}

View File

@@ -0,0 +1,139 @@
<?php
namespace Doctrine\Bundle\DoctrineBundle\Command;
use Doctrine\Bundle\DoctrineBundle\Mapping\DisconnectedMetadataFactory;
use Doctrine\ORM\Tools\EntityRepositoryGenerator;
use InvalidArgumentException;
use RuntimeException;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
/**
* Generate entity classes from mapping information
*
* @final
*/
class GenerateEntitiesDoctrineCommand extends DoctrineCommand
{
/**
* {@inheritDoc}
*/
protected function configure()
{
$this
->setName('doctrine:generate:entities')
->setAliases(['generate:doctrine:entities'])
->setDescription('Generates entity classes and method stubs from your mapping information')
->addArgument('name', InputArgument::REQUIRED, 'A bundle name, a namespace, or a class name')
->addOption('path', null, InputOption::VALUE_REQUIRED, 'The path where to generate entities when it cannot be guessed')
->addOption('no-backup', null, InputOption::VALUE_NONE, 'Do not backup existing entities files.')
->setHelp(<<<EOT
The <info>%command.name%</info> command generates entity classes
and method stubs from your mapping information:
You have to limit generation of entities:
* To a bundle:
<info>php %command.full_name% MyCustomBundle</info>
* To a single entity:
<info>php %command.full_name% MyCustomBundle:User</info>
<info>php %command.full_name% MyCustomBundle/Entity/User</info>
* To a namespace
<info>php %command.full_name% MyCustomBundle/Entity</info>
If the entities are not stored in a bundle, and if the classes do not exist,
the command has no way to guess where they should be generated. In this case,
you must provide the <comment>--path</comment> option:
<info>php %command.full_name% Blog/Entity --path=src/</info>
By default, the unmodified version of each entity is backed up and saved
(e.g. Product.php~). To prevent this task from creating the backup file,
pass the <comment>--no-backup</comment> option:
<info>php %command.full_name% Blog/Entity --no-backup</info>
<error>Important:</error> Even if you specified Inheritance options in your
XML or YAML Mapping files the generator cannot generate the base and
child classes for you correctly, because it doesn't know which
class is supposed to extend which. You have to adjust the entity
code manually for inheritance to work!
EOT
);
}
/**
* {@inheritDoc}
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
trigger_error('The doctrine:generate:entity command has been deprecated.', E_USER_DEPRECATED);
$output->writeln([
' <comment>NOTE:</comment> The <info>doctrine:generate:entities</info> command has been deprecated.',
' To read more about the differences between anemic and rich models go here <info>http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/tutorials/getting-started.html#adding-behavior-to-entities</info>.',
' If you wish to generate your entities, use <info>make:entity --regenerate</info> from MakerBundle instead.',
]);
$manager = new DisconnectedMetadataFactory($this->getDoctrine());
try {
$bundle = $this->getApplication()->getKernel()->getBundle($input->getArgument('name'));
$output->writeln(sprintf('Generating entities for bundle "<info>%s</info>"', $bundle->getName()));
$metadata = $manager->getBundleMetadata($bundle);
} catch (InvalidArgumentException $e) {
$name = strtr($input->getArgument('name'), '/', '\\');
$pos = strpos($name, ':');
if ($pos !== false) {
$name = $this->getDoctrine()->getAliasNamespace(substr($name, 0, $pos)) . '\\' . substr($name, $pos + 1);
}
if (class_exists($name)) {
$output->writeln(sprintf('Generating entity "<info>%s</info>"', $name));
$metadata = $manager->getClassMetadata($name, $input->getOption('path'));
} else {
$output->writeln(sprintf('Generating entities for namespace "<info>%s</info>"', $name));
$metadata = $manager->getNamespaceMetadata($name, $input->getOption('path'));
}
}
$generator = $this->getEntityGenerator();
$backupExisting = ! $input->getOption('no-backup');
$generator->setBackupExisting($backupExisting);
$repoGenerator = new EntityRepositoryGenerator();
foreach ($metadata->getMetadata() as $m) {
if ($backupExisting) {
$basename = substr($m->name, strrpos($m->name, '\\') + 1);
$output->writeln(sprintf(' > backing up <comment>%s.php</comment> to <comment>%s.php~</comment>', $basename, $basename));
}
// Getting the metadata for the entity class once more to get the correct path if the namespace has multiple occurrences
try {
$entityMetadata = $manager->getClassMetadata($m->getName(), $input->getOption('path'));
} catch (RuntimeException $e) {
// fall back to the bundle metadata when no entity class could be found
$entityMetadata = $metadata;
}
$output->writeln(sprintf(' > generating <comment>%s</comment>', $m->name));
$generator->generate([$m], $entityMetadata->getPath());
if (! $m->customRepositoryClassName || strpos($m->customRepositoryClassName, $metadata->getNamespace()) === false) {
continue;
}
$repoGenerator->writeEntityRepositoryClass($m->customRepositoryClassName, $metadata->getPath());
}
}
}

View File

@@ -0,0 +1,161 @@
<?php
namespace Doctrine\Bundle\DoctrineBundle\Command;
use Doctrine\Common\Persistence\ManagerRegistry;
use Doctrine\ORM\Mapping\Driver\DatabaseDriver;
use Doctrine\ORM\Tools\Console\MetadataFilter;
use Doctrine\ORM\Tools\DisconnectedClassMetadataFactory;
use Doctrine\ORM\Tools\Export\ClassMetadataExporter;
use InvalidArgumentException;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
/**
* Import Doctrine ORM metadata mapping information from an existing database.
*
* @final
*/
class ImportMappingDoctrineCommand extends DoctrineCommand
{
/** @var string[] */
private $bundles;
/**
* @param string[] $bundles
*/
public function __construct(ManagerRegistry $doctrine, array $bundles)
{
parent::__construct($doctrine);
$this->bundles = $bundles;
}
/**
* {@inheritDoc}
*/
protected function configure()
{
$this
->setName('doctrine:mapping:import')
->addArgument('name', InputArgument::REQUIRED, 'The bundle or namespace to import the mapping information to')
->addArgument('mapping-type', InputArgument::OPTIONAL, 'The mapping type to export the imported mapping information to')
->addOption('em', null, InputOption::VALUE_OPTIONAL, 'The entity manager to use for this command')
->addOption('shard', null, InputOption::VALUE_REQUIRED, 'The shard connection to use for this command')
->addOption('filter', null, InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, 'A string pattern used to match entities that should be mapped.')
->addOption('force', null, InputOption::VALUE_NONE, 'Force to overwrite existing mapping files.')
->addOption('path', null, InputOption::VALUE_REQUIRED, 'The path where the files would be generated (not used when a bundle is passed).')
->setDescription('Imports mapping information from an existing database')
->setHelp(<<<EOT
The <info>%command.name%</info> command imports mapping information
from an existing database:
Generate annotation mappings into the src/ directory using App as the namespace:
<info>php %command.full_name% App\\\Entity annotation --path=src/Entity</info>
Generate xml mappings into the config/doctrine/ directory using App as the namespace:
<info>php %command.full_name% App\\\Entity xml --path=config/doctrine</info>
Generate XML mappings into a bundle:
<info>php %command.full_name% "MyCustomBundle" xml</info>
You can also optionally specify which entity manager to import from with the
<info>--em</info> option:
<info>php %command.full_name% "MyCustomBundle" xml --em=default</info>
If you don't want to map every entity that can be found in the database, use the
<info>--filter</info> option. It will try to match the targeted mapped entity with the
provided pattern string.
<info>php %command.full_name% "MyCustomBundle" xml --filter=MyMatchedEntity</info>
Use the <info>--force</info> option, if you want to override existing mapping files:
<info>php %command.full_name% "MyCustomBundle" xml --force</info>
EOT
);
}
/**
* {@inheritDoc}
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$type = $input->getArgument('mapping-type') ?: 'xml';
if ($type === 'yaml') {
$type = 'yml';
}
$namespaceOrBundle = $input->getArgument('name');
if (isset($this->bundles[$namespaceOrBundle])) {
$bundle = $this->getApplication()->getKernel()->getBundle($namespaceOrBundle);
$namespace = $bundle->getNamespace() . '\Entity';
$destPath = $bundle->getPath();
if ($type === 'annotation') {
$destPath .= '/Entity';
} else {
$destPath .= '/Resources/config/doctrine';
}
} else {
// assume a namespace has been passed
$namespace = $namespaceOrBundle;
$destPath = $input->getOption('path');
if ($destPath === null) {
throw new InvalidArgumentException('The --path option is required when passing a namespace (e.g. --path=src). If you intended to pass a bundle name, check your spelling.');
}
}
$cme = new ClassMetadataExporter();
$exporter = $cme->getExporter($type);
$exporter->setOverwriteExistingFiles($input->getOption('force'));
if ($type === 'annotation') {
$entityGenerator = $this->getEntityGenerator();
$exporter->setEntityGenerator($entityGenerator);
}
$em = $this->getEntityManager($input->getOption('em'), $input->getOption('shard'));
$databaseDriver = new DatabaseDriver($em->getConnection()->getSchemaManager());
$em->getConfiguration()->setMetadataDriverImpl($databaseDriver);
$emName = $input->getOption('em');
$emName = $emName ? $emName : 'default';
$cmf = new DisconnectedClassMetadataFactory();
$cmf->setEntityManager($em);
$metadata = $cmf->getAllMetadata();
$metadata = MetadataFilter::filter($metadata, $input->getOption('filter'));
if ($metadata) {
$output->writeln(sprintf('Importing mapping information from "<info>%s</info>" entity manager', $emName));
foreach ($metadata as $class) {
$className = $class->name;
$class->name = $namespace . '\\' . $className;
if ($type === 'annotation') {
$path = $destPath . '/' . str_replace('\\', '.', $className) . '.php';
} else {
$path = $destPath . '/' . str_replace('\\', '.', $className) . '.orm.' . $type;
}
$output->writeln(sprintf(' > writing <comment>%s</comment>', $path));
$code = $exporter->exportClassMetadata($class);
$dir = dirname($path);
if (! is_dir($dir)) {
mkdir($dir, 0775, true);
}
file_put_contents($path, $code);
chmod($path, 0664);
}
return 0;
}
$output->writeln('Database does not have any mapping information.');
$output->writeln('');
return 1;
}
}

View File

@@ -0,0 +1,37 @@
<?php
namespace Doctrine\Bundle\DoctrineBundle\Command\Proxy;
use Doctrine\ORM\Tools\Console\Command\ClearCache\MetadataCommand;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
/**
* Command to clear the metadata cache of the various cache drivers.
*/
class ClearMetadataCacheDoctrineCommand extends MetadataCommand
{
/**
* {@inheritDoc}
*/
protected function configure()
{
parent::configure();
$this
->setName('doctrine:cache:clear-metadata')
->setDescription('Clears all metadata cache for an entity manager')
->addOption('em', null, InputOption::VALUE_OPTIONAL, 'The entity manager to use for this command');
}
/**
* {@inheritDoc}
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
DoctrineCommandHelper::setApplicationEntityManager($this->getApplication(), $input->getOption('em'));
return parent::execute($input, $output);
}
}

View File

@@ -0,0 +1,37 @@
<?php
namespace Doctrine\Bundle\DoctrineBundle\Command\Proxy;
use Doctrine\ORM\Tools\Console\Command\ClearCache\QueryCommand;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
/**
* Command to clear the query cache of the various cache drivers.
*/
class ClearQueryCacheDoctrineCommand extends QueryCommand
{
/**
* {@inheritDoc}
*/
protected function configure()
{
parent::configure();
$this
->setName('doctrine:cache:clear-query')
->setDescription('Clears all query cache for an entity manager')
->addOption('em', null, InputOption::VALUE_OPTIONAL, 'The entity manager to use for this command');
}
/**
* {@inheritDoc}
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
DoctrineCommandHelper::setApplicationEntityManager($this->getApplication(), $input->getOption('em'));
return parent::execute($input, $output);
}
}

View File

@@ -0,0 +1,37 @@
<?php
namespace Doctrine\Bundle\DoctrineBundle\Command\Proxy;
use Doctrine\ORM\Tools\Console\Command\ClearCache\ResultCommand;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
/**
* Command to clear the result cache of the various cache drivers.
*/
class ClearResultCacheDoctrineCommand extends ResultCommand
{
/**
* {@inheritDoc}
*/
protected function configure()
{
parent::configure();
$this
->setName('doctrine:cache:clear-result')
->setDescription('Clears result cache for an entity manager')
->addOption('em', null, InputOption::VALUE_OPTIONAL, 'The entity manager to use for this command');
}
/**
* {@inheritDoc}
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
DoctrineCommandHelper::setApplicationEntityManager($this->getApplication(), $input->getOption('em'));
return parent::execute($input, $output);
}
}

View File

@@ -0,0 +1,37 @@
<?php
namespace Doctrine\Bundle\DoctrineBundle\Command\Proxy;
use Doctrine\ORM\Tools\Console\Command\ClearCache\CollectionRegionCommand;
/**
* Command to clear a collection cache region.
*/
class CollectionRegionDoctrineCommand extends DelegateCommand
{
/**
* {@inheritDoc}
*/
protected function configure()
{
parent::configure();
$this->setName('doctrine:cache:clear-collection-region');
}
/**
* {@inheritDoc}
*/
protected function createCommand()
{
return new CollectionRegionCommand();
}
/**
* {@inheritDoc}
*/
protected function getMinimalVersion()
{
return '2.5.0-DEV';
}
}

View File

@@ -0,0 +1,58 @@
<?php
namespace Doctrine\Bundle\DoctrineBundle\Command\Proxy;
use Doctrine\ORM\Tools\Console\Command\ConvertMappingCommand;
use Doctrine\ORM\Tools\Export\Driver\AbstractExporter;
use Doctrine\ORM\Tools\Export\Driver\XmlExporter;
use Doctrine\ORM\Tools\Export\Driver\YamlExporter;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
/**
* Convert Doctrine ORM metadata mapping information between the various supported
* formats.
*/
class ConvertMappingDoctrineCommand extends ConvertMappingCommand
{
/**
* {@inheritDoc}
*/
protected function configure()
{
parent::configure();
$this
->setName('doctrine:mapping:convert')
->addOption('em', null, InputOption::VALUE_OPTIONAL, 'The entity manager to use for this command');
}
/**
* {@inheritDoc}
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
DoctrineCommandHelper::setApplicationEntityManager($this->getApplication(), $input->getOption('em'));
return parent::execute($input, $output);
}
/**
* @param string $toType
* @param string $destPath
*
* @return AbstractExporter
*/
protected function getExporter($toType, $destPath)
{
/** @var AbstractExporter $exporter */
$exporter = parent::getExporter($toType, $destPath);
if ($exporter instanceof XmlExporter) {
$exporter->setExtension('.orm.xml');
} elseif ($exporter instanceof YamlExporter) {
$exporter->setExtension('.orm.yml');
}
return $exporter;
}
}

View File

@@ -0,0 +1,38 @@
<?php
namespace Doctrine\Bundle\DoctrineBundle\Command\Proxy;
use Doctrine\ORM\Tools\Console\Command\SchemaTool\CreateCommand;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
/**
* Command to execute the SQL needed to generate the database schema for
* a given entity manager.
*/
class CreateSchemaDoctrineCommand extends CreateCommand
{
/**
* {@inheritDoc}
*/
protected function configure()
{
parent::configure();
$this
->setName('doctrine:schema:create')
->setDescription('Executes (or dumps) the SQL needed to generate the database schema')
->addOption('em', null, InputOption::VALUE_OPTIONAL, 'The entity manager to use for this command');
}
/**
* {@inheritDoc}
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
DoctrineCommandHelper::setApplicationEntityManager($this->getApplication(), $input->getOption('em'));
return parent::execute($input, $output);
}
}

View File

@@ -0,0 +1,105 @@
<?php
namespace Doctrine\Bundle\DoctrineBundle\Command\Proxy;
use Doctrine\ORM\Version;
use RuntimeException;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
/**
* Command Delegate.
*/
abstract class DelegateCommand extends Command
{
/** @var Command */
protected $command;
/**
* @return Command
*/
abstract protected function createCommand();
/**
* @return string
*/
protected function getMinimalVersion()
{
return '2.3.0-DEV';
}
/**
* @return bool
*/
private function isVersionCompatible()
{
return version_compare(Version::VERSION, $this->getMinimalVersion()) >= 0;
}
/**
* {@inheritDoc}
*/
public function isEnabled()
{
return $this->isVersionCompatible();
}
/**
* @param string $entityManagerName
*
* @return Command
*/
protected function wrapCommand($entityManagerName)
{
if (! $this->isVersionCompatible()) {
throw new RuntimeException(sprintf('"%s" requires doctrine-orm "%s" or newer', $this->getName(), $this->getMinimalVersion()));
}
DoctrineCommandHelper::setApplicationEntityManager($this->getApplication(), $entityManagerName);
$this->command->setApplication($this->getApplication());
return $this->command;
}
/**
* {@inheritDoc}
*/
protected function configure()
{
if ($this->isVersionCompatible()) {
$this->command = $this->createCommand();
$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);
}
}

View File

@@ -0,0 +1,41 @@
<?php
namespace Doctrine\Bundle\DoctrineBundle\Command\Proxy;
use Doctrine\DBAL\Tools\Console\Helper\ConnectionHelper;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\Tools\Console\Helper\EntityManagerHelper;
use Symfony\Bundle\FrameworkBundle\Console\Application;
/**
* Provides some helper and convenience methods to configure doctrine commands in the context of bundles
* and multiple connections/entity managers.
*/
abstract class DoctrineCommandHelper
{
/**
* Convenience method to push the helper sets of a given entity manager into the application.
*
* @param string $emName
*/
public static function setApplicationEntityManager(Application $application, $emName)
{
/** @var EntityManager $em */
$em = $application->getKernel()->getContainer()->get('doctrine')->getManager($emName);
$helperSet = $application->getHelperSet();
$helperSet->set(new ConnectionHelper($em->getConnection()), 'db');
$helperSet->set(new EntityManagerHelper($em), 'em');
}
/**
* Convenience method to push the helper sets of a given connection into the application.
*
* @param string $connName
*/
public static function setApplicationConnection(Application $application, $connName)
{
$connection = $application->getKernel()->getContainer()->get('doctrine')->getConnection($connName);
$helperSet = $application->getHelperSet();
$helperSet->set(new ConnectionHelper($connection), 'db');
}
}

View File

@@ -0,0 +1,37 @@
<?php
namespace Doctrine\Bundle\DoctrineBundle\Command\Proxy;
use Doctrine\ORM\Tools\Console\Command\SchemaTool\DropCommand;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
/**
* Command to drop the database schema for a set of classes based on their mappings.
*/
class DropSchemaDoctrineCommand extends DropCommand
{
/**
* {@inheritDoc}
*/
protected function configure()
{
parent::configure();
$this
->setName('doctrine:schema:drop')
->setDescription('Executes (or dumps) the SQL needed to drop the current database schema')
->addOption('em', null, InputOption::VALUE_OPTIONAL, 'The entity manager to use for this command');
}
/**
* {@inheritDoc}
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
DoctrineCommandHelper::setApplicationEntityManager($this->getApplication(), $input->getOption('em'));
return parent::execute($input, $output);
}
}

View File

@@ -0,0 +1,36 @@
<?php
namespace Doctrine\Bundle\DoctrineBundle\Command\Proxy;
use Doctrine\ORM\Tools\Console\Command\EnsureProductionSettingsCommand;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
/**
* Ensure the Doctrine ORM is configured properly for a production environment.
*/
class EnsureProductionSettingsDoctrineCommand extends EnsureProductionSettingsCommand
{
/**
* {@inheritDoc}
*/
protected function configure()
{
parent::configure();
$this
->setName('doctrine:ensure-production-settings')
->addOption('em', null, InputOption::VALUE_OPTIONAL, 'The entity manager to use for this command');
}
/**
* {@inheritDoc}
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
DoctrineCommandHelper::setApplicationEntityManager($this->getApplication(), $input->getOption('em'));
return parent::execute($input, $output);
}
}

View File

@@ -0,0 +1,37 @@
<?php
namespace Doctrine\Bundle\DoctrineBundle\Command\Proxy;
use Doctrine\ORM\Tools\Console\Command\ClearCache\EntityRegionCommand;
/**
* Command to clear a entity cache region.
*/
class EntityRegionCacheDoctrineCommand extends DelegateCommand
{
/**
* {@inheritDoc}
*/
protected function configure()
{
parent::configure();
$this->setName('doctrine:cache:clear-entity-region');
}
/**
* {@inheritDoc}
*/
protected function createCommand()
{
return new EntityRegionCommand();
}
/**
* {@inheritDoc}
*/
protected function getMinimalVersion()
{
return '2.5.0-DEV';
}
}

View File

@@ -0,0 +1,36 @@
<?php
namespace Doctrine\Bundle\DoctrineBundle\Command\Proxy;
use Doctrine\DBAL\Tools\Console\Command\ImportCommand;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
/**
* Loads an SQL file and executes it.
*/
class ImportDoctrineCommand extends ImportCommand
{
/**
* {@inheritDoc}
*/
protected function configure()
{
parent::configure();
$this
->setName('doctrine:database:import')
->addOption('connection', null, InputOption::VALUE_OPTIONAL, 'The connection to use for this command');
}
/**
* {@inheritDoc}
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
DoctrineCommandHelper::setApplicationConnection($this->getApplication(), $input->getOption('connection'));
return parent::execute($input, $output);
}
}

View File

@@ -0,0 +1,34 @@
<?php
namespace Doctrine\Bundle\DoctrineBundle\Command\Proxy;
use Doctrine\ORM\Tools\Console\Command\InfoCommand;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
/**
* Show information about mapped entities
*/
class InfoDoctrineCommand extends InfoCommand
{
/**
* {@inheritDoc}
*/
protected function configure()
{
$this
->setName('doctrine:mapping:info')
->addOption('em', null, InputOption::VALUE_OPTIONAL, 'The entity manager to use for this command');
}
/**
* {@inheritDoc}
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
DoctrineCommandHelper::setApplicationEntityManager($this->getApplication(), $input->getOption('em'));
return parent::execute($input, $output);
}
}

View File

@@ -0,0 +1,37 @@
<?php
namespace Doctrine\Bundle\DoctrineBundle\Command\Proxy;
use Doctrine\ORM\Tools\Console\Command\ClearCache\QueryRegionCommand;
/**
* Command to clear a query cache region.
*/
class QueryRegionCacheDoctrineCommand extends DelegateCommand
{
/**
* {@inheritDoc}
*/
protected function configure()
{
parent::configure();
$this->setName('doctrine:cache:clear-query-region');
}
/**
* {@inheritDoc}
*/
protected function createCommand()
{
return new QueryRegionCommand();
}
/**
* {@inheritDoc}
*/
protected function getMinimalVersion()
{
return '2.5.0-DEV';
}
}

View File

@@ -0,0 +1,53 @@
<?php
namespace Doctrine\Bundle\DoctrineBundle\Command\Proxy;
use Doctrine\ORM\Tools\Console\Command\RunDqlCommand;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
/**
* Execute a Doctrine DQL query and output the results.
*/
class RunDqlDoctrineCommand extends RunDqlCommand
{
/**
* {@inheritDoc}
*/
protected function configure()
{
parent::configure();
$this
->setName('doctrine:query:dql')
->addOption('em', null, InputOption::VALUE_OPTIONAL, 'The entity manager to use for this command')
->setHelp(<<<EOT
The <info>%command.name%</info> command executes the given DQL query and
outputs the results:
<info>php %command.full_name% "SELECT u FROM UserBundle:User u"</info>
You can also optional specify some additional options like what type of
hydration to use when executing the query:
<info>php %command.full_name% "SELECT u FROM UserBundle:User u" --hydrate=array</info>
Additionally you can specify the first result and maximum amount of results to
show:
<info>php %command.full_name% "SELECT u FROM UserBundle:User u" --first-result=0 --max-result=30</info>
EOT
);
}
/**
* {@inheritDoc}
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
DoctrineCommandHelper::setApplicationEntityManager($this->getApplication(), $input->getOption('em'));
return parent::execute($input, $output);
}
}

View File

@@ -0,0 +1,43 @@
<?php
namespace Doctrine\Bundle\DoctrineBundle\Command\Proxy;
use Doctrine\DBAL\Tools\Console\Command\RunSqlCommand;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
/**
* Execute a SQL query and output the results.
*/
class RunSqlDoctrineCommand extends RunSqlCommand
{
/**
* {@inheritDoc}
*/
protected function configure()
{
parent::configure();
$this
->setName('doctrine:query:sql')
->addOption('connection', null, InputOption::VALUE_OPTIONAL, 'The connection to use for this command')
->setHelp(<<<EOT
The <info>%command.name%</info> command executes the given SQL query and
outputs the results:
<info>php %command.full_name% "SELECT * FROM users"</info>
EOT
);
}
/**
* {@inheritDoc}
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
DoctrineCommandHelper::setApplicationConnection($this->getApplication(), $input->getOption('connection'));
return parent::execute($input, $output);
}
}

View File

@@ -0,0 +1,37 @@
<?php
namespace Doctrine\Bundle\DoctrineBundle\Command\Proxy;
use Doctrine\ORM\Tools\Console\Command\SchemaTool\UpdateCommand;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
/**
* Command to generate the SQL needed to update the database schema to match
* the current mapping information.
*/
class UpdateSchemaDoctrineCommand extends UpdateCommand
{
/**
* {@inheritDoc}
*/
protected function configure()
{
parent::configure();
$this
->setName('doctrine:schema:update')
->addOption('em', null, InputOption::VALUE_OPTIONAL, 'The entity manager to use for this command');
}
/**
* {@inheritDoc}
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
DoctrineCommandHelper::setApplicationEntityManager($this->getApplication(), $input->getOption('em'));
return parent::execute($input, $output);
}
}

View File

@@ -0,0 +1,36 @@
<?php
namespace Doctrine\Bundle\DoctrineBundle\Command\Proxy;
use Doctrine\ORM\Tools\Console\Command\ValidateSchemaCommand as DoctrineValidateSchemaCommand;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
/**
* Command to run Doctrine ValidateSchema() on the current mappings.
*/
class ValidateSchemaCommand extends DoctrineValidateSchemaCommand
{
/**
* {@inheritDoc}
*/
protected function configure()
{
parent::configure();
$this
->setName('doctrine:schema:validate')
->addOption('em', null, InputOption::VALUE_OPTIONAL, 'The entity manager to use for this command');
}
/**
* {@inheritDoc}
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
DoctrineCommandHelper::setApplicationEntityManager($this->getApplication(), $input->getOption('em'));
return parent::execute($input, $output);
}
}