1 <?php
2 /* --------------------------------------------------------------
3 StaticCrossCuttingLoader.inc.php 2015-07-22 gm
4 Gambio GmbH
5 http://www.gambio.de
6 Copyright (c) 2015 Gambio GmbH
7 Released under the GNU General Public License (Version 2)
8 [http://www.gnu.org/licenses/gpl-2.0.html]
9 --------------------------------------------------------------
10 */
11
12 MainFactory::load_class('CrossCuttingLoaderInterface');
13
14 /**
15 * Class CrossCuttingLoader
16 *
17 * CrossCuttingLoader enables loading of mockable objects for cross cutting concerns,
18 * that were not injected to the current object.
19 *
20 * Important:
21 * RegisteredObjectsOnly flag must be enabled for unit testing.
22 *
23 * @category System
24 * @package Loaders
25 * @subpackage CrossCuttingLoader
26 */
27 class CrossCuttingLoader implements CrossCuttingLoaderInterface
28 {
29 /**
30 * @var bool
31 * @todo Move to CrossCuttingLoaderSettings
32 */
33 protected $strictModeEnabled = true;
34
35 /**
36 * @var bool
37 */
38 protected $registeredObjectsOnly = false;
39
40 /**
41 * @var array
42 */
43 protected $registeredObjectArrays = array();
44
45
46 /**
47 * In strict mode ($strictModeEnabled=true) this method accepts classes with implemented
48 * CrossCuttingObjectInterface only. Otherwise it throws an InvalidArgumentException.
49 *
50 * @param string $p_classname
51 *
52 * @return object
53 * @throws InvalidArgumentException
54 */
55 public function getObject($p_classname)
56 {
57 if($this->useRegisteredObjectsOnly())
58 {
59 $object = $this->_getObjectFromRegister($p_classname);
60 }
61 else
62 {
63 $object = $this->_getObjectFromMainFactory($p_classname);
64 }
65
66 $this->_strictModeValidateTypeOf($object);
67
68 return $object;
69 }
70
71
72 /**
73 * RegisteredObjectsOnly Flag Setter
74 *
75 * If RegisteredObjectsOnly is enabled, the loader returns only objects, that were registered by
76 * the registerObject method before. RegisteredObjectsOnly should be enabled in all unit tests.
77 *
78 * @param boolean $bool_status
79 */
80 public function setRegisteredObjectsOnly($bool_status)
81 {
82 $this->registeredObjectsOnly = (bool)$bool_status;
83 }
84
85
86 /**
87 * RegisteredObjectsOnly Flag Getter
88 *
89 * @return boolean
90 */
91 public function useRegisteredObjectsOnly()
92 {
93 return $this->registeredObjectsOnly;
94 }
95
96
97 /**
98 * In strict mode ($strictModeEnabled=true) this method accepts classes with implemented
99 * CrossCuttingObjectInterface only. Otherwise it throws an InvalidArgumentException.
100 *
101 * @param string $p_classname
102 * @param object $object
103 *
104 * @throws InvalidArgumentException
105 */
106 public function registerObject($p_classname, $object)
107 {
108 $this->_strictModeValidateTypeOf($object);
109 $this->registeredObjectArrays[$p_classname] = $object;
110 }
111
112
113 /**
114 * @return void
115 */
116 public function clearRegister()
117 {
118 $this->registeredObjectArrays = array();
119 }
120
121
122 /**
123 * @param string $p_classname
124 *
125 * @return object
126 */
127 protected function _getObjectFromRegister($p_classname)
128 {
129 if(isset($this->registeredObjectArrays[$p_classname]) == false)
130 {
131 throw new LogicException('Requested object [' . htmlentities($p_classname)
132 . '] is not registered. RegisteredObjectsOnly is enabled.');
133 }
134
135 return $this->registeredObjectArrays[$p_classname];
136 }
137
138
139 /**
140 * @param string $p_classname
141 *
142 * @return object
143 */
144 protected function _getObjectFromMainFactory($p_classname)
145 {
146 return MainFactory::create($p_classname);
147 }
148
149
150 /**
151 * @param $object
152 *
153 * @throws InvalidArgumentException
154 */
155 protected function _strictModeValidateTypeOf($object)
156 {
157 if($this->strictModeEnabled)
158 {
159 if(is_a($object, 'CrossCuttingObjectInterface') == false)
160 {
161 throw new InvalidArgumentException('Implemented CrossCuttingObjectInterface expected in strict mode.');
162 }
163 }
164 }
165 }
166