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
13 /**
14 * Class StaticCrossCuttingLoader
15 *
16 * This class wraps the CrossCuttingLoader for static use.
17 *
18 * CrossCuttingLoader enables loading of mockable objects for cross cutting concerns,
19 * that were not injected to the current object.
20 *
21 * @category System
22 * @package Loaders
23 * @subpackage CrossCuttingLoader
24 */
25 class StaticCrossCuttingLoader
26 {
27 /**
28 * @var CrossCuttingLoaderInterface
29 */
30 protected static $crossCuttingLoader = null;
31
32
33 /**
34 * @throws BadFunctionCallException
35 */
36 public function __construct()
37 {
38 throw new BadFunctionCallException('This class is for static use only.');
39 }
40
41
42 /**
43 * In strict mode ($crossCuttingLoader->strictModeEnabled=true) this method accepts classes with implemented
44 * CrossCuttingObjectInterface only. Otherwise it throws an InvalidArgumentException.
45 *
46 * @param string $p_classname
47 *
48 * @return object
49 * @throws InvalidArgumentException
50 */
51 public static function getObject($p_classname)
52 {
53 $crossCuttingLoader = self::_getCrossCuttingLoader();
54
55 return $crossCuttingLoader->getObject($p_classname);
56 }
57
58
59 /**
60 * @return boolean
61 */
62 public static function useRegisteredObjectsOnly()
63 {
64 $crossCuttingLoader = self::_getCrossCuttingLoader();
65
66 return $crossCuttingLoader->useRegisteredObjectsOnly();
67 }
68
69
70 /**
71 * If RegisteredObjectsOnly is enabled, the loader returns only objects, that were registered by
72 * the registerObject method before. RegisteredObjectsOnly should be enabled in all unit tests.
73 *
74 * @param $bool_status
75 */
76 public static function setRegisteredObjectsOnly($bool_status)
77 {
78 $crossCuttingLoader = self::_getCrossCuttingLoader();
79 $crossCuttingLoader->setRegisteredObjectsOnly($bool_status);
80 }
81
82
83 /**
84 * In strict mode ($crossCuttingLoader->strictModeEnabled=true) this method accepts classes with implemented
85 * CrossCuttingObjectInterface only. Otherwise it throws an InvalidArgumentException.
86 *
87 * @param string $p_classname
88 * @param object $object
89 *
90 * @throws InvalidArgumentException
91 */
92 public static function registerObject($p_classname, $object)
93 {
94 $crossCuttingLoader = self::_getCrossCuttingLoader();
95 $crossCuttingLoader->registerObject($p_classname, $object);
96 }
97
98
99 /**
100 * @return void
101 */
102 public static function clearRegister()
103 {
104 $crossCuttingLoader = self::_getCrossCuttingLoader();
105 $crossCuttingLoader->clearRegister();
106 }
107
108
109 /**
110 * @return CrossCuttingLoaderInterface
111 */
112 protected static function _getCrossCuttingLoader()
113 {
114 if(self::$crossCuttingLoader === null)
115 {
116 self::$crossCuttingLoader = MainFactory::create('CrossCuttingLoader');
117 }
118
119 return self::$crossCuttingLoader;
120 }
121 }
122