From facd64ef2f0f131149c10799a2b18cc7700f26c0 Mon Sep 17 00:00:00 2001 From: Marcel Raaijmakers Date: Fri, 6 Jan 2012 16:58:27 +0100 Subject: [PATCH 1/5] enable set visibilty of class fields in EntityGenerator --- lib/Doctrine/ORM/Tools/EntityGenerator.php | 24 ++++++++++++++++--- .../Tests/ORM/Tools/EntityGeneratorTest.php | 3 ++- 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/lib/Doctrine/ORM/Tools/EntityGenerator.php b/lib/Doctrine/ORM/Tools/EntityGenerator.php index 66d79a837..eae008d7f 100644 --- a/lib/Doctrine/ORM/Tools/EntityGenerator.php +++ b/lib/Doctrine/ORM/Tools/EntityGenerator.php @@ -86,6 +86,8 @@ class EntityGenerator /** Whether or not to re-generate entity class if it exists already */ private $_regenerateEntityIfExists = false; + private $_fieldVisibility = 'private'; + private static $_classTemplate = '() { } -'; +'; public function __construct() { @@ -297,6 +299,22 @@ public function () $this->_generateAnnotations = $bool; } + /** + * Set whether or not to generate annotations for the entity + * + * @param bool $bool + * @return void + */ + public function setFieldVisibility($visibility) + { + if($visibility != 'private' && $visibility != 'protected') + { + throw new \InvalidArgumentException('Invalid provided visibilty (only private and protected are allowed): ' . $visibility); + } + + $this->_fieldVisibility = $visibility; + } + /** * Set an annotation prefix. * @@ -699,7 +717,7 @@ public function () } $lines[] = $this->_generateAssociationMappingPropertyDocBlock($associationMapping, $metadata); - $lines[] = $this->_spaces . 'private $' . $associationMapping['fieldName'] + $lines[] = $this->_spaces . $this->_fieldVisibility . ' $' . $associationMapping['fieldName'] . ($associationMapping['type'] == 'manyToMany' ? ' = array()' : null) . ";\n"; } @@ -717,7 +735,7 @@ public function () } $lines[] = $this->_generateFieldMappingPropertyDocBlock($fieldMapping, $metadata); - $lines[] = $this->_spaces . 'private $' . $fieldMapping['fieldName'] + $lines[] = $this->_spaces . $this->_fieldVisibility . ' $' . $fieldMapping['fieldName'] . (isset($fieldMapping['default']) ? ' = ' . var_export($fieldMapping['default'], true) : null) . ";\n"; } diff --git a/tests/Doctrine/Tests/ORM/Tools/EntityGeneratorTest.php b/tests/Doctrine/Tests/ORM/Tools/EntityGeneratorTest.php index 4b02a94d1..3b6c1a803 100644 --- a/tests/Doctrine/Tests/ORM/Tools/EntityGeneratorTest.php +++ b/tests/Doctrine/Tests/ORM/Tools/EntityGeneratorTest.php @@ -26,6 +26,7 @@ class EntityGeneratorTest extends \Doctrine\Tests\OrmTestCase $this->_generator->setGenerateStubMethods(true); $this->_generator->setRegenerateEntityIfExists(false); $this->_generator->setUpdateEntityIfExists(true); + $this->_generator->setFieldVisibility('protected'); } public function tearDown() @@ -133,7 +134,7 @@ class EntityGeneratorTest extends \Doctrine\Tests\OrmTestCase $this->assertTrue($reflClass->hasProperty('id'), "Regenerating keeps property 'id'."); $this->assertTrue($reflClass->hasProperty('test'), "Check for property test failed."); - $this->assertTrue($reflClass->getProperty('test')->isPrivate(), "Check for private property test failed."); + $this->assertTrue($reflClass->getProperty('test')->isProtected(), "Check for private property test failed."); $this->assertTrue($reflClass->hasMethod('getTest'), "Check for method 'getTest' failed."); $this->assertTrue($reflClass->getMethod('getTest')->isPublic(), "Check for public visibility of method 'getTest' failed."); $this->assertTrue($reflClass->hasMethod('setTest'), "Check for method 'getTest' failed."); From f26d43b3eadfbd89d95012430256b4e2d3253a3a Mon Sep 17 00:00:00 2001 From: Marcel Date: Fri, 13 Jan 2012 11:24:35 +0100 Subject: [PATCH 2/5] remove whitespace tabs -> spaces added class constants updated phpdoc --- lib/Doctrine/ORM/Tools/EntityGenerator.php | 29 ++++++++++++------- .../Tests/ORM/Tools/EntityGeneratorTest.php | 2 +- 2 files changed, 20 insertions(+), 11 deletions(-) diff --git a/lib/Doctrine/ORM/Tools/EntityGenerator.php b/lib/Doctrine/ORM/Tools/EntityGenerator.php index eae008d7f..a3f2a250c 100644 --- a/lib/Doctrine/ORM/Tools/EntityGenerator.php +++ b/lib/Doctrine/ORM/Tools/EntityGenerator.php @@ -47,6 +47,15 @@ use Doctrine\ORM\Mapping\ClassMetadataInfo, */ class EntityGenerator { + /** + * Specifies class fields should be protected + */ + const FIELD_VISIBLE_PROTECTED = 'protected'; + /** + * Specifies class fields should be private + */ + const FIELD_VISIBLE_PRIVATE = 'private'; + /** * @var bool */ @@ -86,8 +95,8 @@ class EntityGenerator /** Whether or not to re-generate entity class if it exists already */ private $_regenerateEntityIfExists = false; - private $_fieldVisibility = 'private'; - + private $_fieldVisibility = 'private'; + private static $_classTemplate = '() { } -'; +'; public function __construct() { @@ -300,21 +309,21 @@ public function () } /** - * Set whether or not to generate annotations for the entity + * Set the class fields visibility for the entity (can either be private or protected) * * @param bool $bool * @return void */ public function setFieldVisibility($visibility) { - if($visibility != 'private' && $visibility != 'protected') - { - throw new \InvalidArgumentException('Invalid provided visibilty (only private and protected are allowed): ' . $visibility); - } - + if($visibility != 'private' && $visibility != 'protected') + { + throw new \InvalidArgumentException('Invalid provided visibilty (only private and protected are allowed): ' . $visibility); + } + $this->_fieldVisibility = $visibility; } - + /** * Set an annotation prefix. * diff --git a/tests/Doctrine/Tests/ORM/Tools/EntityGeneratorTest.php b/tests/Doctrine/Tests/ORM/Tools/EntityGeneratorTest.php index 3b6c1a803..f3d84317b 100644 --- a/tests/Doctrine/Tests/ORM/Tools/EntityGeneratorTest.php +++ b/tests/Doctrine/Tests/ORM/Tools/EntityGeneratorTest.php @@ -26,7 +26,7 @@ class EntityGeneratorTest extends \Doctrine\Tests\OrmTestCase $this->_generator->setGenerateStubMethods(true); $this->_generator->setRegenerateEntityIfExists(false); $this->_generator->setUpdateEntityIfExists(true); - $this->_generator->setFieldVisibility('protected'); + $this->_generator->setFieldVisibility(EntityGenerator::FIELD_VISIBLE_PROTECTED); } public function tearDown() From 69f0d70a98874695f6fbda353f0d7cf07ccc4917 Mon Sep 17 00:00:00 2001 From: Marcel Date: Fri, 13 Jan 2012 14:14:28 +0100 Subject: [PATCH 3/5] fix if coding standard fix typo --- lib/Doctrine/ORM/Tools/EntityGenerator.php | 3 +-- tests/Doctrine/Tests/ORM/Tools/EntityGeneratorTest.php | 2 +- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/lib/Doctrine/ORM/Tools/EntityGenerator.php b/lib/Doctrine/ORM/Tools/EntityGenerator.php index a3f2a250c..f81f63bb3 100644 --- a/lib/Doctrine/ORM/Tools/EntityGenerator.php +++ b/lib/Doctrine/ORM/Tools/EntityGenerator.php @@ -316,8 +316,7 @@ public function () */ public function setFieldVisibility($visibility) { - if($visibility != 'private' && $visibility != 'protected') - { + if ($visibility != 'private' && $visibility != 'protected') { throw new \InvalidArgumentException('Invalid provided visibilty (only private and protected are allowed): ' . $visibility); } diff --git a/tests/Doctrine/Tests/ORM/Tools/EntityGeneratorTest.php b/tests/Doctrine/Tests/ORM/Tools/EntityGeneratorTest.php index f3d84317b..cdecdf871 100644 --- a/tests/Doctrine/Tests/ORM/Tools/EntityGeneratorTest.php +++ b/tests/Doctrine/Tests/ORM/Tools/EntityGeneratorTest.php @@ -134,7 +134,7 @@ class EntityGeneratorTest extends \Doctrine\Tests\OrmTestCase $this->assertTrue($reflClass->hasProperty('id'), "Regenerating keeps property 'id'."); $this->assertTrue($reflClass->hasProperty('test'), "Check for property test failed."); - $this->assertTrue($reflClass->getProperty('test')->isProtected(), "Check for private property test failed."); + $this->assertTrue($reflClass->getProperty('test')->isProtected(), "Check for protected property test failed."); $this->assertTrue($reflClass->hasMethod('getTest'), "Check for method 'getTest' failed."); $this->assertTrue($reflClass->getMethod('getTest')->isPublic(), "Check for public visibility of method 'getTest' failed."); $this->assertTrue($reflClass->hasMethod('setTest'), "Check for method 'getTest' failed."); From 72d5d0281a9465ec9cccdf7d555ba7a8edb6a72b Mon Sep 17 00:00:00 2001 From: Marcel Date: Fri, 13 Jan 2012 14:34:34 +0100 Subject: [PATCH 4/5] use !== to check field visibility use class constants --- lib/Doctrine/ORM/Tools/EntityGenerator.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/Doctrine/ORM/Tools/EntityGenerator.php b/lib/Doctrine/ORM/Tools/EntityGenerator.php index f81f63bb3..36ebc52ca 100644 --- a/lib/Doctrine/ORM/Tools/EntityGenerator.php +++ b/lib/Doctrine/ORM/Tools/EntityGenerator.php @@ -316,7 +316,7 @@ public function () */ public function setFieldVisibility($visibility) { - if ($visibility != 'private' && $visibility != 'protected') { + if ($visibility !== EntityGenerator::FIELD_VISIBLE_PRIVATE && $visibility !== EntityGenerator::FIELD_VISIBLE_PROTECTED) { throw new \InvalidArgumentException('Invalid provided visibilty (only private and protected are allowed): ' . $visibility); } From f76d3274136874ec84830006823d7e6fdcbea5df Mon Sep 17 00:00:00 2001 From: Marcel Date: Fri, 13 Jan 2012 14:43:13 +0100 Subject: [PATCH 5/5] use self:: instead of EntityGenerator:: --- lib/Doctrine/ORM/Tools/EntityGenerator.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/Doctrine/ORM/Tools/EntityGenerator.php b/lib/Doctrine/ORM/Tools/EntityGenerator.php index 36ebc52ca..5a7dba0c3 100644 --- a/lib/Doctrine/ORM/Tools/EntityGenerator.php +++ b/lib/Doctrine/ORM/Tools/EntityGenerator.php @@ -316,7 +316,7 @@ public function () */ public function setFieldVisibility($visibility) { - if ($visibility !== EntityGenerator::FIELD_VISIBLE_PRIVATE && $visibility !== EntityGenerator::FIELD_VISIBLE_PROTECTED) { + if ($visibility !== self::FIELD_VISIBLE_PRIVATE && $visibility !== self::FIELD_VISIBLE_PROTECTED) { throw new \InvalidArgumentException('Invalid provided visibilty (only private and protected are allowed): ' . $visibility); }