Пример #1
0
    public static function build(MetaClass $class)
    {
        $out = self::getHead();
        $uses = [Singleton::class, $class->getAutoBusinessClass()];
        if ($type = $class->getType()) {
            $typeName = $type->toString() . ' ';
        } else {
            $typeName = null;
        }
        $interfaces = ' implements Prototyped';
        $uses[] = Prototyped::class;
        $uses[] = $class->getProtoClass();
        if ($class->getPattern()->daoExists() && !$class->getPattern() instanceof AbstractClassPattern) {
            $interfaces .= ', DAOConnected';
            $uses[] = DAOConnected::class;
            $daoName = $class->getName() . 'DAO';
            $uses[] = $class->getDaoClass();
            $dao = <<<EOT
\t/**
\t * @return {$daoName}
\t**/
\tpublic static function dao()
\t{
\t\treturn Singleton::getInstance({$daoName}::class);
\t}

EOT;
        } else {
            $dao = null;
        }
        $out .= <<<EOT
namespace {$class->getNamespace()};


EOT;
        foreach ($uses as $import) {
            $out .= <<<EOT
use {$import};

EOT;
        }
        $out .= <<<EOT

{$typeName}class {$class->getName()} extends Auto{$class->getName()}{$interfaces}
{
EOT;
        if (!$type || $type->getId() !== MetaClassType::CLASS_ABSTRACT) {
            $customCreate = null;
            if ($class->getFinalParent()->getPattern() instanceof InternalClassPattern) {
                $parent = $class;
                while ($parent = $parent->getParent()) {
                    $info = new \ReflectionClass('\\' . $parent->getNamespace() . '\\' . $parent->getName());
                    if ($info->hasMethod('create') && $info->getMethod('create')->getParameters() > 0) {
                        $customCreate = true;
                        break;
                    }
                }
            }
            if ($customCreate) {
                $creator = $info->getMethod('create');
                $declaration = [];
                foreach ($creator->getParameters() as $parameter) {
                    $declaration[] = '$' . $parameter->getName() . ' = ' . ($parameter->getDefaultValue() ? $parameter->getDefaultValue() : 'null');
                }
                $declaration = implode(', ', $declaration);
                $out .= <<<EOT

\t/**
\t * @return {$class->getName()}
\t**/
\tpublic static function create({$declaration})
\t{
\t\treturn new self({$declaration});
\t}
\t\t
EOT;
            } else {
                $out .= <<<EOT

\t/**
\t * @return {$class->getName()}
\t**/
\tpublic static function create()
\t{
\t\treturn new self;
\t}
\t\t
EOT;
            }
            $protoName = 'Proto' . $class->getName();
            $out .= <<<EOT

{$dao}
\t/**
\t * @return {$protoName}
\t**/
\tpublic static function proto()
\t{
\t\treturn Singleton::getInstance({$protoName}::class);
\t}

EOT;
        }
        $out .= <<<EOT

\t// your brilliant stuff goes here
}

EOT;
        return $out . self::getHeel();
    }