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
* 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,
* with the namespace delimiters '\', replaced by dots '.'.
* This is the default behavior.
*
* Example:
* Class: My\Project\Model\User
......@@ -51,8 +52,8 @@ abstract class AbstractFileDriver implements Driver
/**
* 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
* require a naming convention or the convention of 1 class per mapping file.
* all mapping files in advance. It does not require a naming convention
* or the convention of 1 class per mapping file.
*
* @var integer
*/
......@@ -95,7 +96,7 @@ abstract class AbstractFileDriver implements Driver
*/
public function __construct($paths, $mode = self::FILE_PER_CLASS)
{
$this->_paths = $paths;
$this->_paths = (array) $paths;
$this->_mode = $mode;
}
......
......@@ -157,7 +157,7 @@ class Cli
try {
$this->_printer->writeln('Doctrine Command Line Interface' . PHP_EOL, 'HEADER');
// Handle possible multiple tasks on a single command
foreach($processedArgs as $taskData) {
// Retrieve the task name and arguments
......@@ -166,9 +166,13 @@ class Cli
// Check if task exists
if (isset($this->_tasks[$taskName]) && class_exists($this->_tasks[$taskName], true)) {
// Initializing EntityManager
$em = $this->_initializeEntityManager($processedArgs);
// Instantiate and execute the task
$task = new $this->_tasks[$taskName]();
$task->setAvailableTasks($this->_tasks);
$task->setEntityManager($em);
$task->setPrinter($this->_printer);
$task->setArguments($taskArguments);
......@@ -311,4 +315,42 @@ class Cli
// required and optional arguments here?
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
*/
protected $_availableTasks;
/**
* @var EntityManager The EntityManager instance
*/
protected $_em;
/**
......@@ -122,6 +125,26 @@ abstract class AbstractTask
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.
* This means it should detail all parameters, options and the meaning
......@@ -159,27 +182,7 @@ abstract class AbstractTask
*
* @return boolean
*/
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;
}
abstract public function validate();
/**
* Safely execution of task.
......@@ -187,13 +190,4 @@ abstract class AbstractTask
* what is supposed to do.
*/
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
$all = true;
}
$configuration = $this->_em->getConfiguration();
$configuration = $this->getEntityManager()->getConfiguration();
if ($query || $all) {
$this->_doDelete(
......
......@@ -111,8 +111,9 @@ class ConvertMappingTask extends AbstractTask
return false;
}
if ($args['from'][0] == 'database') {
$config = $this->_em->getConfiguration();
$config->setMetadataDriverImpl(new \Doctrine\ORM\Mapping\Driver\DatabaseDriver($this->_em->getConnection()->getSchemaManager()));
$em = $this->getEntityManager();
$config = $em->getConfiguration();
$config->setMetadataDriverImpl(new \Doctrine\ORM\Mapping\Driver\DatabaseDriver($em->getConnection()->getSchemaManager()));
}
return true;
}
......
......@@ -56,12 +56,17 @@ class EnsureProductionSettingsTask extends AbstractTask
{
$printer->writeln('ensure-production-settings', 'KEYWORD');
}
public function validate()
{
return true;
}
public function run()
{
$printer = $this->getPrinter();
try {
$this->_em->getConfiguration()->ensureProductionSettings();
$this->getEntityManager()->getConfiguration()->ensureProductionSettings();
} catch (\Doctrine\Common\DoctrineException $e) {
$printer->writeln($e->getMessage(), 'ERROR');
}
......
......@@ -59,11 +59,8 @@ class GenerateProxiesTask extends AbstractTask
$args = $this->getArguments();
$printer = $this->getPrinter();
if ( ! $this->_requireEntityManager()) {
return false;
}
$metadataDriver = $this->getEntityManager()->getConfiguration()->getMetadataDriverImpl();
$metadataDriver = $this->_em->getConfiguration()->getMetadataDriverImpl();
if ($metadataDriver instanceof \Doctrine\ORM\Mapping\Driver\AnnotationDriver) {
if ( ! isset($args['class-dir'])) {
$printer->writeln("The supplied configuration uses the annotation metadata driver."
......@@ -84,17 +81,19 @@ class GenerateProxiesTask extends AbstractTask
{
$args = $this->getArguments();
$cmf = $this->_em->getMetadataFactory();
$driver = $this->_em->getConfiguration()->getMetadataDriverImpl();
$em = $this->getEntityManager();
$cmf = $em->getMetadataFactory();
$driver = $em->getConfiguration()->getMetadataDriverImpl();
$classes = array();
$preloadedClasses = $driver->preload(true);
foreach ($preloadedClasses as $className) {
$classes[] = $cmf->getMetadataFor($className);
}
$printer = $this->getPrinter();
$factory = $this->_em->getProxyFactory();
$factory = $em->getProxyFactory();
if (empty($classes)) {
$printer->writeln('No classes to process.', 'INFO');
......@@ -103,8 +102,9 @@ class GenerateProxiesTask extends AbstractTask
$factory->generateProxyClasses($classes, isset($args['to-dir']) ? $args['to-dir'] : null);
$printer->writeln('Proxy classes generated to: ' .
(isset($args['to-dir']) ? $args['to-dir'] : $this->_em->getConfiguration()->getProxyDir())
);
$printer->writeln(
'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
$task = new $taskClass();
$task->setAvailableTasks($availableTasks);
$task->setEntityManager($this->getEntityManager());
$task->setPrinter($this->getPrinter());
$task->setArguments($this->getArguments());
......
......@@ -101,7 +101,7 @@ class RunDqlTask extends AbstractTask
$args = $this->getArguments();
try {
$query = $this->_em->createQuery($args['dql']);
$query = $this->getEntityManager()->createQuery($args['dql']);
$resultSet = $query->getResult();
$maxDepth = isset($args['depth']) ? $args['depth'] : 7;
......
......@@ -113,11 +113,13 @@ class RunSqlTask extends AbstractTask
if (isset($args['file'])) {
//TODO
} else if (isset($args['sql'])) {
$conn = $this->getEntityManager()->getConnection();
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);
} else {
$resultSet = $this->_em->getConnection()->executeUpdate($args['sql']);
$resultSet = $conn->executeUpdate($args['sql']);
}
$maxDepth = isset($args['depth']) ? $args['depth'] : 7;
......
......@@ -106,10 +106,6 @@ class SchemaToolTask extends AbstractTask
$args = $this->getArguments();
$printer = $this->getPrinter();
if ( ! $this->_requireEntityManager()) {
return false;
}
if (array_key_exists('re-create', $args)) {
$args['drop'] = true;
$args['create'] = true;
......@@ -130,7 +126,8 @@ class SchemaToolTask extends AbstractTask
return false;
}
$metadataDriver = $this->_em->getConfiguration()->getMetadataDriverImpl();
$metadataDriver = $this->getEntityManager()->getConfiguration()->getMetadataDriverImpl();
if ($metadataDriver instanceof \Doctrine\ORM\Mapping\Driver\AnnotationDriver) {
if ( ! isset($args['class-dir'])) {
$printer->writeln("The supplied configuration uses the annotation metadata driver."
......@@ -155,23 +152,26 @@ class SchemaToolTask extends AbstractTask
$isDrop = isset($args['drop']);
$isUpdate = isset($args['update']);
$cmf = $this->_em->getMetadataFactory();
$driver = $this->_em->getConfiguration()->getMetadataDriverImpl();
$em = $this->getEntityManager();
$cmf = $em->getMetadataFactory();
$driver = $em->getConfiguration()->getMetadataDriverImpl();
$classes = array();
$preloadedClasses = $driver->preload(true);
foreach ($preloadedClasses as $className) {
$classes[] = $cmf->getMetadataFor($className);
}
$printer = $this->getPrinter();
$tool = new SchemaTool($this->_em);
if (empty($classes)) {
$printer->writeln('No classes to process.', 'INFO');
return;
}
$tool = new SchemaTool($em);
if ($isDrop) {
if (isset($args['dump-sql'])) {
foreach ($tool->getDropSchemaSql($classes) as $sql) {
......
......@@ -77,6 +77,6 @@ class VersionTask extends AbstractTask
*/
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(
'path' => 'database.sqlite'
);
// These are required named variables (names can't change!)
$em = \Doctrine\ORM\EntityManager::create($connectionOptions, $config);
$args = array(
$globalArguments = array(
'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