|
| 1 | +# Proxy Service Usage |
| 2 | + |
| 3 | +## Overview |
| 4 | + |
| 5 | +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. |
| 6 | + |
| 7 | +## Methods |
| 8 | + |
| 9 | +### `getProxyObject()` |
| 10 | + |
| 11 | + - Retrieve a proxy object for a given class and method. |
| 12 | + - The proxy will contain all methods of the original class. |
| 13 | + |
| 14 | +- **Parameters:** |
| 15 | + - `$className` (string): The fully qualified class name. |
| 16 | + - `$method` (string): The method name to call. |
| 17 | + - `$args` (array, optional): Arguments to pass to the method. Default is an empty array. |
| 18 | + |
| 19 | +- **Returns:** object|null |
| 20 | + |
| 21 | +### `getStrictProxyObject()` |
| 22 | + |
| 23 | + - Retrieve a proxy object that strictly implements a specified interface. |
| 24 | + - The proxy will only contain the methods of the specified interface, allowing you to limit access to methods. |
| 25 | + - All methods from the given interface must be callabe in the original class. |
| 26 | + |
| 27 | +- **Parameters:** |
| 28 | + - `$className` (string): The fully qualified class name. |
| 29 | + - `$method` (string): The method name to call. |
| 30 | + - `$interface` (string): The interface that the returned proxy object must implement. |
| 31 | + - `$args` (array, optional): Arguments to pass to the method. Default is an empty array. |
| 32 | + |
| 33 | +- **Returns:** object|null |
| 34 | + |
| 35 | +### `getDecoratorProxy()` |
| 36 | + |
| 37 | + - Generate a proxy object that can optionally implement a specified interface. |
| 38 | + - If an interface is provided, the proxy will only contain the methods of that interface. |
| 39 | + - The interface can also include methods not present in the original class. |
| 40 | + * These methods must be addressed by the original class's magic PHP functions, like `__call`. |
| 41 | + |
| 42 | +- **Parameters:** |
| 43 | + - `$className` (string): The fully qualified class name. |
| 44 | + - `$method` (string): The method name to call. |
| 45 | + - `$interface` (string, optional): The interface that the returned proxy object can implement. |
| 46 | + - `$args` (array, optional): Arguments to pass to the method. Default is an empty array. |
| 47 | + |
| 48 | +- **Returns:** object|null |
| 49 | + |
| 50 | +## Example Extending UserInterface and Decorator Proxy |
| 51 | +Let's suppose you wish to extend the functionality of the `User` class with a new method `getAdditionalData()`: |
| 52 | + |
| 53 | +```php |
| 54 | +interface MyExtendedInterface { |
| 55 | + public function getFirstName(); |
| 56 | + public function getAdditionalData(); |
| 57 | +} |
| 58 | +``` |
| 59 | + |
| 60 | +```php |
| 61 | +$proxyService = new ProxyService(new RemoteObjectFactory()); |
| 62 | +$decoratorProxy = $proxyService->getDecoratorProxy(User::class, 'getById', MyExtendedInterface::class, ['12']); |
| 63 | +``` |
| 64 | +When using the `getDecoratorProxy()` method with `MyExtendedInterface`, the `User` class should look like: |
| 65 | + |
| 66 | +```php |
| 67 | +class User { |
| 68 | + |
| 69 | + private string $firstName; |
| 70 | + |
| 71 | + |
| 72 | + public function __construct($firstName, $lastName) { |
| 73 | + $this->firstName = $firstName; |
| 74 | + $this->lastName = $lastName; |
| 75 | + } |
| 76 | + |
| 77 | + public static function getById($id): User { |
| 78 | + // Code to get user by id |
| 79 | + } |
| 80 | + |
| 81 | + public function getFirstName(): string |
| 82 | + return $this->firstName; |
| 83 | + } |
| 84 | + |
| 85 | + public function __call($name, $arguments) { |
| 86 | + // Handle methods that aren't explicitly defined in User but are in MyExtendedInterface |
| 87 | + if ($name == "getAdditionalData") { |
| 88 | + // logic for getAdditionalData |
| 89 | + } |
| 90 | + } |
| 91 | +} |
| 92 | +``` |
| 93 | + |
| 94 | +## Example Using an Incompatible Interface With Strict Proxy |
| 95 | + |
| 96 | +Let's say you mistakenly use an interface that expects a method that isn't in the `User` class: |
| 97 | + |
| 98 | +```php |
| 99 | +interface IncompatibleInterface { |
| 100 | + public function getNonExistentMethod(); |
| 101 | +} |
| 102 | +``` |
| 103 | +### User Class |
| 104 | + |
| 105 | +```php |
| 106 | +class User{ |
| 107 | + private $firstName; |
| 108 | + private $lastName; |
| 109 | + |
| 110 | + public function __construct($firstName, $lastName) { |
| 111 | + $this->firstName = $firstName; |
| 112 | + $this->lastName = $lastName; |
| 113 | + } |
| 114 | + |
| 115 | + public static function getById($id): User { |
| 116 | + // Code to get user by id |
| 117 | + } |
| 118 | + |
| 119 | + public function getFirstName() { |
| 120 | + return $this->firstName; |
| 121 | + } |
| 122 | + |
| 123 | + public function getLastName() { |
| 124 | + return $this->lastName; |
| 125 | + } |
| 126 | +} |
| 127 | +``` |
| 128 | +If you attempt to use `IncompatibleInterface` with the ProxyService, it will result in an error because the `User` class doesn't have `getNonExistentMethod()`. |
| 129 | + |
| 130 | +```php |
| 131 | +try { |
| 132 | +$proxyService = new ProxyService(new RemoteObjectFactory()); |
| 133 | +try { |
| 134 | + $strictProxyObject = $proxyService->getStrictProxyObject(User::class, 'getbyId', IncompatibleInterface::class, ['12']); |
| 135 | +} catch (InvalidArgumentException $e) { |
| 136 | + echo "Error: " . $e->getMessage(); // This will output an error message indicating the incompatibility. |
| 137 | +} |
| 138 | +``` |
| 139 | +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. |
| 140 | + |
| 141 | +## Example Limiting UserInterface |
| 142 | +The following interface ensures that only the `getFirstName()` method can be accessed: |
| 143 | + |
| 144 | +```php |
| 145 | +interface LimitedUserInterface { |
| 146 | + public function getFirstName(); |
| 147 | +} |
| 148 | +``` |
| 149 | + |
| 150 | +### User Class |
| 151 | + |
| 152 | +```php |
| 153 | +class User{ |
| 154 | + private $firstName; |
| 155 | + private $lastName; |
| 156 | + |
| 157 | + public function __construct($firstName, $lastName) { |
| 158 | + $this->firstName = $firstName; |
| 159 | + $this->lastName = $lastName; |
| 160 | + } |
| 161 | + |
| 162 | + public static function getById($id): User { |
| 163 | + // Code to get user by id |
| 164 | + } |
| 165 | + |
| 166 | + public function getFirstName() { |
| 167 | + return $this->firstName; |
| 168 | + } |
| 169 | + |
| 170 | + public function getLastName() { |
| 171 | + return $this->lastName; |
| 172 | + } |
| 173 | +} |
| 174 | +``` |
| 175 | + |
| 176 | +### Example Usage of User Class |
| 177 | +```php |
| 178 | +$user = new User::getById(12); |
| 179 | +echo $user->getFirstName(); // Outputs: John |
| 180 | +echo $user->getLastName(); // Outputs: Doe |
| 181 | +``` |
| 182 | + |
| 183 | +### Using ProxyService With LimitedUserInterface |
| 184 | +You can use the ProxyService with `LimitedUserInterface` to get a decorator proxy object: |
| 185 | + |
| 186 | +```php |
| 187 | +$proxyService = new ProxyService(new RemoteObjectFactory()); |
| 188 | +$decoratorProxyObject = $proxyService->getDecoratorProxy(User::class, 'getById', LimitedUserInterface::class, ['12']); |
| 189 | +echo $decoratorProxyObject->getFirstName(); // Outputs: John |
| 190 | +// The following line would result in an error, even if it exist in the original User class: |
| 191 | +// $decoratorProxyObject->getLastName(); |
| 192 | +``` |
| 193 | + |
| 194 | +**Note:** Other methods from the original User class are restricted in the proxy object due to `LimitedUserInterface`. |
0 commit comments