當前位置: 首頁>>代碼示例>>PHP>>正文


PHP FileGenerator::setUse方法代碼示例

本文整理匯總了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;
 }
開發者ID:eltondias,項目名稱:Relogio,代碼行數:101,代碼來源:Generator.php

示例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);
 }
開發者ID:bradley-holt,項目名稱:zf2,代碼行數:11,代碼來源:FileGeneratorTest.php

示例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'));
 }
開發者ID:pnaq57,項目名稱:zf2demo,代碼行數:7,代碼來源:FileGeneratorTest.php


注:本文中的Zend\Code\Generator\FileGenerator::setUse方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。