Skip to content

Commit

Permalink
Fixed request replacement and added get helpers
Browse files Browse the repository at this point in the history
Signed-off-by: Tom Wright <[email protected]>
  • Loading branch information
betterthanclay committed Oct 5, 2023
1 parent 6157266 commit aeccb52
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 1 deletion.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
* Fixed Request replacement
* Added argument get helpers

## v0.10.0 (2023-10-05)
* Restructured command / argument interface

Expand Down
51 changes: 50 additions & 1 deletion src/Terminus/Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,17 @@ public static function loadAsVeneerPlugin(object $instance): static
* Init with Request
*/
public function __construct(Request $request)
{
$this->setRequest($request);
}


/**
* Set request - must be done early in process
*
* @return $this
*/
public function setRequest(Request $request): static
{
$this->request = $request;

Expand All @@ -59,8 +70,10 @@ public function __construct(Request $request)
}

$name = pathinfo((string)$name, \PATHINFO_FILENAME);
$this->setName($name);
$this->values = null;

parent::__construct($name);
return $this;
}

/**
Expand Down Expand Up @@ -149,6 +162,42 @@ public function get(string $name): bool|string|array|null
return $this->values[$name] ?? null;
}

/**
* Get argument as string
*/
public function getString(string $name): string
{
return Coercion::forceString($this->get($name));
}

/**
* Get argument as bool
*/
public function getBool(string $name): bool
{
return Coercion::toBool($this->get($name));
}

/**
* Get argument as list
*
* @return array<string|bool>
*/
public function getList(string $name): array
{
$value = $this->get($name);

if ($value === null) {
return [];
}

if (is_array($value)) {
return $value;
}

return [$value];
}

/**
* Has argument
*/
Expand Down
16 changes: 16 additions & 0 deletions src/Terminus/Context.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,22 @@ public function newSession(
);
}

/**
* Set request - must be done early in process
*
* @return $this
*/
public function setRequest(Request $request): static
{
$this->session?->setRequest($request);

if (isset($this->command)) {
$this->command->setRequest($request);
}

return $this;
}

/**
* Create request from environment
*
Expand Down
3 changes: 3 additions & 0 deletions stubs/DecodeLabs/Terminus.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ public static function getSession(): Ref1 {
public static function newSession(?Ref2 $request = NULL, ?Ref3 $broker = NULL): Ref1 {
return static::$instance->newSession(...func_get_args());
}
public static function setRequest(Ref2 $request): Inst {
return static::$instance->setRequest(...func_get_args());
}
public static function newRequest(?array $argv = NULL, ?array $server = NULL): Ref2 {
return static::$instance->newRequest(...func_get_args());
}
Expand Down

0 comments on commit aeccb52

Please sign in to comment.