Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add CI and PHP CS Fixer #10

Merged
merged 3 commits into from
Jan 17, 2024
Merged
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
68 changes: 68 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
name: CI

on:
push:
branches: [ 'main' ]
pull_request: ~

permissions:
contents: read

jobs:
test:
name: "Build and Test - PHP ${{ matrix.php }} Symfony:${{ matrix.symfony-version }} ${{ matrix.deps }}"
runs-on: ubuntu-latest
env:
SYMFONY_DEPRECATIONS_HELPER: max[direct]=0
strategy:
matrix:
include:
- symfony-version: 6.4
php: 8.2
deps: low
- symfony-version: 7.0
php: 8.2
deps: high
- symfony-version: 7.0
php: 8.3
deps: high
steps:
- uses: actions/checkout@v4

- name: Validate composer.json and composer.lock
run: composer validate --strict

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
coverage: none

- name: Install dependencies
if: matrix.deps == 'high'
run: composer install

- name: Install dependencies (min)
if: matrix.deps == 'low'
run: composer update --prefer-lowest --prefer-stable

- name: Run test suite
run: php vendor/bin/phpunit

php-cs-fixer:
name: PHP CS Fixer
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: 8.2
coverage: none

- name: Install dependencies
run: composer install

- name: Run PHP CS Fixer
run: php vendor/bin/php-cs-fixer fix -v --dry-run --stop-on-violation
11 changes: 6 additions & 5 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
/vendor/
composer.lock
.phpunit.result.cache
/var/
/.phpunit.cache/
/tests/fixtures/cache/
/tests/fixtures/var/
/.phpunit.cache/
/var/
/vendor/
.php-cs-fixer.cache
.phpunit.result.cache
composer.lock
32 changes: 32 additions & 0 deletions .php-cs-fixer.dist.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

$finder = (new PhpCsFixer\Finder())
->in(__DIR__)
;

