addConstant() 공개 메소드

Adds a constant to the definition
public addConstant ( zephir\ClassConstant $constant )
$constant zephir\ClassConstant
예제 #1
0
 /**
  * Creates a definition for an interface
  *
  * @param string $namespace
  * @param array $topStatement
  * @param array $docblock
  */
 public function preCompileInterface($namespace, $topStatement, $docblock)
 {
     $classDefinition = new ClassDefinition($namespace, $topStatement['name']);
     $classDefinition->setIsExternal($this->_external);
     if (isset($topStatement['extends'])) {
         foreach ($topStatement['extends'] as &$extend) {
             $extend['value'] = $this->getFullName($extend['value']);
         }
         $classDefinition->setImplementsInterfaces($topStatement['extends']);
     }
     $classDefinition->setType('interface');
     if (is_array($docblock)) {
         $classDefinition->setDocBlock($docblock["value"]);
     }
     if (isset($topStatement['definition'])) {
         $definition = $topStatement['definition'];
         /**
          * Register constants
          */
         if (isset($definition['constants'])) {
             foreach ($definition['constants'] as $constant) {
                 $classConstant = new ClassConstant($constant['name'], isset($constant['default']) ? $constant['default'] : null, isset($constant['docblock']) ? $constant['docblock'] : null);
                 $classDefinition->addConstant($classConstant);
             }
         }
         /**
          * Register methods
          */
         if (isset($definition['methods'])) {
             foreach ($definition['methods'] as $method) {
                 $classMethod = new ClassMethod($classDefinition, $method['visibility'], $method['name'], isset($method['parameters']) ? new ClassMethodParameters($method['parameters']) : null, null, isset($method['docblock']) ? $method['docblock'] : null, isset($method['return-type']) ? $method['return-type'] : null, $method);
                 $classDefinition->addMethod($classMethod, $method);
             }
         }
     }
     $this->_classDefinition = $classDefinition;
 }
예제 #2
0
 /**
  * Builds a class definition from reflection
  *
  * @param \ReflectionClass $class
  *
  * @return ClassDefinition
  */
 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) {
                 $params = array('type' => 'parameter', 'name' => $row->getName(), 'const' => 0, 'data-type' => 'variable', 'mandatory' => !$row->isOptional());
                 if (!$params['mandatory']) {
                     try {
                         $params['default'] = $row->getDefaultValue();
                     } catch (\ReflectionException $e) {
                         // TODO: dummy default value
                         $params['default'] = true;
                     }
                 }
                 $parameters[] = $params;
             }
             $classMethod = new ClassMethod($classDefinition, array(), $method->getName(), new ClassMethodParameters($parameters));
             $classMethod->setIsStatic($method->isStatic());
             $classMethod->setIsBundled(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);
         }
     }
     $properties = $class->getProperties();
     if (count($properties) > 0) {
         foreach ($properties as $property) {
             $visibility = array();
             if ($property->isPublic()) {
                 $visibility[] = 'public';
             }
             if ($property->isPrivate()) {
                 $visibility[] = 'private';
             }
             if ($property->isProtected()) {
                 $visibility[] = 'protected';
             }
             if ($property->isStatic()) {
                 $visibility[] = 'static';
             }
             $classProperty = new ClassProperty($classDefinition, $visibility, $property->getName(), null, null, null);
             $classDefinition->addProperty($classProperty);
         }
     }
     $classDefinition->setIsBundled(true);
     return $classDefinition;
 }