作者:jbaffor
项目:ProxyManage
/**
* 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\', []);');
}
作者:enlitepr
项目:zf2-scaffol
/**
* @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);
}
作者:vmalinovski
项目:CallFire-PHP-SD
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);
}
作者:cyberrebel
项目:arango-od
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;
}
作者:John-Edd
项目:ProjetCasto
/**
* @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);
}
作者:enlitepr
项目:zf2-scaffol
/**
* 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);
}
作者:Clon45
项目:batat
/**
* {@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'));
}
作者:nf
项目:pugooro
/**
* @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;
}
作者:enlitepr
项目:zf2-scaffol
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);
}
作者:pgiacomett
项目:zf2rapi
/**
* 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);
}
作者:protobuf-ph
项目:protobuf-plugi
/**
* @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;
}
作者:protobuf-ph
项目:protobuf-plugi
/**
* @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;
}
作者:protobuf-ph
项目:protobuf-plugi
/**
* @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;
}
作者:Flesh19
项目:magent
/**
* 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;
}
作者:nomaan
项目:zf2rapi
/**
* 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);
}
作者:zfrapi
项目:zf2rapi
/**
* Generate an __invoke method
*/
protected function addInvokeMethod()
{
// set action body
$body = ['// add view helper code here', '$output = \'\';', '', 'return $output;'];
$body = implode(AbstractGenerator::LINE_FEED, $body);
// create method
$method = new MethodGenerator();
$method->setName('__invoke');
$method->setBody($body);
// check for api docs
if ($this->config['flagAddDocBlocks']) {
$method->setDocBlock(new DocBlockGenerator('Called when view helper is executed', null, [new ReturnTag(['string'])]));
}
// add method
$this->addMethodFromGenerator($method);
}
作者:Clon45
项目:batat
/**
* @param \ReflectionClass $originalClass
* @param \Zend\Code\Generator\PropertyGenerator $initializerProperty
* @param \Zend\Code\Generator\MethodGenerator $callInitializer
* @param \ProxyManager\ProxyGenerator\PropertyGenerator\PublicPropertiesMap $publicProperties
*/
public function __construct(ReflectionClass $originalClass, PropertyGenerator $initializerProperty, MethodGenerator $callInitializer, PublicPropertiesMap $publicProperties)
{
parent::__construct($originalClass, '__get', array(new ParameterGenerator('name')));
$override = $originalClass->hasMethod('__get');
$callParent = '';
$this->setDocblock(($override ? "{@inheritDoc}\n" : '') . '@param string $name');
if (!$publicProperties->isEmpty()) {
$callParent = 'if (isset(self::$' . $publicProperties->getName() . "[\$name])) {\n" . ' return $this->$name;' . "\n}\n\n";
}
if ($override) {
$callParent .= 'return parent::__get($name);';
} else {
$callParent .= PublicScopeSimulator::getPublicAccessSimulationCode(PublicScopeSimulator::OPERATION_GET, 'name');
}
$this->setBody('$this->' . $initializerProperty->getName() . ' && $this->' . $callInitializer->getName() . '(\'__get\', array(\'name\' => $name));' . "\n\n" . $callParent);
}
作者:zfrapi
项目:zf2rapi
/**
* Generate an __invoke method
*/
protected function addInvokeMethod()
{
// set action body
$body = ['// add controller plugin code here'];
$body = implode(AbstractGenerator::LINE_FEED, $body);
// create method
$method = new MethodGenerator();
$method->setName('__invoke');
$method->setBody($body);
// check for api docs
if ($this->config['flagAddDocBlocks']) {
$method->setDocBlock(new DocBlockGenerator('Called when controller plugin is executed', null, [new ReturnTag(['mixed'])]));
}
// add method
$this->addMethodFromGenerator($method);
}
作者:protobuf-ph
项目:protobuf-plugi
/**
* @param \Protobuf\Compiler\Entity $entity
*
* @return MethodGenerator
*/
protected function generateMethod(Entity $entity)
{
$lines = $this->generateBody($entity);
$body = implode(PHP_EOL, $lines);
$method = MethodGenerator::fromArray(['name' => 'merge', 'body' => $body, 'parameters' => [['name' => 'message', 'type' => '\\Protobuf\\Message']], 'docblock' => ['shortDescription' => "{@inheritdoc}"]]);
return $method;
}
作者:zfrapi
项目:zf2rapi
/**
* Generate a isValid method
*/
protected function addIsValidMethod()
{
// set action body
$body = ['$this->setValue((string) $value);', '', '// add validation code here', '$isValid = true;', '', 'if (!$isValid) {', ' $this->error(self::INVALID);', ' return false;', '}', '', 'return true;'];
$body = implode(AbstractGenerator::LINE_FEED, $body);
// create method
$method = new MethodGenerator();
$method->setName('isValid');
$method->setBody($body);
$method->setParameters([new ParameterGenerator('value', 'mixed')]);
// check for api docs
if ($this->config['flagAddDocBlocks']) {
$method->setDocBlock(new DocBlockGenerator('Called when validator is executed', null, [new ParamTag('value', ['mixed']), new ReturnTag(['mixed'])]));
}
// add method
$this->addMethodFromGenerator($method);
}