data; // tableau à inséré dans du code latex include('src/latex_templates/' . $this->type . '.php'); $this->latex = $latex; // buffer récupéré par ob_get_clean() // QUESTION: // cette utilisation de include(), c'est du génie ou c'est moche ou les deux? // un nouveau fichier est inséré à chaque appel de makeLatex() // puis la mémoire est libérée à la fin de la fonction // le PHP inséré fait donc partie de cette "mémoire" } public function makeTexAndPdf(): void // document polymorphe { if(get_class($this) !== 'EnveloppeVersoLatex') { File::makeFolder($this->latex_path); File::makeFolder($this->pdf_path); } File::makeFile($this->latex_path, $this->file_name, $this->latex); // fichier .tex $this->latexToPdf(); } // commande système pdflatex private function latexToPdf(): void { // paramètre optionnel $output_dir = ''; if($this->pdf_path !== '') { $output_dir = '-output-directory=' . $this->pdf_path; } // compilation exec('pdflatex ' . $output_dir . ' ' . $this->latex_path . $this->file_name); // nettoyage $basename = basename($this->file_name, '.tex'); unlink($this->pdf_path . $basename . '.aux'); unlink($this->pdf_path . $basename . '.log'); } // getters public function getFileName(): string { return $this->file_name; } public function getLatexPath(): string { return $this->latex_path; } public function getPdfPath(): string { return $this->pdf_path; } public function getLatex(): string { return $this->latex; } // setters //~ public function setFileName(string $file_name) //~ { //~ $this->file_name = $file_name; //~ } //~ public function setLatexPath(string $latex_path) //~ { //~ $this->latex_path = $latex_path; //~ } //~ public function setPdfPath(string $pdf_path) //~ { //~ $this->pdf_path = $pdf_path; //~ } public function setData(Object $Object) { $this->data = array_merge($this->data, $Object->getAll()); // nécessite des tableaux associatifs } } abstract class PrestaLatex extends Latex { protected array $date_array; // [année, mois, jour] public function setData(Object $Object): Latex // polymorphisme { $this->data = array_merge($this->data, $Object->getAll()); // nécessite des tableaux associatifs if(get_class($Object) === 'Prestation') { $this->file_name = $Object->getCodePresta() . '.tex'; $this->date_array = explode('-', $Object->getdate()); $this->latex_path = Config::$latex_path . $this->date_array[0] . DIRECTORY_SEPARATOR; $this->pdf_path = Config::$pdf_path . $this->date_array[0] . DIRECTORY_SEPARATOR; } return $this; // pour chainer les méthodes } // forme = code-presta.tex //~ protected function nameTheFile(string $quoi, string $codePresta) //~ { //~ $this->fileName = $quoi . '-' . $codePresta . '.tex'; //~ } public function makeLatex() { $data = $this->data; $date = preg_replace('#\D#', '/', $this->date_array[2] . '/' . $this->date_array[1] . '/' . $this->date_array[0]); // 17/10/2026, avec des / parce que j'aime bien // on obtient la variable $latex avec ob_get_clean() // le include() ici, c'est du génie ou c'est moche ou les deux? // un nouveau fichier php est inséré à chaque itération include('src/latex_templates/' . $this->type . '.php'); // on retourne le buffer // normallement le code PHP inséré avec include est nettoyé en quittant la fonction $this->latex = $latex; //return($latex); } } class DevisLatex extends PrestaLatex // extends Latex { protected string $type = 'devis'; } class FactureLatex extends PrestaLatex // extends Latex { protected string $type = 'facture'; } class LocationLatex extends PrestaLatex // extends Latex { protected string $type = 'location'; } class EnveloppeRectoLatex extends Latex { protected string $type = 'enveloppe_recto'; public function __construct(){ $this->latex_path = Config::$latex_path . 'enveloppes_recto/'; $this->pdf_path = Config::$pdf_path . 'enveloppes_recto/'; } public function setData(Object $Client) // ici de type Clients { $this->file_name = $Client->getCodeClient() . '.tex'; $this->data = array_merge($this->data, $Client->getAll()); // nécessite des tableaux associatifs $this->spacesInPostCode(); // nécessite $this->data } // code postal avec 2 espaces entre chaque chiffre: 2 \ 9 \ 0 \ 0 \ 0 protected function spacesInPostCode() { $this->data['code_postal_espaces'] = implode(' \ ', str_split($this->data['code_postal'])); } } class EnveloppeVersoLatex extends Latex { protected string $type = 'enveloppe_verso'; protected string $file_name = 'enveloppe_verso.tex'; public function __construct(){ $this->latex_path = Config::$latex_path; $this->pdf_path = Config::$pdf_path; } } abstract class ComptaLatex extends Latex { //~ public function __construct(string $quoi, string $annee, int $numeroMois = 0) //~ { //~ nameTheFile($quoi, $annee, $numeroMois); //~ } // forme = Recettes-2022-06-Juin.tex ou Recettes-2022.tex // type de 'annee'? //~ protected function nameTheFile(string $quoi, string $annee, int $numeroMois = 0) //~ { //~ $this->fileName = $quoi . '-' . $annee; //~ $mois = ['', 'Janvier', 'Février', 'Mars', 'Avril', 'Mai', 'Juin', 'Juillet', 'Août', 'Septembre', 'Octobre', 'Novembre', 'Décembre']; //~ if($numeroMois > 0 && $numeroMois <= 12) //~ { //~ $this->fileName .= '-' . $numeroMois . '-' . $mois[$numeroMois]; //~ } //~ $this->fileName .= '.tex'; //~ } } class LivreRecettesLatex extends ComptaLatex {} class RegistreAchatsLatex extends ComptaLatex {} class BilanLatex extends ComptaLatex {}