本文整理汇总了PHP中Nette\PhpGenerator\ClassType::addTrait方法的典型用法代码示例。如果您正苦于以下问题:PHP ClassType::addTrait方法的具体用法?PHP ClassType::addTrait怎么用?PHP ClassType::addTrait使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Nette\PhpGenerator\ClassType
的用法示例。
在下文中一共展示了ClassType::addTrait方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: addTrait
private function addTrait($trait, $typesReference)
{
$traitFull = $trait;
if (array_key_exists($trait, $typesReference)) {
$traitNamespace = $typesReference[$trait];
$traitFull = $traitNamespace . '\\' . $trait;
}
$this->currentClass->getNamespace()->addUse($traitFull);
$this->currentClass->addTrait($traitFull);
$this->info('Add trait', ['class' => $this->currentClass->getName(), 'trait' => $traitFull]);
}
示例2: generateTests
public function generateTests($outputFolder)
{
$container = \Testbench\ContainerFactory::create(FALSE);
$presenterFactory = $container->getByType('Nette\\Application\\IPresenterFactory');
$presenters = $container->findByType('Nette\\Application\\UI\\Presenter');
foreach ($presenters as $presenter) {
$this->renderMethods = $this->handleMethods = $this->componentMethods = [];
/** @var \Nette\Application\UI\Presenter $service */
$service = $container->getService($presenter);
if ($service instanceof \Testbench\Mocks\PresenterMock) {
continue;
}
if ($service instanceof \KdybyModule\CliPresenter) {
//Oh, Kdyby! :-(
continue;
}
$rc = new \ReflectionClass($service);
$renderPrefix = $service->formatActionMethod('') . '|' . $service->formatRenderMethod('');
$methods = $rc->getMethods(\ReflectionMethod::IS_PUBLIC | \ReflectionMethod::IS_PROTECTED);
foreach ($methods as $method) {
$methodName = $method->getName();
if (preg_match("~^({$renderPrefix})[a-z0-9]+~i", $methodName)) {
try {
$optionalArgs = $this->tryCall($service, $methodName, $service->getParameters(), TRUE);
if (preg_match('~.*rss.*~i', $methodName)) {
$this->renderMethods[$methodName] = 'rss';
} elseif (preg_match('~.*sitemap.*~i', $methodName)) {
$this->renderMethods[$methodName] = 'sitemap';
} else {
$requiredArgs = $this->tryCall($service, $methodName, $service->getParameters(), FALSE);
$this->renderMethods[$methodName] = ['action', [$optionalArgs, $requiredArgs]];
}
} catch (\Nette\Application\AbortException $exc) {
$this->renderMethods[$methodName] = ['action', $this->getResponse($service)];
} catch (\Exception $exc) {
$this->renderMethods[$methodName] = ['exception', $exc];
}
}
if (preg_match('~^handle[a-z0-9]+~i', $methodName)) {
if ($methodName === 'handleInvalidLink') {
//internal method
continue;
}
$this->handleMethods[] = $methodName;
}
if (preg_match('~^createComponent[a-z0-9]+~i', $methodName)) {
$method->setAccessible(TRUE);
$form = $method->invoke($service);
if ($form instanceof \Nette\Application\UI\Form) {
$this->componentMethods[$methodName] = $form;
}
}
}
$testClassName = $rc->getShortName() . 'Test';
$testClass = new PhpGenerator\ClassType($testClassName);
$testClass->setExtends('\\Tester\\TestCase');
$testClass->addTrait('\\Testbench\\TPresenter');
$testClass->addComment('@testCase');
foreach ($this->renderMethods as $testMethod => $testMethodType) {
$generatedMethod = $testClass->addMethod('test' . ucfirst($testMethod));
$destination = $presenterFactory->unformatPresenterClass($rc->getName()) . ':';
$destination .= lcfirst(preg_replace('~^(action|render)([a-z]+)~i', '$2', $testMethod));
$extra = NULL;
if (is_array($testMethodType)) {
/** @var \Exception|\Nette\Application\IResponse $extra */
$extra = $testMethodType[1];
$testMethodType = $testMethodType[0];
//FIXME: fuj, hnus
}
switch ($testMethodType) {
case 'rss':
$generatedMethod->addBody('$this->checkRss(?);', [$destination]);
break;
case 'sitemap':
$generatedMethod->addBody('$this->checkSitemap(?);', [$destination]);
break;
case 'action':
if ($extra instanceof \Nette\Application\Responses\RedirectResponse) {
$url = new \Nette\Http\Url($extra->getUrl());
$generatedMethod->addBody('$this->checkRedirect(?, ?);', [$destination, $url->getPath()]);
} elseif ($extra instanceof \Nette\Application\Responses\JsonResponse) {
$generatedMethod->addBody('$this->checkJson(?);', [$destination]);
} else {
if ($extra[0]) {
$generatedMethod->addBody('//FIXME: parameters may not be correct');
$generatedMethod->addBody("\$this->checkAction(?, ?);\n", [$destination, $extra[0]]);
$generatedMethod->addBody('$this->checkAction(?, ?);', [$destination, $extra[1]]);
} else {
$generatedMethod->addBody('$this->checkAction(?);', [$destination]);
}
}
break;
case 'exception':
$this->generateExceptionBody($generatedMethod, $destination, $extra);
break;
}
}
foreach ($this->handleMethods as $testMethod) {
$destination = $presenterFactory->unformatPresenterClass($rc->getName());
$action = lcfirst(preg_replace('~^handle([a-z]+)~i', '$1', $testMethod));
//.........这里部分代码省略.........