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 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
<?php
/* --------------------------------------------------------------
StaticCrossCuttingLoader.inc.php 2015-07-22 gm
Gambio GmbH
http://www.gambio.de
Copyright (c) 2015 Gambio GmbH
Released under the GNU General Public License (Version 2)
[http://www.gnu.org/licenses/gpl-2.0.html]
--------------------------------------------------------------
*/
MainFactory::load_class('CrossCuttingLoaderInterface');
/**
* Class CrossCuttingLoader
*
* CrossCuttingLoader enables loading of mockable objects for cross cutting concerns,
* that were not injected to the current object.
*
* Important:
* RegisteredObjectsOnly flag must be enabled for unit testing.
*
* @category System
* @package Loaders
* @subpackage CrossCuttingLoader
*/
class CrossCuttingLoader implements CrossCuttingLoaderInterface
{
/**
* @var bool
* @todo Move to CrossCuttingLoaderSettings
*/
protected $strictModeEnabled = true;
/**
* @var bool
*/
protected $registeredObjectsOnly = false;
/**
* @var array
*/
protected $registeredObjectArrays = array();
/**
* In strict mode ($strictModeEnabled=true) this method accepts classes with implemented
* CrossCuttingObjectInterface only. Otherwise it throws an InvalidArgumentException.
*
* @param string $p_classname
*
* @return object
* @throws InvalidArgumentException
*/
public function getObject($p_classname)
{
if($this->useRegisteredObjectsOnly())
{
$object = $this->_getObjectFromRegister($p_classname);
}
else
{
$object = $this->_getObjectFromMainFactory($p_classname);
}
$this->_strictModeValidateTypeOf($object);
return $object;
}
/**
* RegisteredObjectsOnly Flag Setter
*
* If RegisteredObjectsOnly is enabled, the loader returns only objects, that were registered by
* the registerObject method before. RegisteredObjectsOnly should be enabled in all unit tests.
*
* @param boolean $bool_status
*/
public function setRegisteredObjectsOnly($bool_status)
{
$this->registeredObjectsOnly = (bool)$bool_status;
}
/**
* RegisteredObjectsOnly Flag Getter
*
* @return boolean
*/
public function useRegisteredObjectsOnly()
{
return $this->registeredObjectsOnly;
}
/**
* In strict mode ($strictModeEnabled=true) this method accepts classes with implemented
* CrossCuttingObjectInterface only. Otherwise it throws an InvalidArgumentException.
*
* @param string $p_classname
* @param object $object
*
* @throws InvalidArgumentException
*/
public function registerObject($p_classname, $object)
{
$this->_strictModeValidateTypeOf($object);
$this->registeredObjectArrays[$p_classname] = $object;
}
/**
* @return void
*/
public function clearRegister()
{
$this->registeredObjectArrays = array();
}
/**
* @param string $p_classname
*
* @return object
*/
protected function _getObjectFromRegister($p_classname)
{
if(isset($this->registeredObjectArrays[$p_classname]) == false)
{
throw new LogicException('Requested object [' . htmlentities($p_classname)
. '] is not registered. RegisteredObjectsOnly is enabled.');
}
return $this->registeredObjectArrays[$p_classname];
}
/**
* @param string $p_classname
*
* @return object
*/
protected function _getObjectFromMainFactory($p_classname)
{
return MainFactory::create($p_classname);
}
/**
* @param $object
*
* @throws InvalidArgumentException
*/
protected function _strictModeValidateTypeOf($object)
{
if($this->strictModeEnabled)
{
if(is_a($object, 'CrossCuttingObjectInterface') == false)
{
throw new InvalidArgumentException('Implemented CrossCuttingObjectInterface expected in strict mode.');
}
}
}
}