Ubiquity 2.5.2
php rapid development framework
Loading...
Searching...
No Matches
CmdTrait.php
Go to the documentation of this file.
1<?php
3
10
11class CmdTrait {
12
13 protected static function parseArguments() {
14 global $argv;
15 $argv_copy = $argv;
16 \array_shift($argv_copy);
17 $out = array();
18 foreach ($argv_copy as $arg) {
19 if (\substr($arg, 0, 2) == '--') {
20 \preg_match("/\=|\:|\ /", $arg, $matches, PREG_OFFSET_CAPTURE);
21 $eqPos = $matches[0][1];
22 if ($eqPos === false) {
23 $key = \substr($arg, 2);
24 $out[$key] = isset($out[$key]) ? $out[$key] : true;
25 } else {
26 $key = \substr($arg, 2, $eqPos - 2);
27 $out[$key] = \substr($arg, $eqPos + 1);
28 }
29 } else if (\substr($arg, 0, 1) == '-') {
30 if (\substr($arg, 2, 1) == '=' || \substr($arg, 2, 1) == ':' || \substr($arg, 2, 1) == ' ') {
31 $key = \substr($arg, 1, 1);
32 $out[$key] = \substr($arg, 3);
33 } else {
34 $chars = \str_split(\substr($arg, 1));
35 foreach ($chars as $char) {
36 $key = $char;
37 $out[$key] = isset($out[$key]) ? $out[$key] : true;
38 }
39 }
40 } else {
41 $out[] = $arg;
42 }
43 }
44 return $out;
45 }
46
47 protected static function getOption($options, $option, $longOption, $default = NULL) {
48 if (\array_key_exists($option, $options)) {
49 $option = $options[$option];
50 } else if (\array_key_exists($longOption, $options)) {
51 $option = $options[$longOption];
52 } else if (isset($default) === true) {
53 $option = $default;
54 } else
55 $option = "";
56 return $option;
57 }
58
59 protected static function getOptionArray($options, $option, $longOption, $default = NULL) {
60 $option = self::getOption($options, $option, $longOption, $default);
61 if (\is_string($option)) {
62 return \explode(',', $option);
63 }
64 return $option;
65 }
66
67 protected static function getBooleanOption($options, $option, $longOption, $default = NULL) {
68 if (\array_key_exists($option, $options)) {
69 $option = $options[$option];
70 } else if (\array_key_exists($longOption, $options)) {
71 $option = $options[$longOption];
72 } else if (isset($default) === true) {
73 $option = $default;
74 } else {
75 $option = false;
76 }
77 if (\filter_var($option, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE) === false) {
78 $option = false;
79 } else {
80 $option = true;
81 }
82 return $option;
83 }
84
85 protected static function hasOption($options, $option, $longOption): bool {
86 return isset($options[$option]) || isset($options[$longOption]);
87 }
88
89 protected static function requiredParam($what, $paramName) {
90 if ($what == null) {
91 ConsoleFormatter::showMessage($paramName . ' is a required parameter', 'error');
92 $answer = Console::question("Enter a value for " . $paramName . ':');
93 if ($answer == null) {
94 exit(1);
95 } else {
96 return $answer;
97 }
98 }
99 return $what;
100 }
101
102 protected static function answerModel($options, $option, $longOption, $part, $config) {
103 $resource = self::getOption($options, $option, $longOption, null);
104 if ($resource == null) {
105 echo ConsoleFormatter::showMessage($longOption . ' is a required parameter! You must add <b>-' . $option . '</b> option.', 'error', $part);
106 $models = CacheManager::getModels($config, true);
107 $answer = Console::question("Enter the " . $longOption . " to add from the following:\n", $models);
108 if (array_search($answer, $models) !== false) {
109 $resource = $answer;
110 } else {
111 echo ConsoleFormatter::showInfo("Cancelled operation.");
112 exit(1);
113 }
114 }
115 return self::getCompleteClassname($config, $resource);
116 }
117
118 protected static function getCompleteClassname($config, $classname, $type = 'models') {
119 $prefix = Startup::getNS($type);
120 $classname = \ltrim($classname, "\\");
121 if (! UString::startswith($classname, $prefix)) {
122 $classname = $prefix . $classname;
123 }
124 return \str_replace("\\\\", "\\", $classname);
125 }
126
127 protected static function getSelectedModels($models, $config) {
128 if ($models != null) {
129 $result = [];
130 $models = \explode(",", $models);
131 foreach ($models as $model) {
132 $model = self::getCompleteClassname($config, $model);
133 if (\class_exists($model)) {
134 $result[] = $model;
135 }
136 }
137 return $result;
138 }
139 return null;
140 }
141
142 protected static function updateDomain($options) {
143 $domain = self::getOption($options, 'o', 'domain', '');
144 if ($domain != '') {
145 $domains = DDDManager::getDomains();
146 if (\array_search($domain, $domains) !== false) {
147 DDDManager::setDomain($domain);
148 } else {
149 throw new \Ubiquity\exceptions\UbiquityException("Domain `$domain` doesn't exist!");
150 }
151 }
152 return $domain;
153 }
154}
155
Manager for caches (Router, Rest, models).
Starts the framework.
Definition Startup.php:19
Ubiquity\devtools\cmd$ConsoleFormatter This class is part of Ubiquity.
static showMessage($content, $type='info', $title=null)
Return a formated message.
static showInfo($content, $dColor=self::CYAN)
Ubiquity\devtools\cmd$Console This class is part of Ubiquity.
Definition Console.php:12
static question($prompt, array $propositions=null, array $options=[])
Ask the user a question and return the answer.
Definition Console.php:30
static hasOption($options, $option, $longOption)
Definition CmdTrait.php:85
static getCompleteClassname($config, $classname, $type='models')
Definition CmdTrait.php:118
static getOption($options, $option, $longOption, $default=NULL)
Definition CmdTrait.php:47
static getSelectedModels($models, $config)
Definition CmdTrait.php:127
static getBooleanOption($options, $option, $longOption, $default=NULL)
Definition CmdTrait.php:67
static getOptionArray($options, $option, $longOption, $default=NULL)
Definition CmdTrait.php:59
static answerModel($options, $option, $longOption, $part, $config)
Definition CmdTrait.php:102
static requiredParam($what, $paramName)
Definition CmdTrait.php:89
Manager for a Domain Driven Design approach.
static setDomain(string $domain)
Sets the active domain.
static getDomains()
Returns an array of existing domains.
String utilities.
Definition UString.php:15
static startswith($hay, $needle)
Definition UString.php:17