/** * Advice that controls an access to the properties * * @param FieldAccess $fieldAccess Joinpoint * * @Around("access(public|protected Demo\Example\PropertyDemo->*)") * @return mixed */ public function aroundFieldAccess(FieldAccess $fieldAccess) { $value = $fieldAccess->proceed(); echo "Calling Around Interceptor for ", $fieldAccess, ", value: ", json_encode($value), PHP_EOL; // $value = 666; You can change the return value for read/write operations in advice! return $value; }
/** * 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; }
/** * Do the stuff you want to do before and after the * field is setted. * * @param FieldAccess $fieldWrite the joinpoint that corresponds to the field write * @return mixed the result of the field set {@link Joinpoint::proceed()}, might be intercepted by the * interceptor. */ public function set(FieldAccess $fieldWrite) { $value = $fieldWrite->proceed(); $adviceMethod = $this->adviceMethod; $adviceMethod($fieldWrite); 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; }
/** * @Go\Before("access(public|protected **->*)") * * @param FieldAccess $access * * @return mixed */ public function beforePropertyAccess(FieldAccess $access) { if (FieldAccess::WRITE !== $access->getAccessType()) { return $access->proceed(); } foreach ($this->propertyWriteCheckers as $checker) { $checker($access); } return $access->proceed(); }
/** * Advice that controls an access to the properties * * @param FieldAccess $fieldAccess Joinpoint * * @Around("access(public|protected Demo\Example\PropertyDemo->*)") * @return mixed */ public function aroundFieldAccess(FieldAccess $fieldAccess) { $isRead = $fieldAccess->getAccessType() == FieldAccess::READ; // proceed all internal advices $fieldAccess->proceed(); if ($isRead) { // if you want to change original property value, then return it by reference $value = $fieldAccess->getValue(); } else { // if you want to change value to set, then return it by reference $value = $fieldAccess->getValueToSet(); } echo "Calling After Interceptor for ", $fieldAccess, ", value: ", json_encode($value), PHP_EOL; }
/** * 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; }