Ubiquity 2.5.2
php rapid development framework
Loading...
Searching...
No Matches
ScaffoldCommand.php
Go to the documentation of this file.
1<?php
3
5use Ubiquity\utils\base\UArray;
7
18
19 protected $name;
20
21 protected $description;
22
23 protected $value;
24
25 protected $aliases;
26
27 protected $parameters;
28
29 protected $uses = [];
30
31 public function __construct($name, $value, $description, $aliases = "", $parameters = "") {
32 $this->name = $name;
33 $this->value = $value;
34 $this->description = $description;
35 $this->aliases = $aliases;
36 $this->parameters = $parameters;
37 }
38
39 protected function createParameters() {
40 $result = [];
41 $parameters = \explode(',', $this->parameters);
42 foreach ($parameters as $param) {
43 if ($param != null) {
44 $result[] = "'{$param}' => Parameter::create('{$param}LongName', 'The {$param} description.', [])";
45 }
46 }
47 if (\count($result) > 0) {
48 $this->uses[] = 'Ubiquity\\devtools\\cmd\\Parameter';
49 }
50 return '[' . \implode(",\n", $result) . ']';
51 }
52
53 protected function addUses() {
54 $result = [];
55 foreach ($this->uses as $use) {
56 $use = \rtrim($use, '\\');
57 $result[] = "use {$use};";
58 }
59 return \implode("\n", $result);
60 }
61
62 public function createNamespace($directory) {
63 if ($directory != null) {
64 return 'namespace ' . \trim(\str_replace(\DS, '\\', $directory), '\\') . ';';
65 }
66 return '';
67 }
68
69 public function getTemplateDir() {
70 return \dirname(__DIR__) . "/scaffolding/templates/";
71 }
72
73 public function create($pattern, &$cmdPath) {
74 @list ($directory, $ext) = \explode('*', $pattern);
75 $directory ??= 'commands';
76 $ext ??= '.cmd.php';
77 $template = 'createCommand.tpl';
78 $classname = \ucfirst($this->name);
79 $filename = $classname . $ext;
80 $aliases = \array_filter(\explode(',', $this->aliases));
81 $variables = [
82 '%name%' => $this->name,
83 '%classname%' => $classname,
84 '%aliases%' => UArray::asPhpArray($aliases, 'array'),
85 '%parameters%' => $this->createParameters(),
86 '%value%' => $this->value,
87 '%description%' => $this->description == null ? ($this->name . ' description.') : \addslashes($this->description),
88 '%uses%' => $this->addUses(),
89 '%namespace%' => $this->createNamespace($directory)
90 ];
91 $path = \ROOT . \DS . '..' . \DS . $directory;
93 $cmdPath = realpath($path) . \DS . $filename;
94 return UFileSystem::openReplaceWriteFromTemplateFile($this->getTemplateDir() . $template, $cmdPath, $variables);
95 }
96}
97
Create a new command Ubiquity\scaffolding$ScaffoldCommand This class is part of Ubiquity.
__construct($name, $value, $description, $aliases="", $parameters="")
File system utilities Ubiquity\utils\base$UFileSystem This class is part of Ubiquity.