Skip to content

Commit

Permalink
feat: add HTML formatter (#69)
Browse files Browse the repository at this point in the history
* add html formatter

* update workflow

* update workflow

* cs-fix

* workflow update

* workflow update
  • Loading branch information
michalsn authored Jun 10, 2024
1 parent 2f61d46 commit 4bf7525
Show file tree
Hide file tree
Showing 25 changed files with 341 additions and 111 deletions.
11 changes: 4 additions & 7 deletions .github/workflows/deptrac.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ jobs:
uses: shivammathur/setup-php@v2
with:
php-version: '8.1'
tools: phive
extensions: intl, json, mbstring, xml
coverage: none
env:
Expand All @@ -42,7 +41,7 @@ jobs:
run: echo "COMPOSER_CACHE_FILES_DIR=$(composer config cache-files-dir)" >> $GITHUB_ENV

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

- name: Cache Deptrac results
uses: actions/cache@v3
uses: actions/cache@v4
with:
path: build
key: ${{ runner.os }}-deptrac-${{ github.sha }}
Expand All @@ -68,7 +67,5 @@ jobs:
- name: Trace dependencies
run: |
sudo phive --no-progress install --global --trust-gpg-keys B8F640134AB1782E,A98E898BB53EB748 qossmic/deptrac
deptrac analyze --cache-file=build/deptrac.cache
env:
GITHUB_AUTH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
composer require --dev qossmic/deptrac-shim
vendor/bin/deptrac analyze --cache-file=build/deptrac.cache
2 changes: 1 addition & 1 deletion .github/workflows/phpcsfixer.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ jobs:
run: echo "COMPOSER_CACHE_FILES_DIR=$(composer config cache-files-dir)" >> $GITHUB_ENV

- name: Cache composer dependencies
uses: actions/cache@v3
uses: actions/cache@v4
with:
path: ${{ env.COMPOSER_CACHE_FILES_DIR }}
key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.json') }}-${{ hashFiles('**/composer.lock') }}
Expand Down
6 changes: 3 additions & 3 deletions .github/workflows/phpstan.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ jobs:
strategy:
fail-fast: false
matrix:
php-versions: ['8.0', '8.1']
php-versions: ['8.1', '8.2', '8.3']

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

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

- name: Cache PHPStan results
uses: actions/cache@v3
uses: actions/cache@v4
with:
path: build/phpstan
key: ${{ runner.os }}-phpstan-${{ github.sha }}
Expand Down
6 changes: 3 additions & 3 deletions .github/workflows/phpunit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ jobs:
if: "!contains(github.event.head_commit.message, '[ci skip]')"
strategy:
matrix:
php-versions: ['8.0', '8.1']
php-versions: ['8.1', '8.2', '8.3']

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

- name: Cache composer dependencies
uses: actions/cache@v3
uses: actions/cache@v4
with:
path: ${{ env.COMPOSER_CACHE_FILES_DIR }}
key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.json') }}-${{ hashFiles('**/composer.lock') }}
Expand All @@ -60,7 +60,7 @@ jobs:
fi
- name: Test with PHPUnit
run: vendor/bin/phpunit --verbose --coverage-text
run: vendor/bin/phpunit --coverage-text
env:
TERM: xterm-256color
TACHYCARDIA_MONITOR_GA: enabled
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/psalm.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ jobs:
run: echo "COMPOSER_CACHE_FILES_DIR=$(composer config cache-files-dir)" >> $GITHUB_ENV

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

- name: Cache Psalm results
uses: actions/cache@v3
uses: actions/cache@v4
with:
path: build/psalm
key: ${{ runner.os }}-psalm-${{ github.sha }}
Expand Down
17 changes: 12 additions & 5 deletions .github/workflows/rector.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ jobs:
strategy:
fail-fast: false
matrix:
php-versions: ['8.0', '8.1']
php-versions: ['8.1', '8.2', '8.3']

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

- name: Cache composer dependencies
uses: actions/cache@v3
uses: actions/cache@v4
with:
path: ${{ env.COMPOSER_CACHE_FILES_DIR }}
key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.json') }}-${{ hashFiles('**/composer.lock') }}
Expand All @@ -60,7 +60,14 @@ jobs:
composer update --no-progress --no-interaction --prefer-dist --optimize-autoloader
fi
- name: Rector Cache
uses: actions/cache@v4
with:
path: /tmp/rector
key: ${{ runner.os }}-rector-${{ github.run_id }}
restore-keys: ${{ runner.os }}-rector-

- run: mkdir -p /tmp/rector

- name: Analyze for refactoring
run: |
composer global require --dev rector/rector:^0.15.1
rector process --dry-run --no-progress-bar
run: vendor/bin/rector process --dry-run --no-progress-bar
17 changes: 13 additions & 4 deletions .php-cs-fixer.dist.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,21 @@
->files()
->in([
__DIR__ . '/src/',
// __DIR__ . '/tests/',
__DIR__ . '/tests/',
])
->exclude('build')
->append([__FILE__]);
->exclude([
'build',
'Views',
])
->append([
__FILE__,
__DIR__ . '/rector.php',
]);

$overrides = [];
$overrides = [
// 'declare_strict_types' => true,
// 'void_return' => true,
];

$options = [
'finder' => $finder,
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
},
"require-dev": {
"codeigniter4/devkit": "^1.0",
"codeigniter4/framework": "^4.1"
"codeigniter4/framework": "^4.3"
},
"minimum-stability": "dev",
"prefer-stable": true,
Expand Down
51 changes: 51 additions & 0 deletions docs/html_formatter.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# HTML Formatter

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).

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.

### Configuration

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:

php spark htmx:publish

Since content negotiation will be triggered for any format other than `json` or `xml`, we have two options:

