Skip to content

Commit 4bf7525

Browse files
authored
feat: add HTML formatter (#69)
* add html formatter * update workflow * update workflow * cs-fix * workflow update * workflow update
1 parent 2f61d46 commit 4bf7525

25 files changed

+341
-111
lines changed

.github/workflows/deptrac.yml

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ jobs:
3232
uses: shivammathur/setup-php@v2
3333
with:
3434
php-version: '8.1'
35-
tools: phive
3635
extensions: intl, json, mbstring, xml
3736
coverage: none
3837
env:
@@ -42,7 +41,7 @@ jobs:
4241
run: echo "COMPOSER_CACHE_FILES_DIR=$(composer config cache-files-dir)" >> $GITHUB_ENV
4342

4443
- name: Cache composer dependencies
45-
uses: actions/cache@v3
44+
uses: actions/cache@v4
4645
with:
4746
path: ${{ env.COMPOSER_CACHE_FILES_DIR }}
4847
key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.json') }}-${{ hashFiles('**/composer.lock') }}
@@ -52,7 +51,7 @@ jobs:
5251
run: mkdir -p build/
5352

5453
- name: Cache Deptrac results
55-
uses: actions/cache@v3
54+
uses: actions/cache@v4
5655
with:
5756
path: build
5857
key: ${{ runner.os }}-deptrac-${{ github.sha }}
@@ -68,7 +67,5 @@ jobs:
6867
6968
- name: Trace dependencies
7069
run: |
71-
sudo phive --no-progress install --global --trust-gpg-keys B8F640134AB1782E,A98E898BB53EB748 qossmic/deptrac
72-
deptrac analyze --cache-file=build/deptrac.cache
73-
env:
74-
GITHUB_AUTH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
70+
composer require --dev qossmic/deptrac-shim
71+
vendor/bin/deptrac analyze --cache-file=build/deptrac.cache

.github/workflows/phpcsfixer.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ jobs:
3737
run: echo "COMPOSER_CACHE_FILES_DIR=$(composer config cache-files-dir)" >> $GITHUB_ENV
3838

3939
- name: Cache composer dependencies
40-
uses: actions/cache@v3
40+
uses: actions/cache@v4
4141
with:
4242
path: ${{ env.COMPOSER_CACHE_FILES_DIR }}
4343
key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.json') }}-${{ hashFiles('**/composer.lock') }}

.github/workflows/phpstan.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ jobs:
2626
strategy:
2727
fail-fast: false
2828
matrix:
29-
php-versions: ['8.0', '8.1']
29+
php-versions: ['8.1', '8.2', '8.3']
3030

3131
steps:
3232
- name: Checkout
@@ -46,7 +46,7 @@ jobs:
4646
run: echo "COMPOSER_CACHE_FILES_DIR=$(composer config cache-files-dir)" >> $GITHUB_ENV
4747

4848
- name: Cache composer dependencies
49-
uses: actions/cache@v3
49+
uses: actions/cache@v4
5050
with:
5151
path: ${{ env.COMPOSER_CACHE_FILES_DIR }}
5252
key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.json') }}-${{ hashFiles('**/composer.lock') }}
@@ -56,7 +56,7 @@ jobs:
5656
run: mkdir -p build/phpstan
5757

5858
- name: Cache PHPStan results
59-
uses: actions/cache@v3
59+
uses: actions/cache@v4
6060
with:
6161
path: build/phpstan
6262
key: ${{ runner.os }}-phpstan-${{ github.sha }}

.github/workflows/phpunit.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ jobs:
2525
if: "!contains(github.event.head_commit.message, '[ci skip]')"
2626
strategy:
2727
matrix:
28-
php-versions: ['8.0', '8.1']
28+
php-versions: ['8.1', '8.2', '8.3']
2929

3030
steps:
3131
- name: Checkout
@@ -45,7 +45,7 @@ jobs:
4545
run: echo "COMPOSER_CACHE_FILES_DIR=$(composer config cache-files-dir)" >> $GITHUB_ENV
4646

4747
- name: Cache composer dependencies
48-
uses: actions/cache@v3
48+
uses: actions/cache@v4
4949
with:
5050
path: ${{ env.COMPOSER_CACHE_FILES_DIR }}
5151
key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.json') }}-${{ hashFiles('**/composer.lock') }}
@@ -60,7 +60,7 @@ jobs:
6060
fi
6161
6262
- name: Test with PHPUnit
63-
run: vendor/bin/phpunit --verbose --coverage-text
63+
run: vendor/bin/phpunit --coverage-text
6464
env:
6565
TERM: xterm-256color
6666
TACHYCARDIA_MONITOR_GA: enabled

.github/workflows/psalm.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ jobs:
4242
run: echo "COMPOSER_CACHE_FILES_DIR=$(composer config cache-files-dir)" >> $GITHUB_ENV
4343

4444
- name: Cache composer dependencies
45-
uses: actions/cache@v3
45+
uses: actions/cache@v4
4646
with:
4747
path: ${{ env.COMPOSER_CACHE_FILES_DIR }}
4848
key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.json') }}-${{ hashFiles('**/composer.lock') }}
@@ -52,7 +52,7 @@ jobs:
5252
run: mkdir -p build/psalm
5353

5454
- name: Cache Psalm results
55-
uses: actions/cache@v3
55+
uses: actions/cache@v4
5656
with:
5757
path: build/psalm
5858
key: ${{ runner.os }}-psalm-${{ github.sha }}

.github/workflows/rector.yml

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ jobs:
2626
strategy:
2727
fail-fast: false
2828
matrix:
29-
php-versions: ['8.0', '8.1']
29+
php-versions: ['8.1', '8.2', '8.3']
3030

3131
steps:
3232
- name: Checkout
@@ -46,7 +46,7 @@ jobs:
4646
run: echo "COMPOSER_CACHE_FILES_DIR=$(composer config cache-files-dir)" >> $GITHUB_ENV
4747

4848
- name: Cache composer dependencies
49-
uses: actions/cache@v3
49+
uses: actions/cache@v4
5050
with:
5151
path: ${{ env.COMPOSER_CACHE_FILES_DIR }}
5252
key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.json') }}-${{ hashFiles('**/composer.lock') }}
@@ -60,7 +60,14 @@ jobs:
6060
composer update --no-progress --no-interaction --prefer-dist --optimize-autoloader
6161
fi
6262
63+
- name: Rector Cache
64+
uses: actions/cache@v4
65+
with:
66+
path: /tmp/rector
67+
key: ${{ runner.os }}-rector-${{ github.run_id }}
68+
restore-keys: ${{ runner.os }}-rector-
69+
70+
- run: mkdir -p /tmp/rector
71+
6372
- name: Analyze for refactoring
64-
run: |
65-
composer global require --dev rector/rector:^0.15.1
66-
rector process --dry-run --no-progress-bar
73+
run: vendor/bin/rector process --dry-run --no-progress-bar

.php-cs-fixer.dist.php

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,21 @@
88
->files()
99
->in([
1010
__DIR__ . '/src/',
11-
// __DIR__ . '/tests/',
11+
__DIR__ . '/tests/',
1212
])
13-
->exclude('build')
14-
->append([__FILE__]);
13+
->exclude([
14+
'build',
15+
'Views',
16+
])
17+
->append([
18+
__FILE__,
19+
__DIR__ . '/rector.php',
20+
]);
1521

16-
$overrides = [];
22+
$overrides = [
23+
// 'declare_strict_types' => true,
24+
// 'void_return' => true,
25+
];
1726

1827
$options = [
1928
'finder' => $finder,

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
},
1818
"require-dev": {
1919
"codeigniter4/devkit": "^1.0",
20-
"codeigniter4/framework": "^4.1"
20+
"codeigniter4/framework": "^4.3"
2121
},
2222
"minimum-stability": "dev",
2323
"prefer-stable": true,

docs/html_formatter.md

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# HTML Formatter
2+
3+
There are times when we want to work with HTMX through an API. In such cases, we may want to use [Response Trait API](https://codeigniter.com/user_guide/outgoing/api_responses.html).
4+
5+
Unfortunately, CodeIgniter does not support HTML formatting for API by default. That's why such a formatter is included here. It will make it easier to work with the API when we want to return data in HTML format.
6+
7+
### Configuration
8+
9+
We should edit the `app/Config/Format.php` file to include the necessary changes. We should add `'text/html'` to the `$supportedResponseFormats` array, and `'text/html' => HTMLFormatter::class` to the `$formatters` array. This will be done automatically when you run the command:
10+
11+
php spark htmx:publish
12+
13+
Since content negotiation will be triggered for any format other than `json` or `xml`, we have two options:
14+
15+
1. Set the custom headers for every request via HTML tag
16+
```html
17+
hx-headers='{"Accept":"text/html"}'
18+
```
19+
2. Move the `'text/html'` entry from the `$supportedResponseFormats` config array to the first position in the array - this way it will be used as the default value.
20+
```php
21+
public array $supportedResponseFormats = [
22+
'text/html'
23+
'application/json',
24+
'application/xml', // machine-readable XML
25+
'text/xml', // human-readable XML
26+
];
27+
```
28+
29+
### Example
30+
31+
This is an sample of using HTML formatter:
32+
33+
```php
34+
<?php
35+
36+
namespace App\Controllers;
37+
38+
use CodeIgniter\API\ResponseTrait;
39+
use CodeIgniter\RESTful\ResourcePresenter;
40+
41+
class Photos extends ResourcePresenter
42+
{
43+
use ResponseTrait;
44+
45+
public function index()
46+
{
47+
$this->format = 'html';
48+
return $this->respondCreated('<div>Some data</div>');
49+
}
50+
}
51+
```

docs/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ It also provides some additional help with **handling errors** and **Debug Toolb
1919
* [Response](response.md)
2020
* [RediretResponse](redirect_response.md)
2121
* [Debug Toolbar](debug_toolbar.md)
22+
* [HTML Formatter](html_formatter.md)
2223
* [Troubleshooting](troubleshooting.md)
2324

2425
### Demos

phpstan.neon.dist

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@ parameters:
77
bootstrapFiles:
88
- vendor/codeigniter4/framework/system/Test/bootstrap.php
99
excludePaths:
10-
- src/Debug/Toolbar.php
10+
- src/Debug/Toolbar.php
11+
- src/Views/*
1112
ignoreErrors:
12-
- '#Call to deprecated method getStatusCode\(\) of class CodeIgniter\\HTTP\\Response:\nTo be replaced by the PSR-7 version \(compatible\)#'
13-
- '#Variable \$testString might not be defined.#'
14-
- '#Variable \$this might not be defined.#'
13+
- '#Variable \$testString might not be defined.#'
14+
- '#Variable \$this might not be defined.#'
1515
universalObjectCratesClasses:
1616
- CodeIgniter\Entity
1717
- CodeIgniter\Entity\Entity

phpunit.xml.dist

Lines changed: 29 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,20 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3-
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.5/phpunit.xsd"
4-
bootstrap="vendor/codeigniter4/framework/system/Test/bootstrap.php"
5-
backupGlobals="false"
6-
beStrictAboutCoversAnnotation="true"
7-
beStrictAboutOutputDuringTests="true"
8-
beStrictAboutTodoAnnotatedTests="true"
9-
colors="true"
10-
convertErrorsToExceptions="true"
11-
convertNoticesToExceptions="true"
12-
convertWarningsToExceptions="true"
13-
executionOrder="random"
14-
failOnRisky="true"
15-
failOnWarning="true"
16-
stopOnError="false"
17-
stopOnFailure="false"
18-
stopOnIncomplete="false"
19-
stopOnSkipped="false"
20-
verbose="true">
21-
22-
<coverage includeUncoveredFiles="true" processUncoveredFiles="true">
23-
<include>
24-
<directory suffix=".php">./src/</directory>
25-
</include>
26-
<exclude>
27-
<directory suffix=".php">./src/Config</directory>
28-
<file>./src/Commands/HtmxPublish.php</file>
29-
<file>./src/Debug/Toolbar.php</file>
30-
</exclude>
3+
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.5/phpunit.xsd"
4+
bootstrap="vendor/codeigniter4/framework/system/Test/bootstrap.php"
5+
backupGlobals="false"
6+
beStrictAboutOutputDuringTests="true"
7+
colors="true"
8+
executionOrder="random"
9+
failOnRisky="true"
10+
failOnWarning="true"
11+
stopOnError="false"
12+
stopOnFailure="false"
13+
stopOnIncomplete="false"
14+
stopOnSkipped="false"
15+
cacheDirectory=".phpunit.cache"
16+
beStrictAboutCoverageMetadata="true">
17+
<coverage includeUncoveredFiles="true">
3118
<report>
3219
<clover outputFile="build/phpunit/clover.xml"/>
3320
<html outputDirectory="build/phpunit/html"/>
@@ -36,59 +23,34 @@
3623
<xml outputDirectory="build/phpunit/xml-coverage"/>
3724
</report>
3825
</coverage>
39-
4026
<testsuites>
4127
<testsuite name="main">
4228
<directory>./tests</directory>
4329
</testsuite>
4430
</testsuites>
45-
4631
<extensions>
47-
<extension class="Nexus\PHPUnit\Extension\Tachycardia">
48-
<arguments>
49-
<array>
50-
<element key="timeLimit">
51-
<double>0.50</double>
52-
</element>
53-
<element key="reportable">
54-
<integer>30</integer>
55-
</element>
56-
<element key="precision">
57-
<integer>2</integer>
58-
</element>
59-
<element key="collectBare">
60-
<boolean>true</boolean>
61-
</element>
62-
<element key="tabulate">
63-
<boolean>true</boolean>
64-
</element>
65-
</array>
66-
</arguments>
67-
</extension>
32+
<bootstrap class="Nexus\PHPUnit\Tachycardia\TachycardiaExtension">
33+
<parameter name="time-limit" value="0.50"/>
34+
<parameter name="report-count" value="30"/>
35+
<parameter name="format" value="table"/>
36+
</bootstrap>
6837
</extensions>
69-
7038
<logging>
7139
<testdoxHtml outputFile="build/phpunit/testdox.html"/>
7240
<testdoxText outputFile="build/phpunit/testdox.txt"/>
7341
<junit outputFile="build/phpunit/junit.xml"/>
7442
</logging>
75-
7643
<php>
7744
<env name="XDEBUG_MODE" value="coverage"/>
7845
<server name="app.baseURL" value="https://example.com/"/>
79-
8046
<!-- Directory containing phpunit.xml -->
8147
<const name="HOMEPATH" value="./"/>
82-
8348
<!-- Directory containing the Paths config file -->
8449
<const name="CONFIGPATH" value="vendor/codeigniter4/framework/app/Config/"/>
85-
8650
<!-- Directory containing the front controller (index.php) -->
8751
<const name="PUBLICPATH" value="./public/"/>
88-
8952
<!-- https://getcomposer.org/xdebug -->
9053
<env name="COMPOSER_DISABLE_XDEBUG_WARN" value="1"/>
91-
9254
<!-- Database configuration -->
9355
<env name="database.tests.strictOn" value="true"/>
9456
<!-- Uncomment to use alternate testing database configuration
@@ -100,4 +62,13 @@
10062
<env name="database.tests.DBPrefix" value="tests_"/>
10163
-->
10264
</php>
65+
<source>
66+
<include>
67+
<directory suffix=".php">./src/</directory>
68+
</include>
69+
<exclude>
70+
<directory suffix=".php">./src/Config</directory>
71+
<directory suffix=".php">./src/Views</directory>
72+
</exclude>
73+
</source>
10374
</phpunit>

0 commit comments

Comments
 (0)