Skip to content

Commit

Permalink
Merge pull request #7 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 dbc1894 + 0edcb39 commit 9217c48
Show file tree
Hide file tree
Showing 14 changed files with 88 additions and 42 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ jobs:
fail-fast: true
matrix:
os: [ubuntu-latest]
php: [7.4, 8.0]
php: [8.0, 8.1]
stability: [prefer-stable]

name: P${{ matrix.php }}
Expand Down
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

All notable changes will be documented in this file.

## 2.0.0 - 2022-05-19

- Craft 4 support
- Feed Me plugin support
- Bulletproof database migration

## 1.0.1 - 2022-01-18

- Fixed phpstan (1.4) level 6
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ php craft resave/matrix-blocks --update-search-index
composer test
```


## Changelog

Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.
Expand Down
13 changes: 8 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,18 @@
"docs": "https://github.com/ostark/craft-relax/blob/main/README.md"
},
"require": {
"php": "^7.4 | ^8.0",
"craftcms/cms": "^3.7.20",
"php": "^7.4||^8.0",
"craftcms/cms": "^4.0.0",
"treeware/plant": "*",
"yiisoft/yii2-queue": "^2.3.3"
},
"require-dev": {
"craftcms/phpstan": "*",
"craftcms/rector": "dev-main",
"friendsofphp/php-cs-fixer": "^3.0",
"nunomaduro/collision": "^5.10",
"phpstan/phpstan": "^1.0",
"pestphp/pest": "^1.2",
"friendsofphp/php-cs-fixer": "^3.0"
"pestphp/pest-plugin-parallel": "^1.0"
},
"autoload": {
"psr-4": {
Expand Down Expand Up @@ -62,5 +64,6 @@
"changelogUrl": "https://raw.githubusercontent.com/ostark/craft-relax/main/CHANGELOG.md",
"class": "ostark\\Relax\\Plugin"
},
"prefer-stable": true
"prefer-stable": true,
"minimum-stability": "dev"
}
11 changes: 5 additions & 6 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
includes:
- vendor/craftcms/phpstan/phpstan.neon
parameters:
level: 6
paths:
- src
scanFiles:
- vendor/yiisoft/yii2/Yii.php
- vendor/craftcms/cms/src/Craft.php
stubFiles:
- vendor/craftcms/cms/stubs/BaseYii.stub
- tests
tmpDir: build/phpstan
checkMissingIterableValueType: false

ignoreErrors:
- "#Unable to resolve the template type T in call to method static method#"
6 changes: 6 additions & 0 deletions src/Handlers/QueueServiceHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,11 @@ public function __invoke(): void
if (isset(Craft::$app->controllerMap['queue']['queue'])) {
Craft::$app->controllerMap['queue']['queue'] = $queue;
}

// Extrawurst for feed-me
if ($feedMe = Craft::$app->getPlugins()->getPlugin('feed-me')) {
/** @phpstan-ignore-next-line */
$feedMe->queue = $queue;
};
}
}
6 changes: 3 additions & 3 deletions src/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@
*/
final class Plugin extends BasePlugin
{
public $schemaVersion = '1.0.0';
public $hasCpSettings = false;
public $hasCpSection = false;
public string $schemaVersion = '1.0.0';
public bool $hasCpSettings = false;
public bool $hasCpSection = false;

public function init(): void
{
Expand Down
15 changes: 9 additions & 6 deletions src/Queue/DefaultHasher.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@

class DefaultHasher implements Hasher
{
public int $seconds = 600;
public int $precisionInMinutes = 10;

public SerializerInterface $serializer;

public function __construct(SerializerInterface $serializer, int $seconds = 600)
public function __construct(SerializerInterface $serializer, int $minutes = 10)
{
$this->serializer = $serializer;
$this->seconds = $seconds;
$this->precisionInMinutes = $minutes;
}

/**
Expand All @@ -27,11 +27,14 @@ public function hash($job): string
{
$hash = md5($this->serializer->serialize($job));

return sprintf("%s__%d", $hash, $this->roundedTimestamp());
return sprintf("%s__%s", $hash, $this->timeSuffix());
}

private function roundedTimestamp(): int
private function timeSuffix(string $format = 'Ymd.His'): string
{
return (int) round(time() / $this->seconds) * $this->seconds;
$precision = $this->precisionInMinutes * 60;
$rounded = (int) ceil(time() / $precision) * $precision;

return date($format, $rounded);
}
}
8 changes: 4 additions & 4 deletions src/Queue/HashedJobQueue.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,10 @@ public function push($job): ?string
* @param int $delay
* @param mixed $priority
*
* @return string|null
* @return string
* @throws \yii\db\Exception
*/
protected function pushMessage($message, $ttr, $delay, $priority): ?string
protected function pushMessage($message, $ttr, $delay, $priority): string
{
$hash = $this->hasher->hash($message);
$found = (new Query())
Expand All @@ -65,7 +65,7 @@ protected function pushMessage($message, $ttr, $delay, $priority): ?string
->count('*', $this->db);

if ($found) {
return null;
return '1';
}

$data = [
Expand All @@ -78,7 +78,7 @@ protected function pushMessage($message, $ttr, $delay, $priority): ?string
self::HASH_COLUMN => $hash,
];

Db::insert($this->tableName, $data, false, $this->db);
Db::insert($this->tableName, $data, $this->db);

return $this->db->getLastInsertID($this->tableName);
}
Expand Down
4 changes: 2 additions & 2 deletions src/SearchIndex/SearchIndexCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@ class SearchIndexCommand extends Command
*/
public array $filters = [];

public function insert($table, $columns, $includeAuditColumns = true)
public function insert($table, $columns): Command
{
if ($table === Table::SEARCHINDEX) {
if ($this->applyFilters($columns)) {
return $this;
}
}

return parent::insert($table, $columns, $includeAuditColumns);
return parent::insert($table, $columns);
}

protected function applyFilters(array $columns): bool
Expand Down
3 changes: 2 additions & 1 deletion src/migrations/Install.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public function safeUp(): bool
{
// Same schema as the `queue` table, but with an additional `job_hash` column
// to prevent duplicated queue messages
$this->dropTableIfExists(HashedJobQueue::TABLE);
$this->createTable(HashedJobQueue::TABLE, [
'id' => $this->primaryKey(),
'channel' => $this->string()->notNull()->defaultValue('queue'),
Expand Down Expand Up @@ -43,7 +44,7 @@ public function safeUp(): bool

public function safeDown(): bool
{
$this->dropTable(HashedJobQueue::TABLE);
$this->dropTableIfExists(HashedJobQueue::TABLE);

return true;
}
Expand Down
7 changes: 0 additions & 7 deletions tests/ExampleTest.php

This file was deleted.

13 changes: 6 additions & 7 deletions tests/Pest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,6 @@
|
*/

expect()->extend('toBeOne', function () {
return $this->toBe(1);
});

/*
|--------------------------------------------------------------------------
Expand All @@ -41,9 +38,11 @@
|
*/

function mockCraftApp()
function disableCustomFields(): void
{
// $mockApp = \craft\test\TestSetup::getMockApp($test);
// \Craft::$app = $mockApp;
// \Yii::$app = $mockApp;
$class = '\craft\behaviors\CustomFieldBehavior';
if (Yii::$container->has($class)) {
spl_autoload_unregister([Craft::class, 'autoload']);
Yii::$container->set($class, new \yii\base\Behavior());
}
}
35 changes: 35 additions & 0 deletions tests/Unit/DefaultHasherTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

declare(strict_types=1);

it('creates a hash from a job object with a time formatted suffix', function () {
$serializer = new \yii\queue\serializers\PhpSerializer();
$hasher = new \ostark\Relax\Queue\DefaultHasher($serializer);

$separator = '__';
$result = $hasher->hash(new DummyJob());
[$md5, $suffix] = explode($separator, $result, 2);

// Not more than 64 chars to fit in the db column
expect(mb_strlen($result))->toBeLessThanOrEqual(64);
expect(mb_strlen($result))->toBeGreaterThan(40);

// Expected format: {md5}__{Ymd.His}
expect($md5)->toHaveLength(32);
expect($suffix)->toEndWith('00');
});


class DummyJob implements \craft\queue\JobInterface
{
public int $prop = 1;

public function getDescription(): string
{
return 'dummy';
}

public function execute($queue): void
{
}
}

0 comments on commit 9217c48

Please sign in to comment.