Skip to content

Commit

Permalink
Apply suggestions from code review
Browse files Browse the repository at this point in the history
Co-authored-by: Manon Cassier <[email protected]>
Co-authored-by: Matthias Schuhmayer <[email protected]>
  • Loading branch information
3 people authored Aug 11, 2023
1 parent d4ced00 commit 42b1d33
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 37 deletions.
37 changes: 22 additions & 15 deletions doc/03_Event_Proxy_Service_Usage.md
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@

# Documentation for `EventProxyService` Class
# Event Proxy Service Usage

## **Overview**
## Overview

`EventProxyService` is a service class that provides functionality to wrap an instance of an object with pre and post method interceptors. These interceptors are triggered right before or after the invocation of the specified methods, respectively.
`EventProxyService` is a service class that provides functionality to wrap an object's instance with `pre` and `post` method interceptors. These interceptors are respectively triggered right before or after the invocation of the specified methods.

When an interceptor is triggered, an event is dispatched using the `EventDispatcher`. The event name is composed of the lower case fully-qualified class name of the original object (with backslashes replaced by dots), the lower case method name, and the prefix (`.pre` or `.post`).

For example, if you have an interceptor on the `save` method of a class named `App\Entity\User` and it's a pre-interceptor, the event name will be `app.entity.user.save.pre`.
For example, for a pre-interceptor on the `save` method of a class named `App\Entity\User`, the event name will be `app.entity.user.save.pre`.

## **Method**
## `getEventDispatcherProxy()` Method

### `getEventDispatcherProxy()`

This method creates a proxy of the given instance, and binds pre and post method interceptors to it.
This method creates a proxy of the given instance and binds `pre` and `post` method interceptors to it.

```php
public function getEventDispatcherProxy(
Expand All @@ -23,23 +22,27 @@ public function getEventDispatcherProxy(
): object;
```

#### Parameters:
**Parameters:**

- `$instance`: The object instance that needs to be proxied.
- `$preInterceptors` (optional): An array of method names that should be intercepted before their invocation.
- `$postInterceptors` (optional): An array of method names that should be intercepted after their invocation.

#### Returns:
**Returns:**

- A proxy of the given `$instance`.

> **Note**: Final Classes can't be proxied.
:::info

Final classes can't be proxied.

:::

---

## **Example Usage with Symfony's Dependency Injection**
## Example Usage With Symfony's Dependency Injection

### **Configuration (`services.yaml`):**
### Configuration (`services.yaml`)

```yaml
services:
Expand All @@ -51,7 +54,7 @@ services:
- { name: kernel.event_listener, event: 'app.entity.user.save.post', method: 'onUserSavePost' }
```
### **Listener Example:**
### Listener Example
To respond to the dispatched events when interceptors are triggered, you can set up listeners. Here's an example of a listener for the `pre` interceptor of the `save` method on the `App\Entity\User` class:

