Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature #30704 [PropertyInfo] Add accessor and mutator extractor inte…
…rface and implementation on reflection (joelwurtz, Korbeil) This PR was merged into the 5.1-dev branch. Discussion ---------- [PropertyInfo] Add accessor and mutator extractor interface and implementation on reflection | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #30248, partially: #22190, #18016, #5013, #9336, #5219, | License | MIT | Doc PR | TODO This PR brings accessor / mutator extraction on the PropertyInfo component, There is no link to existing code, as IMO it should be in another PR as this will add a dependency on property access to the property info component and not sure this is something wanted (although, it will reduce a lot of code base on the property access component as a lot of code seems to be duplicated) Code is extracted from #30248 also there is some new features (that can be removed if not wanted) * Allow extracting private accessor / mutator (will do a new PR that improve private extraction on reflection latter) * Allow extracting static accessor / mutators * Allow extracting constructor mutators Current implementation try to be as close as the PropertyAccess implementation and i did not reuse some methods already available in the class as there is some differences in implementation, but maybe it will be a good time to make this consistent (Looking forward to your input) ? Things that should be done in a new PR: * Linking property info to property access to remove a lot of duplicate code * Add a new system that allow adding Virtual Property based on this extractor Commits ------- 0a92dab Rebase, fix tests, review & update CHANGELOG fc25086 [PropertyInfo] Add accessor and mutator extractor interface and implementation on reflection
- Loading branch information
Showing
13 changed files
with
807 additions
and
342 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
380 changes: 81 additions & 299 deletions
380
src/Symfony/Component/PropertyAccess/PropertyAccessor.php
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
372 changes: 333 additions & 39 deletions
372
src/Symfony/Component/PropertyInfo/Extractor/ReflectionExtractor.php
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\PropertyInfo; | ||
|
||
/** | ||
* The property read info tells how a property can be read. | ||
* | ||
* @author Joel Wurtz <jwurtz@jolicode.com> | ||
* | ||
* @internal | ||
*/ | ||
final class PropertyReadInfo | ||
{ | ||
public const TYPE_METHOD = 'method'; | ||
public const TYPE_PROPERTY = 'property'; | ||
|
||
public const VISIBILITY_PUBLIC = 'public'; | ||
public const VISIBILITY_PROTECTED = 'protected'; | ||
public const VISIBILITY_PRIVATE = 'private'; | ||
|
||
private $type; | ||
|
||
private $name; | ||
|
||
private $visibility; | ||
|
||
private $static; | ||
|
||
private $byRef; | ||
|
||
public function __construct(string $type, string $name, string $visibility, bool $static, bool $byRef) | ||
{ | ||
$this->type = $type; | ||
$this->name = $name; | ||
$this->visibility = $visibility; | ||
$this->static = $static; | ||
$this->byRef = $byRef; | ||
} | ||
|
||
/** | ||
* Get type of access. | ||
*/ | ||
public function getType(): string | ||
{ | ||
return $this->type; | ||
} | ||
|
||
/** | ||
* Get name of the access, which can be a method name or a property name, depending on the type. | ||
*/ | ||
public function getName(): string | ||
{ | ||
return $this->name; | ||
} | ||
|
||
public function getVisibility(): string | ||
{ | ||
return $this->visibility; | ||
} | ||
|
||
public function isStatic(): bool | ||
{ | ||
return $this->static; | ||
} | ||
|
||
/** | ||
* Whether this accessor can be accessed by reference. | ||
*/ | ||
public function canBeReference(): bool | ||
{ | ||
return $this->byRef; | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
src/Symfony/Component/PropertyInfo/PropertyReadInfoExtractorInterface.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\PropertyInfo; | ||
|
||
/** | ||
* Extract read information for the property of a class. | ||
* | ||
* @author Joel Wurtz <jwurtz@jolicode.com> | ||
*/ | ||
interface PropertyReadInfoExtractorInterface | ||
{ | ||
/** | ||
* Get read information object for a given property of a class. | ||
*/ | ||
public function getReadInfo(string $class, string $property, array $context = []): ?PropertyReadInfo; | ||
} |
123 changes: 123 additions & 0 deletions
123
src/Symfony/Component/PropertyInfo/PropertyWriteInfo.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\PropertyInfo; | ||
|
||
/** | ||
* The write mutator defines how a property can be written. | ||
* | ||
* @author Joel Wurtz <jwurtz@jolicode.com> | ||
* | ||
* @internal | ||
*/ | ||
final class PropertyWriteInfo | ||
{ | ||
public const TYPE_NONE = 'none'; | ||
public const TYPE_METHOD = 'method'; | ||
public const TYPE_PROPERTY = 'property'; | ||
public const TYPE_ADDER_AND_REMOVER = 'adder_and_remover'; | ||
public const TYPE_CONSTRUCTOR = 'constructor'; | ||
|
||
public const VISIBILITY_PUBLIC = 'public'; | ||
public const VISIBILITY_PROTECTED = 'protected'; | ||
public const VISIBILITY_PRIVATE = 'private'; | ||
|
||
private $type; | ||
private $name; | ||
private $visibility; | ||
private $static; | ||
private $adderInfo; | ||
private $removerInfo; | ||
private $errors = []; | ||
|
||
public function __construct(string $type = self::TYPE_NONE, string $name = null, string $visibility = null, bool $static = null) | ||
{ | ||
$this->type = $type; | ||
$this->name = $name; | ||
$this->visibility = $visibility; | ||
$this->static = $static; | ||
} | ||
|
||
public function getType(): string | ||
{ | ||
return $this->type; | ||
} | ||
|
||
public function getName(): string | ||
{ | ||
if (null === $this->name) { | ||
throw new \LogicException("Calling getName() when having a mutator of type {$this->type} is not tolerated"); | ||
} | ||
|
||
return $this->name; | ||
} | ||
|
||
public function setAdderInfo(self $adderInfo): void | ||
{ | ||
$this->adderInfo = $adderInfo; | ||
} | ||
|
||
public function getAdderInfo(): self | ||
{ | ||
if (null === $this->adderInfo) { | ||
throw new \LogicException("Calling getAdderInfo() when having a mutator of type {$this->type} is not tolerated"); | ||
} | ||
|
||
return $this->adderInfo; | ||
} | ||
|
||
public function setRemoverInfo(self $removerInfo): void | ||
{ | ||
$this->removerInfo = $removerInfo; | ||
} | ||
|
||
public function getRemoverInfo(): self | ||
{ | ||
if (null === $this->removerInfo) { | ||
throw new \LogicException("Calling getRemoverInfo() when having a mutator of type {$this->type} is not tolerated"); | ||
} | ||
|
||
return $this->removerInfo; | ||
} | ||
|
||
public function getVisibility(): string | ||
{ | ||
if (null === $this->visibility) { | ||
throw new \LogicException("Calling getVisibility() when having a mutator of type {$this->type} is not tolerated"); | ||
} | ||
|
||
return $this->visibility; | ||
} | ||
|
||
public function isStatic(): bool | ||
{ | ||
if (null === $this->static) { | ||
throw new \LogicException("Calling isStatic() when having a mutator of type {$this->type} is not tolerated"); | ||
} | ||
|
||
return $this->static; | ||
} | ||
|
||
public function setErrors(array $errors): void | ||
{ | ||
$this->errors = $errors; | ||
} | ||
|
||
public function getErrors(): array | ||
{ | ||
return $this->errors; | ||
} | ||
|
||
public function hasErrors(): bool | ||
{ | ||
return (bool) \count($this->errors); | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
src/Symfony/Component/PropertyInfo/PropertyWriteInfoExtractorInterface.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\PropertyInfo; | ||
|
||
/** | ||
* Extract write information for the property of a class. | ||
* | ||
* @author Joel Wurtz <jwurtz@jolicode.com> | ||
*/ | ||
interface PropertyWriteInfoExtractorInterface | ||
{ | ||
/** | ||
* Get write information object for a given property of a class. | ||
*/ | ||
public function getWriteInfo(string $class, string $property, array $context = []): ?PropertyWriteInfo; | ||
} |
Oops, something went wrong.