zendframework / zend-code

BSD 3-Clause "New" or "Revised" License
1.68k stars 78 forks source link

Class name referencing - how to #96

Closed basz closed 8 years ago

basz commented 8 years ago

Example of how to generate stuff like so...

ClassName::SOME_CONSTANT = ...

all I seem to manage is

'ClassName::SOME_CONSTANT' = ...

I would like to know how to do this and when I do I will add it as example to the documentation.

Ocramius commented 8 years ago

How are you currently trying it?

On 29 Sep 2016 00:05, "Bas Kamer" notifications@github.com wrote:

Example of how to generate stuff like so...

ClassName::SOME_CONSTANT = ...

all I seem to manage is

'ClassName::SOME_CONSTANT' = ...

I would like to know how to do this and when I do I will add it as example to the documentation.

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/zendframework/zend-code/issues/96, or mute the thread https://github.com/notifications/unsubscribe-auth/AAJakGV84upTkQA2Yzcx_HWc4ylmkSHEks5quuSagaJpZM4KJXgh .

basz commented 8 years ago

I gave up because I couldn't find a solution... back to str_replace and heredocs

$classGenerator    = new \Zend\Code\Generator\ClassGenerator();
$propertyGenerator = new \Zend\Code\Generator\PropertyGenerator();
$value             = new \Zend\Code\Generator\ValueGenerator();
$value->setValue('ClassName::SOME_CONSTANT');
$value->setType(\Zend\Code\Generator\ValueGenerator::TYPE_CONSTANT);
$propertyGenerator->setName("foo");
$propertyGenerator->setDefaultValue($value->generate());

$classGenerator->addPropertyFromGenerator($propertyGenerator);

print $classGenerator->generate();
class 
{

    public $foo = 'ClassName::SOME_CONSTANT';

}
Ocramius commented 8 years ago

That's not a value, but an expression. AFAIK, that doesn't really work with the value generator.

Plus I'm fairly sure that most default value expressions were overhauled in 5.6 :-\

On 29 Sep 2016 09:00, "Bas Kamer" notifications@github.com wrote:

I gave up because I couldn't find a solution... back to str_replace and heredocs

$classGenerator = new \Zend\Code\Generator\ClassGenerator(); $propertyGenerator = new \Zend\Code\Generator\PropertyGenerator(); $value = new \Zend\Code\Generator\ValueGenerator(); $value->setValue('ClassName::SOME_CONSTANT'); $value->setType(\Zend\Code\Generator\ValueGenerator::TYPE_CONSTANT); $propertyGenerator->setName("foo"); $propertyGenerator->setDefaultValue($value->generate());

$classGenerator->addPropertyFromGenerator($propertyGenerator);

print $classGenerator->generate();

class {

public $foo = 'ClassName::SOME_CONSTANT';

}

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/zendframework/zend-code/issues/96#issuecomment-250386472, or mute the thread https://github.com/notifications/unsubscribe-auth/AAJakB2IMWdoztEMH9bXgJ-lGbcTAqczks5qu2IPgaJpZM4KJXgh .

adamlundrigan commented 8 years ago

PropertyGenerator::setDefaultValue expects a PropertyValueGenerator instance. In your code I changed line 3 from ValueGenerator to PropertyValueGenerator:

$classGenerator    = new \Zend\Code\Generator\ClassGenerator();
$propertyGenerator = new \Zend\Code\Generator\PropertyGenerator();
$value             = new \Zend\Code\Generator\PropertyValueGenerator();
$value->setType(\Zend\Code\Generator\ValueGenerator::TYPE_CONSTANT);
$value->setValue('ClassName::SOME_CONSTANT');
$propertyGenerator->setName("foo");
$propertyGenerator->setDefaultValue($value);

$classGenerator->addPropertyFromGenerator($propertyGenerator);

print $classGenerator->generate();

Result:

class 
{

    public $foo = ClassName::SOME_CONSTANT;

}
Ocramius commented 8 years ago

@adamlundrigan neat! Closing here then!