Symfony MakerBundle 中的定制制造商

Custom maker in Symfony MakerBundle

Symfony MakerBundle 文档中有一个关于创建自己的制造商的词。它说:

create a class that extends AbstractMaker in your src/Maker/ directory. And this is really it!

那边慢点。 这个新制造商的定制 template/skeleton 怎么样?我签入了 Symfony\Bundle\MakerBundle\Generator class 并搜索了这样的模板:

$templatePath = __DIR__.'/Resources/skeleton/'.$templateName;

所以当 运行 自定义制造商(命令)时,路径仍然设置为 Symfony MakerBundle 资源。我错过了什么吗?没有从自定义模板生成文件的干净方法?

好吧,如果我查看 Symfony\Bundle\MakerBundle\Generator 中的代码,我会看到:

 private function addOperation(string $targetPath, string $templateName, array $variables)
    {
        if ($this->fileManager->fileExists($targetPath)) {
            throw new RuntimeCommandException(sprintf('The file "%s" can\'t be generated because it already exists.', $this->fileManager->relativizePath($targetPath)));
        }

        $variables['relative_path'] = $this->fileManager->relativizePath($targetPath);

        $templatePath = $templateName;
        if (!file_exists($templatePath)) {
            $templatePath = __DIR__.'/Resources/skeleton/'.$templateName;

            if (!file_exists($templatePath)) {
                throw new \Exception(sprintf('Cannot find template "%s"', $templateName));
            }
        }

        $this->pendingOperations[$targetPath] = [
            'template' => $templatePath,
            'variables' => $variables,
        ];
    }

所以基本上,它首先检查文件是否存在,如果不存在,它会将模板的路径更改为 __DIR__.'/Resources/skeleton/'.$templateName;,如果该文件也不存在,则抛出异常。

所以基本上,如果您提供存在于 $templateName 参数的文件路径,您可以决定从何处加载模板。