return (new PhpCsFixer\Config())
->setRiskyAllowed(true)
->setRules([
'@Symfony' => true,
'@Symfony:risky' => true,
'array_indentation' => true,
'blank_line_between_import_groups' => false,
'compact_nullable_type_declaration' => true,
'fully_qualified_strict_types' => true,
'native_function_invocation' => true,
'no_superfluous_elseif' => true,
'no_useless_else' => true,
'no_useless_return' => true,
'nullable_type_declaration_for_default_null_value' => false,
'return_assignment' => true,
'strict_param' => true,
'trailing_comma_in_multiline' => [
'elements' => ['arguments', 'arrays', 'match', 'parameters'],
],
'void_return' => true,
'yoda_style' => [
'always_move_variable' => true,
],
])
->setFinder($finder)
;
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@
"symfony/framework-bundle": "^6.4 || ^7.0",
"phpstan/phpstan": "^1.10",
"phpstan/extension-installer": "^1.3",
"phpstan/phpstan-symfony": "^1.3"
"phpstan/phpstan-symfony": "^1.3",
"friendsofphp/php-cs-fixer": "^3.41"
},
"config": {
"allow-plugins": {
Expand Down
4 changes: 2 additions & 2 deletions config/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
use Sensiolabs\GotenbergBundle\Pdf\Gotenberg;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
use function Symfony\Component\DependencyInjection\Loader\Configurator\abstract_arg;
use function Symfony\Component\DependencyInjection\Loader\Configurator\service;
use function Symfony\Component\DependencyInjection\Loader\Configurator\param;
use function Symfony\Component\DependencyInjection\Loader\Configurator\service;

return function(ContainerConfigurator $container): void {
return function (ContainerConfigurator $container): void {
$services = $container->services();

$services->set('sensiolabs_gotenberg', Gotenberg::class)
Expand Down
73 changes: 48 additions & 25 deletions src/Builder/BuilderTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
use Symfony\Component\Mime\Part\DataPart;
use Symfony\Component\Mime\Part\File as DataPartFile;
use function Symfony\Component\String\u;
use const JSON_THROW_ON_ERROR;

/**
* @phpstan-import-type ConfigBuilder from BuilderInterface
*
* @phpstan-type ConfigOptions array{
* 'paper_width'?: float,
* 'paper_height'?: float,
Expand Down Expand Up @@ -48,14 +48,15 @@ public function getMultipartFormData(): array
}

/**
* To set configurations by an array of configurations
* To set configurations by an array of configurations.
*
* @param ConfigOptions $configurations
*/
public function setConfigurations(array $configurations): self
{
foreach ($configurations as $property => $value) {
$method = u($property)->camel()->toString();
if (is_callable([$this, $method])) {
if (\is_callable([$this, $method])) {
$this->{$method}($value);
}
}
Expand All @@ -64,8 +65,10 @@ public function setConfigurations(array $configurations): self
}

/**
* Add a twig template for the header
* Add a twig template for the header.
*
* @see https://gotenberg.dev/docs/routes#header--footer
*
* @param array<string, mixed> $context
*/
public function header(string $path, array $context = []): self
Expand All @@ -74,8 +77,10 @@ public function header(string $path, array $context = []): self
}

/**
* Add a twig template for the footer
* Add a twig template for the footer.
*
* @see https://gotenberg.dev/docs/routes#header--footer
*
* @param array<string, mixed> $context
*/
public function footer(string $path, array $context = []): self
Expand All @@ -84,9 +89,10 @@ public function footer(string $path, array $context = []): self
}

/**
* Add some assets as img, css, js
* Add some assets as img, css, js.
*
* Assets are not loaded in header and footer
*
* @see https://gotenberg.dev/docs/routes#url-into-pdf-route
*/
public function assets(string ...$pathToAssets): self
Expand Down Expand Up @@ -119,6 +125,7 @@ public function assets(string ...$pathToAssets): self
* A4 - 8.27 x 11.7
* A5 - 5.83 x 8.27
* A6 - 4.13 x 5.83
*
* @see https://gotenberg.dev/docs/routes#page-properties-chromium
*/
public function paperSize(float $width, float $height): self
Expand All @@ -131,6 +138,7 @@ public function paperSize(float $width, float $height): self

/**
* Overrides the default margins (e.g., 0.39), in inches.
*
* @see https://gotenberg.dev/docs/routes#page-properties-chromium
*/
public function margins(float $top, float $bottom, float $left, float $right): self
Expand All @@ -144,7 +152,8 @@ public function margins(float $top, float $bottom, float $left, float $right): s
}

/**
* Define whether to prefer page size as defined by CSS. (Default false)
* Define whether to prefer page size as defined by CSS. (Default false).
*
* @see https://gotenberg.dev/docs/routes#page-properties-chromium
*/
public function preferCssPageSize(): self
Expand All @@ -155,7 +164,8 @@ public function preferCssPageSize(): self
}

/**
* Prints the background graphics. (Default false)
* Prints the background graphics. (Default false).
*
* @see https://gotenberg.dev/docs/routes#page-properties-chromium
*/
public function printBackground(): self
Expand All @@ -167,7 +177,8 @@ public function printBackground(): self

/**
* Hides default white background and allows generating PDFs with
* transparency. (Default false)
* transparency. (Default false).
*
* @see https://gotenberg.dev/docs/routes#page-properties-chromium
*/
public function omitBackground(): self
Expand All @@ -178,7 +189,8 @@ public function omitBackground(): self
}

/**
* Sets the paper orientation to landscape. (Default false)
* Sets the paper orientation to landscape. (Default false).
*
* @see https://gotenberg.dev/docs/routes#page-properties-chromium
*/
public function landscape(): self
Expand All @@ -189,7 +201,8 @@ public function landscape(): self
}

/**
* The scale of the page rendering (e.g., 1.0). (Default 1.0)
* The scale of the page rendering (e.g., 1.0). (Default 1.0).
*
* @see https://gotenberg.dev/docs/routes#page-properties-chromium
*/
public function scale(float $scale): self
Expand All @@ -200,7 +213,8 @@ public function scale(float $scale): self
}

