Skip to content

Commit

Permalink
Merge pull request #54 from ostark/feature/craft4
Browse files Browse the repository at this point in the history
Feature/craft4
  • Loading branch information
Oliver Stark authored May 19, 2022
2 parents 7839e42 + 1889037 commit abab77c
Show file tree
Hide file tree
Showing 17 changed files with 127 additions and 58 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

All notable changes to this project will be documented in this file.

## [3.0.0] - 2022-05-12
- Craft 4 support
- PHP 8 syntax

## [2.3.0] - 2021-05-04
### Changed
- Added dependency `treeware/plant`
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ This package is [Treeware](https://treeware.earth). If you use it in production,

## Requirements

* Craft 3
* Craft 3 or 4
* Permissions to execute a php binary
* proc_open()
* **PHP >=7.1** (for PHP 7.0 use `ostark/craft-async-queue:1.3.*`)
Expand Down
24 changes: 17 additions & 7 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@
}
],
"require": {
"php": ">=7.1",
"craftcms/cms": "^3.0.0",
"symfony/process": "^4.2|^5.0",
"php": "^8.0",
"craftcms/cms": "^4.0.0",
"symfony/process": "^5.0",
"treeware/plant": "^0.1.0"
},
"autoload": {
Expand All @@ -40,11 +40,21 @@
"changelogUrl": "https://raw.githubusercontent.com/ostark/craft-async-queue/master/CHANGELOG.md"
},
"require-dev": {
"phpunit/phpunit": "^7.0|^8.0",
"vimeo/psalm": "^4.4"
"phpunit/phpunit": "^8.0|^9.0",
"craftcms/phpstan": "*",
"craftcms/rector": "dev-main"
},
"scripts": {
"psalm": "vendor/bin/psalm",
"phpstan": "vendor/bin/phpstan",
"tests": "vendor/bin/phpunit --testdox"
}
},
"config": {
"allow-plugins": {
"yiisoft/yii2-composer": true,
"craftcms/plugin-installer": true,
"treeware/plant": true
}
},
"prefer-stable": true,
"minimum-stability": "dev"
}
13 changes: 13 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
includes:
- vendor/craftcms/phpstan/phpstan.neon
parameters:
level: 6
bootstrapFiles:
- tests/bootstrap.php
paths:
- src
- tests
tmpDir: build/phpstan
checkMissingIterableValueType: false
ignoreErrors:
- "#Negated boolean expression is always false#"
53 changes: 53 additions & 0 deletions rector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

declare(strict_types=1);

use craft\rector\SetList as CraftSetList;
use Rector\Config\RectorConfig;
use Rector\Core\ValueObject\PhpVersion;
use Rector\PHPUnit\Set\PHPUnitSetList;
use Rector\Renaming\Rector\Name\RenameClassRector;
use Rector\Set\ValueObject\LevelSetList;
use Rector\Set\ValueObject\SetList;

return static function (RectorConfig $rectorConfig): void {

// PHP 8.0
$rectorConfig->phpVersion(PhpVersion::PHP_80);
$rectorConfig->import(LevelSetList::UP_TO_PHP_80);

// Misc
$rectorConfig->import(CraftSetList::CRAFT_CMS_40);
$rectorConfig->import(SetList::DEAD_CODE);
$rectorConfig->import(SetList::TYPE_DECLARATION);
$rectorConfig->import(SetList::CODE_QUALITY);
$rectorConfig->import(SetList::CODING_STYLE);
$rectorConfig->import(PHPUnitSetList::PHPUNIT_90);

$rectorConfig->skip([
\Rector\CodingStyle\Rector\Encapsed\EncapsedStringsToSprintfRector::class,
\Rector\CodingStyle\Rector\ClassConst\RemoveFinalFromConstRector::class,
\Rector\CodeQuality\Rector\If_\SimplifyIfReturnBoolRector::class,
\Rector\CodeQuality\Rector\Ternary\UnnecessaryTernaryExpressionRector::class,
\Rector\CodeQuality\Rector\If_\ExplicitBoolCompareRector::class,
\Rector\CodingStyle\Rector\Encapsed\EncapsedStringsToSprintfRector::class,
\Rector\CodingStyle\Rector\ClassConst\RemoveFinalFromConstRector::class,
\Rector\CodingStyle\Rector\PostInc\PostIncDecToPreIncDecRector::class,
\Rector\CodingStyle\Rector\ClassMethod\UnSpreadOperatorRector::class,
\Rector\CodeQuality\Rector\If_\SimplifyIfReturnBoolRector::class,
\Rector\CodeQuality\Rector\Ternary\UnnecessaryTernaryExpressionRector::class,
\Rector\CodingStyle\Rector\ClassConst\VarConstantCommentRector::class,
\Rector\CodeQuality\Rector\If_\ExplicitBoolCompareRector::class,
\Rector\Php74\Rector\FuncCall\ArraySpreadInsteadOfArrayMergeRector::class,
\Rector\Php73\Rector\FuncCall\JsonThrowOnErrorRector::class,
\Rector\Php80\Rector\Class_\ClassPropertyAssignToConstructorPromotionRector::class,
\Rector\TypeDeclaration\Rector\FunctionLike\ReturnTypeDeclarationRector::class
]);


};





