Ubiquity 2.5.2
php rapid development framework
Loading...
Searching...
No Matches
ClassCreator.php
Go to the documentation of this file.
1<?php
2
3
5
6
10
12
13 private string $template;
14 private string $classname;
15 private string $namespace;
16 private string $extendsOrImplements;
17 private string $classContent;
18 private array $classAttributes;
19
20 private function getTemplateDir() {
21 return \dirname(__DIR__) . "/templates/";
22 }
23
25 $this->classname=$classname;
26 $this->namespace=$namespace;
27 $this->extendsOrImplements=$extendsOrImplements;
28 $this->classContent=$classContent;
29 $this->template='class.tpl';
30 $this->classAttributes=[];
31 $this->uses=[];
32 }
33
34 public function generate(): bool {
35 $namespaceVar = '';
36 if (UString::isNotNull($this->namespace)) {
37 $namespaceVar = "namespace {$this->namespace};";
38 }
39 $variables = [
40 '%classname%' => $this->classname,
41 '%namespace%' => $namespaceVar,
42 '%uses%' => $this->getUsesStr(),
43 '%extendsOrImplements%' => $this->extendsOrImplements,
44 '%classContent%' => $this->classContent,
45 '%classAttributes%'=>\implode("\n", $this->classAttributes)
46 ];
47 $templateDir = $this->getTemplateDir();
48 $directory = UFileSystem::getDirFromNamespace($this->namespace);
49 UFileSystem::safeMkdir($directory);
50 $filename = UFileSystem::cleanFilePathname($directory . \DS . $this->classname . '.php');
51 if (! \file_exists($filename)) {
52 UFileSystem::openReplaceWriteFromTemplateFile($templateDir . $this->template, $filename, $variables);
53 return true;
54 }
55 return false;
56 }
57
61 public function getTemplate(): string {
62 return $this->template;
63 }
64
68 public function setTemplate(string $template): void {
69 $this->template = $template;
70 }
71
72 public function addClassAttribute($attribute) {
73 $this->classAttributes[]=$attribute;
74 }
75}
__construct($classname, $namespace, $extendsOrImplements='', $classContent='')
File system utilities Ubiquity\utils\base$UFileSystem This class is part of Ubiquity.
String utilities.
Definition UString.php:15