Skip to content

Commit

Permalink
Merge pull request #136 from FriendsOfCake/5.x
Browse files Browse the repository at this point in the history
Init Cake5 upgrade.
  • Loading branch information
ADmad authored Oct 13, 2023
2 parents 1d5ada1 + c2e539c commit 37dcf88
Show file tree
Hide file tree
Showing 17 changed files with 181 additions and 204 deletions.
5 changes: 2 additions & 3 deletions .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,10 @@ tests export-ignore
.editorconfig export-ignore
.gitattributes export-ignore
.gitignore export-ignore
.stickler.yml export-ignore
.travis.yml export-ignore
phpcs.xml.dist export-ignore
phpcs.xml export-ignore
phpstan.neon export-ignore
phpstan-baseline.neon export-ignore
phpunit.xml.dist export-ignore
psalm.xml export-ignore
psalm-baseline.xml export-ignore
.phive export-ignore
102 changes: 8 additions & 94 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,105 +4,19 @@ on:
push:
branches:
- 4.x
- 5.x
pull_request:
branches:
- '*'

permissions:
contents: read

jobs:
testsuite:
runs-on: ubuntu-22.04
strategy:
fail-fast: false
matrix:
php-version: ['7.4', '8.2']
prefer-lowest: ['']
include:
- php-version: '7.4'
prefer-lowest: 'prefer-lowest'

steps:
- uses: actions/checkout@v3

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php-version }}
extensions: mbstring, intl
coverage: pcov

- name: Get composer cache directory
id: composer-cache
run: echo "::set-output name=dir::$(composer config cache-files-dir)"

- name: Get date part for cache key
id: key-date
run: echo "::set-output name=date::$(date +'%Y-%m')"

- name: Cache composer dependencies
uses: actions/cache@v3
with:
path: ${{ steps.composer-cache.outputs.dir }}
key: ${{ runner.os }}-composer-${{ steps.key-date.outputs.date }}-${{ hashFiles('composer.json') }}-${{ matrix.prefer-lowest }}

- name: Composer Install
run: |
if ${{ matrix.prefer-lowest == 'prefer-lowest' }}; then
composer update --prefer-lowest --prefer-stable
else
composer update
fi
- name: Configure PHPUnit matcher
if: matrix.php-version == '7.4'
uses: mheap/phpunit-matcher-action@main

- name: Run PHPUnit
run: |
if [[ ${{ matrix.php-version }} == '7.4' ]]; then
export CODECOVERAGE=1 && vendor/bin/phpunit --verbose --coverage-clover=coverage.xml
else
vendor/bin/phpunit
fi
- name: Submit code coverage
if: success() && matrix.php-version == '7.4'
uses: codecov/codecov-action@v3
uses: cakephp/.github/.github/workflows/testsuite-with-db.yml@5.x
secrets: inherit

cs-stan:
name: Coding Standard & Static Analysis
runs-on: ubuntu-22.04

steps:
- uses: actions/checkout@v3

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: '7.4'
extensions: mbstring, intl
tools: cs2pr
coverage: none

- name: Get composer cache directory
id: composer-cache
run: echo "::set-output name=dir::$(composer config cache-files-dir)"

- name: Get date part for cache key
id: key-date
run: echo "::set-output name=date::$(date +'%Y-%m')"

- name: Cache composer dependencies
uses: actions/cache@v3
with:
path: ${{ steps.composer-cache.outputs.dir }}
key: ${{ runner.os }}-composer-${{ steps.key-date.outputs.date }}-${{ hashFiles('composer.json') }}-${{ matrix.prefer-lowest }}

- name: Composer install
run: composer stan-setup

- name: Run PHP CodeSniffer
run: composer cs-check

- name: Run phpstan
if: success() || failure()
run: vendor/bin/phpstan analyse --error-format=github
uses: cakephp/.github/.github/workflows/cs-stan.yml@5.x
secrets: inherit
5 changes: 5 additions & 0 deletions .phive/phars.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<phive xmlns="https://phar.io/phive">
<phar name="phpstan" version="1.10.38" installed="1.10.38" location="./tools/phpstan" copy="false"/>
<phar name="psalm" version="5.15.0" installed="5.15.0" location="./tools/psalm" copy="false"/>
</phive>
10 changes: 0 additions & 10 deletions .stickler.yml

This file was deleted.

20 changes: 12 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

Quickly enable CSV output of your model data.