/**
* Page ranges to print, e.g., '1-5, 8, 11-13'. (default All pages)
* Page ranges to print, e.g., '1-5, 8, 11-13'. (default All pages).
*
* @see https://gotenberg.dev/docs/routes#page-properties-chromium
*/
public function nativePageRanges(string $range): self
Expand All @@ -212,7 +226,8 @@ public function nativePageRanges(string $range): self

/**
* Sets the duration (i.e., "1s", "2ms", etc.) to wait when loading an HTML
* document before converting it to PDF. (default None)
* document before converting it to PDF. (default None).
*
* @see https://gotenberg.dev/docs/routes#wait-before-rendering
*/
public function waitDelay(string $delay): self
Expand All @@ -224,9 +239,10 @@ public function waitDelay(string $delay): self

/**
* Sets the JavaScript expression to wait before converting an HTML
* document to PDF until it returns true. (default None)
* document to PDF until it returns true. (default None).
*
* For instance: "window.status === 'ready'".
*
* @see https://gotenberg.dev/docs/routes#wait-before-rendering
*/
public function waitForExpression(string $expression): self
Expand All @@ -237,7 +253,8 @@ public function waitForExpression(string $expression): self
}

/**
* Forces Chromium to emulate, either "screen" or "print". (default "print")
* Forces Chromium to emulate, either "screen" or "print". (default "print").
*
* @see https://gotenberg.dev/docs/routes#console-exceptions
*/
public function emulatedMediaType(string $mediaType): self
Expand All @@ -248,7 +265,8 @@ public function emulatedMediaType(string $mediaType): self
}

/**
* Overrides the default "User-Agent" header.(default None)
* Overrides the default "User-Agent" header.(default None).
*
* @see https://gotenberg.dev/docs/routes#custom-http-headers
*/
public function userAgent(string $userAgent): self
Expand All @@ -260,16 +278,18 @@ public function userAgent(string $userAgent): self

/**
* Sets extra HTTP headers that Chromium will send when loading the HTML
* document. (default None)
* document. (default None).
*
* @see https://gotenberg.dev/docs/routes#custom-http-headers
*
* @param array<string, string> $headers
*/
public function extraHttpHeaders(array $headers): self
{
if (0 !== count($headers)) {
$json = json_encode($headers, flags: JSON_THROW_ON_ERROR);
if (0 !== \count($headers)) {
$json = json_encode($headers, flags: \JSON_THROW_ON_ERROR);

if (is_string($json)) {
if (\is_string($json)) {
$this->multipartFormData[] = ['extraHttpHeaders' => $json];
}
}
Expand All @@ -279,7 +299,8 @@ public function extraHttpHeaders(array $headers): self

/**
* Forces Gotenberg to return a 409 Conflict response if there are
* exceptions in the Chromium console. (default false)
* exceptions in the Chromium console. (default false).
*
* @see https://gotenberg.dev/docs/routes#console-exceptions
*/
public function failOnConsoleExceptions(): self
Expand All @@ -290,7 +311,8 @@ public function failOnConsoleExceptions(): self
}

/**
* Sets the PDF format of the resulting PDF. (default None)
* Sets the PDF format of the resulting PDF. (default None).
*
* @See https://gotenberg.dev/docs/routes#pdfa-chromium.
*/
public function pdfFormat(string $format): self
Expand All @@ -301,7 +323,8 @@ public function pdfFormat(string $format): self
}

/**
* Enable PDF for Universal Access for optimal accessibility. (default false)
* Enable PDF for Universal Access for optimal accessibility. (default false).
*
* @See https://gotenberg.dev/docs/routes#pdfa-chromium.
*/
public function pdfUniversalAccess(): self
Expand Down Expand Up @@ -384,11 +407,11 @@ private function fileExtensionChecker(string $filePath, string|array $acceptExte
$file = new File($this->resolveFilePath($filePath));
$extension = $file->getExtension();

if (is_string($acceptExtension)) {
if (\is_string($acceptExtension)) {
$acceptExtension = [$acceptExtension];
}

if (! in_array($extension, $acceptExtension, true)) {
if (!\in_array($extension, $acceptExtension, true)) {
throw new HttpException(400, "The extension file {$extension} is not available in Gotenberg.");
}
}
Expand Down
Loading