summaryrefslogtreecommitdiff
blob: faf4d4f0d2e7b577edc34be153429e99ea538e6a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
A context object (see [Encapsulate Context Pattern][ak]) collects commonly used service objects and encapsulate those objects within a single available instance.

## Components
* ContextResource describes an interface to access Store, Settings, and a DependencyBuilder context
* ContextAware describes an interface to access a context object
* ContextInjector describes an interface to inject an context object
* ExtensionContext implements the ContextResource interface
* EmptyContext implements the ContextResource interface, returning null objects

#### Example
```php
class Foo implements ContextAware {

	/** @var ContextResource */
	protected $context = null;

	/**
	 * @since 1.9
	 *
	 * @param ContextResource $context
	 */
	public function __construct( ContextResource $context = null ) {
		$this->context = $context;
	}

	public function withContext() {

		if ( $this->context === null ) {
			$this->context = new ExtensionContext();
		}

		return $this->context;
	}

	public function getBaz() {
		return $this->withContext()->getDependencyBuilder()->newObject( 'Baz' );
	}

}
```
```php
class Bar implements ContextAware, ContextInjector {

	/** @var ContextResource */
	protected $context = null;

	public function invokeContext( ContextResource $context ) {
		$this->context = $context;
	}

	public function withContext() {
		return $this->context;
	}

	public function getBaz() {
		return $this->withContext()->getDependencyBuilder()->newObject( 'Baz' );
	}

}
```
```php
$foo = new Foo( new ExtensionContext() );
$baz = $foo->getBaz();

$bar = new Bar();
$bar->invokeContext( new ExtensionContext() );
$baz = $bar->getBaz();
```

For information on "how to use" the DependencyBuilder, see [here](dic.md).

[ak]: http://accu.org/index.php/journals/246  "The Encapsulate Context Pattern"