Expand Down Expand Up @@ -82,7 +85,7 @@ class InterceptorListener
}
```

### **Using `EventProxyService` in a Controller:**
### Using `EventProxyService` in a Controller

```php
use Pimcore\Bundle\StaticResolverBundle\Proxy\Service\EventProxyService;
Expand All @@ -109,4 +112,8 @@ class YourController extends AbstractController
}
```

> **Note**: When the pre-interceptor for the `save` method is triggered, the `onUserSavePre` method in the `InterceptorListener` class will be executed, and you can handle the event as needed.
:::info

When the pre-interceptor for the `save` method is triggered, the `onUserSavePre` method in the `InterceptorListener` class will be executed, and you can handle the event as needed.

:::
44 changes: 22 additions & 22 deletions doc/04_Proxy_Service_Usage.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
# Documentation for `ProxyService` Class
# Proxy Service Usage

## Overview:
## Overview

The `ProxyService` class offers the capability to generate proxy objects for specified classes and methods. It provides methods to create proxies in various configurations including a strict or decorator mode which restricts the proxy object to only expose methods specified by an interface.

## Methods:
## Methods

### `getProxyObject`
### `getProxyObject()`

- Retrieve a proxy object for a given class and method.
- The proxy will contain all methods of the original class.
Expand All @@ -18,10 +18,10 @@ The `ProxyService` class offers the capability to generate proxy objects for spe

- **Returns:** object|null

### `getStrictProxyObject`
### `getStrictProxyObject()`

- Retrieve a proxy object that strictly implements a specified interface.\
- The proxy will only contain the methods of the specified interface, allowing you to limit access to methods.\
- Retrieve a proxy object that strictly implements a specified interface.
- The proxy will only contain the methods of the specified interface, allowing you to limit access to methods.
- All methods from the given interface must be callabe in the original class.

- **Parameters:**
Expand All @@ -32,7 +32,7 @@ The `ProxyService` class offers the capability to generate proxy objects for spe

- **Returns:** object|null

### `getDecoratorProxy`
### `getDecoratorProxy()`

- Generate a proxy object that can optionally implement a specified interface.
- If an interface is provided, the proxy will only contain the methods of that interface.
Expand All @@ -47,21 +47,21 @@ The `ProxyService` class offers the capability to generate proxy objects for spe

- **Returns:** object|null

## **Example Extending UserInterface and decorater proxy**
Suppose you wish to extend the functionality of the User class with a new method, say getAdditionalData.
## Example Extending UserInterface and Decorator Proxy
Let's suppose you wish to extend the functionality of the `User` class with a new method `getAdditionalData()`:

```php
interface MyExtendedInterface {
public function getFirstName();
public function getAdditionalData();
public function getFirstName();
public function getAdditionalData();
}
```

```php
$proxyService = new ProxyService(new RemoteObjectFactory());
$decoratorProxy = $proxyService->getDecoratorProxy(User::class, 'getById', MyExtendedInterface::class, ['12']);
```
When using the getDecoratorProxy with the MyExtendedInterface, the User class should look something like:
When using the `getDecoratorProxy()` method with `MyExtendedInterface`, the `User` class should look like:

```php
class User {
Expand Down Expand Up @@ -91,13 +91,13 @@ class User {
}
```

## **Example using an Incompatible Interface with strict proxy**
## Example Using an Incompatible Interface With Strict Proxy

Let's say you mistakenly use an interface that expects a method that isn't in the User class.
Let's say you mistakenly use an interface that expects a method that isn't in the `User` class:

```php
interface IncompatibleInterface {
public function getNonExistentMethod();
public function getNonExistentMethod();
}
```
### User Class
Expand Down Expand Up @@ -125,7 +125,7 @@ class User{
}
}
```
If you attempt to use IncompatibleInterface with the ProxyService, it will result in an error because User class doesn't have getNonExistentMethod.
If you attempt to use `IncompatibleInterface` with the ProxyService, it will result in an error because the `User` class doesn't have `getNonExistentMethod()`.

```php
try {
Expand All @@ -136,10 +136,10 @@ try {
echo "Error: " . $e->getMessage(); // This will output an error message indicating the incompatibility.
}
```
In this error case, the getStrictProxyObject will throw an InvalidServiceException because of the attempt to proxy a method (getNonExistentMethod) that does not exist in the original User class.
In this error case, the `getStrictProxyObject()` will throw an `InvalidServiceException` because of the attempt to proxy a method (`getNonExistentMethod()`) that does not exist in the original `User` class.

## **Example Limiting UserInterface**
The following interface ensures that only the `getFirstName` method can be accessed:
## Example Limiting UserInterface
The following interface ensures that only the `getFirstName()` method can be accessed:

```php
interface LimitedUserInterface {
Expand Down Expand Up @@ -180,8 +180,8 @@ echo $user->getFirstName(); // Outputs: John
echo $user->getLastName(); // Outputs: Doe
```

### Using ProxyService with LimitedUserInterface
Using the ProxyService with `LimitedUserInterface` to get a decorator proxy object:
### Using ProxyService With LimitedUserInterface
You can use the ProxyService with `LimitedUserInterface` to get a decorator proxy object:

```php
$proxyService = new ProxyService(new RemoteObjectFactory());
Expand Down

0 comments on commit 42b1d33

Please sign in to comment.