nette / php-generator

🐘 Generates neat PHP code for you. Supports new PHP 8.3 features.
https://doc.nette.org/php-generator
Other
2.11k stars 138 forks source link

Support for namespaces #6

Closed jakubkulhan closed 9 years ago

jakubkulhan commented 10 years ago
dg commented 10 years ago

php-generator is set of totally low level classes. So I think that generating of "use" and "namespace" statements should not be part of ClassType, but it is task for new class.

jakubkulhan commented 10 years ago

I think this is still pretty low-level. "use" statements might not, but "namespace" statement is necessarily a part of class declaration (class Foo\Bar\Baz and Nette\Bar\Baz are totally different classes).

(btw ClassType::from() worked with namespaces before - https://github.com/jakubkulhan/php-generator/commit/1bdc05b1472ac395deb297a1d96542acc0af00e3#diff-553ff5a7ad6591c63da96be5cd103826L95)

I'd really like to use nette/php-generator, because I don't want to write this logic over and over again. However, without proper namespace support it's unusable for me at the moment.

So I would create Nette\PhpGenerator\ClassFile class that would extend ClassType and handled statements the proper way, ok?

dg commented 10 years ago

:+1: for namespace support, but it must be done well ;)

ClassFile is solution, but one file can contain more than one class.

jakubkulhan commented 10 years ago

Yes, one file can container several classes. But is it something, one would want to generate? I think not.

There could be class PhpFile, that would contain more ClassTypes. ClassType would be low-level, PhpFile would handle namespace and use statements. Would that be a better design?

JanTvrdik commented 10 years ago

But is it something, one would want to generate?

Yes, Nette generates multiple classes to a single file when you use generated factories.

jakubkulhan commented 10 years ago

Yes, Nette generates multiple classes to a single file when you use generated factories.

Ok then. Adding PhpFile atop ClassType seems the best solution then.

fprochazka commented 10 years ago

Maybe something like PhpFile -> Namespace[] -> Class[] might be ok?

dg commented 10 years ago

With user-defined use-statements is better Class[] array of [namespace, uses, Class], because namespace is scope.

(Btw user-defined use-statements makes it much more complicated, the question is whether it is needed for generated code.)

fprochazka commented 10 years ago

@dg the question is... does this have to be used only for "don't ever touch this by hand" generated code?

fprochazka commented 10 years ago

@dg you're right, the namespaces can be autodetected and only adds complexity, the use imports can be all done automatically by the PhpFile class.

jakubkulhan commented 10 years ago

@dg As @fprochazka pointed out, more classes can be inside namespace. Class declaration inside PHP files is position-independent and use statements are really relative only to current namespace, not a class. I find using object better than array of arrays.

So I created PhpFileFragment that manages namespace statement, use statements and one or more classes. PhpFile only manages fragments and delegates class/interface/trait creation to them.

dg commented 10 years ago

Maybe PhpNamespace is better than PhpFileFragment.

jakubkulhan commented 10 years ago

PhpNamespace seems more logical. Should I squash into a single commit?

dg commented 10 years ago

I have partially merged it 62149a7b4bba2a1a43530ae7cb7d9cc96830ef9f 1287ec7bbc646acdaabf94d02bd412832e60e264, with some coding style and minor logic modifications and (yet) without "auto-using" in ClassType https://github.com/dg/nette-php-generator/commit/a600d4a17e9c81ab2813ff4eba17c7893edc3500 (I am not sure it is good idea).

jakubkulhan commented 10 years ago

Great!

dg commented 10 years ago

why not auto-using: this will lead to exception

jakubkulhan commented 10 years ago

Good point, this would be weird behavior. It is better when uses are more explicit.