public static function getClassOfMetaClass(MetaClass $class, $addBackslash = false) { if ($class->getPattern() instanceof InternalClassPattern) { throw new WrongArgumentException(); } else { return ($addBackslash ? '\\' : '') . $class->getNamespace() . '\\' . $class->getName(); } }
protected static function buildPointers(MetaClass $class) { $out = null; if (!$class->getPattern() instanceof AbstractClassPattern) { if ($source = $class->getSourceLink()) { $out .= <<<EOT \tprotected \$linkName = '{$source}'; \t EOT; } if ($class->getIdentifier()->getColumnName() !== 'id') { $out .= <<<EOT public function getIdName() { \treturn '{$class->getIdentifier()->getColumnName()}'; } EOT; } $fullClassName = MetaClassNameBuilder::getClassOfMetaClass($class, true); $out .= <<<EOT public function getTable() { \treturn '{$class->getTableName()}'; } public function getObjectName() { \treturn '{$fullClassName}'; } EOT; if ($class->isSequenceless()) { $out .= <<<EOT public function getSequence() { \tthrow new \\Hesper\\Core\\Exception\\UnimplementedFeatureException(); } EOT; } else { $out .= <<<EOT public function getSequence() { \treturn '{$class->getTableName()}_id'; } EOT; } } elseif ($class->getWithInternalProperties()) { $out .= <<<EOT // no get{Table,ObjectName,Sequence} for abstract class EOT; } if ($liaisons = $class->getReferencingClasses()) { $uncachers = []; foreach ($liaisons as $className) { $uncachers[] = $className . '::dao()->uncacheLists();'; } $uncachers = implode("\n", $uncachers); $out .= <<<EOT public function uncacheLists() { {$uncachers} return parent::uncacheLists(); } EOT; } return $out; }
/** * @return MetaConfiguration **/ private function checkClassSanity(MetaClass $class, \ReflectionClass $info) { switch ($class->getTypeId()) { case null: break; case MetaClassType::CLASS_ABSTRACT: Assert::isTrue($info->isAbstract(), 'class ' . $info->getName() . ' expected to be abstract'); Assert::isTrue($class->getPattern() instanceof AbstractClassPattern, 'class ' . $info->getName() . ' must use AbstractClassPattern'); break; case MetaClassType::CLASS_FINAL: Assert::isTrue($info->isFinal(), 'class ' . $info->getName() . ' expected to be final'); break; case MetaClassType::CLASS_SPOOKED: default: Assert::isUnreachable(); break; } if ($public = $info->getProperties(\ReflectionProperty::IS_PUBLIC)) { Assert::isUnreachable($class->getName() . ' contains properties with evil visibility:' . "\n" . print_r($public, true)); } return $this; }
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(); }