[ Index ] |
PHP Cross Reference of phpBB-3.3.14-deutsch |
[Summary view] [Print] [Text view]
1 <?php 2 3 declare(strict_types=1); 4 5 namespace ProxyManager\ProxyGenerator\LazyLoadingGhost\MethodGenerator; 6 7 use ProxyManager\Generator\MagicMethodGenerator; 8 use Zend\Code\Generator\ParameterGenerator; 9 use ProxyManager\ProxyGenerator\LazyLoadingGhost\PropertyGenerator\PrivatePropertiesMap; 10 use ProxyManager\ProxyGenerator\LazyLoadingGhost\PropertyGenerator\ProtectedPropertiesMap; 11 use ProxyManager\ProxyGenerator\PropertyGenerator\PublicPropertiesMap; 12 use ProxyManager\ProxyGenerator\Util\PublicScopeSimulator; 13 use ReflectionClass; 14 use Zend\Code\Generator\MethodGenerator; 15 use Zend\Code\Generator\PropertyGenerator; 16 17 /** 18 * Magic `__set` for lazy loading ghost objects 19 * 20 * @author Marco Pivetta <ocramius@gmail.com> 21 * @license MIT 22 */ 23 class MagicSet extends MagicMethodGenerator 24 { 25 /** 26 * @var string 27 */ 28 private $callParentTemplate = <<<'PHP' 29 %s 30 31 if (isset(self::$%s[$name])) { 32 return ($this->$name = $value); 33 } 34 35 if (isset(self::$%s[$name])) { 36 // check protected property access via compatible class 37 $callers = debug_backtrace(\DEBUG_BACKTRACE_PROVIDE_OBJECT, 2); 38 $caller = isset($callers[1]) ? $callers[1] : []; 39 $object = isset($caller['object']) ? $caller['object'] : ''; 40 $expectedType = self::$%s[$name]; 41 42 if ($object instanceof $expectedType) { 43 return ($this->$name = $value); 44 } 45 46 $class = isset($caller['class']) ? $caller['class'] : ''; 47 48 if ($class === $expectedType || is_subclass_of($class, $expectedType) || $class === 'ReflectionProperty') { 49 return ($this->$name = $value); 50 } 51 } elseif (isset(self::$%s[$name])) { 52 // check private property access via same class 53 $callers = debug_backtrace(\DEBUG_BACKTRACE_PROVIDE_OBJECT, 2); 54 $caller = isset($callers[1]) ? $callers[1] : []; 55 $class = isset($caller['class']) ? $caller['class'] : ''; 56 57 static $accessorCache = []; 58 59 if (isset(self::$%s[$name][$class])) { 60 $cacheKey = $class . '#' . $name; 61 $accessor = isset($accessorCache[$cacheKey]) 62 ? $accessorCache[$cacheKey] 63 : $accessorCache[$cacheKey] = \Closure::bind(function ($instance, $value) use ($name) { 64 return ($instance->$name = $value); 65 }, null, $class); 66 67 return $accessor($this, $value); 68 } 69 70 if ('ReflectionProperty' === $class) { 71 $tmpClass = key(self::$%s[$name]); 72 $cacheKey = $tmpClass . '#' . $name; 73 $accessor = isset($accessorCache[$cacheKey]) 74 ? $accessorCache[$cacheKey] 75 : $accessorCache[$cacheKey] = \Closure::bind(function ($instance, $value) use ($name) { 76 return ($instance->$name = $value); 77 }, null, $tmpClass); 78 79 return $accessor($this, $value); 80 } 81 } 82 83 %s 84 PHP; 85 86 /** 87 * @param ReflectionClass $originalClass 88 * @param PropertyGenerator $initializerProperty 89 * @param MethodGenerator $callInitializer 90 * @param PublicPropertiesMap $publicProperties 91 * @param ProtectedPropertiesMap $protectedProperties 92 * @param PrivatePropertiesMap $privateProperties 93 * 94 * @throws \Zend\Code\Generator\Exception\InvalidArgumentException 95 * @throws \InvalidArgumentException 96 */ 97 public function __construct( 98 ReflectionClass $originalClass, 99 PropertyGenerator $initializerProperty, 100 MethodGenerator $callInitializer, 101 PublicPropertiesMap $publicProperties, 102 ProtectedPropertiesMap $protectedProperties, 103 PrivatePropertiesMap $privateProperties 104 ) { 105 parent::__construct( 106 $originalClass, 107 '__set', 108 [new ParameterGenerator('name'), new ParameterGenerator('value')] 109 ); 110 111 $override = $originalClass->hasMethod('__set'); 112 113 $parentAccess = 'return parent::__set($name, $value);'; 114 115 if (! $override) { 116 $parentAccess = PublicScopeSimulator::getPublicAccessSimulationCode( 117 PublicScopeSimulator::OPERATION_SET, 118 'name', 119 'value' 120 ); 121 } 122 123 $this->setBody(sprintf( 124 $this->callParentTemplate, 125 '$this->' . $initializerProperty->getName() . ' && $this->' . $callInitializer->getName() 126 . '(\'__set\', array(\'name\' => $name, \'value\' => $value));', 127 $publicProperties->getName(), 128 $protectedProperties->getName(), 129 $protectedProperties->getName(), 130 $privateProperties->getName(), 131 $privateProperties->getName(), 132 $privateProperties->getName(), 133 $parentAccess 134 )); 135 } 136 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Mon Nov 25 19:05:08 2024 | Cross-referenced by PHPXref 0.7.1 |