コード例 #1
0
ファイル: Injector.php プロジェクト: brick/di
 /**
  * @param \ReflectionClass $class
  * @param object           $object
  *
  * @return void
  */
 private function injectProperties(\ReflectionClass $class, $object)
 {
     foreach ($this->reflectionTools->getClassProperties($class) as $property) {
         if ($this->policy->isPropertyInjected($property)) {
             $value = $this->resolver->getPropertyValue($property);
             $property->setAccessible(true);
             $property->setValue($object, $value);
         }
     }
 }
コード例 #2
0
ファイル: Packer.php プロジェクト: brick/brick
 /**
  * @param mixed   $variable The variable to copy.
  * @param boolean $pack     True to pack, false to unpack.
  * @param array   $visited  The visited objects, for recursive calls.
  * @param int     $level    The nesting level.
  *
  * @return mixed
  */
 private function copy($variable, $pack, array &$visited = [], $level = 0)
 {
     if (is_object($variable)) {
         $hash = spl_object_hash($variable);
         if (isset($visited[$hash])) {
             return $visited[$hash];
         }
         $processed = $pack ? $this->objectPacker->pack($variable) : $this->objectPacker->unpack($variable);
         if ($processed) {
             return $visited[$hash] = $processed;
         }
         $class = new \ReflectionClass($variable);
         $properties = $this->reflectionTools->getClassProperties($class);
         if (!$class->isUserDefined()) {
             if ($class->isCloneable()) {
                 return $visited[$hash] = clone $variable;
             } else {
                 return $visited[$hash] = $variable;
             }
         }
         $visited[$hash] = $copy = $class->newInstanceWithoutConstructor();
         foreach ($properties as $property) {
             $property->setAccessible(true);
             $value = $property->getValue($variable);
             $processed = $this->copy($value, $pack, $visited, $level + 1);
             $property->setValue($copy, $processed);
         }
         return $copy;
     }
     if (is_array($variable)) {
         foreach ($variable as &$value) {
             $value = $this->copy($value, $pack, $visited, $level + 1);
         }
     }
     return $variable;
 }