Skip to content

Commit

Permalink
Merge pull request #328 from FriendsOfCake/cake5
Browse files Browse the repository at this point in the history
Cake5
  • Loading branch information
ADmad authored Oct 3, 2023
2 parents 134c9ae + bc22edd commit aaf5138
Show file tree
Hide file tree
Showing 25 changed files with 338 additions and 302 deletions.
4 changes: 1 addition & 3 deletions .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@ 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
Expand Down
44 changes: 11 additions & 33 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,23 @@ on:
branches:
- '*'

permissions:
contents: read

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

steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4

- name: Setup PHP
uses: shivammathur/setup-php@v2
Expand Down Expand Up @@ -51,41 +54,16 @@ jobs:

- name: Run PHPUnit
run: |
if [[ ${{ matrix.php-version }} == '7.4' ]]; then
vendor/bin/phpunit --verbose --coverage-clover=coverage.xml
if [[ ${{ matrix.php-version }} == '8.1' ]]; then
vendor/bin/phpunit --coverage-clover=coverage.xml
else
vendor/bin/phpunit
fi
- name: Code Coverage Report
if: matrix.php-version == '7.4'
if: matrix.php-version == '8.1'
uses: codecov/codecov-action@v3

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
coverage: none
tools: cs2pr, vimeo/psalm:4.26, phpstan:1.8

- name: Composer Install
run: composer install

- name: Run phpcs
run: vendor/bin/phpcs -q --report=checkstyle --standard=vendor/cakephp/cakephp-codesniffer/CakePHP src/ tests/ | cs2pr

- name: Run psalm
if: success() || failure()
run: psalm --output-format=github

- name: Run phpstan
if: success() || failure()
run: phpstan analyse
uses: cakephp/.github/.github/workflows/[email protected]
secrets: inherit
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
/phpunit.xml
/tmp
.phpunit.result.cache
/.idea/
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.37" installed="1.10.37" location="./tools/phpstan" copy="false"/>
<phar name="psalm" version="5.15.0" installed="5.15.0" location="./tools/psalm" copy="false"/>
</phive>
35 changes: 25 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
Plugin containing CakePdf lib which will use a PDF engine to convert HTML to PDF.

Engines included in the plugin:
* DomPdf (^0.8. Using ^2.0 is highly recommended as lower versions have various security vulnerabilities)
* DomPdf (^2.0)
* Mpdf (^8.0.4)
* Tcpdf (^6.3)
* WkHtmlToPdf **RECOMMENDED ENGINE**
Expand All @@ -33,7 +33,7 @@ composer require friendsofcake/cakepdf
CakePdf does not include any of the supported PDF engines, you need to install
the ones you intend to use yourself.

Packages for the recommend wkhtmltopdf engine can be downloaded from https://wkhtmltopdf.org/downloads.html.
Packages for the recommend `wkhtmltopdf` engine can be downloaded from https://wkhtmltopdf.org/downloads.html.
DomPdf, Mpdf and Tcpdf can be installed via composer using one of the following commands:

```
Expand Down Expand Up @@ -110,6 +110,8 @@ Configure::write('CakePdf', [
```

```php
use CakePdf\View\Pdfiew;

class InvoicesController extends AppController
{
// In your Invoices controller you could set additional configs,
Expand All @@ -126,6 +128,18 @@ class InvoicesController extends AppController
);
$this->set('invoice', $invoice);
}

/**
* Add Pdfiew::class to this $viewClasses property so that CakePHP automatically
* switches the view class for URLs ending with `.pdf` or with appropriate `Accept` header.
*
* @see https://book.cakephp.org/5/en/controllers.html#content-type-negotiation
* @var array<string>
*/
public function viewClasses(): array
{
return $this->viewClasses[] = Pdfiew::class;
}
}
```

Expand Down Expand Up @@ -173,22 +187,23 @@ Many people mix both ways and don't get the expected results.

### 1: Render as PDF (including forced download) in the browser with PdfView

You can create PDF view and layout files for your controller actions and have
them automatically rendered. Place the view templates in a 'pdf' subdir, for
You can create PDF template and layout files for your controller actions and have
them automatically rendered. Place the templates in a `pdf` subdir, for
instance `templates/Invoices/pdf/view.php`, layouts will be in
`templates/layout/pdf/default.php`.