1. Set the custom headers for every request via HTML tag
```html
hx-headers='{"Accept":"text/html"}'
```
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.
```php
public array $supportedResponseFormats = [
'text/html'
'application/json',
'application/xml', // machine-readable XML
'text/xml', // human-readable XML
];
```

### Example

This is an sample of using HTML formatter:

```php
<?php

namespace App\Controllers;

use CodeIgniter\API\ResponseTrait;
use CodeIgniter\RESTful\ResourcePresenter;

class Photos extends ResourcePresenter
{
use ResponseTrait;

public function index()
{
$this->format = 'html';
return $this->respondCreated('<div>Some data</div>');
}
}
```
1 change: 1 addition & 0 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ It also provides some additional help with **handling errors** and **Debug Toolb
* [Response](response.md)
* [RediretResponse](redirect_response.md)
* [Debug Toolbar](debug_toolbar.md)
* [HTML Formatter](html_formatter.md)
* [Troubleshooting](troubleshooting.md)

### Demos
Expand Down
8 changes: 4 additions & 4 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ parameters:
bootstrapFiles:
- vendor/codeigniter4/framework/system/Test/bootstrap.php
excludePaths:
- src/Debug/Toolbar.php
- src/Debug/Toolbar.php
- src/Views/*
ignoreErrors:
- '#Call to deprecated method getStatusCode\(\) of class CodeIgniter\\HTTP\\Response:\nTo be replaced by the PSR-7 version \(compatible\)#'
- '#Variable \$testString might not be defined.#'
- '#Variable \$this might not be defined.#'
- '#Variable \$testString might not be defined.#'
- '#Variable \$this might not be defined.#'
universalObjectCratesClasses:
- CodeIgniter\Entity
- CodeIgniter\Entity\Entity
Expand Down
87 changes: 29 additions & 58 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,33 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.5/phpunit.xsd"
bootstrap="vendor/codeigniter4/framework/system/Test/bootstrap.php"
backupGlobals="false"
beStrictAboutCoversAnnotation="true"
beStrictAboutOutputDuringTests="true"
beStrictAboutTodoAnnotatedTests="true"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
executionOrder="random"
failOnRisky="true"
failOnWarning="true"
stopOnError="false"
stopOnFailure="false"
stopOnIncomplete="false"
stopOnSkipped="false"
verbose="true">

<coverage includeUncoveredFiles="true" processUncoveredFiles="true">
<include>
<directory suffix=".php">./src/</directory>
</include>
<exclude>
<directory suffix=".php">./src/Config</directory>
<file>./src/Commands/HtmxPublish.php</file>
<file>./src/Debug/Toolbar.php</file>
</exclude>
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.5/phpunit.xsd"
bootstrap="vendor/codeigniter4/framework/system/Test/bootstrap.php"
backupGlobals="false"
beStrictAboutOutputDuringTests="true"
colors="true"
executionOrder="random"
failOnRisky="true"
failOnWarning="true"
stopOnError="false"
stopOnFailure="false"
stopOnIncomplete="false"
stopOnSkipped="false"
cacheDirectory=".phpunit.cache"
beStrictAboutCoverageMetadata="true">
<coverage includeUncoveredFiles="true">
<report>
<clover outputFile="build/phpunit/clover.xml"/>
<html outputDirectory="build/phpunit/html"/>
Expand All @@ -36,59 +23,34 @@
<xml outputDirectory="build/phpunit/xml-coverage"/>
</report>
</coverage>

<testsuites>
<testsuite name="main">
<directory>./tests</directory>
</testsuite>
</testsuites>

<extensions>
<extension class="Nexus\PHPUnit\Extension\Tachycardia">
<arguments>
<array>
<element key="timeLimit">
<double>0.50</double>
</element>
<element key="reportable">
<integer>30</integer>
</element>
<element key="precision">
<integer>2</integer>
</element>
<element key="collectBare">
<boolean>true</boolean>
</element>
<element key="tabulate">
<boolean>true</boolean>
</element>
</array>
</arguments>
</extension>
<bootstrap class="Nexus\PHPUnit\Tachycardia\TachycardiaExtension">
<parameter name="time-limit" value="0.50"/>
<parameter name="report-count" value="30"/>
<parameter name="format" value="table"/>
</bootstrap>
</extensions>

<logging>
<testdoxHtml outputFile="build/phpunit/testdox.html"/>
<testdoxText outputFile="build/phpunit/testdox.txt"/>
<junit outputFile="build/phpunit/junit.xml"/>
</logging>

<php>
<env name="XDEBUG_MODE" value="coverage"/>
<server name="app.baseURL" value="https://example.com/"/>

<!-- Directory containing phpunit.xml -->
<const name="HOMEPATH" value="./"/>

<!-- Directory containing the Paths config file -->
<const name="CONFIGPATH" value="vendor/codeigniter4/framework/app/Config/"/>

<!-- Directory containing the front controller (index.php) -->
<const name="PUBLICPATH" value="./public/"/>

<!-- https://getcomposer.org/xdebug -->
<env name="COMPOSER_DISABLE_XDEBUG_WARN" value="1"/>

<!-- Database configuration -->
<env name="database.tests.strictOn" value="true"/>
<!-- Uncomment to use alternate testing database configuration
Expand All @@ -100,4 +62,13 @@
<env name="database.tests.DBPrefix" value="tests_"/>
-->
</php>
<source>
<include>
<directory suffix=".php">./src/</directory>
</include>
<exclude>
<directory suffix=".php">./src/Config</directory>
<directory suffix=".php">./src/Views</directory>
</exclude>
</source>
</phpunit>
Loading

0 comments on commit 4bf7525

Please sign in to comment.