Nette\PhpGenerator\ClassType::from PHP Method

from() public static method

public static from ( $from ) : self
return self
    public static function from($from)
    {
        $from = $from instanceof \ReflectionClass ? $from : new \ReflectionClass($from);
        if (PHP_VERSION_ID >= 70000 && $from->isAnonymous()) {
            $class = new static('anonymous');
        } else {
            $class = new static($from->getShortName(), new PhpNamespace($from->getNamespaceName()));
        }
        $class->type = $from->isInterface() ? 'interface' : ($from->isTrait() ? 'trait' : 'class');
        $class->final = $from->isFinal() && $class->type === 'class';
        $class->abstract = $from->isAbstract() && $class->type === 'class';
        $class->implements = $from->getInterfaceNames();
        $class->comment = $from->getDocComment() ? preg_replace('#^\\s*\\* ?#m', '', trim($from->getDocComment(), "/* \r\n\t")) : NULL;
        if ($from->getParentClass()) {
            $class->extends = $from->getParentClass()->getName();
            $class->implements = array_diff($class->implements, $from->getParentClass()->getInterfaceNames());
        }
        foreach ($from->getProperties() as $prop) {
            if ($prop->isDefault() && $prop->getDeclaringClass()->getName() === $from->getName()) {
                $class->properties[$prop->getName()] = Property::from($prop);
            }
        }
        foreach ($from->getMethods() as $method) {
            if ($method->getDeclaringClass()->getName() === $from->getName()) {
                $class->methods[$method->getName()] = Method::from($method)->setNamespace($class->namespace);
            }
        }
        return $class;
    }

Usage Example

Example #1
0
 /**
  * @param string $className
  * @return string
  */
 public function generate($className)
 {
     if (!class_exists($className)) {
         throw new InvalidArgumentException("Unknown class {$className}. Check for typos in class name and make sure" . " that given class is properly loaded.");
     }
     $origin = new ReflectionClass($className);
     $interface = ClassType::from(self::ACCESSOR_INTERFACE);
     $namespace = new PhpNamespace($this->naming->getNamespace());
     $class = $namespace->addClass($this->naming->deriveClassName($className));
     $class->addDocument("This class was automatically generated by Markatom/Accessor library.");
     $class->addImplement(self::ACCESSOR_INTERFACE);
     $class->addConst('TARGET', $origin->getName());
     $class->addProperty('writer')->setVisibility('private');
     $class->setMethods($interface->getMethods());
     $class->getMethod('read')->setVisibility('public')->setBody($this->generateReadBody($origin));
     $class->getMethod('write')->setVisibility('public')->setBody($this->generateWriteBody($origin));
     return (string) $namespace;
 }