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

chore: craft5 support #18

Merged
merged 8 commits into from
Apr 24, 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
37 changes: 37 additions & 0 deletions .github/workflows/code-analysis.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
name: Code Analysis

on:
pull_request:
workflow_dispatch:
permissions:
contents: read
jobs:
code_analysis:
strategy:
fail-fast: false
matrix:
actions:
- name: 'PHPStan'
run: composer phpstan
- name: 'Coding Standards'
run: composer check-cs
name: ${{ matrix.actions.name }}
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Cache Composer dependencies
uses: actions/cache@v4
with:
path: /tmp/composer-cache
key: ${{ runner.os }}-${{ hashFiles('**/composer.lock') }}
- name: Setup PHP
id: setup-php
uses: shivammathur/setup-php@v2
with:
php-version: 8.2
extensions: 'ctype,curl,dom,iconv,imagick,intl,json,mbstring,openssl,pcre,pdo,reflection,spl,zip'
ini-values: post_max_size=256M, max_execution_time=180, memory_limit=512M
tools: composer:v2
- name: Install Composer dependencies
run: composer install --no-interaction --no-ansi --no-progress
- run: ${{ matrix.actions.run }}
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](http://keepachangelog.com/) and this project adheres to [Semantic Versioning](http://semver.org/).

## 5.0.0 - 2024-04-23

### Added
- Craft 5 support

## 3.0.0 - 2022-10-03

### Added
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ To set the header of the file (the first row):
To set the filename:
```twig
{% set currentDate = now|date('Y-m-d') %}
{% do beam.setFilename('report-#{currentDate}') %}
{% do beam.setFilename("report-#{currentDate}") %}
```

To overwrite the content:
Expand Down
26 changes: 22 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "superbig/craft3-beam",
"description": "Generate CSVs and XLS files in your templates",
"type": "craft-plugin",
"version": "3.0.0",
"version": "5.0.0",
"keywords": [
"craft",
"cms",
Expand All @@ -22,9 +22,14 @@
}
],
"require": {
"craftcms/cms": "^4.0.0",
"league/csv": "^8.1|^9.0",
"mk-j/php_xlsxwriter": "^0.38.0"
"craftcms/cms": "^5.0.0",
"league/csv": "^9.0",
"mk-j/php_xlsxwriter": "^0.39.0"
},
"require-dev": {
"craftcms/ecs": "dev-main",
"craftcms/phpstan": "dev-main",
"craftcms/rector": "dev-main"
},
"repositories": [
{
Expand All @@ -37,6 +42,19 @@
"superbig\\beam\\": "src/"
}
},
"scripts": {
"phpstan": "phpstan --ansi --memory-limit=1G",
"check-cs": "ecs check --ansi",
"fix-cs": "ecs check --fix --ansi"
},
"config": {
"allow-plugins": {
"craftcms/plugin-installer": true,
"yiisoft/yii2-composer": true
},
"optimize-autoloader": true,
"sort-packages": true
},
"extra": {
"name": "Beam",
"handle": "beam",
Expand Down
14 changes: 14 additions & 0 deletions ecs.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

use craft\ecs\SetList;
use Symplify\EasyCodingStandard\Config\ECSConfig;

return static function(ECSConfig $ecsConfig): void {
$ecsConfig->paths([
__DIR__ . '/src',
__FILE__,
]);

$ecsConfig->parallel();
$ecsConfig->sets([SetList::CRAFT_CMS_4]);
};
6 changes: 6 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
includes:
- %currentWorkingDirectory%/vendor/craftcms/phpstan/phpstan.neon
parameters:
level: 5
paths:
- src
18 changes: 8 additions & 10 deletions src/Beam.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,14 @@

namespace superbig\beam;

use superbig\beam\services\BeamService as BeamServiceService;
use superbig\beam\variables\BeamVariable;

use Craft;
use craft\base\Plugin;
use craft\services\Plugins;
use craft\events\PluginEvent;
use craft\web\UrlManager;
use craft\web\twig\variables\CraftVariable;

use craft\events\RegisterUrlRulesEvent;
use craft\web\twig\variables\CraftVariable;
use craft\web\UrlManager;
use superbig\beam\services\BeamService as BeamServiceService;
use superbig\beam\variables\BeamVariable;

use yii\base\Event;

Expand Down Expand Up @@ -56,23 +54,23 @@ public function init()
Event::on(
UrlManager::class,
UrlManager::EVENT_REGISTER_SITE_URL_RULES,
function (RegisterUrlRulesEvent $event) {
function(RegisterUrlRulesEvent $event) {
$event->rules['beam/download'] = 'beam/default';
}
);

Event::on(
UrlManager::class,
UrlManager::EVENT_REGISTER_CP_URL_RULES,
function (RegisterUrlRulesEvent $event) {
function(RegisterUrlRulesEvent $event) {
$event->rules['beam/download'] = 'beam/default';
}
);

Event::on(
CraftVariable::class,
CraftVariable::EVENT_INIT,
function (Event $event) {
function(Event $event) {
/** @var CraftVariable $variable */
$variable = $event->sender;
$variable->set('beam', BeamVariable::class);
Expand Down
20 changes: 4 additions & 16 deletions src/controllers/DefaultController.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@

namespace superbig\beam\controllers;

use superbig\beam\Beam;

use Craft;

use craft\web\Controller;
use superbig\beam\Beam;
use yii\web\NotFoundHttpException;

/**
Expand All @@ -23,20 +23,8 @@
*/
class DefaultController extends Controller
{

// Protected Properties
// =========================================================================

/**
* @var bool|array Allows anonymous access to this controller's actions.
* The actions must be in 'kebab-case'
* @access protected
*/
protected array|int|bool $allowAnonymous = ['index'];

// Public Methods
// =========================================================================

/**
* @return mixed
* @throws NotFoundHttpException
Expand All @@ -45,15 +33,15 @@ class DefaultController extends Controller
public function actionIndex()
{
$request = Craft::$app->getRequest();
$hash = $request->getRequiredParam('hash');
$hash = $request->getRequiredParam('hash');

$config = Beam::$plugin->beamService->downloadHash($hash);

if (!$config) {
throw new NotFoundHttpException();
}

$path = $config['path'];
$path = $config['path'];
$filename = $config['filename'];

return Craft::$app->getResponse()->sendFile($path, $filename, [
Expand Down
70 changes: 18 additions & 52 deletions src/models/BeamModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,38 +10,22 @@

namespace superbig\beam\models;

use superbig\beam\Beam;

use Craft;
use craft\base\Model;

use superbig\beam\Beam;

/**
* @author Superbig
* @package Beam
* @since 2.0.0
*/
class BeamModel extends Model
{
// Public Properties
// =========================================================================

/** @var array */
public $header = [];

/** @var array */
public $content = [];

/** @var array */
public $rows = [];

/** @var string */
public $filename = 'output';

/** @var string */
public $sheetName = 'Sheet';

// Public Methods
// =========================================================================
public array $header = [];
public array $content = [];
public array $rows = [];
public string $filename = 'output';
public string $sheetName = 'Sheet';

public function init(): void
{
Expand All @@ -52,12 +36,7 @@ public function init(): void
}
}

/**
* @param array $content
*
* @return $this
*/
public function append(array $content = [])
public function append(array $content = []): static
{
if (isset($content[0]) && !\is_array($content[0])) {
$content = [$content];
Expand All @@ -68,35 +47,30 @@ public function append(array $content = [])
return $this;
}

public function setHeader($headers = [])
public function setHeader(array $headers = []): static
{
$this->header = $headers;

return $this;
}

public function getContent()
public function getContent(): array
{
return $this->content;
}

/**
* @param $content
*
* @return $this
*/
public function setContent($content)
public function setContent(array $content): static
{
$this->content = $content;

return $this;
}

public function getConfig()
public function getConfig(): array
{
return [
'header' => $this->header,
'rows' => $this->content,
'rows' => $this->content,
];
}

Expand All @@ -107,39 +81,31 @@ public function getFilename($ext = null)
return "$filename.$ext";
}

public function setFilename($filename = null)
public function setFilename($filename = null): static
{
$this->filename = $filename;

return $this;
}

public function csv($filename = null)
public function csv($filename = null): void
{
if ($filename) {
$this->filename = $filename;
}

return Beam::$plugin->beamService->csv($this);
Beam::$plugin->beamService->csv($this);
}

public function xlsx($filename = null)
public function xlsx($filename = null): void
{
if ($filename) {
$this->filename = $filename;
}

return Beam::$plugin->beamService->xlsx($this);
}

public function html()
{

Beam::$plugin->beamService->xlsx($this);
}

/**
* @inheritdoc
*/
public function rules(): array
{
return [
Expand Down
Loading