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 
<?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]
   --------------------------------------------------------------
*/


/**
 * Class StaticCrossCuttingLoader
 *
 * This class wraps the CrossCuttingLoader for static use.
 *
 * CrossCuttingLoader enables loading of mockable objects for cross cutting concerns,
 * that were not injected to the current object.
 *
 * @category   System
 * @package    Loaders
 * @subpackage CrossCuttingLoader
 */
class StaticCrossCuttingLoader
{
    /**
     * @var CrossCuttingLoaderInterface
     */
    protected static $crossCuttingLoader = null;
    
    
    /**
     * @throws BadFunctionCallException
     */
    public function __construct()
    {
        throw new BadFunctionCallException('This class is for static use only.');
    }
    
    
    /**
     * In strict mode ($crossCuttingLoader->strictModeEnabled=true) this method accepts classes with implemented
     * CrossCuttingObjectInterface only. Otherwise it throws an InvalidArgumentException.
     *
     * @param string $p_classname
     *
     * @return object
     * @throws InvalidArgumentException
     */
    public static function getObject($p_classname)
    {
        $crossCuttingLoader = self::_getCrossCuttingLoader();
        
        return $crossCuttingLoader->getObject($p_classname);
    }
    
    
    /**
     * @return boolean
     */
    public static function useRegisteredObjectsOnly()
    {
        $crossCuttingLoader = self::_getCrossCuttingLoader();
        
        return $crossCuttingLoader->useRegisteredObjectsOnly();
    }
    
    
    /**
     * 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 $bool_status
     */
    public static function setRegisteredObjectsOnly($bool_status)
    {
        $crossCuttingLoader = self::_getCrossCuttingLoader();
        $crossCuttingLoader->setRegisteredObjectsOnly($bool_status);
    }
    
    
    /**
     * In strict mode ($crossCuttingLoader->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 static function registerObject($p_classname, $object)
    {
        $crossCuttingLoader = self::_getCrossCuttingLoader();
        $crossCuttingLoader->registerObject($p_classname, $object);
    }
    
    
    /**
     * @return void
     */
    public static function clearRegister()
    {
        $crossCuttingLoader = self::_getCrossCuttingLoader();
        $crossCuttingLoader->clearRegister();
    }
    
    
    /**
     * @return CrossCuttingLoaderInterface
     */
    protected static function _getCrossCuttingLoader()
    {
        if(self::$crossCuttingLoader === null)
        {
            self::$crossCuttingLoader = MainFactory::create('CrossCuttingLoader');
        }
        
        return self::$crossCuttingLoader;
    }
}