Пример #1
0
 public static function fromReflection(ReflectionProperty $reflection = NULL)
 {
     if (!defined('PHP_VERSION_ID')) {
         $v = explode('.', PHP_VERSION);
         define('PHP_VERSION_ID', $v[0] * 10000 + $v[1] * 100 + $v[2]);
     }
     if ($reflection === null) {
         return null;
     }
     if (func_num_args() > 1) {
         $stack = func_get_arg(1);
     } else {
         $stack = new \ArrayObject();
     }
     $stackExpression = $reflection->getDeclaringClass()->getName() . '::' . $reflection->getName();
     if (isset($stack[$stackExpression])) {
         return $stack[$stackExpression];
     }
     $stack[$stackExpression] = $instance = new Property($reflection);
     if (func_num_args() > 2) {
         $reader = func_get_arg(2);
     } else {
         $reader = new AnnotationReader();
     }
     if (func_num_args() > 3) {
         $phpParser = func_get_arg(3);
     } else {
         $phpParser = new PhpParser();
     }
     $instance->name = $reflection->getName();
     $instance->public = $reflection->isPublic();
     $instance->private = $reflection->isPrivate();
     $instance->protected = $reflection->isProtected();
     $instance->static = $reflection->isStatic();
     $instance->default = $reflection->isDefault();
     $instance->modifiers = $reflection->getModifiers();
     $instance->docComment = $reflection->getDocComment();
     $instance->annotations = $reader->getPropertyAnnotations($reflection);
     $instance->declaringClass = Type::fromReflection($reflection->getDeclaringClass() ? $reflection->getDeclaringClass() : null, $stack, $reader, $phpParser);
     $defaultProperties = $reflection->getDeclaringClass()->getDefaultProperties();
     if (isset($defaultProperties[$instance->name])) {
         $instance->defaultValue = $defaultProperties[$instance->name];
     }
     if (preg_match('/@var\\s+([a-zA-Z0-9\\\\\\[\\]_]+)/', $instance->docComment, $m)) {
         $typeString = $m[1];
     }
     if (isset($typeString)) {
         $instance->type = MixedType::fromString($typeString, $stack, $reader, $phpParser, $instance->declaringClass);
     } else {
         $instance->type = MixedType::getInstance();
     }
     return $instance;
 }
Пример #2
0
 /**
  * @param string[] $fileNames
  * @throws MetaException
  * @return boolean
  */
 public function processFiles(array $fileNames)
 {
     $types = array();
     foreach ($fileNames as $fileName) {
         require_once $fileName;
     }
     foreach (array_merge(get_declared_classes(), get_declared_interfaces(), get_declared_traits()) as $typeName) {
         $rc = new \ReflectionClass($typeName);
         if ($rc->getFileName() && in_array(realpath($rc->getFileName()), $fileNames)) {
             $types[] = Type::fromReflection($rc);
         }
     }
     $matched = false;
     foreach ($types as $type) {
         $result = $this->compile($type);
         if ($result === null) {
             continue;
         }
         $matched = true;
         $outputFileName = $this->createOutputFileName($type, $result->getClass());
         $outputDirectory = dirname($outputFileName);
         if (!is_dir($outputDirectory)) {
             if (!mkdir($outputDirectory, 0777, true)) {
                 throw new MetaException("Could not create output directory '{$outputDirectory}'.");
             }
         }
         $content = (string) $result->getFile();
         // do not overwrite files with same content
         if (!file_exists($outputFileName) || md5_file($outputFileName) !== md5($content)) {
             if (!file_put_contents($outputFileName, $content)) {
                 throw new MetaException("Could not write output to file '{$outputFileName}'.");
             }
         }
     }
     return $matched;
 }
