Skip to content

Commit

Permalink
Merge pull request #2 from justbetter/feature/actions
Browse files Browse the repository at this point in the history
Added actions for phpstan, pint and changelogger
  • Loading branch information
BobWez98 authored Jun 20, 2024
2 parents 8d5f7c0 + ca24099 commit 5728067
Show file tree
Hide file tree
Showing 8 changed files with 125 additions and 20 deletions.
24 changes: 24 additions & 0 deletions .github/workflows/analyse.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
name: analyse

on: ['push', 'pull_request']

jobs:
analyse:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: 8.3
extensions: dom, curl, libxml, mbstring, zip, pdo, sqlite, pdo_sqlite, bcmath, soap, intl, gd, exif, iconv, imagick, fileinfo
coverage: none

- name: Install dependencies
run: |
composer require "laravel/framework:10.*" "orchestra/testbench:8.*" --no-interaction --no-update
composer update --prefer-stable --prefer-dist --no-interaction
- name: Analyse
run: composer analyse
29 changes: 29 additions & 0 deletions .github/workflows/changelog.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
name: "Update Changelog"

on:
release:
types: [ published, edited, deleted ]

jobs:
generate:
runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v3
with:
ref: ${{ github.event.release.target_commitish }}

- name: Generate changelog
uses: justbetter/generate-changelogs-action@main
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
repository: ${{ github.repository }}

- name: Commit CHANGELOG
uses: stefanzweifel/git-auto-commit-action@v4
with:
branch: ${{ github.event.release.target_commitish }}
commit_message: Update CHANGELOG
file_pattern: CHANGELOG.md
32 changes: 32 additions & 0 deletions .github/workflows/style.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
name: style

on:
push:
branches:
- master
jobs:
style:
name: Style
runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v4

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: 8.3
extensions: dom, curl, libxml, mbstring, zip, pdo, sqlite, pdo_sqlite, bcmath, soap, intl, gd, exif, iconv, imagick, fileinfo
coverage: none

- name: Install dependencies
run: composer install

- name: Style
run: composer fix-style

- name: Commit Changes
uses: stefanzweifel/git-auto-commit-action@v4
with:
commit_message: Fix styling changes
15 changes: 13 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,16 @@
"description": "Addon that adds a glide blade directive",
"type": "package",
"require": {
"php": "^8.0|^8.1",
"laravel/framework": "*",
"php": "^8.2|^8.3",
"laravel/framework": "^10.0",
"statamic/cms": "^4.0"
},
"require-dev": {
"laravel/pint": "^1.7",
"larastan/larastan": "^2.5",
"phpstan/phpstan-mockery": "^1.1",
"orchestra/testbench": "^8.0|^9.0"
},
"autoload": {
"psr-4": {
"JustBetter\\GlideDirective\\": "src/"
Expand Down Expand Up @@ -41,5 +47,10 @@
"role": "Developer"
}
],
"scripts": {
"analyse": "phpstan",
"fix-style": "pint",
"style": "pint --test"
},
"minimum-stability": "dev"
}
2 changes: 1 addition & 1 deletion config/glide-directive.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,5 @@
// Set 'webp' for WebP only.
// Set 'mime_type' for the original image mime type.
// Set 'both' to use both sources.
'sources' => 'webp'
'sources' => 'webp',
];
9 changes: 9 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
includes:
- ./vendor/larastan/larastan/extension.neon
- ./vendor/phpstan/phpstan-mockery/extension.neon

parameters:
paths:
- src
level: 8
checkMissingIterableValueType: false
25 changes: 13 additions & 12 deletions src/Responsive.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@

namespace JustBetter\GlideDirective;

use Illuminate\Contracts\View\Factory;
use Illuminate\Contracts\View\View;
use Statamic\Assets\Asset;
use Statamic\Statamic;

class Responsive
{
public static function handle(...$arguments)
public static function handle(mixed ...$arguments): Factory|View
{
$image = $arguments[0];
$image = get_class($image) === 'Statamic\Fields\Value' ? $image->value() : $image;
Expand All @@ -24,11 +26,11 @@ public static function handle(...$arguments)
]);
}

