1
|
|
|
<?php |
2
|
|
|
namespace Example; |
3
|
|
|
|
4
|
|
|
use SlidesWorker\ServiceLocator\ServiceLocator; |
5
|
|
|
use SlidesWorker\ServiceLocator\ServiceLocatorInterface; |
6
|
|
|
use SlidesWorker\ServiceLocator\ServiceLocatorAwareInterface; |
7
|
|
|
use SlidesWorker\ServiceLocator\ServiceLocatorAwareTrait; |
8
|
|
|
|
9
|
|
|
// only php 5.4 and higher |
10
|
|
|
class ServiceCanHoldServiceLocator1 |
11
|
|
|
{ |
12
|
|
|
use ServiceLocatorAwareTrait; |
13
|
|
|
} |
14
|
|
|
|
15
|
|
|
// php 5.3 |
16
|
|
|
class ServiceCanHoldServiceLocator2 implements ServiceLocatorAwareInterface |
17
|
|
|
{ |
18
|
|
|
protected $serviceLocator; |
19
|
|
|
|
20
|
|
|
public function setServiceLocator(ServiceLocatorInterface $serviceLocator) |
21
|
|
|
{ |
22
|
|
|
$this->serviceLocator = $serviceLocator; |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
public function getServiceLocator() |
26
|
|
|
{ |
27
|
|
|
return $this->serviceLocator; |
28
|
|
|
} |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
// php 5.3 |
32
|
|
|
class ServiceCanHoldServiceLocator3 |
33
|
|
|
{ |
34
|
|
|
protected $serviceLocator; |
35
|
|
|
|
36
|
|
|
public function setServiceLocator(ServiceLocatorInterface $serviceLocator) |
37
|
|
|
{ |
38
|
|
|
$this->serviceLocator = $serviceLocator; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function getServiceLocator() |
42
|
|
|
{ |
43
|
|
|
return $this->serviceLocator; |
44
|
|
|
} |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
// setup ServiceLocator |
48
|
|
|
$serviceLocator = new ServiceLocator(); |
49
|
|
|
|
50
|
|
|
// Hold with trait |
51
|
|
|
$serviceLocator->setFactory('service1', function (ServiceLocatorInterface $locator) { |
|
|
|
|
52
|
|
|
return new ServiceCanHoldServiceLocator1(); |
53
|
|
|
}); |
54
|
|
|
|
55
|
|
|
// Hold with interface |
56
|
|
|
$serviceLocator->setFactory('service2', function (ServiceLocatorInterface $locator) { |
|
|
|
|
57
|
|
|
return new ServiceCanHoldServiceLocator1(); |
58
|
|
|
}); |
59
|
|
|
|
60
|
|
|
// Hold with custom |
61
|
|
|
$serviceLocator->setFactory('service3', function (ServiceLocatorInterface $locator) { |
62
|
|
|
$service = new ServiceCanHoldServiceLocator3(); |
63
|
|
|
$service->setServiceLocator($locator); |
64
|
|
|
return $service; |
65
|
|
|
}); |
66
|
|
|
|
67
|
|
|
$serviceLocator->get('service1'); |
68
|
|
|
$serviceLocator->get('service2'); |
69
|
|
|
$serviceLocator->get('service3'); |
70
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.