Esempio n. 1
0
 /**
  * Creates the property shortcuts
  *
  * @param array $property
  * @param ClassDefinition $classDefinition
  * @throws CompilerException
  */
 protected function _processShorcuts(array $property, ClassDefinition $classDefinition)
 {
     foreach ($property['shortcuts'] as $shortcut) {
         if (substr($property['name'], 0, 1) == '_') {
             $name = substr($property['name'], 1);
         } else {
             $name = $property['name'];
         }
         switch ($shortcut['name']) {
             case 'get':
                 $classDefinition->addMethod(new ClassMethod($classDefinition, array('public'), 'get' . ucfirst($name), null, new StatementsBlock(array(array('type' => 'return', 'expr' => array('type' => 'property-access', 'left' => array('type' => 'variable', 'value' => 'this'), 'right' => array('type' => 'variable', 'value' => $property['name']))))), isset($shortcut['docblock']) ? $shortcut['docblock'] : isset($property['docblock']) ? $property['docblock'] : null, null, $shortcut), $shortcut);
                 break;
             case 'set':
                 $classDefinition->addMethod(new ClassMethod($classDefinition, array('public'), 'set' . ucfirst($name), new ClassMethodParameters(array(array('type' => 'parameter', 'name' => $name, 'const' => 0, 'data-type' => 'variable', 'mandatory' => 0))), new StatementsBlock(array(array('type' => 'let', 'assignments' => array(array('assign-type' => 'object-property', 'operator' => 'assign', 'variable' => 'this', 'property' => $property['name'], 'expr' => array('type' => 'variable', 'value' => $name, 'file' => $property['file'], 'line' => $property['line'], 'char' => $property['char']), 'file' => $property['file'], 'line' => $property['line'], 'char' => $property['char']))))), isset($shortcut['docblock']) ? $shortcut['docblock'] : isset($property['docblock']) ? $property['docblock'] : null, null, $shortcut), $shortcut);
                 break;
             case 'toString':
             case '__toString':
                 $classDefinition->addMethod(new ClassMethod($classDefinition, array('public'), '__toString', null, new StatementsBlock(array(array('type' => 'return', 'expr' => array('type' => 'property-access', 'left' => array('type' => 'variable', 'value' => 'this'), 'right' => array('type' => 'variable', 'value' => $property['name']))))), isset($shortcut['docblock']) ? $shortcut['docblock'] : isset($property['docblock']) ? $property['docblock'] : null, null, $shortcut), $shortcut);
                 break;
             default:
                 throw new CompilerException("Unknown shortcut '" . $shortcut['name'] . "'", $shortcut);
         }
     }
 }
Esempio n. 2
0
 /**
  * Builds a class definition from reflection
  *
  * @param \ReflectionClass $class
  */
 public static function buildFromReflection(\ReflectionClass $class)
 {
     $classDefinition = new ClassDefinition($class->getNamespaceName(), $class->getName());
     $methods = $class->getMethods();
     if (count($methods) > 0) {
         foreach ($methods as $method) {
             $parameters = array();
             foreach ($method->getParameters() as $row) {
                 $parameters[] = array('type' => 'parameter', 'name' => $row->getName(), 'const' => 0, 'data-type' => 'variable', 'mandatory' => !$row->isOptional());
             }
             $classMethod = new ClassMethod($classDefinition, array(), $method->getName(), new ClassMethodParameters($parameters));
             $classMethod->setIsStatic($method->isStatic());
             $classMethod->setIsInternal(true);
             $classDefinition->addMethod($classMethod);
         }
     }
     $constants = $class->getConstants();
     if (count($constants) > 0) {
         foreach ($constants as $constantName => $constantValue) {
             $type = self::_convertPhpConstantType(gettype($constantValue));
             $classConstant = new ClassConstant($constantName, array('value' => $constantValue, 'type' => $type), null);
             $classDefinition->addConstant($classConstant);
         }
     }
     $classDefinition->setIsInternal(true);
     return $classDefinition;
 }