Skip to content

[Feature] Add private properties interception #412

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

Merged
merged 3 commits into from
Feb 14, 2019
Merged
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
2 changes: 1 addition & 1 deletion demos/Demo/Aspect/PropertyInterceptorAspect.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class PropertyInterceptorAspect implements Aspect
*
* @param FieldAccess $fieldAccess Joinpoint
*
* @Around("access(public|protected Demo\Example\PropertyDemo->*)")
* @Around("access(public|protected|private Demo\Example\PropertyDemo->*)")
* @return mixed
*/
public function aroundFieldAccess(FieldAccess $fieldAccess)
Expand Down
3 changes: 3 additions & 0 deletions demos/Demo/Example/PropertyDemo.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ class PropertyDemo

protected $protectedProperty = 456;

private $privateProperty = 'test';

protected $indirectModificationCheck = [4, 5, 6];

public function showProtected()
Expand All @@ -38,5 +40,6 @@ public function __construct()
if (count($this->indirectModificationCheck) !== 6) {
throw new \RuntimeException("Indirect modification doesn't work!");
}
$this->privateProperty = $this->privateProperty . 'bar';
}
}
16 changes: 11 additions & 5 deletions src/Aop/Framework/ClassFieldAccess.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,18 +117,24 @@ public function &getValueToSet()
*/
public function ensureScopeRule(int $stackLevel = 2): void
{
$property = $this->reflectionProperty;

if ($property->isProtected()) {
$property = $this->reflectionProperty;
$isProtected = $property->isProtected();
$isPrivate = $property->isPrivate();
if ($isProtected || $isPrivate) {
$backTrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, $stackLevel+1);
$accessor = $backTrace[$stackLevel] ?? [];
$propertyClass = $property->class;
if (isset($accessor['class'])) {
if ($accessor['class'] === $propertyClass || is_subclass_of($accessor['class'], $propertyClass)) {
// For private and protected properties its ok to access from the same class
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

if ($accessor['class'] === $propertyClass) {
return;
}
// For protected properties its ok to access from any subclass
if ($isProtected && is_subclass_of($accessor['class'], $propertyClass)) {
return;
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The exception message in line 137 below is probably no longer accurate: Cannot access protected property....

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, somehow have forgotten about this line )

}
throw new AspectException("Cannot access protected property {$propertyClass}::{$property->name}");
throw new AspectException("Cannot access property {$propertyClass}::{$property->name}");
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/Core/AdviceMatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ private function getAdvicesFromAdvisor(

// Check properties in class only for property filters
if ($filterKind & Aop\PointFilter::KIND_PROPERTY) {
$mask = ReflectionProperty::IS_PUBLIC | ReflectionProperty::IS_PROTECTED;
$mask = ReflectionProperty::IS_PUBLIC | ReflectionProperty::IS_PROTECTED | ReflectionProperty::IS_PRIVATE;
foreach ($class->getProperties($mask) as $property) {
if ($filter->matches($property, $class) && !$property->isStatic()) {
$classAdvices[AspectContainer::PROPERTY_PREFIX][$property->name][$advisorId] = $advisor->getAdvice();
Expand Down
15 changes: 9 additions & 6 deletions src/Proxy/Part/InterceptedConstructorGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,16 +85,19 @@ private function getConstructorBody(array $interceptedProperties): string
$assocProperties = [];
$listProperties = [];
foreach ($interceptedProperties as $propertyName) {
$assocProperties[] = " '{$propertyName}' => &\$this->{$propertyName}";
$listProperties[] = " \$this->{$propertyName}";
$assocProperties[] = " '{$propertyName}' => &\$target->{$propertyName}";
$listProperties[] = " \$target->{$propertyName}";
}
$lines = [
'$this->__properties = [',
'$accessor = function(array &$propertyStorage, object $target) {',
' $propertyStorage = [',
implode(',' . PHP_EOL, $assocProperties),
'];',
'unset(',
' ];',
' unset(',
implode(',' . PHP_EOL, $listProperties),
');'
' );',
'};',
'($accessor->bindTo($this, parent::class))($this->__properties, $this);'
];

return implode(PHP_EOL, $lines);
Expand Down
19 changes: 11 additions & 8 deletions tests/Go/Proxy/Part/InterceptedConstructorGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,14 +93,17 @@ public function testGenerateWithProperties(): void
$expectedCode = preg_replace('/^\s+|\s+$/m', '', '
public function __construct()
{
$this->__properties = [
\'foo\' => &$this->foo,
\'bar\' => &$this->bar
];
unset(
$this->foo,
$this->bar
);
$accessor = function(array &$propertyStorage, object $target) {
$propertyStorage = [
\'foo\' => &$target->foo,
\'bar\' => &$target->bar
];
unset(
$target->foo,
$target->bar
);
};
($accessor->bindTo($this, parent::class))($this->__properties, $this);
}'
);
$this->assertSame($expectedCode, $generatedCode);
Expand Down