/** * @param ClassMetadataInfo $metadata * @param string $outputDir * @param Closure $collectionNameBuilder */ public function generate(ClassMetadataInfo $metadata, $outputDir, Closure $collectionNameBuilder) { $tpl = file_get_contents(CodeGeneratorHelper::absPath(EntityCollectionTemplate::class, RootPath::path())); $tplNs = CodeGeneratorHelper::ns(EntityCollectionTemplate::class); $tplSimpleName = CodeGeneratorHelper::simpleName(EntityCollectionTemplate::class); $entityFqcn = CodeGeneratorHelper::simpleName(EntityFqcn::class); $collectionClassName = $collectionNameBuilder($metadata->getName()); $code = CodeGeneratorHelper::render($tpl, array($tplNs => CodeGeneratorHelper::ns($collectionClassName), $tplSimpleName => CodeGeneratorHelper::simpleName($collectionClassName), $entityFqcn => CodeGeneratorHelper::fqn($metadata->getName()))); CodeGeneratorHelper::save($collectionClassName, $code, $outputDir); }
/** * @param ClassMetadataInfo[] $metadatas * @param $name * @return ClassMetadataInfo|null */ public static function findMetadataByClassName($metadatas, $name) { $name = CodeGeneratorHelper::nfqn($name); foreach ($metadatas as $metadata) { if ($metadata->getName() === $name) { return $metadata; } } return null; }
public function generate($fullClassName, $outputDir) { $fqn = CodeGeneratorHelper::fqn($fullClassName); $reflectionClass = new ReflectionClass($fullClassName); $ns = $reflectionClass->getName(); $methods = $reflectionClass->getMethods(ReflectionMethod::IS_PUBLIC); $code = []; foreach ($methods as $method) { /** @var ReflectionMethod $method */ $name = $method->getName(); if ($this->accept($method)) { list($hints, $uses, $applies) = $this->prepareHintUseApply($method); if ($method->isStatic()) { $code[] = sprintf(static::$fnStaticMethodTemplate, $name, $hints, $uses, $fqn, $name, $applies); } else { $code[] = sprintf(static::$fnInstanceMethodTemplate, $name, $hints, $fqn, $uses, $name, $applies); } } } $body = sprintf(static::$bodyTemplate, $ns, implode("\n", $code)); CodeGeneratorHelper::save("{$fullClassName}/Fn", $body, $outputDir); }
protected function generateEntityStubMethod(ClassMetadataInfo $metadata, $type, $fieldName, $typeHint = null, $defaultValue = null) { $methodName = $type . Inflector::classify($fieldName); $variableName = Inflector::camelize($fieldName); if (in_array($type, array("add", "remove"))) { $methodName = Inflector::singularize($methodName); $variableName = Inflector::singularize($variableName); } if ($this->hasMethod($methodName, $metadata)) { return ''; } $this->staticReflection[$metadata->name]['methods'][] = strtolower($methodName); $var = sprintf('%sMethodTemplate', $type); $template = static::${$var}; $methodTypeHint = null; $types = Type::getTypesMap(); $variableType = $typeHint ? $this->getType($typeHint) : null; if ($typeHint && !isset($types[$typeHint])) { $variableType = '\\' . ltrim($variableType, '\\'); $methodTypeHint = '\\' . $typeHint . ' '; } $replacements = array('<description>' => ucfirst($type) . ' ' . $variableName, '<methodTypeHint>' => $methodTypeHint, '<variableType>' => $variableType, '<variableName>' => $variableName, '<methodName>' => $methodName, '<fieldName>' => $fieldName, '<variableDefault>' => $defaultValue !== null ? ' = ' . $defaultValue : '', '<entity>' => CodeGeneratorHelper::fqn($metadata->getName())); $method = str_replace(array_keys($replacements), array_values($replacements), $template); return $this->prefixCodeWithSpaces($method); }
public function getFieldNameByColumnName($metadatas, $entity, $columnName) { $metadata = CodeGeneratorHelper::findMetadataByClassName($metadatas, $entity); return $metadata->fieldNames[$columnName]; }
public function getBaseQueryCollectionClassNameFqn() { return CodeGeneratorHelper::fqn(BaseQueryCollection::class); }