16 changes: 8 additions & 8 deletions src/BackgroundProcess.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,14 @@ public function start()

try {
$process->run();
} catch (\Symfony\Component\Process\Exception\RuntimeException $e) {
$e = new RuntimeException($e->getMessage());
$e->setProcess($process);
throw $e;
} catch (\Symfony\Component\Process\Exception\LogicException $e) {
$e = new LogicException($e->getMessage());
$e->setProcess($process);
throw $e;
} catch (\Symfony\Component\Process\Exception\RuntimeException $runtimeException) {
$runtimeException = new RuntimeException($runtimeException->getMessage());
$runtimeException->setProcess($process);
throw $runtimeException;
} catch (\Symfony\Component\Process\Exception\LogicException $logicException) {
$logicException = new LogicException($logicException->getMessage());
$logicException->setProcess($process);
throw $logicException;
}

return $process;
Expand Down
2 changes: 1 addition & 1 deletion src/Exceptions/LogicException.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

class LogicException extends \LogicException implements ProcessException
{
protected $process;
protected Process $process;

public function setProcess(Process $process): void
{
Expand Down
7 changes: 3 additions & 4 deletions src/Handlers/BackgroundQueueHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,13 @@ class BackgroundQueueHandler

/**
* BackgroundQueue constructor.
*
* @param \ostark\AsyncQueue\Plugin $plugin
*/
public function __construct(Plugin $plugin)
{
$this->plugin = $plugin;
}

public function __invoke(PushEvent $event)
public function __invoke(PushEvent $event): void
{
$context = ($event->job instanceof JobInterface)
? $event->job->getDescription()
Expand All @@ -39,7 +37,7 @@ public function __invoke(PushEvent $event)
$this->plugin->getRateLimiter()->increment();
$handled = true;

} catch (PhpExecutableNotFound $e) {
} catch (PhpExecutableNotFound) {
Craft::debug(
'QueueHandler::startBackgroundProcess() (PhpExecutableNotFound)',
'async-queue'
Expand Down Expand Up @@ -69,6 +67,7 @@ protected function logPushEvent(PushEvent $event, bool $handled = false): void
if (!YII_DEBUG) {
return;
}

if ($event->job instanceof BaseJob) {
Craft::debug(
Craft::t(
Expand Down
2 changes: 1 addition & 1 deletion src/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public function getRateLimiter(): RateLimiter
/**
* Creates and returns the model used to store the plugin’s settings.
*/
protected function createSettingsModel() : Settings
protected function createSettingsModel() : ?\craft\base\Model
{
return new Settings();
}
Expand Down
25 changes: 11 additions & 14 deletions src/QueueCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,21 @@

class QueueCommand extends Component
{
const DEFAULT_SCRIPT = "craft";
const DEFAULT_ARGS = "queue/run";
const EVENT_PREPARE_COMMAND = 'prepareCommand';
public const DEFAULT_SCRIPT = "craft";

protected $scriptName;
protected $scriptArgs;
public const DEFAULT_ARGS = "queue/run";

public const EVENT_PREPARE_COMMAND = 'prepareCommand';

protected string $scriptName;

protected string $scriptArgs;

/**
* QueueCommand constructor.
*
* @param string|null $scriptName
* @param string|null $scriptArgs
* @param array $config
*/
public function __construct(string $scriptName = null, string $scriptArgs = null, array $config = [])
{
Expand All @@ -32,7 +34,6 @@ public function __construct(string $scriptName = null, string $scriptArgs = null
/**
* @param callable|null $wrapper
*
* @return string
* @throws \ostark\AsyncQueue\Exceptions\PhpExecutableNotFound
*/
public function getPreparedCommand(callable $wrapper = null): string
Expand All @@ -44,18 +45,14 @@ public function getPreparedCommand(callable $wrapper = null): string
throw new PhpExecutableNotFound('Unable to find php executable.');
}

$commandLine = join(" ", [$php, $this->scriptName, $this->scriptArgs]);
$commandLine = implode(" ", [$php, $this->scriptName, $this->scriptArgs]);

return $this->decorate($commandLine);
}


/**
* Wrapper
*
* @param string $commandLine
*
* @return string
*/
protected function decorate(string $commandLine): string
{
Expand All @@ -64,12 +61,12 @@ protected function decorate(string $commandLine): string
$this->trigger(self::EVENT_PREPARE_COMMAND, $event);
$commandLine = $event->commandLine;

if ($event->useDefaultDecoration === false) {
if (!$event->useDefaultDecoration) {
return $commandLine;
}

// default decoration
return "nice $commandLine > /dev/null 2>&1 &";
return "nice -n 15 {$commandLine} > /dev/null 2>&1 &";
}

}
2 changes: 1 addition & 1 deletion src/RateLimiter.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public function canIUse(string $context = null): bool
{
try {
$reserved = $this->queue->getTotalReserved();
} catch (\Exception $exception) {
} catch (\Exception) {
$reserved = 0;
}

Expand Down
6 changes: 1 addition & 5 deletions src/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ class Settings extends Model

/**
* Settings constructor.
*
* @param array $config
*/
public function __construct(array $config = [])
{
Expand All @@ -42,12 +40,10 @@ public function __construct(array $config = [])
/**
* Env var access with default
*
* @param string $name
* @param mixed $default
*
* @return false|string
*/
protected function env($name, $default = null)
protected function env(string $name, $default = null): false|string
{
if (getenv($name) === false) {
return (string)$default;
Expand Down
6 changes: 3 additions & 3 deletions src/TestUtility/TestJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,18 @@
class TestJob extends BaseJob
{

public $counter = 1;
public int $counter = 1;

/**
* @param \yii\queue\Queue|QueueInterface $queue The queue the job belongs to
*/
public function execute($queue)
public function execute($queue): void
{
sleep(10);
}


public function defaultDescription()
protected function defaultDescription(): ?string
{
return 'Async Queue Test Job ' . $this->counter;
}
Expand Down
10 changes: 3 additions & 7 deletions src/TestUtility/Utility.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ class Utility extends \craft\base\Utility

/**
* Returns the utility’s unique identifier.
*
* @return string
*/
public static function id(): string
{
Expand All @@ -30,15 +28,13 @@ public static function displayName(): string
}


public static function iconPath(): string
public static function iconPath(): ?string
{
return __DIR__ . '/icon.svg';
}

/**
* Returns the utility's content HTML.
*
* @return string
*/
public static function contentHtml(): string
{
Expand Down Expand Up @@ -68,12 +64,12 @@ public static function setup(Plugin $plugin): void
// Register the Utility
Event::on(
Utilities::class,
Utilities::EVENT_REGISTER_UTILITY_TYPES, function (RegisterComponentTypesEvent $event) {
Utilities::EVENT_REGISTER_UTILITY_TYPES, function (RegisterComponentTypesEvent $event): void {
$event->types[] = Utility::class;
});

// Tune the template path
Event::on(View::class, View::EVENT_REGISTER_CP_TEMPLATE_ROOTS, function (RegisterTemplateRootsEvent $e) use ($plugin) {
Event::on(View::class, View::EVENT_REGISTER_CP_TEMPLATE_ROOTS, function (RegisterTemplateRootsEvent $e) use ($plugin): void {
$e->roots[$plugin->getHandle()] = __DIR__;
});

Expand Down
2 changes: 1 addition & 1 deletion tests/BackgroundProcessRunTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public function tearDown(): void
/**
* @covers \ostark\AsyncQueue\BackgroundProcess::start
*/
public function test_start_default_dummy_script_success()
public function test_start_default_dummy_script_success(): void
{
$command = new \ostark\AsyncQueue\QueueCommand('craft.php', 'queue/run');
$bgProcess = new BackgroundProcess($command);
Expand Down
Loading

0 comments on commit abab77c

Please sign in to comment.