1
0
mirror of synced 2025-01-31 04:21:44 +03:00

enable set visibilty of class fields in EntityGenerator

This commit is contained in:
Marcel Raaijmakers 2012-01-06 16:58:27 +01:00
parent 3fff83cd13
commit facd64ef2f
2 changed files with 23 additions and 4 deletions

View File

@ -86,6 +86,8 @@ class EntityGenerator
/** Whether or not to re-generate entity class if it exists already */ /** Whether or not to re-generate entity class if it exists already */
private $_regenerateEntityIfExists = false; private $_regenerateEntityIfExists = false;
private $_fieldVisibility = 'private';
private static $_classTemplate = private static $_classTemplate =
'<?php '<?php
@ -297,6 +299,22 @@ public function <methodName>()
$this->_generateAnnotations = $bool; $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. * Set an annotation prefix.
* *
@ -699,7 +717,7 @@ public function <methodName>()
} }
$lines[] = $this->_generateAssociationMappingPropertyDocBlock($associationMapping, $metadata); $lines[] = $this->_generateAssociationMappingPropertyDocBlock($associationMapping, $metadata);
$lines[] = $this->_spaces . 'private $' . $associationMapping['fieldName'] $lines[] = $this->_spaces . $this->_fieldVisibility . ' $' . $associationMapping['fieldName']
. ($associationMapping['type'] == 'manyToMany' ? ' = array()' : null) . ";\n"; . ($associationMapping['type'] == 'manyToMany' ? ' = array()' : null) . ";\n";
} }
@ -717,7 +735,7 @@ public function <methodName>()
} }
$lines[] = $this->_generateFieldMappingPropertyDocBlock($fieldMapping, $metadata); $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"; . (isset($fieldMapping['default']) ? ' = ' . var_export($fieldMapping['default'], true) : null) . ";\n";
} }

View File

@ -26,6 +26,7 @@ class EntityGeneratorTest extends \Doctrine\Tests\OrmTestCase
$this->_generator->setGenerateStubMethods(true); $this->_generator->setGenerateStubMethods(true);
$this->_generator->setRegenerateEntityIfExists(false); $this->_generator->setRegenerateEntityIfExists(false);
$this->_generator->setUpdateEntityIfExists(true); $this->_generator->setUpdateEntityIfExists(true);
$this->_generator->setFieldVisibility('protected');
} }
public function tearDown() 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('id'), "Regenerating keeps property 'id'.");
$this->assertTrue($reflClass->hasProperty('test'), "Check for property test failed."); $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->hasMethod('getTest'), "Check for method 'getTest' failed.");
$this->assertTrue($reflClass->getMethod('getTest')->isPublic(), "Check for public visibility of 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."); $this->assertTrue($reflClass->hasMethod('setTest'), "Check for method 'getTest' failed.");