本文整理汇总了PHP中Nette\PhpGenerator\ClassType::addExtend方法的典型用法代码示例。如果您正苦于以下问题:PHP ClassType::addExtend方法的具体用法?PHP ClassType::addExtend怎么用?PHP ClassType::addExtend使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Nette\PhpGenerator\ClassType
的用法示例。
在下文中一共展示了ClassType::addExtend方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: 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));
}
示例2: onGenerate
public function onGenerate(AbstractMetaSpec $spec, MetaSpecMatcher $matcher, Type $type, ClassType $class)
{
$namespace = $class->getNamespace();
// extend base class
$namespace->addUse($type->getName(), null, $typeAlias);
$class->addExtend($type->getName());
$class->addComment("Meta class for \\{$type->getName()}")->addComment("")->addComment("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!")->addComment("!!! !!!")->addComment("!!! THIS CLASS HAS BEEN AUTO-GENERATED, DO NOT EDIT !!!")->addComment("!!! !!!")->addComment("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
// constructor
$constructor = $class->addMethod("__construct");
$constructor->addComment("Constructor")->addBody("self::\$instance = \$this; // avoids cyclic dependency stack overflow");
if ($type->getConstructor()) {
if ($type->getConstructor()->isPublic()) {
$constructor->setVisibility("public");
} elseif ($type->getConstructor()->isProtected()) {
$constructor->setVisibility("protected");
} elseif ($type->getConstructor()->isPrivate()) {
$constructor->setVisibility("private");
}
} else {
$constructor->setVisibility("private");
}
// implement base interface
$namespace->addUse("Skrz\\Meta\\MetaInterface", null, $metaInterfaceAlias);
$class->addImplement("Skrz\\Meta\\MetaInterface");
// getInstance() method
$instance = $class->addProperty("instance");
$instance->setStatic(true);
$instance->setVisibility("private");
$instance->addComment("@var {$class->getName()}");
$getInstance = $class->addMethod("getInstance");
$getInstance->setStatic(true);
$getInstance->addComment("Returns instance of this meta class")->addComment("")->addComment("@return {$class->getName()}");
$getInstance->addBody("if (self::\$instance === null) {")->addBody("\tnew self(); // self::\$instance assigned in __construct")->addBody("}")->addBody("return self::\$instance;");
// create() method
$create = $class->addMethod("create");
$create->setStatic(true);
$create->addComment("Creates new instance of \\{$type->getName()}")->addComment("")->addComment("@throws \\InvalidArgumentException")->addComment("")->addComment("@return {$typeAlias}");
$create->addBody("switch (func_num_args()) {");
$maxArguments = 8;
$constructMethod = $type->getConstructor();
for ($i = 0; $i <= $maxArguments; ++$i) {
$create->addBody("\tcase {$i}:");
if ($constructMethod && $i < $constructMethod->getNumberOfRequiredParameters()) {
$create->addBody("\t\tthrow new \\InvalidArgumentException('At least {$constructMethod->getNumberOfRequiredParameters()} arguments have to be supplied.');");
} else {
$args = array();
for ($j = 0; $j < $i; ++$j) {
$args[] = "func_get_arg({$j})";
}
$create->addBody("\t\treturn new {$typeAlias}(" . implode(", ", $args) . ");");
}
}
$create->addBody("\tdefault:");
$create->addBody("\t\tthrow new \\InvalidArgumentException('More than {$maxArguments} arguments supplied, please be reasonable.');");
$create->addBody("}");
// reset() method
$reset = $class->addMethod("reset");
$reset->setStatic(true);
$reset->addComment("Resets properties of \\{$type->getName()} to default values\n")->addComment("")->addComment("@param {$typeAlias} \$object")->addComment("")->addComment("@throws \\InvalidArgumentException")->addComment("")->addComment("@return void");
$reset->addParameter("object");
$reset->addBody("if (!(\$object instanceof {$typeAlias})) {")->addBody("\tthrow new \\InvalidArgumentException('You have to pass object of class {$type->getName()}.');")->addBody("}");
foreach ($type->getProperties() as $property) {
if ($property->hasAnnotation("Skrz\\Meta\\Transient")) {
continue;
}
if ($property->isPrivate()) {
throw new MetaException("Private property '{$type->getName()}::\${$property->getName()}'. " . "Either make the property protected/public if you need to process it, " . "or mark it using @Transient annotation.");
}
$reset->addBody("\$object->{$property->getName()} = " . var_export($property->getDefaultValue(), true) . ";");
}
// hash() method
$hash = $class->addMethod("hash");
$hash->setStatic(true);
$hash->addComment("Computes hash of \\{$type->getName()}")->addComment("")->addComment("@param object \$object")->addComment("@param string|resource \$algoOrCtx")->addComment("@param bool \$raw")->addComment("")->addComment("@return string|void");
$hash->addParameter("object");
$hash->addParameter("algoOrCtx")->setDefaultValue("md5")->setOptional(true);
$hash->addParameter("raw")->setDefaultValue(false)->setOptional(true);
$hash->addBody("if (is_string(\$algoOrCtx)) {")->addBody("\t\$ctx = hash_init(\$algoOrCtx);")->addBody("} else {")->addBody("\t\$ctx = \$algoOrCtx;")->addBody("}")->addBody("");
foreach ($type->getProperties() as $property) {
if ($property->hasAnnotation("Skrz\\Meta\\Transient")) {
continue;
}
if ($property->hasAnnotation("Skrz\\Meta\\Hash")) {
continue;
}
$objectPath = "\$object->{$property->getName()}";
$hash->addBody("if (isset({$objectPath})) {");
$hash->addBody("\thash_update(\$ctx, " . var_export($property->getName(), true) . ");");
$baseType = $property->getType();
$indent = "\t";
$before = "";
$after = "";
for ($i = 0; $baseType instanceof ArrayType; ++$i) {
$arrayType = $baseType;
$baseType = $arrayType->getBaseType();
$before .= "{$indent}foreach ({$objectPath} instanceof \\Traversable ? {$objectPath} : (array){$objectPath} as \$v{$i}) {\n";
$after = "{$indent}}\n" . $after;
$indent .= "\t";
$objectPath = "\$v{$i}";
}
//.........这里部分代码省略.........