本文整理汇总了PHP中Nette\PhpGenerator\ClassType::addConst方法的典型用法代码示例。如果您正苦于以下问题:PHP ClassType::addConst方法的具体用法?PHP ClassType::addConst怎么用?PHP ClassType::addConst使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Nette\PhpGenerator\ClassType
的用法示例。
在下文中一共展示了ClassType::addConst方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: onGenerate
public function onGenerate(AbstractMetaSpec $spec, MetaSpecMatcher $matcher, Type $type, ClassType $class)
{
$class->addConst("CLASS_NAME", $type->getName());
$class->addConst("SHORT_NAME", $type->getShortName());
$class->addConst("ENTITY_NAME", lcfirst($type->getShortName()));
foreach ($type->getProperties() as $property) {
$const = strtoupper(trim(preg_replace("/([A-Z])/", "_\$1", $property->getName()), "_"));
if (isset(self::$reserved[$const])) {
throw new MetaException("Property name constant for {$type->getName()}::\${$property->getName()} would result in reserved word.");
}
$class->addConst($const, $property->getName());
}
}
示例2: execute
protected function execute(InputInterface $input, OutputInterface $output)
{
$config = $this->getApplication()->getConfig();
$dialog = $this->getHelper('dialog');
$className = $input->getArgument('className');
$modelName = $input->getArgument('modelName');
$endPoint = $input->getArgument('endPoint');
$model = $this->getModel($modelName);
$buildDirectory = $config['build']['classes'];
$buildPath = $buildDirectory . '/' . $className . '.php';
if (file_exists($buildPath)) {
if (!$dialog->askConfirmation($output, sprintf('<question>Class file "%s" exists, overwrite?</question>', $buildPath), false)) {
return;
}
}
$modelConfig = ['properties' => $model->properties];
$configsDirectory = $config['build']['configs'];
$configPath = realpath($configsDirectory . '/' . $modelName . '.json');
if (file_exists($configPath)) {
$modelConfig = json_decode(file_get_contents($configPath), true);
}
$namespace = new PhpNamespace($config['namespace']);
$namespace->addUse($config['extends']);
$class = new ClassType($className, $namespace);
$class->addExtend($config['extends']);
if (!empty($endPoint)) {
$class->addConst("ENDPOINT", $endPoint);
}
foreach ($model->properties as $propertyName => $propertyDef) {
if (in_array($propertyName, $modelConfig['properties'], true)) {
$property = $class->addProperty($propertyName)->setVisibility('public');
$accessorMethod = $class->addMethod($this->toCamelCase("get_" . $propertyName));
$accessorMethod->setBody('return $this->' . $propertyName . ';');
$mutatorMethod = $class->addMethod($this->toCamelCase("set_" . $propertyName));
$mutatorMethod->addParameter($propertyName);
$mutatorMethod->setBody('$this->' . $propertyName . ' = $' . $propertyName . ';');
if (is_string($propertyDef['type'])) {
$property->addDocument("@var {$propertyDef['type']}");
} else {
$property->addDocument("@var mixed");
}
} else {
$output->writeln(sprintf("<info>Skipped property %s</info>", $propertyName));
}
}
file_put_contents($buildPath, str_replace("\t", " ", "<?php\n{$namespace}{$class}"));
// TODO: replace with PHP_CodeSniffer library
exec(sprintf('vendor/bin/phpcbf --standard=PSR2 --encoding=utf-8 "%s"', $buildPath));
$output->writeln(sprintf("<info>Class %s created</info>", $buildPath));
}
示例3: doDecorate
/**
* @param PhpNamespace $namespace
* @param ClassType $class
* @param Column $column
* @return void
*/
public function doDecorate(Column $column, ClassType $class, PhpNamespace $namespace)
{
switch ($column->getType()) {
// Map: DateTime
case ColumnTypes::TYPE_DATETIME:
$column->setType('DateTime');
if ($column->getDefault() !== NULL) {
$column->setDefault('now');
}
$namespace->addUse('Nette\\Utils\\DateTime');
break;
// Map: Enum
// Map: Enum
case ColumnTypes::TYPE_ENUM:
foreach ($column->getEnum() as $enum) {
$name = Strings::upper($column->getName()) . '_' . $enum;
$class->addConst($name, $enum);
}
if ($column->getDefault() !== NULL) {
$column->setDefault(Strings::upper($column->getName()) . '_' . $column->getDefault());
}
break;
}
}
示例4: onGenerate
public function onGenerate(AbstractMetaSpec $spec, MetaSpecMatcher $matcher, Type $type, ClassType $class)
{
$ns = $class->getNamespace();
$ns->addUse("Skrz\\Meta\\Protobuf\\ProtobufMetaInterface");
$ns->addUse($type->getName(), null, $typeAlias);
$ns->addUse("Skrz\\Meta\\Protobuf\\Binary", null, $binary);
$ns->addUse("Skrz\\Meta\\Protobuf\\ProtobufException", null, $protobufExceptionAlias);
$class->addImplement("Skrz\\Meta\\Protobuf\\ProtobufMetaInterface");
foreach ($type->getProperties() as $property) {
if ($property->hasAnnotation("Skrz\\Meta\\Transient")) {
continue;
}
/** @var ProtobufField $field */
$field = $property->getAnnotation("Skrz\\Meta\\Protobuf\\ProtobufField");
$class->addConst(strtoupper(trim(preg_replace("/([A-Z])/", "_\$1", $property->getName() . "ProtobufField"), "_")), $field->number);
}
$fromProtobuf = $class->addMethod("fromProtobuf");
$fromProtobuf->setStatic(true);
$fromProtobuf->addParameter("input");
$fromProtobuf->addParameter("object", null)->setOptional(true);
$fromProtobuf->addParameter("start", 0)->setReference(true)->setOptional(true);
$fromProtobuf->addParameter("end", null)->setOptional(true);
$fromProtobuf->addComment("Creates \\{$type->getName()} object from serialized Protocol Buffers message.")->addComment("")->addComment("@param string \$input")->addComment("@param {$typeAlias} \$object")->addComment("@param int \$start")->addComment("@param int \$end")->addComment("")->addComment("@throws \\Exception")->addComment("")->addComment("@return {$typeAlias}");
$fromProtobuf->addBody("if (\$object === null) {")->addBody("\t\$object = new {$typeAlias}();")->addBody("}")->addBody("")->addBody("if (\$end === null) {")->addBody("\t\$end = strlen(\$input);")->addBody("}")->addBody("");
$fromProtobuf->addBody("while (\$start < \$end) {");
$fromProtobuf->addBody("\t\$tag = {$binary}::decodeVarint(\$input, \$start);");
$fromProtobuf->addBody("\t\$wireType = \$tag & 0x7;");
$fromProtobuf->addBody("\t\$number = \$tag >> 3;");
$fromProtobuf->addBody("\tswitch (\$number) {");
foreach ($type->getProperties() as $property) {
if ($property->hasAnnotation("Skrz\\Meta\\Transient")) {
continue;
}
$propertyType = $property->getType();
$baseType = $propertyType;
if ($baseType instanceof ArrayType) {
$baseType = $baseType->getBaseType();
}
/** @var ProtobufField $field */
$field = $property->getAnnotation("Skrz\\Meta\\Protobuf\\ProtobufField");
$fromProtobuf->addBody("\t\tcase {$field->number}:");
if ($field->packed) {
$binaryWireType = WireTypeEnum::toBinaryWireType(WireTypeEnum::STRING);
} else {
$binaryWireType = WireTypeEnum::toBinaryWireType($field->wireType);
}
$fromProtobuf->addBody("\t\t\tif (\$wireType !== {$binaryWireType}) {");
$fromProtobuf->addBody("\t\t\t\tthrow new {$protobufExceptionAlias}('Unexpected wire type ' . \$wireType . ', expected {$binaryWireType}.', \$number);");
$fromProtobuf->addBody("\t\t\t}");
$propertyLhs = "\$object->{$property->getName()}";
if ($propertyType->isArray()) {
$fromProtobuf->addBody("\t\t\tif (!(isset({$propertyLhs}) && is_array({$propertyLhs}))) {");
$fromProtobuf->addBody("\t\t\t\t{$propertyLhs} = array();");
$fromProtobuf->addBody("\t\t\t}");
$propertyLhs .= "[]";
}
if ($field->packed) {
$fromProtobuf->addBody("\t\t\t\$packedLength = {$binary}::decodeVarint(\$input, \$start);")->addBody("\t\t\t\$expectedPacked = \$start + \$packedLength;")->addBody("\t\t\tif (\$expectedPacked > \$end) {")->addBody("\t\t\t\tthrow new {$protobufExceptionAlias}('Not enough data.');")->addBody("\t\t\t}")->addBody("\t\t\twhile (\$start < \$expectedPacked) {");
$indent = "\t\t\t\t";
} else {
$indent = "\t\t\t";
}
switch ($field->wireType) {
case WireTypeEnum::VARINT:
$fromProtobuf->addBody("{$indent}{$propertyLhs} = " . ($baseType instanceof BoolType ? "(bool)" : "") . "{$binary}::decodeVarint(\$input, \$start);");
break;
case WireTypeEnum::ZIGZAG:
$fromProtobuf->addBody("{$indent}{$propertyLhs} = {$binary}::decodeZigzag(\$input, \$start);");
break;
case WireTypeEnum::FIXED64:
$fromProtobuf->addBody("{$indent}\$expectedStart = \$start + 8;")->addBody("{$indent}if (\$expectedStart > \$end) {")->addBody("{$indent}\tthrow new {$protobufExceptionAlias}('Not enough data.');")->addBody("{$indent}}");
if ($baseType instanceof FloatType) {
$fromProtobuf->addBody("{$indent}{$propertyLhs} = {$binary}::decodeDouble(\$input, \$start);");
} elseif ($baseType instanceof IntType && $field->unsigned) {
$fromProtobuf->addBody("{$indent}{$propertyLhs} = {$binary}::decodeUint64(\$input, \$start);");
} elseif ($baseType instanceof IntType && !$field->unsigned) {
$fromProtobuf->addBody("{$indent}{$propertyLhs} = {$binary}::decodeInt64(\$input, \$start);");
} else {
throw new MetaException("Property {$type->getName()}::\${$property->getName()} has wire type '{$field->wireType}' and base type {$baseType}. " . "'{$field->wireType}' supports only float or int base types.");
}
$fromProtobuf->addBody("{$indent}if (\$start !== \$expectedStart) {")->addBody("{$indent}\tthrow new {$protobufExceptionAlias}('Unexpected start. Expected ' . \$expectedStart . ', got ' . \$start . '.', \$number);")->addBody("{$indent}}");
break;
case WireTypeEnum::FIXED32:
$fromProtobuf->addBody("{$indent}\$expectedStart = \$start + 4;")->addBody("{$indent}if (\$expectedStart > \$end) {")->addBody("{$indent}\tthrow new {$protobufExceptionAlias}('Not enough data.');")->addBody("{$indent}}");
if ($baseType instanceof FloatType) {
$fromProtobuf->addBody("{$indent}{$propertyLhs} = {$binary}::decodeFloat(\$input, \$start);");
} elseif ($baseType instanceof IntType && $field->unsigned) {
$fromProtobuf->addBody("{$indent}{$propertyLhs} = {$binary}::decodeUint32(\$input, \$start);");
} elseif ($baseType instanceof IntType && !$field->unsigned) {
$fromProtobuf->addBody("{$indent}{$propertyLhs} = {$binary}::decodeInt32(\$input, \$start);");
} else {
throw new MetaException("Property {$type->getName()}::\${$property->getName()} has wire type '{$field->wireType}' and base type {$baseType}. " . "'{$field->wireType}' supports only float or int base types.");
}
$fromProtobuf->addBody("{$indent}if (\$start !== \$expectedStart) {")->addBody("{$indent}\tthrow new {$protobufExceptionAlias}('Unexpected start. Expected ' . \$expectedStart . ', got ' . \$start . '.', \$number);")->addBody("{$indent}}");
break;
case WireTypeEnum::STRING:
$fromProtobuf->addBody("{$indent}\$length = {$binary}::decodeVarint(\$input, \$start);")->addBody("{$indent}\$expectedStart = \$start + \$length;")->addBody("{$indent}if (\$expectedStart > \$end) {")->addBody("{$indent}\tthrow new {$protobufExceptionAlias}('Not enough data.');")->addBody("{$indent}}");
if ($baseType instanceof StringType) {
$fromProtobuf->addBody("{$indent}{$propertyLhs} = substr(\$input, \$start, \$length);");
$fromProtobuf->addBody("{$indent}\$start += \$length;");
//.........这里部分代码省略.........