Make sure your `InvoicesController` class
[loads the `RequestHandler` component](http://book.cakephp.org/3.0/en/controllers/components/request-handling.html)
and browse to `http://localhost/invoices/view/1.pdf`
Then for e.g. accessing the URL `http://localhost/invoices/view/1.pdf` would
give you the generated PDF.

Additionally you can map resources by adding `Router::mapResources(['Invoices']);`
to your routes file and you can access the same document at
`http://localhost/invoices/1.pdf`.

In case you don't want to use the `pdf` extension in your URLs, you can omit
registering it in your routes configuration. Then in your controller action
specify the view class to be used:
registering it in your routes configuration. You can then set the `Accept`
request header to `application/pdf` to make CakePHP automatically switch to `PdfView`
(required the `viewClasses()` method to be set as shown above), or explicity switch
the view class in your controller action:

```php
$this->viewBuilder()->setClassName('CakePdf.Pdf');
Expand All @@ -210,7 +225,7 @@ $this->viewBuilder()->setOption(

### 2: Create PDF for email attachment, file storage etc.

You can use CakePdf lib to create raw PDF data with a view template.
You can use CakePdf to create raw PDF data with a view template.
The view file path would look like `templates/pdf/newsletter.php`.
Layout file path would be like `templates/layout/pdf/default.php`
Note that layouts for both usage types are within same directory, but the view
Expand Down
23 changes: 17 additions & 6 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@
"homepage": "http://github.com/friendsofcake/CakePdf",
"license": "MIT",
"require": {
"cakephp/cakephp": "^4.0"
"php": ">=8.1",
"cakephp/cakephp": "^5.0.1"
},
"require-dev": {
"phpunit/phpunit": "~8.5.0 || ^9.3",
"phpunit/phpunit": "^10.3",
"dompdf/dompdf": "^2.0",
"mpdf/mpdf": "^8.0.4",
"mpdf/mpdf": "^8.1.6",
"tecnickcom/tcpdf": "^6.3",
"cakephp/cakephp-codesniffer": "^4.2"
"cakephp/cakephp-codesniffer": "^5.0"
},
"suggest": {
"dompdf/dompdf": "If you wish to use the DomPdf engine",
Expand All @@ -32,8 +33,18 @@
"TestApp\\": "tests/test_app/src/"
}
},
"replace": {
"ceeram/cakepdf": "self.version"
"minimum-stability": "stable",
"scripts": {
"check": [
"@test",
"@cs-check"
],
"cs-check": "phpcs -p --parallel=16",
"cs-fix": "phpcbf -p --parallel=16",
"stan": "phpstan analyse && psalm.phar",
"stan-setup": "cp composer.json composer.backup && composer require --dev phpstan/phpstan:^1.7 psalm/phar:~4.27.0 && mv composer.backup composer.json",
"test": "phpunit",
"test-coverage": "phpunit --coverage-clover=clover.xml"
},
"config": {
"allow-plugins": {
Expand Down
16 changes: 0 additions & 16 deletions config/bootstrap.php

This file was deleted.

6 changes: 6 additions & 0 deletions phpcs.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0"?>
<ruleset name="CakePdf">
<config name="installed_paths" value="../../cakephp/cakephp-codesniffer" />

<rule ref="CakePHP" />
</ruleset>
38 changes: 12 additions & 26 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,27 +1,13 @@
<phpunit
backupGlobals="false"
backupStaticAttributes="false"
bootstrap="./tests/bootstrap.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnError="false"
stopOnFailure="false"
stopOnIncomplete="false"
stopOnSkipped="false"
>

<testsuites>
<testsuite name="CakePdf Plugin Tests">
<directory>./tests/TestCase</directory>
</testsuite>
</testsuites>

<filter>
<whitelist>
<directory suffix=".php">./src/</directory>
</whitelist>
</filter>
<?xml version="1.0"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" backupGlobals="false" bootstrap="./tests/bootstrap.php" colors="true" processIsolation="false" stopOnError="false" stopOnFailure="false" stopOnIncomplete="false" stopOnSkipped="false" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.3/phpunit.xsd" backupStaticProperties="false">
<testsuites>
<testsuite name="CakePdf Plugin Tests">
<directory>./tests/TestCase</directory>
</testsuite>
</testsuites>
<source>
<include>
<directory suffix=".php">./src/</directory>
</include>
</source>
</phpunit>
37 changes: 37 additions & 0 deletions src/CakePdfPlugin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php
declare(strict_types=1);

namespace CakePdf;

use Cake\Core\BasePlugin;

class CakePdfPlugin extends BasePlugin
{
/**
* Do bootstrap or not
*
* @var bool
*/
protected bool $bootstrapEnabled = false;

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

/**
* Enable middleware
*
* @var bool
*/
protected bool $middlewareEnabled = false;

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

0 comments on commit aaf5138

Please sign in to comment.