public static function getPresets(Asset $image)
public static function getPresets(Asset $image): array
{
$config = config('statamic.assets.image_manipulation.presets');

if (!config('justbetter.glide-directive.placeholder') && isset($config['placeholder'])) {
if (! config('justbetter.glide-directive.placeholder') && isset($config['placeholder'])) {
unset($config['placeholder']);
}

Expand All @@ -46,7 +48,7 @@ public static function getPresets(Asset $image)

$index = 0;
foreach ($configPresets as $preset => $data) {
$size = $data['w'] . 'w';
$size = $data['w'].'w';

if ($index < (count($configPresets) - 1)) {
$size .= ', ';
Expand All @@ -55,18 +57,17 @@ public static function getPresets(Asset $image)
if (self::canUseWebpSource()) {
$glideUrl = Statamic::tag($preset === 'placeholder' ? 'glide:data_url' : 'glide')->params(['preset' => $preset, 'src' => $image->url(), 'format' => 'webp', 'fit' => $data['crop'] ?? 'crop_focal'])->fetch();
if ($glideUrl) {
$presets['webp'] .= $glideUrl . ' ' . $size;
$presets['webp'] .= $glideUrl.' '.$size;
}
}

if (self::canUseMimeTypeSource()) {
$glideUrl = Statamic::tag($preset === 'placeholder' ? 'glide:data_url' : 'glide')->params(['preset' => $preset, 'src' => $image->url(), 'fit' => $data['crop'] ?? 'crop_focal'])->fetch();
if ($glideUrl) {
$presets[$image->mimeType()] .= $glideUrl . ' ' . $size;
$presets[$image->mimeType()] .= $glideUrl.' '.$size;
}
}


if ($preset === 'placeholder') {
$glideUrl = Statamic::tag('glide:data_url')->params(['preset' => 'placeholder', 'src' => $image->url(), 'fit' => $data['crop'] ?? 'crop_focal'])->fetch();
if ($glideUrl) {
Expand All @@ -77,7 +78,7 @@ public static function getPresets(Asset $image)
$index++;
}

if (!isset($presets['placeholder'])) {
if (! isset($presets['placeholder'])) {
$glideUrl = Statamic::tag('glide:data_url')->params(['preset' => collect($configPresets)->keys()->first(), 'src' => $image->url(), 'fit' => 'crop_focal'])->fetch();
$presets['placeholder'] = $glideUrl;
}
Expand All @@ -93,12 +94,12 @@ protected static function getPresetsByRatio(Asset $image, array $config): array
// filter config based on aspect ratio
// if ratio < 1 get all presets with a height bigger than the width, else get all presets with width equal or bigger than the height.
if ($ratio < 0.999) {
$presets = $presets->filter(fn($preset, $key) => $preset['h'] > $preset['w']);
$presets = $presets->filter(fn ($preset, $key) => $preset['h'] > $preset['w']);
if ($presets->isNotEmpty() && isset($config['placeholder'])) {
$presets->prepend($config['placeholder'], 'placeholder');
}
} else {
$presets = $presets->filter(fn($preset, $key) => $key === 'placeholder' || $preset['w'] >= $preset['h']);
$presets = $presets->filter(fn ($preset, $key) => $key === 'placeholder' || $preset['w'] >= $preset['h']);
}

return $presets->isNotEmpty() ? $presets->toArray() : $config;
Expand All @@ -109,9 +110,9 @@ protected static function getAttributeBag(array $arguments): string
$excludedAttributes = ['src', 'class', 'alt', 'width', 'height', 'onload'];

return collect($arguments)
->filter(fn ($value, $key) => !in_array($key, $excludedAttributes))
->filter(fn ($value, $key) => ! in_array($key, $excludedAttributes))
->map(function ($value, $key) {
return $key . '="' . $value . '"';
return $key.'="'.$value.'"';
})->implode(' ');
}

Expand Down
9 changes: 4 additions & 5 deletions src/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,12 @@ public function boot(): void

protected function bootConfig(): static
{
$this->mergeConfigFrom(__DIR__ . '/../config/glide-directive.php', 'justbetter.glide-directive');
$this->mergeConfigFrom(__DIR__.'/../config/glide-directive.php', 'justbetter.glide-directive');

$this->publishes([
__DIR__ . '/../config/glide-directive.php' => config_path('glide-directive.php'),
__DIR__.'/../config/glide-directive.php' => config_path('glide-directive.php'),
], 'config');


if (empty(config('statamic.assets.image_manipulation.presets'))) {
config()->set('statamic.assets.image_manipulation.presets', config('justbetter.glide-directive.presets'));
}
Expand All @@ -43,7 +42,7 @@ protected function bootDirectives(): static

protected function bootViews(): static
{
$this->loadViewsFrom(__DIR__ . '/../resources/views', 'statamic-glide-directive');
$this->loadViewsFrom(__DIR__.'/../resources/views', 'statamic-glide-directive');

return $this;
}
Expand All @@ -54,4 +53,4 @@ protected function bootClasses(): static

return $this;
}
}
}

0 comments on commit 5728067

Please sign in to comment.