Skip to content

function to change the name path of the template Template.tpl.php #562

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

Open
wants to merge 6 commits into
base: 1.x
Choose a base branch
from

Conversation

Jonathanlight
Copy link

No description provided.

@weaverryan
Copy link
Member

Hey @jonathankablan!

Thanks for the PR :). This topic is being discussed in #560. For the record, I'm against making the templates overrideable. Well, at least, making them easily overrideable. The reason is that we don't guarantee backward-compatibility with the templates: we may (in a minor version) change the variables that we pass into a template, which could break a custom template.

That's not to say that I'm against it completely, but it needs to be clearly and "advanced" thing to do, and should possibly be a little difficult.

About your PR, I definitely don't like the templateNameEntity and templateNameRepository. Whatever solution we choose, should be a global solution - not something that affects just one maker command. Your rootTemplateName (which should really be called rootTemplateDirectory) would solve this, as then you have a hook point for any template.

If you're willing to put in some work and push on this, let me know. I can make some suggestions about how we could make this possible, but still "hard enough" that it's for advanced users ;).

Cheers!

@Jonathanlight
Copy link
Author

Thanks for this first reading, I had wanted to have the possibility to change the TemplateName but this is not really essential for the blow. I would like to allow the change of the RootTemplateDirectory in the Generator class simply which won't change anything to the current operation of the Generator class, and discuss the other methods further.

Thanks

* @param string $templateName
* @return string
*/
public function supports(string $templateName): string;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
public function supports(string $templateName): string;
public function supports(string $templateName): bool;

This will return a boolean. The idea is that MakerRenderer will look a bit different than you have. It will look like this:

class MakerRenderer
{
    /**
     * @var MakerTemplateRendererInterface[]
     */
    private $templateRenderers;

    public function __construct(array $templateRenderers)
    {
        $this->templateRenderers = $templateRenderers;
    }

    public function renderTemplate(string $templateName, array $variables)
    {
        foreach ($this->templateRenderers as $templateRenderer) {
            if ($templateRenderer->supports($templateName)) {
                return $templateRenderer->render($templateName, $variables);
            }
        }
        
        throw new \Exception(sprintf('No template renderers for template "%s"', $templateName);
    }
}

There would be ONE class in MakerBundle that implements MakerTemplateRendererInterface (and users could add more by using a dependency injection tag). It would probably look like this:

class MakerDefaultTemplateRenderer implements MakerTemplateRendererInterface
{
    public function supports(string $templateName): string
    {
        $templatePath = __DIR__.'/Resources/skeleton/'.$templateName;

        return file_exists($templatePath);
    }

    public function render(string $templateName, array $variables)
    {
        $templatePath = __DIR__.'/Resources/skeleton/'.$templateName;

        // this is basically the current logic for rendering templates
        $contents = $this->fileManager->parseTemplate($templatePath, $variables);
        $this->fileManager->dumpFile($templatePath, $contents);
    }
}

* @param string $templateName
* @return string
*/
public function render(string $templateName = null): string;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
public function render(string $templateName = null): string;
public function render(string $templateName, array $variables): string;

@@ -64,6 +77,11 @@ public function generateClass(string $className, string $templateName, array $va

/**
* Generate a normal file from a template.
*
* @param string $targetPath
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This information does not add value because we already have the type in the signature. You should delete them.
BTW, PHP-CS-Fixer should remove it 🤔

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

okay Max

@jrushlow jrushlow added the Status: Needs Work Additional work is needed label Nov 9, 2020
@weaverryan weaverryan changed the base branch from master to main November 16, 2020 20:01
@jrushlow jrushlow added the Feature New Feature label May 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Feature New Feature Status: Needs Work Additional work is needed
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants