本文整理匯總了PHP中Zend\Code\Generator\MethodGenerator類的典型用法代碼示例。如果您正苦於以下問題:PHP MethodGenerator類的具體用法?PHP MethodGenerator怎麽用?PHP MethodGenerator使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
在下文中一共展示了MethodGenerator類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的PHP代碼示例。
示例1: __construct
/**
* Constructor
*
* @param PropertyGenerator $initializerProperty
* @param ZendMethodGenerator $callInitializer
*/
public function __construct(PropertyGenerator $initializerProperty, ZendMethodGenerator $callInitializer)
{
parent::__construct('initializeProxy');
$this->setDocblock('{@inheritDoc}');
$this->setReturnType('bool');
$this->setBody('return $this->' . $initializerProperty->getName() . ' && $this->' . $callInitializer->getName() . '(\'initializeProxy\', []);');
}
示例2: build
/**
* @param State|\Scaffold\State $state
* @return State|void
*/
public function build(State $state)
{
$model = $state->getModel('options-factory');
$generator = new ClassGenerator($model->getName());
$generator->setImplementedInterfaces(['FactoryInterface']);
$model->setGenerator($generator);
$generator->addUse('Zend\\ServiceManager\\FactoryInterface');
$generator->addUse('Zend\\ServiceManager\\ServiceLocatorInterface');
$options = $state->getModel('options');
$key = $options->getServiceName();
$key = substr($key, 0, -7);
$body = <<<EOF
\$config = \$serviceLocator->get('Config');
return new {$options->getClassName()}(
isset(\$config['{$key}'])
? \$config['{$key}']
: []
);
EOF;
$method = new MethodGenerator('createService');
$method->setParameter(new ParameterGenerator('serviceLocator', 'ServiceLocatorInterface'));
$method->setBody($body);
$doc = new DocBlockGenerator('');
$doc->setTag(new Tag(['name' => 'inhertidoc']));
$method->setDocBlock($doc);
$generator->addMethodFromGenerator($method);
}
示例3: addToString
public function addToString()
{
$classGenerator = $this->getClassGenerator();
$methodGenerator = new CodeGenerator\MethodGenerator();
$methodGenerator->setName('__toString');
$idAttribute = 'id';
$body = <<<METHODBODY
\$attributes = array(\$this->{$idAttribute});
METHODBODY;
foreach (static::$secondaryProperties as $attributeName => $propertyName) {
$body .= <<<METHODBODY
if (!empty(\$this->{$propertyName})) {
\$attributes[] = "{$attributeName}={\$this->{$propertyName}}";
}
METHODBODY;
}
$body .= <<<METHODBODY
foreach (\$this->attributes as \$attributeName => \$value) {
if (!empty(\$value)) {
\$attributes[] = "{\$attributeName}={\$value}";
}
}
return implode('!', \$attributes);
METHODBODY;
$methodGenerator->setBody($body);
$classGenerator->addMethodFromGenerator($methodGenerator);
}
示例4: getConstructor
protected function getConstructor()
{
$defaultValue = new ValueGenerator([], ValueGenerator::TYPE_ARRAY);
$setterParam = new ParameterGenerator('properties', 'array', $defaultValue);
$methodGenerator = new MethodGenerator('__construct', [$setterParam]);
$methodGenerator->setBody('$this->properties = $properties;');
return $methodGenerator;
}
示例5: addMethodIfNotFinal
/**
* @param ReflectionClass $originalClass
* @param ClassGenerator $classGenerator
* @param MethodGenerator $generatedMethod
*
* @return void|false
*/
public static function addMethodIfNotFinal(ReflectionClass $originalClass, ClassGenerator $classGenerator, MethodGenerator $generatedMethod)
{
$methodName = $generatedMethod->getName();
if ($originalClass->hasMethod($methodName) && $originalClass->getMethod($methodName)->isFinal()) {
return false;
}
$classGenerator->addMethodFromGenerator($generatedMethod);
}
示例6: buildFactory
/**
* Build method factory
*
* @param ClassGenerator $generator
*/
public function buildFactory(ClassGenerator $generator)
{
$docBlock = new DocBlockGenerator('@return ' . $this->config->getName());
$factory = new MethodGenerator();
$factory->setDocBlock($docBlock);
$factory->setName('factory');
$factory->setBody('return new ' . $this->config->getName() . '();');
$generator->addMethodFromGenerator($factory);
}
示例7: setUp
/**
* {@inheritDoc}
*/
protected function setUp()
{
$this->initializer = $this->getMock('Zend\\Code\\Generator\\PropertyGenerator');
$this->initMethod = $this->getMock('Zend\\Code\\Generator\\MethodGenerator');
$this->publicProperties = $this->getMockBuilder('ProxyManager\\ProxyGenerator\\PropertyGenerator\\PublicPropertiesMap')->disableOriginalConstructor()->getMock();
$this->initializer->expects($this->any())->method('getName')->will($this->returnValue('foo'));
$this->initMethod->expects($this->any())->method('getName')->will($this->returnValue('baz'));
$this->publicProperties->expects($this->any())->method('isEmpty')->will($this->returnValue(false));
$this->publicProperties->expects($this->any())->method('getName')->will($this->returnValue('bar'));
}
示例8: addMethod
/**
* @param string $name
* @param Code $code
* @return PhpMethod
*/
public function addMethod($name, Code $code)
{
$this->code[$name] = $code;
$method = new MethodGenerator();
$method->setName($name);
$method->setVisibility(MethodGenerator::VISIBILITY_PUBLIC);
// $code remains object until turning to string
$method->setBody($code);
$this->addMethodFromGenerator($method);
return $method;
}
示例9: addTestIdMethod
public function addTestIdMethod(ClassGenerator $generator, State $state)
{
$method = new MethodGenerator('testGetSetId');
$class = $state->getEntityModel()->getClassName();
$code = <<<EOF
\$object = new {$class}();
\$object->setId(123);
\$this->assertEquals(123, \$object->getId());
EOF;
$method->setBody($code);
$generator->addMethodFromGenerator($method);
}
示例10: addInitMethod
/**
* Generate an init method
*/
protected function addInitMethod()
{
// set action body
$body = ['// add form elements and form configuration here'];
$body = implode(AbstractGenerator::LINE_FEED, $body);
// create method
$method = new MethodGenerator();
$method->setName('init');
$method->setBody($body);
// check for api docs
if ($this->config['flagAddDocBlocks']) {
$method->setDocBlock(new DocBlockGenerator('Generate form by adding elements'));
}
// add method
$this->addMethodFromGenerator($method);
}
示例11: generateClearMethod
/**
* @param \Protobuf\Compiler\Entity $entity
*
* @return \Zend\Code\Generator\GeneratorInterface
*/
protected function generateClearMethod(Entity $entity)
{
$lines = $this->generateBody($entity);
$body = implode(PHP_EOL, $lines);
$method = MethodGenerator::fromArray(['name' => 'clear', 'body' => $body, 'docblock' => ['shortDescription' => "{@inheritdoc}"]]);
return $method;
}
示例12: generateMethod
/**
* @param \Protobuf\Compiler\Entity $entity
*
* @return string
*/
protected function generateMethod(Entity $entity)
{
$lines = $this->generateBody($entity);
$body = implode(PHP_EOL, $lines);
$method = MethodGenerator::fromArray(['name' => 'fromArray', 'body' => $body, 'static' => true, 'parameters' => [['name' => 'values', 'type' => 'array']], 'docblock' => ['shortDescription' => "{@inheritdoc}"]]);
return $method;
}
示例13: generateConstructorMethod
/**
* @param \Protobuf\Compiler\Entity $entity
*
* @return \Zend\Code\Generator\GeneratorInterface
*/
protected function generateConstructorMethod(Entity $entity)
{
$lines = $this->generateBody($entity);
$body = implode(PHP_EOL, $lines);
$method = MethodGenerator::fromArray(['name' => '__construct', 'body' => $body, 'parameters' => [['name' => 'stream', 'type' => 'mixed', 'defaultValue' => null], ['name' => 'configuration', 'type' => '\\Protobuf\\Configuration', 'defaultValue' => null]], 'docblock' => ['shortDescription' => '{@inheritdoc}']]);
return $method;
}
示例14: fromReflection
/**
* Build a Code Generation Php Object from a Class Reflection
*
* @param ClassReflection $classReflection
* @return TraitGenerator
*/
public static function fromReflection(ClassReflection $classReflection)
{
// class generator
$cg = new static($classReflection->getName());
$cg->setSourceContent($cg->getSourceContent());
$cg->setSourceDirty(false);
if ($classReflection->getDocComment() != '') {
$cg->setDocBlock(DocBlockGenerator::fromReflection($classReflection->getDocBlock()));
}
// set the namespace
if ($classReflection->inNamespace()) {
$cg->setNamespaceName($classReflection->getNamespaceName());
}
$properties = array();
foreach ($classReflection->getProperties() as $reflectionProperty) {
if ($reflectionProperty->getDeclaringClass()->getName() == $classReflection->getName()) {
$properties[] = PropertyGenerator::fromReflection($reflectionProperty);
}
}
$cg->addProperties($properties);
$methods = array();
foreach ($classReflection->getMethods() as $reflectionMethod) {
$className = $cg->getNamespaceName() ? $cg->getNamespaceName() . '\\' . $cg->getName() : $cg->getName();
if ($reflectionMethod->getDeclaringClass()->getName() == $className) {
$methods[] = MethodGenerator::fromReflection($reflectionMethod);
}
}
$cg->addMethods($methods);
return $cg;
}
示例15: addInitMethod
/**
* Generate an init method
*/
protected function addInitMethod()
{
// set action body
$body = array('// add input objects here');
$body = implode(AbstractGenerator::LINE_FEED, $body);
// create method
$method = new MethodGenerator();
$method->setName('init');
$method->setBody($body);
// check for api docs
if ($this->config['flagAddDocBlocks']) {
$method->setDocBlock(new DocBlockGenerator('Generate input filter by adding inputs'));
}
// add method
$this->addMethodFromGenerator($method);
}