Commit c6678a0f authored by jwage's avatar jwage

[2.0] Renaming orm:generate-entity-stubs to orm:generate-entities to be...

[2.0] Renaming orm:generate-entity-stubs to orm:generate-entities to be consistent and fixed a few bugs
parent c41a08a6
...@@ -84,7 +84,7 @@ class CliController extends AbstractNamespace ...@@ -84,7 +84,7 @@ class CliController extends AbstractNamespace
->addTask('schema-tool', $ns . '\SchemaToolTask') ->addTask('schema-tool', $ns . '\SchemaToolTask')
->addTask('version', $ns . '\VersionTask') ->addTask('version', $ns . '\VersionTask')
->addTask('convert-d1-schema', $ns . '\ConvertDoctrine1SchemaTask') ->addTask('convert-d1-schema', $ns . '\ConvertDoctrine1SchemaTask')
->addTask('generate-entity-stubs', $ns . '\GenerateEntityStubsTask'); ->addTask('generate-entities', $ns . '\GenerateEntitiesTask');
$ns = 'Doctrine\DBAL\Tools\Cli\Tasks'; $ns = 'Doctrine\DBAL\Tools\Cli\Tasks';
$this->addNamespace('Dbal') $this->addNamespace('Dbal')
......
...@@ -40,7 +40,7 @@ use Doctrine\Common\Cli\Tasks\AbstractTask, ...@@ -40,7 +40,7 @@ use Doctrine\Common\Cli\Tasks\AbstractTask,
* @author Jonathan Wage <jonwage@gmail.com> * @author Jonathan Wage <jonwage@gmail.com>
* @author Roman Borschel <roman@code-factory.org> * @author Roman Borschel <roman@code-factory.org>
*/ */
class GenerateEntityStubsTask extends ConvertMappingTask class GenerateEntitiesTask extends ConvertMappingTask
{ {
/** /**
* @inheritdoc * @inheritdoc
...@@ -53,7 +53,7 @@ class GenerateEntityStubsTask extends ConvertMappingTask ...@@ -53,7 +53,7 @@ class GenerateEntityStubsTask extends ConvertMappingTask
)); ));
$doc = $this->getDocumentation(); $doc = $this->getDocumentation();
$doc->setName('generate-entity-stubs') $doc->setName('generate-entities')
->setDescription('Generate entity classes and method stubs from your mapping information.') ->setDescription('Generate entity classes and method stubs from your mapping information.')
->getOptionGroup() ->getOptionGroup()
->addOption($options); ->addOption($options);
...@@ -82,7 +82,7 @@ class GenerateEntityStubsTask extends ConvertMappingTask ...@@ -82,7 +82,7 @@ class GenerateEntityStubsTask extends ConvertMappingTask
$dest = realpath($arguments['dest']); $dest = realpath($arguments['dest']);
$generator = new EntityGenerator(); $generator = new EntityGenerator();
$generator->setGenerateAnnotations(true); $generator->setGenerateAnnotations(false);
$generator->setGenerateStubMethods(true); $generator->setGenerateStubMethods(true);
$generator->setRegenerateEntityIfExists(false); $generator->setRegenerateEntityIfExists(false);
$generator->setUpdateEntityIfExists(true); $generator->setUpdateEntityIfExists(true);
......
...@@ -54,8 +54,8 @@ class EntityGenerator ...@@ -54,8 +54,8 @@ class EntityGenerator
/** Whether or not the current ClassMetadataInfo instance is new or old */ /** Whether or not the current ClassMetadataInfo instance is new or old */
private $_isNew; private $_isNew;
/** If $_isNew is false then current code contains the current code from disk */ /** If isNew is false then this variable contains instance of ReflectionClass for current entity */
private $_currentCode; private $_reflection;
/** Number of spaces to use for indention in generated code */ /** Number of spaces to use for indention in generated code */
private $_numSpaces = 4; private $_numSpaces = 4;
...@@ -119,6 +119,11 @@ class EntityGenerator ...@@ -119,6 +119,11 @@ class EntityGenerator
$this->_isNew = ! file_exists($path); $this->_isNew = ! file_exists($path);
if ( ! $this->_isNew) {
require_once $path;
$this->_reflection = new \ReflectionClass($metadata->name);
}
// If entity doesn't exist or we're re-generating the entities entirely // If entity doesn't exist or we're re-generating the entities entirely
if ($this->_isNew || ( ! $this->_isNew && $this->_regenerateEntityIfExists)) { if ($this->_isNew || ( ! $this->_isNew && $this->_regenerateEntityIfExists)) {
file_put_contents($path, $this->generateEntityClass($metadata)); file_put_contents($path, $this->generateEntityClass($metadata));
...@@ -166,12 +171,12 @@ class EntityGenerator ...@@ -166,12 +171,12 @@ class EntityGenerator
*/ */
public function generateUpdatedEntityClass(ClassMetadataInfo $metadata, $path) public function generateUpdatedEntityClass(ClassMetadataInfo $metadata, $path)
{ {
$this->_currentCode = file_get_contents($path); $currentCode = file_get_contents($path);
$body = $this->_generateEntityBody($metadata); $body = $this->_generateEntityBody($metadata);
$last = strrpos($this->_currentCode, '}'); $last = strrpos($currentCode, '}');
$code = substr($this->_currentCode, 0, $last) . $body . '}'; $code = substr($currentCode, 0, $last) . $body . '}';
return $code; return $code;
} }
...@@ -300,7 +305,7 @@ class EntityGenerator ...@@ -300,7 +305,7 @@ class EntityGenerator
if ($this->_isNew) { if ($this->_isNew) {
return false; return false;
} else { } else {
return strpos($this->_currentCode, '$' . $property) !== false ? true : false; return $this->_reflection->hasProperty($property);
} }
} }
...@@ -309,7 +314,7 @@ class EntityGenerator ...@@ -309,7 +314,7 @@ class EntityGenerator
if ($this->_isNew) { if ($this->_isNew) {
return false; return false;
} else { } else {
return strpos($this->_currentCode, 'function ' . $method) !== false ? true : false; return $this->_reflection->hasMethod($method);
} }
} }
...@@ -394,7 +399,9 @@ class EntityGenerator ...@@ -394,7 +399,9 @@ class EntityGenerator
private function _generateTableAnnotation($metadata) private function _generateTableAnnotation($metadata)
{ {
$table = array(); $table = array();
if ($metadata->primaryTable['name']) {
$table[] = 'name="' . $metadata->primaryTable['name'] . '"'; $table[] = 'name="' . $metadata->primaryTable['name'] . '"';
}
if (isset($metadata->primaryTable['schema'])) { if (isset($metadata->primaryTable['schema'])) {
$table[] = 'schema="' . $metadata->primaryTable['schema'] . '"'; $table[] = 'schema="' . $metadata->primaryTable['schema'] . '"';
......
...@@ -11,6 +11,17 @@ require_once __DIR__ . '/../../TestInit.php'; ...@@ -11,6 +11,17 @@ require_once __DIR__ . '/../../TestInit.php';
class EntityGeneratorTest extends \Doctrine\Tests\OrmTestCase class EntityGeneratorTest extends \Doctrine\Tests\OrmTestCase
{ {
private $_generator;
public function setUp()
{
$this->_generator = new EntityGenerator();
$this->_generator->setGenerateAnnotations(true);
$this->_generator->setGenerateStubMethods(true);
$this->_generator->setRegenerateEntityIfExists(false);
$this->_generator->setUpdateEntityIfExists(true);
}
public function testWriteEntityClass() public function testWriteEntityClass()
{ {
$metadata = new ClassMetadataInfo('EntityGeneratorBook'); $metadata = new ClassMetadataInfo('EntityGeneratorBook');
...@@ -33,22 +44,20 @@ class EntityGeneratorTest extends \Doctrine\Tests\OrmTestCase ...@@ -33,22 +44,20 @@ class EntityGeneratorTest extends \Doctrine\Tests\OrmTestCase
)); ));
$metadata->setIdGeneratorType(ClassMetadataInfo::GENERATOR_TYPE_AUTO); $metadata->setIdGeneratorType(ClassMetadataInfo::GENERATOR_TYPE_AUTO);
$generator = new EntityGenerator(); $this->_generator->writeEntityClass($metadata, __DIR__);
$generator->setGenerateAnnotations(true);
$generator->setGenerateStubMethods(true);
$generator->setRegenerateEntityIfExists(false);
$generator->setUpdateEntityIfExists(true);
$generator->writeEntityClass($metadata, __DIR__);
$path = __DIR__ . '/EntityGeneratorBook.php'; $path = __DIR__ . '/EntityGeneratorBook.php';
$this->assertTrue(file_exists($path)); $this->assertTrue(file_exists($path));
require_once $path; require_once $path;
return $metadata;
} }
/** /**
* @depends testWriteEntityClass * @depends testWriteEntityClass
* @param ClassMetadata $metadata
*/ */
public function testGeneratedEntityClassMethods() public function testGeneratedEntityClassMethods($metadata)
{ {
$this->assertTrue(method_exists('\EntityGeneratorBook', 'getId')); $this->assertTrue(method_exists('\EntityGeneratorBook', 'getId'));
$this->assertTrue(method_exists('\EntityGeneratorBook', 'setName')); $this->assertTrue(method_exists('\EntityGeneratorBook', 'setName'));
...@@ -74,6 +83,24 @@ class EntityGeneratorTest extends \Doctrine\Tests\OrmTestCase ...@@ -74,6 +83,24 @@ class EntityGeneratorTest extends \Doctrine\Tests\OrmTestCase
$book->addAuthor('Test'); $book->addAuthor('Test');
$this->assertEquals(array('Test'), $book->getAuthor()); $this->assertEquals(array('Test'), $book->getAuthor());
return $metadata;
}
/**
* @depends testGeneratedEntityClassMethods
* @param ClassMetadata $metadata
*/
public function testEntityUpdatingWorks($metadata)
{
$metadata->mapField(array('fieldName' => 'test', 'type' => 'varchar'));
$this->_generator->writeEntityClass($metadata, __DIR__);
$code = file_get_contents(__DIR__ . '/EntityGeneratorBook.php');
$this->assertTrue(strstr($code, 'private $test;') !== false);
$this->assertTrue(strstr($code, 'private $test;') !== false);
$this->assertTrue(strstr($code, 'public function getTest(') !== false);
$this->assertTrue(strstr($code, 'public function setTest(') !== false);
unlink(__DIR__ . '/EntityGeneratorBook.php'); unlink(__DIR__ . '/EntityGeneratorBook.php');
} }
} }
\ 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