Merge pull request #253 from mrmkrs/protectedfields
enable set visibilty of class fields in EntityGenerator
This commit is contained in:
commit
3c4d2cd890
@ -47,6 +47,15 @@ use Doctrine\ORM\Mapping\ClassMetadataInfo,
|
|||||||
*/
|
*/
|
||||||
class EntityGenerator
|
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
|
* @var bool
|
||||||
*/
|
*/
|
||||||
@ -86,6 +95,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
|
||||||
|
|
||||||
@ -299,6 +310,21 @@ public function <methodName>()
|
|||||||
$this->_generateAnnotations = $bool;
|
$this->_generateAnnotations = $bool;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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 !== self::FIELD_VISIBLE_PRIVATE && $visibility !== self::FIELD_VISIBLE_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.
|
||||||
*
|
*
|
||||||
@ -724,7 +750,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";
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -742,7 +768,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";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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(EntityGenerator::FIELD_VISIBLE_PROTECTED);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function tearDown()
|
public function tearDown()
|
||||||
@ -135,7 +136,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 protected 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.");
|
||||||
|
Loading…
x
Reference in New Issue
Block a user