-
Notifications
You must be signed in to change notification settings - Fork 78
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Cawllec/console request #398
Conversation
src/Request/ConsoleRequest.php
Outdated
/** | ||
* Create a new console request instance. | ||
* | ||
* @param array $commandArray An array of the command line input |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Alignment bug. :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this something StyleCI could lint for?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not yet, but it is planned. I think it's actually been planned for a very long time, since I think you mentioned this feature last year too. :P
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Haha, explains the deja vu I felt when I typed that :p
src/Request/ConsoleRequest.php
Outdated
/** | ||
* The unformated console command. | ||
* | ||
* @var array |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
string[]
I think is the type here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
They're allegedly the same, but string[]
is more explicit to the expected array elements
src/Request/ConsoleRequest.php
Outdated
* | ||
* @return void | ||
*/ | ||
public function __construct(array $commandArray) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Conventional to match the internal property name, so name the param $command
?
src/Request/BasicResolver.php
Outdated
@@ -12,7 +12,11 @@ class BasicResolver implements ResolverInterface | |||
public function resolve() | |||
{ | |||
if (!isset($_SERVER['REQUEST_METHOD'])) { | |||
return new NullRequest(); | |||
if ((PHP_SAPI == 'cli') && (isset($_SERVER['argv']))) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The extra brackets are not needed, and ===
should be used for the string comparison.
src/Request/ConsoleRequest.php
Outdated
*/ | ||
public function getContext() | ||
{ | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
return implode(' ', $this->command)
perhaps would be a good context, or perhaps, limit this to only the first upto 2 parts of the array?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The first two parts would be good, running this with artisan
commands as well as plain php
produces useful results, moreso than the default which is the top line of the stacktrace.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This code will return at most the first 2 parts of the command, concatenated together with a space. If the array is smaller than 2 elements, it is tolerant to that, and will just return the singleton array, or the empty array.
return implode(' ', array_slice($this->command, 0, 2));
Reviewed this in some detail. :) |
src/Request/ConsoleRequest.php
Outdated
*/ | ||
public function isRequest() | ||
{ | ||
return true; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This probably has to be false
, otherwise, this pull request would be considered a breaking change. Previously, "request" meant "HTTP request", not just "invocation context", or whatever name we wanna give to this. :P
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree, to avoid potential issues :)
src/Request/ConsoleRequest.php
Outdated
*/ | ||
public function getMetaData() | ||
{ | ||
if (is_array($this->command)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No need for the is_array
check. It is guaranteed to be true. PHP doesn't allow null
through an array
type.
src/Request/ConsoleRequest.php
Outdated
$arguments = []; | ||
$options = []; | ||
foreach (array_slice($this->command, 1) as $arg) { | ||
$arg[0] == '-' ? $options[] = $arg : $arguments[] = $arg; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Assignment inside ternaries is somewhat unusual. Perhaps consider just a plain old if. :)
src/Request/BasicResolver.php
Outdated
@@ -12,7 +12,11 @@ class BasicResolver implements ResolverInterface | |||
public function resolve() | |||
{ | |||
if (!isset($_SERVER['REQUEST_METHOD'])) { | |||
return new NullRequest(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This method could be updated to avoid the nested ifs:
/**
* Resolve the current request.
*
* @return \Bugsnag\Request\RequestInterface
*/
public function resolve()
{
if (isset($_SERVER['REQUEST_METHOD'])) {
return new PhpRequest(
$_SERVER,
empty($_SESSION) ? [] : $_SESSION,
empty($_COOKIE) ? [] : $_COOKIE,
static::getRequestHeaders($_SERVER),
static::getInputParams($_SERVER, $_POST)
);
}
if (PHP_SAPI === 'cli' && isset($_SERVER['argv'])) {
return new ConsoleRequest($_SERVER['argv']);
}
return new NullRequest();
}
src/Request/ConsoleRequest.php
Outdated
*/ | ||
public function getContext() | ||
{ | ||
return implode(' ', array_slice($this->command, 0, 2)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just realized that php doesn't include the executable in the arguments list. Maybe we should just take the first part of the array then, or implode the whole thing. Ping @kattrali - what do you think is the most sensible option here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think first few args still works well enough. The cases I can try/think of:
php [filename]
=>[filename]
php [filename] foo bar
=>[filename] foo
php [filename] --foo bar --baz red
=>[filename] --foo
<-- this one is a lil oddphp artisan migrate
=>artisan migrate
I could see the value in imploding the whole thing, but also don't want context to get arbitrarily long. We could max four args for now and revisit with feedback.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
but also don't want context to get arbitrarily long
Yeh, totally. We need to find the balance where it is useful but not unbounded.
src/Request/ConsoleRequest.php
Outdated
$arguments = []; | ||
$options = []; | ||
foreach (array_slice($this->command, 1) as $arg) { | ||
if ($arg[0] == '-') { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Safest option is to do: if (isset($arg[0]) && $arg[0] === '-') {
.
'Command' => 'some', | ||
'Arguments' => ['test', 'command'], | ||
'Options' => ['--opt'], | ||
]], $report->getMetaData()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Probably worth also doing an assertion on the report context too.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@GrahamCampbell Should there be a test which is different from this one?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, ha. No, that looks great. ;)
Left 2 more tiny comments. Otherwise, looks good. 🚀 🚀 🚀 |
'Command' => 'some', | ||
'Arguments' => ['test', 'command'], | ||
'Options' => ['--opt'], | ||
]], $report->getMetaData()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, ha. No, that looks great. ;)
🚢 🚢 🚢 |
Re this issue in bugsnag-laravel this implements a basic console-request class that will take an array of the console commands and attach them as meta-data.
This gives a handler for framework implementations, while leaving it framework-specific on how console input is acquired.