Commit 49bcc69f authored by guilhermeblanco's avatar guilhermeblanco

[2.0] Moved EntityManager creation to be always available in CLI Tasks

parent 10e3407e
...@@ -40,6 +40,7 @@ abstract class AbstractFileDriver implements Driver ...@@ -40,6 +40,7 @@ abstract class AbstractFileDriver implements Driver
* adhere to the convention of 1 mapping file per class and the file names of * adhere to the convention of 1 mapping file per class and the file names of
* the mapping files must correspond to the full class name, including namespace, * the mapping files must correspond to the full class name, including namespace,
* with the namespace delimiters '\', replaced by dots '.'. * with the namespace delimiters '\', replaced by dots '.'.
* This is the default behavior.
* *
* Example: * Example:
* Class: My\Project\Model\User * Class: My\Project\Model\User
...@@ -51,8 +52,8 @@ abstract class AbstractFileDriver implements Driver ...@@ -51,8 +52,8 @@ abstract class AbstractFileDriver implements Driver
/** /**
* The PRELOAD mode is an operating mode of the FileDriver where it loads * The PRELOAD mode is an operating mode of the FileDriver where it loads
* all mapping files in advance. This is the default behavior. It does not * all mapping files in advance. It does not require a naming convention
* require a naming convention or the convention of 1 class per mapping file. * or the convention of 1 class per mapping file.
* *
* @var integer * @var integer
*/ */
...@@ -95,7 +96,7 @@ abstract class AbstractFileDriver implements Driver ...@@ -95,7 +96,7 @@ abstract class AbstractFileDriver implements Driver
*/ */
public function __construct($paths, $mode = self::FILE_PER_CLASS) public function __construct($paths, $mode = self::FILE_PER_CLASS)
{ {
$this->_paths = $paths; $this->_paths = (array) $paths;
$this->_mode = $mode; $this->_mode = $mode;
} }
......
...@@ -157,7 +157,7 @@ class Cli ...@@ -157,7 +157,7 @@ class Cli
try { try {
$this->_printer->writeln('Doctrine Command Line Interface' . PHP_EOL, 'HEADER'); $this->_printer->writeln('Doctrine Command Line Interface' . PHP_EOL, 'HEADER');
// Handle possible multiple tasks on a single command // Handle possible multiple tasks on a single command
foreach($processedArgs as $taskData) { foreach($processedArgs as $taskData) {
// Retrieve the task name and arguments // Retrieve the task name and arguments
...@@ -166,9 +166,13 @@ class Cli ...@@ -166,9 +166,13 @@ class Cli
// Check if task exists // Check if task exists
if (isset($this->_tasks[$taskName]) && class_exists($this->_tasks[$taskName], true)) { if (isset($this->_tasks[$taskName]) && class_exists($this->_tasks[$taskName], true)) {
// Initializing EntityManager
$em = $this->_initializeEntityManager($processedArgs);
// Instantiate and execute the task // Instantiate and execute the task
$task = new $this->_tasks[$taskName](); $task = new $this->_tasks[$taskName]();
$task->setAvailableTasks($this->_tasks); $task->setAvailableTasks($this->_tasks);
$task->setEntityManager($em);
$task->setPrinter($this->_printer); $task->setPrinter($this->_printer);
$task->setArguments($taskArguments); $task->setArguments($taskArguments);
...@@ -311,4 +315,42 @@ class Cli ...@@ -311,4 +315,42 @@ class Cli
// required and optional arguments here? // required and optional arguments here?
return $task->validate(); return $task->validate();
} }
/**
* Initialized Entity Manager for Tasks
*
* @param array CLI Task arguments
* @return EntityManager
*/
private function _initializeEntityManager(& $args)
{
// Initialize EntityManager
$configFile = ( ! isset($args['config'])) ? './cli-config.php' : $args['config'];
if (file_exists($configFile)) {
// Including configuration file
require $configFile;
// Check existance of EntityManager
if ( ! isset($em)) {
throw new \Doctrine\Common\DoctrineException(
'No EntityManager created in configuration'
);
}
// Check for gloal argument options here
if (isset($globalArguments)) {
// Merge arguments. Values specified via the CLI take preference.
$args = array_merge($globalArguments, $args);
}
return $em;
} else {
throw new \Doctrine\Common\DoctrineException(
'Requested configuration file [' . $configFile . '] does not exist'
);
}
return null;
}
} }
\ No newline at end of file
...@@ -60,6 +60,9 @@ abstract class AbstractTask ...@@ -60,6 +60,9 @@ abstract class AbstractTask
*/ */
protected $_availableTasks; protected $_availableTasks;
/**
* @var EntityManager The EntityManager instance
*/
protected $_em; protected $_em;
/** /**
...@@ -122,6 +125,26 @@ abstract class AbstractTask ...@@ -122,6 +125,26 @@ abstract class AbstractTask
return $this->_availableTasks; return $this->_availableTasks;
} }
/**
* Defines the EntityManager
*
* @param EntityManager The EntityManager instance
*/
public function setEntityManager($em)
{
$this->_em = $em;
}
/**
* Retrieves current EntityManager
*
* @return EntityManager
*/
public function getEntityManager()
{
return $this->_em;
}
/** /**
* Expose to CLI Output Printer the extended help of the given task. * Expose to CLI Output Printer the extended help of the given task.
* This means it should detail all parameters, options and the meaning * This means it should detail all parameters, options and the meaning
...@@ -159,27 +182,7 @@ abstract class AbstractTask ...@@ -159,27 +182,7 @@ abstract class AbstractTask
* *
* @return boolean * @return boolean
*/ */
public function validate() abstract public function validate();
{
if ( ! isset($this->_arguments['config'])) {
if (file_exists('./cli-config.php')) {
require './cli-config.php';
}
} else {
require $this->_arguments['config'];
}
// $em and $args come from the config
if (isset($em)) {
$this->_em = $em;
}
if (isset($args)) {
// Merge arguments. Values specified via the CLI take preference.
$this->_arguments = array_merge($args, $this->_arguments);
}
return true;
}
/** /**
* Safely execution of task. * Safely execution of task.
...@@ -187,13 +190,4 @@ abstract class AbstractTask ...@@ -187,13 +190,4 @@ abstract class AbstractTask
* what is supposed to do. * what is supposed to do.
*/ */
abstract public function run(); abstract public function run();
protected function _requireEntityManager()
{
if ( ! isset($this->_em)) {
$this->_printer->writeln('No EntityManager created in configuration but required by task ' . get_class($this), 'ERROR');
return false;
}
return true;
}
} }
\ No newline at end of file
...@@ -120,7 +120,7 @@ class ClearCacheTask extends AbstractTask ...@@ -120,7 +120,7 @@ class ClearCacheTask extends AbstractTask
$all = true; $all = true;
} }
$configuration = $this->_em->getConfiguration(); $configuration = $this->getEntityManager()->getConfiguration();
if ($query || $all) { if ($query || $all) {
$this->_doDelete( $this->_doDelete(
......
...@@ -111,8 +111,9 @@ class ConvertMappingTask extends AbstractTask ...@@ -111,8 +111,9 @@ class ConvertMappingTask extends AbstractTask
return false; return false;
} }
if ($args['from'][0] == 'database') { if ($args['from'][0] == 'database') {
$config = $this->_em->getConfiguration(); $em = $this->getEntityManager();
$config->setMetadataDriverImpl(new \Doctrine\ORM\Mapping\Driver\DatabaseDriver($this->_em->getConnection()->getSchemaManager())); $config = $em->getConfiguration();
$config->setMetadataDriverImpl(new \Doctrine\ORM\Mapping\Driver\DatabaseDriver($em->getConnection()->getSchemaManager()));
} }
return true; return true;
} }
......
...@@ -56,12 +56,17 @@ class EnsureProductionSettingsTask extends AbstractTask ...@@ -56,12 +56,17 @@ class EnsureProductionSettingsTask extends AbstractTask
{ {
$printer->writeln('ensure-production-settings', 'KEYWORD'); $printer->writeln('ensure-production-settings', 'KEYWORD');
} }
public function validate()
{
return true;
}
public function run() public function run()
{ {
$printer = $this->getPrinter(); $printer = $this->getPrinter();
try { try {
$this->_em->getConfiguration()->ensureProductionSettings(); $this->getEntityManager()->getConfiguration()->ensureProductionSettings();
} catch (\Doctrine\Common\DoctrineException $e) { } catch (\Doctrine\Common\DoctrineException $e) {
$printer->writeln($e->getMessage(), 'ERROR'); $printer->writeln($e->getMessage(), 'ERROR');
} }
......
...@@ -59,11 +59,8 @@ class GenerateProxiesTask extends AbstractTask ...@@ -59,11 +59,8 @@ class GenerateProxiesTask extends AbstractTask
$args = $this->getArguments(); $args = $this->getArguments();
$printer = $this->getPrinter(); $printer = $this->getPrinter();
if ( ! $this->_requireEntityManager()) { $metadataDriver = $this->getEntityManager()->getConfiguration()->getMetadataDriverImpl();
return false;
}
$metadataDriver = $this->_em->getConfiguration()->getMetadataDriverImpl();
if ($metadataDriver instanceof \Doctrine\ORM\Mapping\Driver\AnnotationDriver) { if ($metadataDriver instanceof \Doctrine\ORM\Mapping\Driver\AnnotationDriver) {
if ( ! isset($args['class-dir'])) { if ( ! isset($args['class-dir'])) {
$printer->writeln("The supplied configuration uses the annotation metadata driver." $printer->writeln("The supplied configuration uses the annotation metadata driver."
...@@ -84,17 +81,19 @@ class GenerateProxiesTask extends AbstractTask ...@@ -84,17 +81,19 @@ class GenerateProxiesTask extends AbstractTask
{ {
$args = $this->getArguments(); $args = $this->getArguments();
$cmf = $this->_em->getMetadataFactory(); $em = $this->getEntityManager();
$driver = $this->_em->getConfiguration()->getMetadataDriverImpl(); $cmf = $em->getMetadataFactory();
$driver = $em->getConfiguration()->getMetadataDriverImpl();
$classes = array(); $classes = array();
$preloadedClasses = $driver->preload(true); $preloadedClasses = $driver->preload(true);
foreach ($preloadedClasses as $className) { foreach ($preloadedClasses as $className) {
$classes[] = $cmf->getMetadataFor($className); $classes[] = $cmf->getMetadataFor($className);
} }
$printer = $this->getPrinter(); $printer = $this->getPrinter();
$factory = $this->_em->getProxyFactory(); $factory = $em->getProxyFactory();
if (empty($classes)) { if (empty($classes)) {
$printer->writeln('No classes to process.', 'INFO'); $printer->writeln('No classes to process.', 'INFO');
...@@ -103,8 +102,9 @@ class GenerateProxiesTask extends AbstractTask ...@@ -103,8 +102,9 @@ class GenerateProxiesTask extends AbstractTask
$factory->generateProxyClasses($classes, isset($args['to-dir']) ? $args['to-dir'] : null); $factory->generateProxyClasses($classes, isset($args['to-dir']) ? $args['to-dir'] : null);
$printer->writeln('Proxy classes generated to: ' . $printer->writeln(
(isset($args['to-dir']) ? $args['to-dir'] : $this->_em->getConfiguration()->getProxyDir()) 'Proxy classes generated to: ' .
); (isset($args['to-dir']) ? $args['to-dir'] : $em->getConfiguration()->getProxyDir())
);
} }
} }
\ No newline at end of file
...@@ -80,6 +80,7 @@ class HelpTask extends AbstractTask ...@@ -80,6 +80,7 @@ class HelpTask extends AbstractTask
$task = new $taskClass(); $task = new $taskClass();
$task->setAvailableTasks($availableTasks); $task->setAvailableTasks($availableTasks);
$task->setEntityManager($this->getEntityManager());
$task->setPrinter($this->getPrinter()); $task->setPrinter($this->getPrinter());
$task->setArguments($this->getArguments()); $task->setArguments($this->getArguments());
......
...@@ -101,7 +101,7 @@ class RunDqlTask extends AbstractTask ...@@ -101,7 +101,7 @@ class RunDqlTask extends AbstractTask
$args = $this->getArguments(); $args = $this->getArguments();
try { try {
$query = $this->_em->createQuery($args['dql']); $query = $this->getEntityManager()->createQuery($args['dql']);
$resultSet = $query->getResult(); $resultSet = $query->getResult();
$maxDepth = isset($args['depth']) ? $args['depth'] : 7; $maxDepth = isset($args['depth']) ? $args['depth'] : 7;
......
...@@ -113,11 +113,13 @@ class RunSqlTask extends AbstractTask ...@@ -113,11 +113,13 @@ class RunSqlTask extends AbstractTask
if (isset($args['file'])) { if (isset($args['file'])) {
//TODO //TODO
} else if (isset($args['sql'])) { } else if (isset($args['sql'])) {
$conn = $this->getEntityManager()->getConnection();
if (preg_match('/^select/i', $args['sql'])) { if (preg_match('/^select/i', $args['sql'])) {
$stmt = $this->_em->getConnection()->execute($args['sql']); $stmt = $conn->execute($args['sql']);
$resultSet = $stmt->fetchAll(\Doctrine\DBAL\Connection::FETCH_ASSOC); $resultSet = $stmt->fetchAll(\Doctrine\DBAL\Connection::FETCH_ASSOC);
} else { } else {
$resultSet = $this->_em->getConnection()->executeUpdate($args['sql']); $resultSet = $conn->executeUpdate($args['sql']);
} }
$maxDepth = isset($args['depth']) ? $args['depth'] : 7; $maxDepth = isset($args['depth']) ? $args['depth'] : 7;
......
...@@ -106,10 +106,6 @@ class SchemaToolTask extends AbstractTask ...@@ -106,10 +106,6 @@ class SchemaToolTask extends AbstractTask
$args = $this->getArguments(); $args = $this->getArguments();
$printer = $this->getPrinter(); $printer = $this->getPrinter();
if ( ! $this->_requireEntityManager()) {
return false;
}
if (array_key_exists('re-create', $args)) { if (array_key_exists('re-create', $args)) {
$args['drop'] = true; $args['drop'] = true;
$args['create'] = true; $args['create'] = true;
...@@ -130,7 +126,8 @@ class SchemaToolTask extends AbstractTask ...@@ -130,7 +126,8 @@ class SchemaToolTask extends AbstractTask
return false; return false;
} }
$metadataDriver = $this->_em->getConfiguration()->getMetadataDriverImpl(); $metadataDriver = $this->getEntityManager()->getConfiguration()->getMetadataDriverImpl();
if ($metadataDriver instanceof \Doctrine\ORM\Mapping\Driver\AnnotationDriver) { if ($metadataDriver instanceof \Doctrine\ORM\Mapping\Driver\AnnotationDriver) {
if ( ! isset($args['class-dir'])) { if ( ! isset($args['class-dir'])) {
$printer->writeln("The supplied configuration uses the annotation metadata driver." $printer->writeln("The supplied configuration uses the annotation metadata driver."
...@@ -155,23 +152,26 @@ class SchemaToolTask extends AbstractTask ...@@ -155,23 +152,26 @@ class SchemaToolTask extends AbstractTask
$isDrop = isset($args['drop']); $isDrop = isset($args['drop']);
$isUpdate = isset($args['update']); $isUpdate = isset($args['update']);
$cmf = $this->_em->getMetadataFactory(); $em = $this->getEntityManager();
$driver = $this->_em->getConfiguration()->getMetadataDriverImpl(); $cmf = $em->getMetadataFactory();
$driver = $em->getConfiguration()->getMetadataDriverImpl();
$classes = array(); $classes = array();
$preloadedClasses = $driver->preload(true); $preloadedClasses = $driver->preload(true);
foreach ($preloadedClasses as $className) { foreach ($preloadedClasses as $className) {
$classes[] = $cmf->getMetadataFor($className); $classes[] = $cmf->getMetadataFor($className);
} }
$printer = $this->getPrinter(); $printer = $this->getPrinter();
$tool = new SchemaTool($this->_em);
if (empty($classes)) { if (empty($classes)) {
$printer->writeln('No classes to process.', 'INFO'); $printer->writeln('No classes to process.', 'INFO');
return; return;
} }
$tool = new SchemaTool($em);
if ($isDrop) { if ($isDrop) {
if (isset($args['dump-sql'])) { if (isset($args['dump-sql'])) {
foreach ($tool->getDropSchemaSql($classes) as $sql) { foreach ($tool->getDropSchemaSql($classes) as $sql) {
......
...@@ -77,6 +77,6 @@ class VersionTask extends AbstractTask ...@@ -77,6 +77,6 @@ class VersionTask extends AbstractTask
*/ */
public function run() public function run()
{ {
$this->getPrinter()->writeln('You are currently running Doctrine 2.0.0 Alpha 1', 'INFO'); $this->getPrinter()->writeln('You are currently running Doctrine 2.0.0 Alpha 3', 'INFO');
} }
} }
\ No newline at end of file
...@@ -33,7 +33,9 @@ $connectionOptions = array( ...@@ -33,7 +33,9 @@ $connectionOptions = array(
'path' => 'database.sqlite' 'path' => 'database.sqlite'
); );
// These are required named variables (names can't change!)
$em = \Doctrine\ORM\EntityManager::create($connectionOptions, $config); $em = \Doctrine\ORM\EntityManager::create($connectionOptions, $config);
$args = array(
$globalArguments = array(
'class-dir' => './Entities' 'class-dir' => './Entities'
); );
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment