Ocramius/ProxyManager — pull request #83 by Ocramius (2013-08-31 22:34:55+00:00)
Title: Feature/access interceptor scope localizer

This PR implements "scope localizing" proxies.

## Problem

The problem is that some objects may pass themselves to others. Here's a quick example to clarify when this happens:

```php
class Task
{
public function run($args)
{
$this->args = $args;
}
}

class TaskRunner
{
public function runTask(Task $task)
{
return $task->run($this);
}
}
```

When proxying this class with a value holder, the wrapped instance is passed to `Task#run()`:

```php
$taskRunner = $proxyFactory->createProxy(new TaskRunner());

$task = new Task();

$taskRunner->runTask($task);

var_dump($task->args); // not the proxy!!!
```

We want our own wrapped instance to be passed to `Task#run()`.

To do so, instead of keeping a reference to the original service, we can localize its scope. To do so, we can use the idea of @mnapoli of building ["friend services"](http://en.mnapoli.fr/friend-services/). Friend services share internal state, but don't need to keep a reference to each other:

```php
class TaskRunnerProxy
{
public function __construct(TaskRunner $taskRunner)
{
$this->property1 = & $taskRunner->property1;
$this->property2 = & $taskRunner->property2;
$this->property3 = & $taskRunner->property3;
$this->property4 = & $taskRunner->property4;
}
}
```

This example is over-simplified and covers only properties that are accessible from the scope of `TaskRunnerProxy`, but the concept is developed further in this pull request.