33namespace Utopia \CLI ;
44
55use Exception ;
6- use Utopia \CLI \Adapters \Generic ;
76use Utopia \DI \Container ;
87use Utopia \DI \Dependency ;
98use Utopia \Http \Hook ;
@@ -82,8 +81,8 @@ class CLI
8281 /**
8382 * CLI constructor.
8483 *
85- * @param Adapter $adapter
86- * @param array $args
84+ * @param Adapter $adapter
85+ * @param array $args
8786 *
8887 * @throws Exception
8988 */
@@ -93,7 +92,7 @@ public function __construct(Adapter $adapter, array $args = [])
9392 throw new Exception ('CLI tasks can only work from the command line ' );
9493 }
9594
96- $ this ->args = $ this ->parse ((!empty ($ args ) || !isset ($ _SERVER ['argv ' ])) ? $ args : $ _SERVER ['argv ' ]);
95+ $ this ->args = $ this ->parse ((! empty ($ args ) || ! isset ($ _SERVER ['argv ' ])) ? $ args : $ _SERVER ['argv ' ]);
9796
9897 @\cli_set_process_title ($ this ->command );
9998
@@ -151,7 +150,7 @@ public function error(): Hook
151150 *
152151 * Add a new command task
153152 *
154- * @param string $name
153+ * @param string $name
155154 * @return Task
156155 */
157156 public function task (string $ name ): Task
@@ -166,15 +165,15 @@ public function task(string $name): Task
166165 /**
167166 * If a resource has been created return it, otherwise create it and then return it
168167 *
169- * @param string $name
168+ * @param string $name
170169 * @return mixed
171170 *
172171 * @throws Exception
173172 */
174173 public function getResource (string $ name ): mixed
175174 {
176- if (!$ this ->container ->has ($ name )) {
177- throw new Exception ('Failed to find resource: " ' . $ name . '" ' );
175+ if (! $ this ->container ->has ($ name )) {
176+ throw new Exception ('Failed to find resource: " ' . $ name. '" ' );
178177 }
179178
180179 return $ this ->container ->get ($ name );
@@ -183,8 +182,9 @@ public function getResource(string $name): mixed
183182 /**
184183 * Get Resources By List
185184 *
186- * @param array $list
185+ * @param array $list
187186 * @return array
187+ *
188188 * @throws Exception
189189 */
190190 public function getResources (array $ list ): array
@@ -201,7 +201,7 @@ public function getResources(array $list): array
201201 /**
202202 * Set a new resource callback
203203 *
204- * @param Dependency $dependency
204+ * @param Dependency $dependency
205205 * @return void
206206 *
207207 * @throws Exception
@@ -214,7 +214,7 @@ public function setResource(Dependency $dependency): void
214214 /**
215215 * task-name --foo=test
216216 *
217- * @param array $args
217+ * @param array $args
218218 * @return array
219219 *
220220 * @throws Exception
@@ -277,8 +277,9 @@ public function match(): ?Task
277277 * Get Params
278278 * Get runtime params for the provided Hook
279279 *
280- * @param Hook $hook
280+ * @param Hook $hook
281281 * @return array
282+ *
282283 * @throws Exception
283284 */
284285 protected function getParams (Hook $ hook ): array
@@ -290,18 +291,15 @@ protected function getParams(Hook $hook): array
290291
291292 $ this ->validate ($ key , $ param , $ value );
292293
293- $ key = str_replace ('- ' ,'_ ' ,$ key );
294- $ camelCase = \lcfirst (\str_replace ('_ ' , '' , \ucwords ($ key , '_ ' )));
295-
296- $ params [$ camelCase ] = $ value ;
294+ $ params [$ this ->camelCaseIt ($ key )] = $ value ;
297295 }
298296
299297 foreach ($ hook ->getDependencies () as $ dependency ) {
300- if (array_key_exists ($ dependency , $ params )){
298+ if (array_key_exists ($ this -> camelCaseIt ( $ dependency) , $ params )) {
301299 continue ;
302300 }
303301
304- $ params [] = $ this ->getResource ($ dependency );
302+ $ params [$ this -> camelCaseIt ( $ dependency ) ] = $ this ->getResource ($ dependency );
305303 }
306304
307305 return $ params ;
@@ -311,6 +309,7 @@ protected function getParams(Hook $hook): array
311309 * Run
312310 *
313311 * @return $this
312+ *
314313 * @throws Exception
315314 */
316315 public function run (): self
@@ -336,7 +335,7 @@ public function run(): self
336335 } catch (Exception $ e ) {
337336 foreach ($ this ->errors as $ hook ) {
338337 $ error = new Dependency ();
339- $ error ->setName ('error ' )->setCallback (fn () => $ e );
338+ $ error ->setName ('error ' )->setCallback (fn () => $ e );
340339
341340 $ this ->setResource ($ error );
342341 \call_user_func_array ($ hook ->getAction (), $ this ->getParams ($ hook ));
@@ -372,9 +371,9 @@ public function getArgs(): array
372371 *
373372 * Creates an validator instance and validate given value with given rules.
374373 *
375- * @param string $key
376- * @param array $param
377- * @param mixed $value
374+ * @param string $key
375+ * @param array $param
376+ * @param mixed $value
378377 *
379378 * @throws Exception
380379 */
@@ -389,16 +388,16 @@ protected function validate(string $key, array $param, $value): void
389388 }
390389
391390 // is the validator object an instance of the Validator class
392- if (!$ validator instanceof Validator) {
391+ if (! $ validator instanceof Validator) {
393392 throw new Exception ('Validator object is not an instance of the Validator class ' , 500 );
394393 }
395394
396- if (!$ validator ->isValid ($ value )) {
397- throw new Exception ('Invalid ' . $ key . ': ' . $ validator ->getDescription (), 400 );
395+ if (! $ validator ->isValid ($ value )) {
396+ throw new Exception ('Invalid ' . $ key. ': ' . $ validator ->getDescription (), 400 );
398397 }
399398 } else {
400- if (!$ param ['optional ' ]) {
401- throw new Exception ('Param " ' . $ key . '" is not optional. ' , 400 );
399+ if (! $ param ['optional ' ]) {
400+ throw new Exception ('Param " ' . $ key. '" is not optional. ' , 400 );
402401 }
403402 }
404403 }
@@ -414,4 +413,12 @@ public function reset(): void
414413 {
415414 $ this ->container = new Container ();
416415 }
416+
417+ private function camelCaseIt ($ key ): string
418+ {
419+ $ key = str_replace ('- ' , '_ ' , $ key );
420+ $ camelCase = \lcfirst (\str_replace ('_ ' , '' , \ucwords ($ key , '_ ' )));
421+
422+ return $ camelCase ;
423+ }
417424}
0 commit comments