Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature request: add builders for namespace, use, and traits. #62

Closed
hinikato opened this issue May 23, 2013 · 12 comments
Closed

Feature request: add builders for namespace, use, and traits. #62

hinikato opened this issue May 23, 2013 · 12 comments

Comments

@hinikato
Copy link

I have similar to #61 feature request: It would be nice to add builders for namespace and use statements as well to add a capability to generate a whole PHP file: https://github.com/myak/framework/blob/master/lib/Myak/Code/CodeGenerator.php (see ns() and uses() methods). Probably builder for traits would be also useful.

@hinikato
Copy link
Author

One note: probably uses() should be replaced with _use() and ns() with _namespace() for consistency.

@nikic
Copy link
Owner

nikic commented May 23, 2013

Agree, though I think having separate builder classes for things like uses excessive (it just has the getNode method and nothing else). Maybe in these cases we can directly return the constructed node from the method in the factory? Or would that not be so good design-wise (not all values that are returned will have a getNode() method)?

@Trismegiste What do you think about this?

@Trismegiste
Copy link

I agree it seems excessive to create one builder and to call 2 methods ("ns" & "getNode") instead of chaining 2 "new". Furthermore, I think this kind of factories must be placed at the "file level".

That's why I'm planning to create a builder for PHP files (with some PSR-0/1/2 guidances) and I've put this factories for "namespace" and "use" in that builder. It's currently unfinished and unshaped and I don't believe I must be placed in PHPParser but you can see it in :
https://github.com/Trismegiste/Mondrian/blob/feature_phpfile_builder/Parser/PhpFileBuilder.php#L70

In the end you can make

$fac = new BuilderFactory();
$file = $fac->file('sample.php')
                ->ns('Kitty')
                ->declaring(
                        $fac->class('Soft')
                        ->implement('Warm')
                        ->addStmt(
                                $fac
                                ->method('purr')
                        )
                )->getNode();

@hinikato
Copy link
Author

I agree with both points:

  1. It is excessive, if Nodes can be used directly without intermediates Builders, builders can be not required.
  2. File level Builder is more logically correct in this case, because BuilderFactory is factory of Builders, not Builder itself. So if Builders will not be used for namespace and use nodes, building of Nodes can be done with File level Builder.

@jimmyko
Copy link

jimmyko commented Mar 10, 2014

I am looking for the same features. It seems more logical to separate them from other builders. Any schedule to port this feature?

@hinikato
Copy link
Author

The changes above were implemented here: https://github.com/myak/framework/tree/master/lib/Myak/Code/CodeGen. I would be happy to move of them to the PhpParser, probably with some changes.

@akiessling
Copy link

Any news on this one? I'm looking for the same features, but the repo from hinikato seems to be gone

@nikic
Copy link
Owner

nikic commented Dec 19, 2014

Trait builder has been added: 4387454

@nikic
Copy link
Owner

nikic commented Dec 19, 2014

Namespace builder has also been added: 01643e0

@sh-dude
Copy link

sh-dude commented Mar 10, 2015

Is it a possible to generate the following code using BuilderFactory and PrettyPrinter?

<?php
namespace {
    use Foo\BarTrait;
    use PHPUnit_Framework_TestCase as TestCase;

    class Test
    {
    }

    class MyTest extends TestCase
    {
    }
}

If I try to use namespace with empty name I get LogicException:

$builderFactory = new BuilderFactory();
$builder = $builderFactory->namespace(null);// ! Exception here
    // Add other statements here  
$prettyPrinter->prettyPrintFile([$builder->getNode()]);

If no please consider ability to add support of this case.

@nikic
Copy link
Owner

nikic commented Mar 10, 2015

@sh-dude Done: f9fc2fc

@nikic
Copy link
Owner

nikic commented Mar 10, 2015

"Use" builder added as well in 0c34706, so closing this FR.

@nikic nikic closed this as completed Mar 10, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants