from Outils.Directory import GenerateDirectory
from pathlib import Path

def createFileTwigExtension(nom, specifique = ''):
    
    generateDirectory = GenerateDirectory(nom, "/Modules/"+nom+"/TwigExtension", specifique)
    directory = generateDirectory.getDirectory()
    namespace = generateDirectory.getNamespace()
    nom = generateDirectory.getNom(True)

    Path(directory).mkdir(parents=True, exist_ok=True)

    with open(directory+"/Outil"+nom+"Extension.php","x", encoding='utf-8') as file:
        file.write("<?php\n\n")
        file.write("namespace "+namespace+";\n\n")
        file.write("use Psr\Container\ContainerInterface;\n\n")
        file.write("class Outil"+nom+"Extension extends \Twig\Extension\AbstractExtension\n")
        file.write("{\n")
        file.write("    public function __construct(ContainerInterface $contenaire)\n")
        file.write("    {\n")
        file.write("    }\n\n")
        file.write("    public function getFilters()\n")
        file.write("    {\n")
        file.write("        return [\n")
        file.write("            'myFilter"+nom+"' => new \Twig\TwigFilter('myFilter"+nom+"', [$this, 'myFilter"+nom+"'])\n")
        file.write("        ];\n")
        file.write("    }\n\n")
        file.write("    public function getFunctions()\n")
        file.write("    {\n")
        file.write("        return [\n")
        file.write("            new \Twig\TwigFunction('myFonction"+nom+"', [\n")
        file.write("                $this,\n")
        file.write("                'myFonction"+nom+"'\n")
        file.write("            ], [\n")
        file.write("                'is_safe' => [\n")
        file.write("                    'html'\n")
        file.write("                ]\n")
        file.write("            ])\n")
        file.write("        ];\n")
        file.write("    }\n\n")
        file.write("    public function myFilter"+nom+"($input)\n")
        file.write("    {\n")
        file.write("        return true;\n")
        file.write("    }\n\n")
        file.write("    public function myFonction"+nom+"($input)\n")
        file.write("    {\n")
        file.write("        return 'ok';\n")
        file.write("    }\n")
        file.write("}\n")
        file.close()
