本文整理汇总了PHP中Zend\Code\Generator\FileGenerator::setUse方法的典型用法代码示例。如果您正苦于以下问题:PHP FileGenerator::setUse方法的具体用法?PHP FileGenerator::setUse怎么用?PHP FileGenerator::setUse使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Zend\Code\Generator\FileGenerator
的用法示例。
在下文中一共展示了FileGenerator::setUse方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: getCodeGenerator
//.........这里部分代码省略.........
continue;
}
$methodName = isset($methodData['name']) ? $methodData['name'] : $methodData['method'];
$methodParams = $methodData['params'];
// Create method parameter representation
foreach ($methodParams as $key => $param) {
if (null === $param || is_scalar($param) || is_array($param)) {
$string = var_export($param, 1);
if (strstr($string, '::__set_state(')) {
throw new Exception\RuntimeException('Arguments in definitions may not contain objects');
}
$methodParams[$key] = $string;
} elseif ($param instanceof GeneratorInstance) {
$methodParams[$key] = sprintf('$this->%s()', $this->normalizeAlias($param->getName()));
} else {
$message = sprintf('Unable to use object arguments when generating method calls. Encountered with class "%s", method "%s", parameter of type "%s"', $name, $methodName, get_class($param));
throw new Exception\RuntimeException($message);
}
}
// Strip null arguments from the end of the params list
$reverseParams = array_reverse($methodParams, true);
foreach ($reverseParams as $key => $param) {
if ('NULL' === $param) {
unset($methodParams[$key]);
continue;
}
break;
}
$methods .= sprintf("\$object->%s(%s);\n", $methodName, implode(', ', $methodParams));
}
// Generate caching statement
$storage = '';
if ($im->hasSharedInstance($name, $params)) {
$storage = sprintf("\$this->services['%s'] = \$object;\n", $name);
}
// Start creating getter
$getterBody = '';
// Create fetch of stored service
if ($im->hasSharedInstance($name, $params)) {
$getterBody .= sprintf("if (isset(\$this->services['%s'])) {\n", $name);
$getterBody .= sprintf("%sreturn \$this->services['%s'];\n}\n\n", $indent, $name);
}
// Creation and method calls
$getterBody .= sprintf("%s\n", $creation);
$getterBody .= $methods;
// Stored service
$getterBody .= $storage;
// End getter body
$getterBody .= "return \$object;\n";
$getterDef = new MethodGenerator();
$getterDef->setName($getter);
$getterDef->setBody($getterBody);
$getters[] = $getterDef;
// Get cases for case statements
$cases = array($name);
if (isset($aliases[$name])) {
$cases = array_merge($aliases[$name], $cases);
}
// Build case statement and store
$statement = '';
foreach ($cases as $value) {
$statement .= sprintf("%scase '%s':\n", $indent, $value);
}
$statement .= sprintf("%sreturn \$this->%s();\n", str_repeat($indent, 2), $getter);
$caseStatements[] = $statement;
}
// Build switch statement
$switch = sprintf("switch (%s) {\n%s\n", '$name', implode("\n", $caseStatements));
$switch .= sprintf("%sdefault:\n%sreturn parent::get(%s, %s);\n", $indent, str_repeat($indent, 2), '$name', '$params');
$switch .= "}\n\n";
// Build get() method
$nameParam = new ParameterGenerator();
$nameParam->setName('name');
$paramsParam = new ParameterGenerator();
$paramsParam->setName('params')->setType('array')->setDefaultValue(array());
$get = new MethodGenerator();
$get->setName('get');
$get->setParameters(array($nameParam, $paramsParam));
$get->setBody($switch);
// Create getters for aliases
$aliasMethods = array();
foreach ($aliases as $class => $classAliases) {
foreach ($classAliases as $alias) {
$aliasMethods[] = $this->getCodeGenMethodFromAlias($alias, $class);
}
}
// Create class code generation object
$container = new ClassGenerator();
$container->setName($this->containerClass)->setExtendedClass('ServiceLocator')->addMethodFromGenerator($get)->addMethods($getters)->addMethods($aliasMethods);
// Create PHP file code generation object
$classFile = new FileGenerator();
$classFile->setUse('Zend\\Di\\ServiceLocator')->setClass($container);
if (null !== $this->namespace) {
$classFile->setNamespace($this->namespace);
}
if (null !== $filename) {
$classFile->setFilename($filename);
}
return $classFile;
}
示例2: testGeneratesUseStatements
/**
* @group ZF-11218
*/
public function testGeneratesUseStatements()
{
$file = new FileGenerator();
$file->setUse('My\\Baz')->setUses(array(array('Your\\Bar', 'bar')));
$generated = $file->generate();
$this->assertContains('use My\\Baz;', $generated);
$this->assertContains('use Your\\Bar as bar;', $generated);
}
示例3: testSetUseDoesntGenerateMultipleIdenticalUseStatements
public function testSetUseDoesntGenerateMultipleIdenticalUseStatements()
{
$file = new FileGenerator();
$file->setUse('My\\Baz')->setUse('My\\Baz');
$generated = $file->generate();
$this->assertSame(strpos($generated, 'use My\\Baz'), strrpos($generated, 'use My\\Baz'));
}