Line | |
---|
1 | <?php
|
---|
2 |
|
---|
3 | /**
|
---|
4 | * Reflection class which knows about setter based injection depedencies.
|
---|
5 | * @package Crafty
|
---|
6 | * @author Chris Corbyn
|
---|
7 | */
|
---|
8 | class net_sourceforge_phpcrafty_ComponentReflector extends ReflectionClass
|
---|
9 | {
|
---|
10 |
|
---|
11 | /**
|
---|
12 | * Properties to be injected when instantiated.
|
---|
13 | * @var mixed[]
|
---|
14 | */
|
---|
15 | private $_properties = array();
|
---|
16 |
|
---|
17 | /**
|
---|
18 | * Create a new ComponentReflector for the given $className with the given
|
---|
19 | * $properties.
|
---|
20 | * @param string $className
|
---|
21 | * @param mixed[] $properties
|
---|
22 | */
|
---|
23 | public function __construct($className, $properties = array())
|
---|
24 | {
|
---|
25 | parent::__construct($className);
|
---|
26 | $this->_properties = $properties;
|
---|
27 | }
|
---|
28 |
|
---|
29 | /**
|
---|
30 | * Create an instance with a list of arguments passed to the constructor.
|
---|
31 | * @param mixed $param1
|
---|
32 | * @param mixed $param2...
|
---|
33 | * @return object
|
---|
34 | */
|
---|
35 | public function newInstance()
|
---|
36 | {
|
---|
37 | return $this->newInstanceArgs(func_get_args());
|
---|
38 | }
|
---|
39 |
|
---|
40 | /**
|
---|
41 | * Create an instance with the given array of arguments in the constructor.
|
---|
42 | * @param mixed[] $args
|
---|
43 | * @return object
|
---|
44 | */
|
---|
45 | public function newInstanceArgs($args)
|
---|
46 | {
|
---|
47 | if ($this->getConstructor())
|
---|
48 | {
|
---|
49 | $o = parent::newInstanceArgs($args);
|
---|
50 | }
|
---|
51 | else
|
---|
52 | {
|
---|
53 | $o = parent::newInstance();
|
---|
54 | }
|
---|
55 |
|
---|
56 | foreach ($this->_properties as $k => $v)
|
---|
57 | {
|
---|
58 | $setter = 'set' . ucfirst($k);
|
---|
59 | $this->getMethod($setter)->invoke($o, $v);
|
---|
60 | }
|
---|
61 |
|
---|
62 | return $o;
|
---|
63 | }
|
---|
64 |
|
---|
65 | }
|
---|
Note:
See
TracBrowser
for help on using the repository browser.