Skip to content
Open
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
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ permissions:

jobs:
testsuite:
uses: cakephp/.github/.github/workflows/testsuite-with-db.yml@5.x
uses: cakephp/.github/.github/workflows/testsuite-with-db.yml@6.x
secrets: inherit

cs-stan:
uses: cakephp/.github/.github/workflows/cs-stan.yml@5.x
uses: cakephp/.github/.github/workflows/cs-stan.yml@6.x
secrets: inherit
2 changes: 1 addition & 1 deletion .phive/phars.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<phive xmlns="https://phar.io/phive">
<phar name="phpstan" version="2.1.55" installed="2.1.55" location="./tools/phpstan" copy="false"/>
<phar name="phpstan" version="2.3.6" location="./tools/phpstan" copy="false" installed="2.3.6"/>
</phive>
25 changes: 16 additions & 9 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,23 @@
"source": "https://github.com/cakephp/queue"
},
"require": {
"php": ">=8.2",
"cakephp/cakephp": "^5.4.0",
"enqueue/simple-client": "^0.10",
"php": ">=8.5",
"cakephp/cakephp": "6.x-dev",
"enqueue/simple-client": "^0.10.27",
"enqueue/dsn": "^0.10.27",
"enqueue/null": "^0.10.27",
"psr/log": "^3.0",
"ramsey/uuid": "^4.7.0"
"queue-interop/amqp-interop": "^0.8.2",
"queue-interop/queue-interop": "^0.8.2",
"ramsey/uuid": "^4.7.0",
"symfony/config": "^6.4.3 || ^7.0.3 || ^8.0"
},
"require-dev": {
"cakephp/bake": "^3.5.1",
"cakephp/cakephp-codesniffer": "^5.3",
"cakephp/bake": "4.x-dev",
"cakephp/cakephp-codesniffer": "6.x-dev",
"enqueue/enqueue": "^0.10.27",
"enqueue/fs": "^0.10",
"phpunit/phpunit": "^11.5.3 || ^12.1.3 || ^13.0"
"enqueue/fs": "^0.10.27",
"phpunit/phpunit": "^13.0"
},
"suggest": {
"cakephp/bake": "Required if you want to generate jobs.",
Expand All @@ -56,6 +61,8 @@
},
"sort-packages": true
},
"minimum-stability": "dev",
"prefer-stable": true,
"scripts": {
"check": [
"@cs-check",
Expand All @@ -67,7 +74,7 @@
"phpstan": "tools/phpstan analyse",
"stan-baseline": "tools/phpstan --generate-baseline",
"stan-setup": "phive install",
"rector-setup": "cp composer.json composer.backup && composer require --dev rector/rector:\"~2.5.0\" && mv composer.backup composer.json",
"rector-setup": "cp composer.json composer.backup && composer require --dev rector/rector:\"~2.8.0\" && mv composer.backup composer.json",
"rector-check": "vendor/bin/rector process --dry-run",
"rector-fix": "vendor/bin/rector process",
"test": "phpunit",
Expand Down
2 changes: 1 addition & 1 deletion phpstan-baseline.neon
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
parameters:
ignoreErrors:
-
message: '#^Parameter \#1 \$callback of static method Closure\:\:fromCallable\(\) expects callable\(\)\: mixed, array\{mixed, string\} given\.$#'
message: '#^Parameter \#1 \$callback of static method Closure\:\:fromCallable\(\) expects callable\(\)\: mixed, array\{object, string\} given\.$#'
identifier: argument.type
count: 1
path: src/Job/Message.php
31 changes: 31 additions & 0 deletions src/Attribute/Config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php
declare(strict_types=1);

/**
* CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org/)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org/)
* @link https://cakephp.org CakePHP(tm) Project
* @since 4.0.0
* @license https://opensource.org/licenses/MIT MIT License
*/
namespace Cake\Queue\Attribute;

use Attribute;

#[Attribute(Attribute::TARGET_CLASS)]
readonly class Config
{
/**
* @param string $config Queue config name.
*/
public function __construct(
public string $config,
) {
}
}
31 changes: 31 additions & 0 deletions src/Attribute/Delay.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php
declare(strict_types=1);

/**
* CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org/)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org/)
* @link https://cakephp.org CakePHP(tm) Project
* @since 4.0.0
* @license https://opensource.org/licenses/MIT MIT License
*/
namespace Cake\Queue\Attribute;

use Attribute;

#[Attribute(Attribute::TARGET_CLASS)]
readonly class Delay
{
/**
* @param int $delay Seconds to delay the job for.
*/
public function __construct(
public int $delay,
) {
}
}
31 changes: 31 additions & 0 deletions src/Attribute/Expires.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php
declare(strict_types=1);

/**
* CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org/)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org/)
* @link https://cakephp.org CakePHP(tm) Project
* @since 4.0.0
* @license https://opensource.org/licenses/MIT MIT License
*/
namespace Cake\Queue\Attribute;

use Attribute;

#[Attribute(Attribute::TARGET_CLASS)]
readonly class Expires
{
/**
* @param int $expires Seconds after which the message expires.
*/
public function __construct(
public int $expires,
) {
}
}
194 changes: 194 additions & 0 deletions src/Attribute/JobAttributeReader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,194 @@
<?php
declare(strict_types=1);

/**
* CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org/)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org/)
* @link https://cakephp.org CakePHP(tm) Project
* @since 4.0.0
* @license https://opensource.org/licenses/MIT MIT License
*/
namespace Cake\Queue\Attribute;

use ReflectionClass;

/**
* Resolves additive job defaults declared via attributes.
*
* Resolution order everywhere: explicit value > attribute > static property > global default.
* Child class attributes win over parent class attributes.
*/
class JobAttributeReader
{
/**
* Cached attribute instances per job class.
*
* @var array<string, array<string, object|null>>
*/
protected static array $cache = [];

/**
* Get attribute instance from class hierarchy, if present.
*
* Child class attributes win over parent class attributes.
*
* @param class-string|object $target Job class name or instance.
* @param class-string $attribute Attribute class name.
*/
public static function get(string|object $target, string $attribute): ?object
{
$class = is_object($target) ? $target::class : $target;

if (array_key_exists($attribute, static::$cache[$class] ?? [])) {
return static::$cache[$class][$attribute];
}

$resolved = null;
foreach (static::hierarchy($class) as $candidate) {
$reflection = new ReflectionClass($candidate);
$attributes = $reflection->getAttributes($attribute);
if ($attributes !== []) {
$resolved = $attributes[0]->newInstance();
}
}

static::$cache[$class][$attribute] = $resolved;

return $resolved;
}

/**
* Get configured queue name for a job, if declared.
*
* @param class-string|object $target Job class name or instance.
*/
public static function getQueue(string|object $target): ?string
{
$attribute = static::get($target, Queue::class);

return $attribute instanceof Queue ? $attribute->queue : null;
}

/**
* Get configured config name for a job, if declared.
*
* @param class-string|object $target Job class name or instance.
*/
public static function getConfig(string|object $target): ?string
{
$attribute = static::get($target, Config::class);

return $attribute instanceof Config ? $attribute->config : null;
}

/**
* Get configured delay for a job, if declared.
*
* @param class-string|object $target Job class name or instance.
*/
public static function getDelay(string|object $target): ?int
{
$attribute = static::get($target, Delay::class);

return $attribute instanceof Delay ? $attribute->delay : null;
}

/**
* Get configured expiry for a job, if declared.
*
* @param class-string|object $target Job class name or instance.
*/
public static function getExpires(string|object $target): ?int
{
$attribute = static::get($target, Expires::class);

return $attribute instanceof Expires ? $attribute->expires : null;
}

/**
* Get configured priority for a job, if declared.
*
* @param class-string|object $target Job class name or instance.
*/
public static function getPriority(string|object $target): ?string
{
$attribute = static::get($target, Priority::class);

return $attribute instanceof Priority ? $attribute->priority : null;
}

/**
* Get configured max attempts for a job.
*
* Attribute wins over static property. Both remain supported.
*
* @param class-string|object $target Job class name or instance.
*/
public static function getMaxAttempts(string|object $target): ?int
{
$attribute = static::get($target, MaxAttempts::class);
if ($attribute instanceof MaxAttempts) {
return $attribute->maxAttempts;
}

$class = is_object($target) ? $target::class : $target;
if (property_exists($class, 'maxAttempts') && isset($class::$maxAttempts)) {
$value = $class::$maxAttempts;

return is_int($value) ? $value : null;
}

return null;
}

/**
* Check if a job should be treated as unique.
*
* Attribute wins over static property. Both remain supported.
*
* @param class-string|object $target Job class name or instance.
*/
public static function isUnique(string|object $target): bool
{
$attribute = static::get($target, ShouldBeUnique::class);
if ($attribute instanceof ShouldBeUnique) {
return $attribute->shouldBeUnique;
}

$class = is_object($target) ? $target::class : $target;

return property_exists($class, 'shouldBeUnique') && !empty($class::$shouldBeUnique);
}

/**
* Clear the attribute instance cache.
*
* Useful for testing or when classes are reloaded.
*
* @return void
*/
public static function clearCache(): void
{
static::$cache = [];
}

/**
* Parent-to-child class hierarchy, including the class itself.
*
* @param class-string $class Job class name.
* @return list<class-string>
*/
protected static function hierarchy(string $class): array
{
$hierarchy = array_reverse(array_values(class_parents($class)));
$hierarchy[] = $class;

return $hierarchy;
}
}
31 changes: 31 additions & 0 deletions src/Attribute/MaxAttempts.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php
declare(strict_types=1);

/**
* CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org/)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org/)
* @link https://cakephp.org CakePHP(tm) Project
* @since 4.0.0
* @license https://opensource.org/licenses/MIT MIT License
*/
namespace Cake\Queue\Attribute;

use Attribute;

#[Attribute(Attribute::TARGET_CLASS)]
readonly class MaxAttempts
{
/**
* @param int $maxAttempts Maximum number of job attempts.
*/
public function __construct(
public int $maxAttempts,
) {
}
}
Loading
Loading