Closed jakubkulhan closed 9 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.
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?
:+1: for namespace support, but it must be done well ;)
ClassFile is solution, but one file can contain more than one class.
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?
But is it something, one would want to generate?
Yes, Nette generates multiple classes to a single file when you use generated factories.
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.
Maybe something like PhpFile -> Namespace[] -> Class[]
might be ok?
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.)
@dg the question is... does this have to be used only for "don't ever touch this by hand" generated code?
@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.
@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.
Maybe PhpNamespace is better than PhpFileFragment.
PhpNamespace seems more logical. Should I squash into a single commit?
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).
Great!
why not auto-using: this will lead to exception
Good point, this would be weird behavior. It is better when use
s are more explicit.