public static function fromInstance($name = null, $instance) { $className = get_class($instance); $class = new \ReflectionClass($className); $metadata = new ObjectMetadata(); $metadata->setClassName($className); $metadata->setName($name); $properties = array(); $allProperties = \ReflectionProperty::IS_PUBLIC | \ReflectionProperty::IS_PROTECTED | \ReflectionProperty::IS_PRIVATE; $reflectionProperties = $class->getProperties($allProperties); foreach ($reflectionProperties as $prop) { $prop->setAccessible(true); $name = $prop->getName(); $value = $prop->getValue($instance); $type = gettype($value) === 'object' ? get_class($value) : gettype($value); $propertyMeta = new PropertyMetadata(); $propertyMeta->setName($name); $propertyMeta->setType($type); $property = new Property($propertyMeta); $property->setValue($value); array_push($properties, $property); } $object = new Object($metadata); $object->setInstance($instance); $object->setProperties($properties); return $object; }
/** * * @param \Rhapsody\SetupBundle\Model\ObjectMetadata $metadata */ public function applyAttributes(&$metadata) { $name = $this->getAttribute('name'); if (!empty($name)) { $this->getLog()->debug('Apply name attribute with value: ' . $name); $metadata->setName($name); } $class = $this->getAttribute('class'); if (!empty($class)) { $this->getLog()->debug('Apply class attribute with value: ' . $class); $metadata->setClassName($class); } $parent = $this->getAttribute('parent'); if (!empty($parent)) { $this->getLog()->debug('Apply parent attribute with value: ' . $parent); $metadata->setParentRef($parent); } $ref = $this->getAttribute('ref'); if (!empty($ref)) { $this->getLog()->debug('Apply ref attribute with value: ' . $ref); $metadata->setRef($ref); } $abstract = $this->getBooleanAttribute('abstract'); $this->getLog()->debug('Apply abstract attribute with value: ' . ($abstract ? 'true' : 'false') . ' type(' . gettype($abstract) . ')'); $metadata->setAbstract($abstract); $transient = $this->getBooleanAttribute('transient'); $this->getLog()->debug('Apply transient attribute with value: ' . ($transient ? 'true' : 'false')); $metadata->setTransient($transient); }