Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle readonly public properties, cf. #954 #999

Open
wants to merge 1 commit into
base: 3.4.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
199 changes: 166 additions & 33 deletions src/Proxy/ProxyGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,11 +143,21 @@ class <proxyShortClassName> extends \<className> implements \<baseProxyInterface
*/
public $__isInitialized__ = false;

/**
* @var array<string, null> properties to be lazy loaded, indexed by property name
*/
public static $identifier = <identifier>;

/**
* @var array<string, null> properties to be lazy loaded, indexed by property name
*/
public static $lazyPropertiesNames = <lazyPropertiesNames>;

/**
* @var array<string, null> readonly public properties
*/
public static $readonlyPropertiesNames = <readonlyPropertiesNames>;

/**
* @var array<string, mixed> default values of properties to be lazy loaded, with keys being the property names
*
Expand Down Expand Up @@ -409,7 +419,7 @@ public function generateEnumUseStatements(ClassMetadata $class): string
}

$defaultProperties = $class->getReflectionClass()->getDefaultProperties();
$lazyLoadedPublicProperties = $this->getLazyLoadedPublicPropertiesNames($class);
$lazyLoadedPublicProperties = $this->getWriteableLazyLoadedPublicPropertiesNames($class);
$enumClasses = [];

foreach ($class->getReflectionClass()->getProperties(ReflectionProperty::IS_PUBLIC) as $property) {
Expand Down Expand Up @@ -453,7 +463,7 @@ private function generateClassName(ClassMetadata $class)
*/
private function generateLazyPropertiesNames(ClassMetadata $class)
{
$lazyPublicProperties = $this->getLazyLoadedPublicPropertiesNames($class);
$lazyPublicProperties = $this->getWriteableLazyLoadedPublicPropertiesNames($class);
$values = [];

foreach ($lazyPublicProperties as $name) {
Expand All @@ -463,6 +473,40 @@ private function generateLazyPropertiesNames(ClassMetadata $class)
return var_export($values, true);
}

/**
* Generates the array representation of readonly public properties.
*
* @return string
*/
private function generateReadonlyPropertiesNames(ClassMetadata $class)
{
$readonlyPublicProperties = $this->getReadonlyPublicPropertiesNames($class);
$values = [];

foreach ($readonlyPublicProperties as $name) {
$values[$name] = null;
}

return var_export($values, true);
}

/**
* Generates the array representation of readonly public properties.
*
* @return string
*/
private function generateIdentifier(ClassMetadata $class)
{
$identifier = $class->getIdentifier();
$values = [];

foreach ($identifier as $name) {
$values[$name] = null;
}

return var_export($values, true);
}

/**
* Generates the array representation of lazy loaded public properties names.
*
Expand All @@ -486,9 +530,19 @@ public function __construct(?\Closure $initializer = null, ?\Closure $cloner = n

EOT;

$unsetReadonlyPublicProperties = array_map(static function (string $name): string {
return ' unset($this->' . $name . ');';
}, $this->getReadonlyPublicPropertiesNames($class));

if ($unsetReadonlyPublicProperties !== []) {
$constructorImpl .= ' (function () { unset( ' .
implode(', ', array_map(static fn ($name) => '$this->' . $name, $this->getReadonlyPublicPropertiesNames($class))) .
' ); } )(...)->bindTo($this, \\' . $class->getName() . '::class)->__invoke();' . "\n";
}

$toUnset = array_map(static function (string $name): string {
return '$this->' . $name;
}, $this->getLazyLoadedPublicPropertiesNames($class));
}, $this->getWriteableLazyLoadedPublicPropertiesNames($class));

return $constructorImpl . ($toUnset === [] ? '' : ' unset(' . implode(', ', $toUnset) . ");\n")
. <<<'EOT'
Expand All @@ -506,14 +560,15 @@ public function __construct(?\Closure $initializer = null, ?\Closure $cloner = n
*/
private function generateMagicGet(ClassMetadata $class)
{
$lazyPublicProperties = $this->getLazyLoadedPublicPropertiesNames($class);
$reflectionClass = $class->getReflectionClass();
$hasParentGet = false;
$returnReference = '';
$inheritDoc = '';
$name = '$name';
$parametersString = '$name';
$returnTypeHint = null;
$lazyPublicProperties = $this->getWriteableLazyLoadedPublicPropertiesNames($class);
$readonlyPublicProperties = $this->getReadonlyPublicPropertiesNames($class);
$reflectionClass = $class->getReflectionClass();
$hasParentGet = false;
$returnReference = '';
$inheritDoc = '';
$name = '$name';
$parametersString = '$name';
$returnTypeHint = null;

if ($reflectionClass->hasMethod('__get')) {
$hasParentGet = true;
Expand All @@ -531,7 +586,7 @@ private function generateMagicGet(ClassMetadata $class)
$returnTypeHint = $this->getMethodReturnType($methodReflection);
}

if (empty($lazyPublicProperties) && ! $hasParentGet) {
if (empty($readonlyPublicProperties) && empty($lazyPublicProperties) && ! $hasParentGet) {
return '';
}

Expand All @@ -545,9 +600,9 @@ public function {$returnReference}__get($parametersString)$returnTypeHint

EOT;

if (! empty($lazyPublicProperties)) {
if (! empty($lazyPublicProperties) || ! empty($readonlyPublicProperties)) {
$magicGet .= <<<'EOT'
if (\array_key_exists($name, self::$lazyPropertiesNames)) {
if (\array_key_exists($name, self::$lazyPropertiesNames) || \array_key_exists($name, self::$readonlyPropertiesNames)) {
$this->__initializer__ && $this->__initializer__->__invoke($this, '__get', [$name]);
EOT;

Expand Down Expand Up @@ -605,12 +660,13 @@ public function {$returnReference}__get($parametersString)$returnTypeHint
*/
private function generateMagicSet(ClassMetadata $class)
{
$lazyPublicProperties = $this->getLazyLoadedPublicPropertiesNames($class);
$reflectionClass = $class->getReflectionClass();
$hasParentSet = false;
$inheritDoc = '';
$parametersString = '$name, $value';
$returnTypeHint = null;
$lazyPublicProperties = $this->getWriteableLazyLoadedPublicPropertiesNames($class);
$readonlyPublicProperties = $this->getReadonlyPublicPropertiesNames($class);
$reflectionClass = $class->getReflectionClass();
$hasParentSet = false;
$inheritDoc = '';
$parametersString = '$name, $value';
$returnTypeHint = null;

if ($reflectionClass->hasMethod('__set')) {
$hasParentSet = true;
Expand All @@ -621,7 +677,7 @@ private function generateMagicSet(ClassMetadata $class)
$returnTypeHint = $this->getMethodReturnType($methodReflection);
}

if (empty($lazyPublicProperties) && ! $hasParentSet) {
if (empty($readonlyPublicProperties) && empty($lazyPublicProperties) && ! $hasParentSet) {
return '';
}

Expand All @@ -636,6 +692,21 @@ public function __set($parametersString)$returnTypeHint

EOT;

if (! empty($readonlyPublicProperties)) {
$magicSet .= <<<EOT

if ( array_key_exists( \$name, self::\$readonlyPropertiesNames ) ) {
if ( isset( \$this->\$name ) ) {
trigger_error(sprintf("Cannot modify readonly property {$class->getName()}::%s", \$name), E_USER_ERROR);
}

(function(string \$name, mixed \$value) { \$this->\$name = \$value; })(...)->bindTo( \$this, \\{$class->getName()}::class)->__invoke(\$name, \$value);

return;
}
EOT;
}

if (! empty($lazyPublicProperties)) {
$magicSet .= <<<'EOT'
if (\array_key_exists($name, self::$lazyPropertiesNames)) {
Expand Down Expand Up @@ -686,18 +757,20 @@ public function __set($parametersString)$returnTypeHint
*/
private function generateMagicIsset(ClassMetadata $class)
{
$lazyPublicProperties = $this->getLazyLoadedPublicPropertiesNames($class);
$hasParentIsset = $class->getReflectionClass()->hasMethod('__isset');
$parametersString = '$name';
$returnTypeHint = null;
$lazyPublicProperties = $this->getWriteableLazyLoadedPublicPropertiesNames($class);
$readonlyPublicProperties = $this->getReadonlyPublicPropertiesNames($class);
$identifier = $class->getIdentifier();
$hasParentIsset = $class->getReflectionClass()->hasMethod('__isset');
$parametersString = '$name';
$returnTypeHint = null;

if ($hasParentIsset) {
$methodReflection = $class->getReflectionClass()->getMethod('__isset');
$parametersString = $this->buildParametersString($methodReflection->getParameters(), ['name']);
$returnTypeHint = $this->getMethodReturnType($methodReflection);
}

if (empty($lazyPublicProperties) && ! $hasParentIsset) {
if (empty($readonlyPublicProperties) && empty($lazyPublicProperties) && ! $hasParentIsset) {
return '';
}

Expand All @@ -712,6 +785,17 @@ public function __isset($parametersString)$returnTypeHint
{

EOT;
if (! empty($readonlyPublicProperties)) {
$magicIsset .= <<<'EOT'
if (\array_key_exists($name, self::$readonlyPropertiesNames) ) {
$this->__initializer__ && !\array_key_exists($name, self::$identifier) && $this->__initializer__->__invoke($this, '__isset', [$name]);

return isset($this->$name);
}


EOT;
}

if (! empty($lazyPublicProperties)) {
$magicIsset .= <<<'EOT'
Expand Down Expand Up @@ -765,7 +849,7 @@ public function __sleep()$returnTypeHint
$properties = array_merge(['__isInitialized__'], parent::__sleep());

if ($this->__isInitialized__) {
$properties = array_diff($properties, array_keys(self::$lazyPropertiesNames));
$properties = array_diff($properties, array_keys(self::$lazyPropertiesNames), array_keys(self::$readonlyPropertiesNames));
}

return $properties;
Expand All @@ -786,7 +870,7 @@ public function __sleep()$returnTypeHint
: $prop->getName();
}

$lazyPublicProperties = $this->getLazyLoadedPublicPropertiesNames($class);
$lazyPublicProperties = $this->getWriteableLazyLoadedPublicPropertiesNames($class);
$protectedProperties = array_diff($allProperties, $lazyPublicProperties);

foreach ($allProperties as &$property) {
Expand Down Expand Up @@ -822,7 +906,7 @@ private function generateWakeupImpl(ClassMetadata $class)
$hasParentWakeup = $reflectionClass->hasMethod('__wakeup');

$unsetPublicProperties = [];
foreach ($this->getLazyLoadedPublicPropertiesNames($class) as $lazyPublicProperty) {
foreach ($this->getWriteableLazyLoadedPublicPropertiesNames($class) as $lazyPublicProperty) {
$unsetPublicProperties[] = '$this->' . $lazyPublicProperty;
}

Expand Down Expand Up @@ -851,6 +935,14 @@ public function __wakeup()$returnTypeHint

EOT;

$unsetReadonlyPublicProperties = array_diff($this->getReadonlyPublicPropertiesNames($class), $class->getIdentifier());

if ($unsetReadonlyPublicProperties !== []) {
$wakeupImpl .= ' (function () { unset( ' .
implode(', ', array_map(static fn ($name) => '$this->' . $name, $unsetReadonlyPublicProperties)) .
' ); } )(...)->bindTo($this, \\' . $class->getName() . '::class)->__invoke();' . "\n";
}

if (! empty($unsetPublicProperties)) {
$wakeupImpl .= "\n unset(" . implode(', ', $unsetPublicProperties) . ');';
}
Expand Down Expand Up @@ -879,12 +971,28 @@ private function generateCloneImpl(ClassMetadata $class)
$inheritDoc = $hasParentClone ? '{@inheritDoc}' : '';
$callParentClone = $hasParentClone ? "\n parent::__clone();\n" : '';

return <<<EOT
$cloner = <<<EOT
/**
* $inheritDoc
*/
public function __clone()$returnTypeHint
{

if ( ! \$this->__isInitialized__) {

EOT;
$unsetReadonlyPublicProperties = array_diff($this->getReadonlyPublicPropertiesNames($class), $class->getIdentifier());

if ($unsetReadonlyPublicProperties !== []) {
$cloner .= ' (function () { unset( ' .
implode(', ', array_map(static fn ($name) => '$this->' . $name, $unsetReadonlyPublicProperties)) .
' ); } )(...)->bindTo($this, \\' . $class->getName() . '::class)->__invoke();' . "\n";
}

return $cloner . <<<EOT

}

\$this->__cloner__ && \$this->__cloner__->__invoke(\$this, '__clone', []);
$callParentClone }
EOT;
Expand Down Expand Up @@ -1026,14 +1134,39 @@ private function isShortIdentifierGetter($method, ClassMetadata $class)
*
* @return array<int, string>
*/
private function getLazyLoadedPublicPropertiesNames(ClassMetadata $class): array
private function getWriteableLazyLoadedPublicPropertiesNames(ClassMetadata $class): array
{
$properties = [];

foreach ($class->getReflectionClass()->getProperties(ReflectionProperty::IS_PUBLIC) as $property) {
$name = $property->getName();

if (
(! $class->hasField($name) && ! $class->hasAssociation($name)) || $class->isIdentifier($name)
|| (method_exists($property, 'isReadonly') && $property->isReadOnly())
) {
continue;
}

$properties[] = $name;
}

return $properties;
}

/**
* Generates the list of readonly public properties.
*
* @return array<int, string>
*/
private function getReadonlyPublicPropertiesNames(ClassMetadata $class): array
{
$properties = [];

foreach ($class->getReflectionClass()->getProperties(ReflectionProperty::IS_PUBLIC) as $property) {
$name = $property->getName();

if ((! $class->hasField($name) && ! $class->hasAssociation($name)) || $class->isIdentifier($name)) {
if ((! method_exists($property, 'isReadonly') || ! $property->isReadOnly())) {
continue;
}

Expand All @@ -1051,7 +1184,7 @@ private function getLazyLoadedPublicPropertiesNames(ClassMetadata $class): array
private function getLazyLoadedPublicProperties(ClassMetadata $class)
{
$defaultProperties = $class->getReflectionClass()->getDefaultProperties();
$lazyLoadedPublicProperties = $this->getLazyLoadedPublicPropertiesNames($class);
$lazyLoadedPublicProperties = $this->getWriteableLazyLoadedPublicPropertiesNames($class);
$defaultValues = [];

foreach ($class->getReflectionClass()->getProperties(ReflectionProperty::IS_PUBLIC) as $property) {
Expand Down
Loading