getField() public method

Gets the field being accessed.
public getField ( ) : ReflectionProperty
return ReflectionProperty the field being accessed.
 /**
  * @param FieldAccess $access
  *
  * @return void
  *
  * @throws \ErrorException|\Exception
  */
 public function __invoke(FieldAccess $access)
 {
     if (FieldAccess::WRITE !== $access->getAccessType()) {
         return;
     }
     $that = $access->getThis();
     $contextClass = $that ? get_class($that) : $access->getField()->getDeclaringClass()->getName();
     $baseCheckers = [new IntegerTypeChecker(), new CallableTypeChecker(), new StringTypeChecker(), new GenericObjectTypeChecker(), new ObjectTypeChecker(), new ResourceTypeChecker(), new MixedTypeChecker(), new NullTypeChecker()];
     (new ApplyTypeChecks(new TypedTraversableChecker(...$baseCheckers), ...$baseCheckers))->__invoke((new PropertyTypeFinder())->__invoke($access->getField(), $contextClass), $access->getValueToSet());
 }
Beispiel #2
0
 /**
  * Method that should be called around property
  *
  * @param FieldAccess $property Joinpoint
  *
  * @Around("access(* Example->*)")
  * @return mixed
  */
 public function aroundFieldAccess(FieldAccess $property)
 {
     $type = $property->getAccessType() === FieldAccess::READ ? 'read' : 'write';
     $value = $property->proceed();
     echo "Calling Around Interceptor for field: ", get_class($property->getThis()), "->", $property->getField()->getName(), ", access: {$type}", ", value: ", json_encode($value), "<br>\n";
     return $value;
 }
 /**
  * Advice that controls an access to the properties
  *
  * @param FieldAccess $property Joinpoint
  *
  * @Around("access(* Demo\Example\PropertyDemo->*)")
  * @return mixed
  */
 public function aroundFieldAccess(FieldAccess $property)
 {
     $type = $property->getAccessType() === FieldAccess::READ ? 'read' : 'write';
     $value = $property->proceed();
     echo "Calling Around Interceptor for field: ", get_class($property->getThis()), "->", $property->getField()->getName(), ", access: {$type}", ", value: ", json_encode($value), PHP_EOL;
     // $value = 666; You can change the return value for read/write operations in advice!
     return $value;
 }
Beispiel #4
0
 /**
  * Intercepts access to autowired properties and injects specified dependency
  *
  * @Around("@access(Warlock\Annotation\Autowired)")
  *
  * @param FieldAccess $joinpoint Autowiring joinpoint
  *
  * @return mixed
  */
 public function beforeAccessingAutowiredProperty(FieldAccess $joinpoint)
 {
     $obj = $joinpoint->getThis();
     $field = $joinpoint->getField();
     if ($joinpoint->getAccessType() == FieldAccess::READ) {
         /** @var Autowired $autowired */
         $autowired = $this->reader->getPropertyAnnotation($field, self::ANNOTATION_NAME);
         $strategy = ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE;
         if (!$autowired->required) {
             $strategy = ContainerInterface::NULL_ON_INVALID_REFERENCE;
         }
         $value = $this->container->get($autowired, $strategy);
     } else {
         $value = $joinpoint->proceed();
     }
     $field->setValue($obj, $value);
     return $value;
 }
 /**
  * @param FieldAccess $access
  *
  * @return void
  *
  * @throws \RuntimeException
  */
 public function __invoke(FieldAccess $access)
 {
     if (!($that = $access->getThis())) {
         return;
     }
     if (FieldAccess::WRITE !== $access->getAccessType()) {
         return;
     }
     $field = $access->getField();
     $field->setAccessible(true);
     // simplistic check - won't check for multiple assignments of "null" to a "null" valued field
     if (null === ($currentValue = $field->getValue($that))) {
         return;
     }
     if (!(new DocBlock($field))->getTagsByName('immutable')) {
         return;
     }
     $newValue = $access->getValueToSet();
     throw new \RuntimeException(sprintf('Trying to overwrite property %s#$%s of object %s#%s with a value of type "%s".' . ' The property was already given a value of type %s', $field->getDeclaringClass()->getName(), $field->getName(), get_class($that), spl_object_hash($that), is_object($newValue) ? get_class($newValue) : gettype($newValue), is_object($currentValue) ? get_class($currentValue) : gettype($currentValue)));
 }