Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
language: php

php:
- 7.2
- 7.3
- 7.4
- 8.1
- nightly

sudo: false
Expand Down
57 changes: 34 additions & 23 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,26 @@ PHP's `getopt()` provides little functionality and is prone to failure where one

### Installation

*Garden CLI requires PHP 7.0 or higher*
*Garden CLI requires PHP 8.1 or higher*

Garden CLI is [PSR-4](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-4-autoloader.md) compliant and can be installed using [composer](//getcomposer.org). Just add `vanilla/garden-cli` to your composer.json.

```json
"require": {
"vanilla/garden-cli": "~2.0"
"vanilla/garden-cli": "~3.0"
}
```

### Upgrading from 2.x

* This version supports PHP 8.1 and later only.
* The previously deprecated `LogFormatter` class and unit tests is now removed.
* In the past you could set a custom time format using `setTimeFormat('%F %T')`.
Internally Garden CLI used PHP's [`strftime()`](https://www.php.net/manual/en/function.strftime.php)
which is now deprecated. This has been replaced by the PHP [`date()`](https://www.php.net/manual/en/function.date.php)
function which accepts these [formats](https://www.php.net/manual/en/datetime.format.php).
For example, to have the same output as `setTimeFormat('%F %T')`, simply use `setTimeFormat('Y-m-d H:i:s')`.

## Defining The CLI

The `Cli` class provides a fluent interface for defining commands, opts, and args.
Expand Down Expand Up @@ -327,13 +337,13 @@ things like install scripts, scripts that take a long time, or scripts you put i
When using the `TaskLogger` you want to think in terms of messages and tasks. A message is a single log item to output
to the user. A task has a begin and an end and can be nested as much as you want. Messages are output using the various PSR-3 methods while tasks are output with `begin()` and `end()`. Here are all of the methods you can use to log tasks.

| Method | Notes |
| ------ | ----- |
| `begin` | Log the beginning of a task. |
| `beginDebug`, `beginInfo`, `beginNotice`, `beginWarning`, `beginError`, `beginCritical`, `beginAlert`, `beginEmergency` | Log the beginning of a task with the given log level. |
| `end` | Log the end of a task with the same level as it began. |
| `endError` | Log the end of a task that resulted in an error. |
| `endHttpStatus` | Log the end of a task with an HTTP status. The log level is calculated from the number of the status. |
| Method | Notes |
|-------------------------------------------------------------------------------------------------------------------------|-------------------------------------------------------------------------------------------------------|
| `begin` | Log the beginning of a task. |
| `beginDebug`, `beginInfo`, `beginNotice`, `beginWarning`, `beginError`, `beginCritical`, `beginAlert`, `beginEmergency` | Log the beginning of a task with the given log level. |
| `end` | Log the end of a task with the same level as it began. |
| `endError` | Log the end of a task that resulted in an error. |
| `endHttpStatus` | Log the end of a task with an HTTP status. The log level is calculated from the number of the status. |

#### Task Nesting and Durations

Expand Down Expand Up @@ -372,13 +382,13 @@ $log->end('done.');

If you create and use a `TaskLogger` object it will output nicely to the console out of the box. Under the hood it is using a `StreamLogger` object to handle the formatting of the tasks to an output stream, in this case stdout. You can replace or modify the `StreamLogger` if you want to control logging in a more granular level. Here are some options.

| Method | Default | Notes |
| ------ | ------- | ----- |
| `setLineFormat` | `'[{time}] {message}'` | Set the format of lines. Use the `{level}`, `{time}`, `{message}` strings to move the components around. |
| `setColorizeOutput` | automatic | Whether or not to use console colors. |
| `setBufferBegins` | `true` | Attempt to put task begin/end messages on the same line. Turn this off if you plan on writing to the log concurrently. |
| `setTimeFormat` | `'%F %T'` | Set the time format. This can be a `strftime` string or a callback. |
| `setLevelFormat` | nothing | Set a callback to format a `LogLevel` constant. |
| Method | Default | Notes |
|---------------------|------------------------|------------------------------------------------------------------------------------------------------------------------|
| `setLineFormat` | `'[{time}] {message}'` | Set the format of lines. Use the `{level}`, `{time}`, `{message}` strings to move the components around. |
| `setColorizeOutput` | automatic | Whether or not to use console colors. |
| `setBufferBegins` | `true` | Attempt to put task begin/end messages on the same line. Turn this off if you plan on writing to the log concurrently. |
| `setTimeFormat` | `'Y-m-d H:i:s'` | Set the time format. This can be a `date` string or a callback. |
| `setLevelFormat` | nothing | Set a callback to format a `LogLevel` constant. |

#### Example

Expand All @@ -402,10 +412,11 @@ $log = new TaskLogger($fmt);

You can give the `TaskLogger` any PSR-3 compliant logger and it will send its output to it. In order to use some of the special task functionality, you'll have to inspect the `$contenxt` argument of your `log` method. Here the fields that you may receive.

| Field | Type | Notes |
| ----- | ---- | ----- |
| `TaskLogger::FIELD_TIME` | `int` | The timestamp of the message. |
| `TaskLogger::FIELD_INDENT` | `int` | The indent level of the message. |
| `TaskLogger::FIELD_BEGIN` | `bool` | True if the message denotes the beginning of a task. |
| `TaskLogger::FIELD_END` | `bool` | True if the message denotes the end of a task. |
| `TaskLogger::FIELD_DURATION` | `float` | The duration of a task in seconds and milliseconds. |
| Field | Type | Notes |
|------------------------------|---------|------------------------------------------------------|
| `TaskLogger::FIELD_TIME` | `int` | The timestamp of the message. |
| `TaskLogger::FIELD_INDENT` | `int` | The indent level of the message. |
| `TaskLogger::FIELD_BEGIN` | `bool` | True if the message denotes the beginning of a task. |
| `TaskLogger::FIELD_END` | `bool` | True if the message denotes the end of a task. |
| `TaskLogger::FIELD_DURATION` | `float` | The duration of a task in seconds and milliseconds. |

15 changes: 9 additions & 6 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,17 @@
}
],
"require": {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would suggest to use a require for "php 7.4" as the code is basically compatible with 7.4

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @blacktek, unfortunately it is not backwards compatible with PHP 7.4 due to keywords such mixed being used as parameters or return types amongst other things.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm currently using it on 7.4 too and is working. 7.4 doesn't support multiple union return types (e.g. string | bool), that I remember were not used. Are you sure that those are currently used?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assumed there would be problems since the psalm test fails but if these warnings/errors can be fixed quickly in order to return composer.json to 7.4 it may be worth it. I will look into it when I have some time.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had a quick look but I'm guessing you are not using my Fix/php81 fork in PHP 7.4. My fork uses things like the mixed keyword as function parameters and function return types. Many switch statements were replaced by match statements. Many methods returns the static keyword. The throw new statement is used. Union types are used as function arguments, and so on.

My fork is strictly for PHP 8.1 which is why I asked the repository owner whether two separate branches can be released, i.e. a new 4.x branch alongside the 3.x branch.

Alternatively they could just resurrect their repository but it seems the primary contributor is no longer available and active on Github and have not been for a year.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's possible that you're right. I'm struggling a bit using your Fix/php81 on php8.1 and probably you're right that I use the old dev-master on php7.4

tnx

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you are struggling to set up my fork in composer.json here is an extract you can use:

{
  "require": {
    "php": "^8.1",
    "vanilla/garden-cli": "dev-fix/php81"
  },
  "repositories": [
    { "type": "vcs", "url":  "https://github.com/donatello-za/garden-cli" }
  ]
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tnx a lot :) this is exactly what I was using.

"php": ">=7.2",
"php": ">=8.1",
"ext-json": "*",
"psr/log": "^1.0"
"psr/log": "^2.0"
},
"require-dev": {
"ergebnis/composer-normalize": "^2.8",
"phpdocumentor/reflection-docblock": "^4.3",
"phpunit/phpunit": "^8",
"phpdocumentor/reflection-docblock": "^5.3",
"phpunit/phpunit": "^9.5",
"vanilla/garden-container": "^3.0",
"vanilla/standards": "^1.3",
"vimeo/psalm": "^3.16"
"vimeo/psalm": "^4.26"
},
"suggest": {
"ext-pdo": "Required for the DbUtils class.",
Expand All @@ -28,7 +28,10 @@
},
"config": {
"platform": {
"php": "7.2"
"php": "8.1"
},
"allow-plugins": {
"ergebnis/composer-normalize": true
}
},
"autoload": {
Expand Down
46 changes: 22 additions & 24 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>

<phpunit backupGlobals="false"
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
backupGlobals="false"
backupStaticAttributes="false"
colors="true"
convertErrorsToExceptions="true"
Expand All @@ -9,26 +9,24 @@
processIsolation="false"
stopOnFailure="false"
bootstrap="vendor/autoload.php"
>
<php>
<ini name="intl.default_locale" value="en"/>
<ini name="intl.error_level" value="0"/>
<ini name="memory_limit" value="-1"/>
<ini name="date.timezone" value="America/Montreal"/>
</php>

<testsuites>
<testsuite name="Garden Test Suite">
<directory suffix="Test.php">./tests/</directory>
</testsuite>
</testsuites>

<filter>
<whitelist addUncoveredFilesFromWhitelist="false">
<directory suffix=".php">src</directory>
<exclude>
<directory suffix=".php">vendor</directory>
</exclude>
</whitelist>
</filter>
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
<coverage includeUncoveredFiles="false">
<include>
<directory suffix=".php">src</directory>
</include>
<exclude>
<directory suffix=".php">vendor</directory>
</exclude>
</coverage>
<php>
<ini name="intl.default_locale" value="en"/>
<ini name="intl.error_level" value="0"/>
<ini name="memory_limit" value="-1"/>
<ini name="date.timezone" value="America/Montreal"/>
</php>
<testsuites>
<testsuite name="Garden Test Suite">
<directory suffix="Test.php">./tests/</directory>
</testsuite>
</testsuites>
</phpunit>
Loading