Exemplo n.º 1
0
 /**
  * This method returns an object matching the description specified by the element.
  *
  * @access public
  * @param Spring\Object\Parser $parser                      a reference to the parser
  * @param \SimpleXMLElement $element                        the element to be parsed
  * @return mixed                                            an object matching the description
  *                                                          specified by the element
  * @throws Throwable\Parse\Exception                        indicates that a problem occurred
  *                                                          when parsing
  */
 public function getObject(Spring\Object\Parser $parser, \SimpleXMLElement $element)
 {
     $attributes = $element->attributes();
     if (isset($attributes['delegate-object']) && isset($attributes['delegate-method'])) {
         $delegate_object = $parser->valueOf($attributes['delegate-object']);
         if ($parser->isClassName($delegate_object) && class_exists($delegate_object)) {
             $delegate_method = $parser->valueOf($attributes['delegate-method']);
             if ($parser->isMethodName($delegate_method) && method_exists($delegate_object, $delegate_method)) {
                 return array($delegate_object, $delegate_method);
             } else {
                 throw new Throwable\Parse\Exception('Unable to process Spring XML. Expected a valid method name, but got ":name".', array(':name' => $delegate_method));
             }
         } else {
             throw new Throwable\Parse\Exception('Unable to process Spring XML. Expected a valid class name, but got ":name".', array(':name' => $delegate_object));
         }
     } else {
         throw new Throwable\Parse\Exception('Unable to process Spring XML. Tag ":tag" is missing a valid class name and/or method name.', array(':tag' => 'function'));
     }
 }
Exemplo n.º 2
0
 /**
  * This method returns an object matching the description specified by the element.
  *
  * @access public
  * @param Spring\Object\Parser $parser                      a reference to the parser
  * @param \SimpleXMLElement $element                        the element to be parsed
  * @return mixed                                            an object matching the description
  *                                                          specified by the element
  * @throws Throwable\Parse\Exception                        indicates that a problem occurred
  *                                                          when parsing
  *
  * @see https://vcfvct.wordpress.com/2012/12/03/init-method%E3%80%81postconstruct%E3%80%81afterpropertiesset/
  */
 public function getObject(Spring\Object\Parser $parser, \SimpleXMLElement $element)
 {
     $attributes = $element->attributes();
     $class = $object = null;
     if (isset($attributes['factory-object']) && isset($attributes['factory-method'])) {
         $factory_object = $parser->valueOf($attributes['factory-object']);
         if ($parser->isClassName($factory_object) && class_exists($factory_object)) {
             $class = new \ReflectionClass($factory_object);
             $factory_method = $parser->valueOf($attributes['factory-method']);
             if ($parser->isMethodName($factory_method) && $class->hasMethod($factory_method)) {
                 $method = $class->getMethod($factory_method);
                 if (!$method->isPublic() || !$method->isStatic() || $method->isAbstract() || $method->isDestructor()) {
                     throw new Throwable\Parse\Exception('Unable to process Spring XML. Expected a valid method name, but got ":name".', array(':name' => $factory_method));
                 }
                 $constructor_args = $this->getConstructorArgs($parser, $element);
                 $object = $method->invokeArgs($class, $constructor_args);
             } else {
                 throw new Throwable\Parse\Exception('Unable to process Spring XML. Expected a valid method name, but got ":name".', array(':name' => $factory_method));
             }
         } else {
             throw new Throwable\Parse\Exception('Unable to process Spring XML. Expected a valid class name, but got ":name".', array(':name' => $factory_object));
         }
     } else {
         if (isset($attributes['type'])) {
             $type = $parser->valueOf($attributes['type']);
             if (!$parser->isClassName($type)) {
                 throw new Throwable\Parse\Exception('Unable to process Spring XML. Expected a valid class name, but got ":type".', array(':type' => $type));
             }
             $type = str_replace('.', '\\', $type);
             if (preg_match('/^(\\\\)?stdClass$/', $type)) {
                 $object = new \stdClass();
             } else {
                 if (preg_match('/^(\\\\)?Unicity\\\\Core\\\\Data\\\\Undefined$/', $type)) {
                     $object = Core\Data\Undefined::instance();
                 } else {
                     $class = new \ReflectionClass($type);
                     if ($class->isAbstract()) {
                         throw new Throwable\Parse\Exception('Unable to process Spring XML. Expected a valid class name, but got ":type".', array(':type' => $type));
                     }
                     $constructor_args = $this->getConstructorArgs($parser, $element);
                     if (isset($attributes['factory-method'])) {
                         $factory_method = $parser->valueOf($attributes['factory-method']);
                         if ($parser->isMethodName($factory_method) && $class->hasMethod($factory_method)) {
                             $method = $class->getMethod($factory_method);
                             if (!$method->isPublic() || !$method->isStatic() || $method->isAbstract() || $method->isDestructor()) {
                                 throw new Throwable\Parse\Exception('Unable to process Spring XML. Expected a valid method name, but got ":name".', array(':name' => $factory_method));
                             }
                             $object = $method->invokeArgs(null, $constructor_args);
                         } else {
                             throw new Throwable\Parse\Exception('Unable to process Spring XML. Expected a valid method name, but got ":name".', array(':name' => $factory_method));
                         }
                     } else {
                         $object = $class->newInstanceArgs($constructor_args);
                     }
                 }
             }
         } else {
             throw new Throwable\Parse\Exception('Unable to process Spring XML. Tag ":tag" is missing ":attribute" attribute.', array(':tag' => 'object', ':attribute' => 'type'));
         }
     }
     $type = gettype($object);
     if ($type == 'object') {
         $this->getProperties($parser, $element, $object);
         if ($object instanceof Spring\InitializingObject) {
             $object->afterPropertiesSet();
         }
         if (isset($attributes['init-method'])) {
             $init_method = $parser->valueOf($attributes['init-method']);
             if ($parser->isMethodName($init_method) && $class->hasMethod($init_method)) {
                 $method = $class->getMethod($init_method);
                 if (!$method->isPublic() || $method->isStatic() || $method->isAbstract() || $method->isDestructor()) {
                     throw new Throwable\Parse\Exception('Unable to process Spring XML. Expected a valid method name, but got ":name".', array(':name' => $init_method));
                 }
                 $method->invoke($object);
             } else {
                 throw new Throwable\Parse\Exception('Unable to process Spring XML. Expected a valid method name, but got ":name".', array(':name' => $init_method));
             }
         }
         if ($object instanceof Spring\FactoryObject) {
             $object = $object->getObject();
             $type = gettype($object);
         }
     }
     if (!in_array($type, array('object', 'NULL', 'array', 'string'))) {
         throw new Throwable\Parse\Exception('Unable to process Spring XML. Expected an object to be returned, but got ":type".', array(':type' => $type));
     }
     return $object;
 }