Skip to content

Commit d48b696

Browse files
committed
chore: Updating and tests
1 parent bedbca0 commit d48b696

6 files changed

Lines changed: 83 additions & 61 deletions

File tree

src/CLI/Adapter.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,36 +13,40 @@ public function __construct(int $workerNum = 0)
1313

1414
/**
1515
* Starts the Server.
16+
*
1617
* @param $callback
1718
* @return self
1819
*/
1920
abstract public function start($callback): self;
2021

2122
/**
2223
* Stops the Server.
24+
*
2325
* @return self
2426
*/
2527
abstract public function stop(): self;
2628

2729
/**
2830
* Is called when a Worker starts.
29-
* @param callable $callback
31+
*
32+
* @param callable $callback
3033
* @return self
3134
*/
3235
abstract public function onWorkerStart(callable $callback): self;
3336

3437
/**
3538
* Is called when a Worker stops.
36-
* @param callable $callback
39+
*
40+
* @param callable $callback
3741
* @return self
3842
*/
3943
abstract public function onWorkerStop(callable $callback): self;
4044

4145
/**
4246
* Is called when a job is processed.
43-
* @param callable $callback
47+
*
48+
* @param callable $callback
4449
* @return self
4550
*/
4651
abstract public function onJob(callable $callback): self;
47-
4852
}

src/CLI/Adapters/Generic.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,10 @@
22

33
namespace Utopia\CLI\Adapters;
44

5-
use Swoole\Process\Pool;
6-
use Swoole\Runtime;
75
use Utopia\CLI\Adapter;
86

97
class Generic extends Adapter
108
{
11-
129
public function __construct(int $workerNum = 0)
1310
{
1411
parent::__construct($workerNum);
@@ -39,6 +36,7 @@ public function onWorkerStop(callable $callback): self
3936
public function onJob(callable $callback): self
4037
{
4138
call_user_func($callback);
39+
4240
return $this;
4341
}
4442
}

src/CLI/Adapters/Swoole.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,16 @@ public function start($callback): self
2323
$this->pool->set(['enable_coroutine' => true]);
2424

2525
$this->onWorkerStart($callback);
26-
$this->onWorkerStop(fn() => $this->pool->shutdown());
26+
$this->onWorkerStop(fn () => $this->pool->shutdown());
2727
$this->pool->start();
28+
2829
return $this;
2930
}
3031

3132
public function stop(): self
3233
{
3334
$this->pool->shutdown();
35+
3436
return $this;
3537
}
3638

src/CLI/CLI.php

Lines changed: 34 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
namespace Utopia\CLI;
44

55
use Exception;
6-
use Utopia\CLI\Adapters\Generic;
76
use Utopia\DI\Container;
87
use Utopia\DI\Dependency;
98
use 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

Comments
 (0)