Пример #3
0
 /**
  * Type string
  *
  * @param string $string
  * @throws \InvalidArgumentException
  * @return ArrayType|BoolType|CallableType|FloatType|IntType|MixedType|ResourceType|StringType|Type
  */
 public static function fromString($string)
 {
     $lowercaseString = trim(strtolower($string), "\\");
     // TODO: variant types
     if ($lowercaseString === "mixed") {
         return MixedType::getInstance();
     } elseif ($lowercaseString === "scalar") {
         return ScalarType::getInstance();
     } elseif ($lowercaseString === "object") {
         return ObjectType::getInstance();
     } elseif ($lowercaseString === "void" || $lowercaseString === "null") {
         return VoidType::getInstance();
     } elseif ($lowercaseString === "numeric" || $lowercaseString === "number") {
         return NumericType::getInstance();
     } elseif ($lowercaseString === "int" || $lowercaseString === "integer") {
         return IntType::getInstance();
     } elseif ($lowercaseString === "float" || $lowercaseString === "double") {
         return FloatType::getInstance();
     } elseif ($lowercaseString === "bool" || $lowercaseString === "boolean") {
         return BoolType::getInstance();
     } elseif ($lowercaseString === "string") {
         return StringType::getInstance();
     } elseif ($lowercaseString === "resource" || $lowercaseString === "stream") {
         return ResourceType::getInstance();
     } elseif ($lowercaseString === "callable" || $lowercaseString === "callback" || trim($lowercaseString, "\\") === "closure") {
         return CallableType::getInstance();
     } elseif (strncmp($lowercaseString, "array", 5) === 0) {
         return ArrayType::create(MixedType::getInstance());
     } else {
         if (func_num_args() > 1) {
             $stack = func_get_arg(1);
         } else {
             $stack = new \ArrayObject();
         }
         if (func_num_args() > 2) {
             $reader = func_get_arg(2);
         } else {
             $reader = new AnnotationReader();
         }
         if (func_num_args() > 3) {
             $phpParser = func_get_arg(3);
         } else {
             $phpParser = new PhpParser();
         }
         if (func_num_args() > 4 && func_get_arg(4) !== null) {
             /** @var Type $declaringType */
             $declaringType = func_get_arg(4);
             $useStatements = $declaringType->getUseStatements();
         } else {
             $declaringType = null;
             $useStatements = array();
         }
         if ($lowercaseString === "\$this" || $lowercaseString === "self" || $lowercaseString === "static") {
             if ($declaringType === null) {
                 throw new \InvalidArgumentException("Type string references declaring class, but no declaring class given.");
             }
             return $declaringType;
         } elseif (substr($string, -2) === "[]") {
             $baseString = substr($string, 0, strlen($string) - 2);
             return ArrayType::create(MixedType::fromString($baseString, $stack, $reader, $phpParser, $declaringType));
         } else {
             if ($string[0] === "\\") {
                 $typeName = trim($string, "\\");
             } elseif (isset($useStatements[$lowercaseString])) {
                 $typeName = $useStatements[$lowercaseString];
                 // TODO: `use` with namespace (e.g. `use Doctrine\Mapping as ORM;`)
             } elseif ($declaringType !== null) {
                 $typeName = $declaringType->getNamespaceName() . "\\" . $string;
             } else {
                 $typeName = $string;
             }
             return Type::fromReflection(new \ReflectionClass($typeName), $stack, $reader, $phpParser);
         }
     }
 }
Пример #4
0
 public static function fromReflection(ReflectionMethod $reflection = NULL)
 {
     if (!defined('PHP_VERSION_ID')) {
         $v = explode('.', PHP_VERSION);
         define('PHP_VERSION_ID', $v[0] * 10000 + $v[1] * 100 + $v[2]);
     }
     if ($reflection === null) {
         return null;
     }
     if (func_num_args() > 1) {
         $stack = func_get_arg(1);
     } else {
         $stack = new \ArrayObject();
     }
     $stackExpression = $reflection->getDeclaringClass()->getName() . '::' . $reflection->getName();
     if (isset($stack[$stackExpression])) {
         return $stack[$stackExpression];
     }
     $stack[$stackExpression] = $instance = new Method($reflection);
     if (func_num_args() > 2) {
         $reader = func_get_arg(2);
     } else {
         $reader = new AnnotationReader();
     }
     if (func_num_args() > 3) {
         $phpParser = func_get_arg(3);
     } else {
         $phpParser = new PhpParser();
     }
     $instance->public = $reflection->isPublic();
     $instance->private = $reflection->isPrivate();
     $instance->protected = $reflection->isProtected();
     $instance->abstract = $reflection->isAbstract();
     $instance->final = $reflection->isFinal();
     $instance->static = $reflection->isStatic();
     $instance->constructor = $reflection->isConstructor();
     $instance->destructor = $reflection->isDestructor();
     $instance->modifiers = $reflection->getModifiers();
     $instance->closure = $reflection->isClosure();
     $instance->deprecated = $reflection->isDeprecated();
     $instance->internal = $reflection->isInternal();
     $instance->userDefined = $reflection->isUserDefined();
     $instance->generator = PHP_VERSION_ID >= 50500 ? $reflection->isGenerator() : null;
     $instance->docComment = $reflection->getDocComment();
     $instance->endLine = $reflection->getEndLine();
     $instance->extensionName = $reflection->getExtensionName();
     $instance->fileName = $reflection->getFileName();
     $instance->name = $reflection->getName();
     $instance->namespaceName = $reflection->getNamespaceName();
     $instance->numberOfParameters = $reflection->getNumberOfParameters();
     $instance->numberOfRequiredParameters = $reflection->getNumberOfRequiredParameters();
     $instance->shortName = $reflection->getShortName();
     $instance->startLine = $reflection->getStartLine();
     $instance->staticVariables = $reflection->getStaticVariables();
     $instance->annotations = $reader->getMethodAnnotations($reflection);
     $instance->declaringClass = Type::fromReflection($reflection->getDeclaringClass() ? $reflection->getDeclaringClass() : null, $stack, $reader, $phpParser);
     if (preg_match('/@return\\s+([a-zA-Z0-9\\\\\\[\\]_]+)/', $instance->docComment, $m)) {
         $typeString = $m[1];
     }
     if (isset($typeString)) {
         $instance->type = MixedType::fromString($typeString, $stack, $reader, $phpParser, $instance->declaringClass);
     } else {
         $instance->type = VoidType::getInstance();
     }
     return $instance;
 }
Пример #5
0
 /**
  * @return Type
  */
 public function getParentClass()
 {
     if (!$this->parentClassInitialized) {
         $this->parentClass = Type::fromReflection($this->reflection->getParentClass() ? $this->reflection->getParentClass() : null);
         $this->parentClassInitialized = true;
     }
     return $this->parentClass;
 }