| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  * Phossa Project | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * PHP version 5.4 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * @category  Library | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * @package   Phossa2\Di | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * @copyright Copyright (c) 2016 phossa.com | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * @license   http://mit-license.org/ MIT License | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * @link      http://www.phossa.com/ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | /*# declare(strict_types=1); */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | namespace Phossa2\Di; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | use Phossa2\Di\Message\Message; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | use Phossa2\Di\Traits\ArrayAccessTrait; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | use Phossa2\Shared\Base\ObjectAbstract; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | use Interop\Container\ContainerInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | use Phossa2\Di\Exception\RuntimeException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | use Phossa2\Di\Exception\NotFoundException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | use Phossa2\Shared\Reference\DelegatorTrait; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | use Phossa2\Shared\Reference\DelegatorInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | use Phossa2\Config\Interfaces\WritableInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  * Delegator | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |  * A writable and array accessable container delegator | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |  * @package Phossa2\Di | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |  * @author  Hong Zhang <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |  * @see     ObjectAbstract | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |  * @see     ContainerInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |  * @see     DelegatorInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |  * @see     WritableInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |  * @see     \ArrayAccess | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |  * @version 2.0.0 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |  * @since   2.0.0 added | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  | class Delegator extends ObjectAbstract implements ContainerInterface, DelegatorInterface, \ArrayAccess, WritableInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |     use DelegatorTrait, ArrayAccessTrait; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |      * {@inheritDoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |     public function get($id) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |         if ($this->hasInLookup($id)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |             return $this->getFromLookup($id); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |             throw new NotFoundException( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |                 Message::get(Message::DI_SERVICE_NOTFOUND, $id), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |                 Message::DI_SERVICE_NOTFOUND | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |             ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |      * {@inheritDoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |     public function has($id) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |         return $this->hasInLookup($id); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |      * {@inheritDoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |     public function set(/*# string */ $id, $value) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |         if ($this->isWritable()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |             $this->writable->set($id, $value); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |             return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |             throw new RuntimeException( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |                 Message::get(Message::DI_CONTAINER_READONLY, $id), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |                 Message::DI_CONTAINER_READONLY | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |             ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |      * Override 'isWritable()' in 'Phossa2\Config\Traits\WritableTrait' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |      * {@inheritDoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |     public function isWritable()/*# : bool */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |         foreach ($this->lookup_pool as $reg) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |             if ($reg instanceof WritableInterface && | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |                 $reg->isWritable() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |             ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |                 $this->setWritable($reg); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |                 return true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |         return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |      * {@inheritDoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |     protected function isValidRegistry($registry)/*# : bool */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |         return $registry instanceof ContainerInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |      * {@inheritDoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |     protected function hasInRegistry( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |         $registry, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |         /*# string */ $key | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |     )/*# : bool */ { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |         /* @var $registry ContainerInterface */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |         return $registry->has($key); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |      * {@inheritDoc} | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 123 |  |  |      */ | 
            
                                                        
            
                                    
            
            
                | 124 |  |  |     protected function getFromRegistry( | 
            
                                                        
            
                                    
            
            
                | 125 |  |  |         $registry, | 
            
                                                        
            
                                    
            
            
                | 126 |  |  |         /*# string */ $key | 
            
                                                        
            
                                    
            
            
                | 127 |  |  |     ) { | 
            
                                                        
            
                                    
            
            
                | 128 |  |  |         /* @var $registry ContainerInterface */ | 
            
                                                        
            
                                    
            
            
                | 129 |  |  |         return $registry->get($key); | 
            
                                                        
            
                                    
            
            
                | 130 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 131 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 132 |  |  |  |