This branch is for CakePHP **4.x**. For details see [version map](https://github.com/FriendsOfCake/cakephp-csvview/wiki#cakephp-version-map).
This branch is for CakePHP **5.x**. For details see [version map](https://github.com/FriendsOfCake/cakephp-csvview/wiki#cakephp-version-map).

## Background

Expand Down Expand Up @@ -182,18 +182,22 @@ to set how null values should be displayed in the CSV.

#### Automatic view class switching

You can use router's extension parsing feature and the `RequestHandlerComponent` to
automatically have the CsvView class switched in as follows.
You can use the controller's content negotiation feature to automatically have
the CsvView class switched in as follows.

Enable `csv` extension parsing for all routes using `Router::extensions('csv')`
in your app's `routes.php` or using `$routes->addExtensions()` within required
scope.
Enable `csv` extension parsing using `$routes->addExtensions(['csv'])` within required
scope in your app's `routes.php`.

```php
// PostsController.php

// In your controller's initialize() method:
$this->loadComponent('RequestHandler');
// Add the CsvView class for content type negotiation
public function initialize(): void
{
parent::initialize();

$this->addViewClasses(['csv' => 'CsvView.Csv']);
}

// Controller action
public function index()
Expand Down
31 changes: 15 additions & 16 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,38 +18,37 @@
"role": "Maintainer"
},
{
"name":"ADmad",
"role":"Contributor",
"name": "ADmad",
"role": "Contributor",
"homepage": "https://github.com/admad"
},
{
"name":"Mark Scherer",
"role":"Contributor",
"name": "Mark Scherer",
"role": "Contributor",
"homepage": "https://github.com/dereuromark"
},
{
"name":"Joshua Paling",
"role":"Contributor",
"name": "Joshua Paling",
"role": "Contributor",
"homepage": "https://github.com/joshuapaling"
},
{
"name":"Gaurish Sharma",
"role":"Contributor",
"name": "Gaurish Sharma",
"role": "Contributor",
"homepage": "https://github.com/gaurish"
},
{
"name":"Gregory Gaskill",
"role":"Contributor",
"homepage":"https://github.com/chronon"
"name": "Gregory Gaskill",
"role": "Contributor",
"homepage": "https://github.com/chronon"
}
],
"require":{
"php": ">=7.4",
"cakephp/cakephp": "^4.2"
"require": {
"cakephp/cakephp": "^5.0"
},
"require-dev": {
"phpunit/phpunit": "^9.5.0",
"cakephp/cakephp-codesniffer": "^4.0"
"phpunit/phpunit": "^10.1",
"cakephp/cakephp-codesniffer": "^5.0"
},
"autoload": {
"psr-4": {
Expand Down
4 changes: 2 additions & 2 deletions phpcs.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0"?>
<ruleset name="CakePHP Bake">
<ruleset name="CsvView">
<config name="installed_paths" value="../../cakephp/cakephp-codesniffer" />
<arg value="nps"/>
<arg value="s"/>

<file>src/</file>
<file>tests/</file>
Expand Down
1 change: 1 addition & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
parameters:
level: 8
checkMissingIterableValueType: false
checkGenericClassInNonGenericObjectType: false
paths:
- src/
bootstrapFiles:
Expand Down
37 changes: 12 additions & 25 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,32 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit
colors="true"
bootstrap="tests/bootstrap.php"
>

<!-- Add any additional test suites you want to run here -->
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" bootstrap="tests/bootstrap.php" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.4/phpunit.xsd">
<testsuites>
<testsuite name="csv-view">
<directory>tests/TestCase/</directory>
<directory>tests/TestCase/</directory>
</testsuite>
</testsuites>

<!-- Setup a listener for fixtures -->
<listeners>
<listener class="Cake\TestSuite\Fixture\FixtureInjector">
<arguments>
<object class="Cake\TestSuite\Fixture\FixtureManager"/>
</arguments>
</listener>
</listeners>

<filter>
<whitelist>
<extensions>
<bootstrap class="Cake\TestSuite\Fixture\Extension\PHPUnitExtension"/>
</extensions>
<php>
<env name="FIXTURE_SCHEMA_METADATA" value="./tests/schema.php"/>
</php>
<source>
<include>
<directory suffix=".php">src/</directory>
<exclude>
<file>src/Plugin.php</file>
</exclude>
</whitelist>
</filter>

</include>
</source>
</phpunit>
22 changes: 4 additions & 18 deletions src/Plugin.php → src/CsvViewPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,50 +5,36 @@

use Cake\Core\BasePlugin;
use Cake\Core\PluginApplicationInterface;
use Cake\Event\EventInterface;
use Cake\Event\EventManager;
use Cake\Http\ServerRequest;

class Plugin extends BasePlugin
class CsvViewPlugin extends BasePlugin
{
/**
* Plugin name.
*
* @var string
*/
protected $name = 'CsvView';
protected ?string $name = 'CsvView';

/**
* Load routes or not
*
* @var bool
*/
protected $routesEnabled = false;
protected bool $routesEnabled = false;

/**
* Console middleware
*
* @var bool
*/
protected $consoleEnabled = false;
protected bool $consoleEnabled = false;

/**
* @inheritDoc
*/
public function bootstrap(PluginApplicationInterface $app): void
{
/**
* Add CsvView to View class map through RequestHandler, if available, on Controller initialisation
*
* @link https://book.cakephp.org/4/en/controllers/components/request-handling.html#using-custom-viewclasses
*/
EventManager::instance()->on('Controller.initialize', function (EventInterface $event) {
$controller = $event->getSubject();
if ($controller->components()->has('RequestHandler')) {
$controller->RequestHandler->setConfig('viewClassMap.csv', 'CsvView.Csv');
}
});

/**
* Add a request detector named "csv" to check whether the request was for a CSV,
* either through accept header or file extension
Expand Down
Loading

0 comments on commit 37dcf88

Please sign in to comment.