================= .. raw:: html
<br />
Note
This part is accessible from the webtools, so if you created your project with the -a option or with the create-project command..
From the webtools, activate the commands part,
or go directly to http://127.0.0.1:8090/Admin/commands
.
Activate the Commands tab to get the list of existing devtools commands.
It is possible to get help on a command (which produces a result equivalent to Ubiquity help cmdName
).
Clicking on the run button of a command displays a form to enter the parameters (or executes it directly if it takes none).
After entering the parameters, the execution produces a result.
Return to My commands tab: It is possible to save a sequence of commands (with stored parameters), and then execute the same sequence:
Click on the add command suite
Add the desired commands and modify the parameters:
The validation generates the suite:
Clicking on the run button of the suite executes the list of commands it contains:
Click on the Create devtools command button.
Enter the characteristics of the new command:
- The command name
- The command value: name of the main argument
- The command parameters: In case of multiple parameters, use comma as separator
- The command description
- The command aliases: In case of multiple aliases, use comma as separator
Note
Custom commands are created in the app/commands folder of the project.
The generated class:
namespace commands;
use Ubiquity\devtools\cmd\commands\AbstractCustomCommand;
use Ubiquity\devtools\cmd\ConsoleFormatter;
use Ubiquity\devtools\cmd\Parameter;
class CreateArray extends AbstractCustomCommand {
protected function getValue(): string {
return 'jsonValue';
}
protected function getAliases(): array {
return array("createarray","arrayFromJson");
}
protected function getName(): string {
return 'createArray';
}
protected function getParameters(): array {
return ['f' => Parameter::create('fLongName', 'The f description.', [])];
}
protected function getExamples(): array {
return ['Sample use of createArray'=>'Ubiquity createArray jsonValue'];
}
protected function getDescription(): string {
return 'Creates an array from JSON and save to file';
}
public function run($config, $options, $what, ...$otherArgs) {
//TODO implement command behavior
echo ConsoleFormatter::showInfo('Run createArray command');
}
}
The CreateArray command implemented:
namespace commands;
use Ubiquity\devtools\cmd\commands\AbstractCustomCommand;
use Ubiquity\devtools\cmd\ConsoleFormatter;
use Ubiquity\devtools\cmd\Parameter;
use Ubiquity\utils\base\UFileSystem;
class CreateArray extends AbstractCustomCommand {
protected function getValue(): string {
return 'jsonValue';
}
protected function getAliases(): array {
return array(
"createarray",
"arrayFromJson"
);
}
protected function getName(): string {
return 'createArray';
}
protected function getParameters(): array {
return [
'f' => Parameter::create('filename', 'The filename to create.', [])
];
}
protected function getExamples(): array {
return [
'Save an array in test.php' => "Ubiquity createArray \"{\\\"created\\\":true}\" -f=test.php"
];
}
protected function getDescription(): string {
return 'Creates an array from JSON and save to file';
}
public function run($config, $options, $what, ...$otherArgs) {
echo ConsoleFormatter::showInfo('Run createArray command');
$array = \json_decode($what, true);
$error = \json_last_error();
if ($error != 0) {
echo ConsoleFormatter::showMessage(\json_last_error_msg(), 'error');
} else {
$filename = self::getOption($options, 'f', 'filename');
if ($filename != null) {
UFileSystem::save($filename, "<?php\nreturn " . var_export($array, true) . ";\n");
echo ConsoleFormatter::showMessage("$filename succefully created!", 'success', 'CreateArray');
} else {
echo ConsoleFormatter::showMessage("Filename must have a value!", 'error');
}
}
}
}
The new command is accessible from the devtools, as long as it is in the project:
Ubiquity help createArray
Ubiquity createArray "{\"b\":true,\"i\":5,\"s\":\"string\"}" -f=test.php