Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 62904a6

Browse files
committed
feature #51152 [Scheduler] Add AbstractTriggerDecorator (kbond)
This PR was merged into the 6.4 branch. Discussion ---------- [Scheduler] Add `AbstractTriggerDecorator` | Q | A | ------------- | --- | Branch? | 6.4 | Bug fix? | no | New feature? | yes | Deprecations? | no | Tickets | n/a | License | MIT | Doc PR | n/a I have a need to know what trigger _decorators_ are used and what the _real_ trigger is. This PR adds an `AbstractTriggerDecorator` that the current decorators (`Jitter` & `ExcludeTime`) extend. ```php $trigger = new ExcludeTimeTrigger(new JitterTrigger(CronExpressionTrigger::fromSpec('#midnight', new MyMessage())); $trigger->inner(); // CronExpressionTrigger $trigger->decorators(); // [ExcludeTimeTrigger, JitterTrigger] ``` Commits ------- 3ca7eb3 [Scheduler] Add `AbstractTriggerDecorator`
2 parents 07eea6d + 3ca7eb3 commit 62904a6

File tree

6 files changed

+95
-3
lines changed

6 files changed

+95
-3
lines changed

src/Symfony/Component/Scheduler/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ CHANGELOG
55
---
66

77
* Allow setting timezone of next run date in CronExpressionTrigger
8+
* Add `AbstractTriggerDecorator`
89

910
6.3
1011
---
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Scheduler\Tests\Trigger;
13+
14+
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\Scheduler\Trigger\ExcludeTimeTrigger;
16+
use Symfony\Component\Scheduler\Trigger\JitterTrigger;
17+
use Symfony\Component\Scheduler\Trigger\TriggerInterface;
18+
19+
class AbstractDecoratedTriggerTest extends TestCase
20+
{
21+
public function testCanGetInnerTrigger()
22+
{
23+
$trigger = new JitterTrigger($inner = $this->createMock(TriggerInterface::class));
24+
25+
$this->assertSame($inner, $trigger->inner());
26+
$this->assertSame([$trigger], iterator_to_array($trigger->decorators()));
27+
}
28+
29+
public function testCanGetNestedInnerTrigger()
30+
{
31+
$trigger = new ExcludeTimeTrigger(
32+
$jitter = new JitterTrigger($inner = $this->createMock(TriggerInterface::class)),
33+
new \DateTimeImmutable(),
34+
new \DateTimeImmutable(),
35+
);
36+
37+
$this->assertSame($inner, $trigger->inner());
38+
$this->assertSame([$trigger, $jitter], iterator_to_array($trigger->decorators()));
39+
}
40+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Scheduler\Trigger;
13+
14+
/**
15+
* @author Kevin Bond <[email protected]>
16+
*/
17+
abstract class AbstractDecoratedTrigger implements TriggerInterface
18+
{
19+
public function __construct(private TriggerInterface $inner)
20+
{
21+
}
22+
23+
final public function inner(): TriggerInterface
24+
{
25+
$inner = $this->inner;
26+
27+
while ($inner instanceof self) {
28+
$inner = $inner->inner;
29+
}
30+
31+
return $inner;
32+
}
33+
34+
/**
35+
* @return \Traversable<self>
36+
*/
37+
final public function decorators(): \Traversable
38+
{
39+
yield $this;
40+
41+
$inner = $this->inner;
42+
43+
while ($inner instanceof self) {
44+
yield $inner;
45+
46+
$inner = $inner->inner;
47+
}
48+
}
49+
}

src/Symfony/Component/Scheduler/Trigger/ExcludeTimeTrigger.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,14 @@
1414
/**
1515
* @experimental
1616
*/
17-
final class ExcludeTimeTrigger implements TriggerInterface
17+
final class ExcludeTimeTrigger extends AbstractDecoratedTrigger
1818
{
1919
public function __construct(
2020
private readonly TriggerInterface $inner,
2121
private readonly \DateTimeImmutable $from,
2222
private readonly \DateTimeImmutable $until,
2323
) {
24+
parent::__construct($inner);
2425
}
2526

2627
public function __toString(): string

src/Symfony/Component/Scheduler/Trigger/JitterTrigger.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,14 @@
1414
/**
1515
* @author Kevin Bond <[email protected]>
1616
*/
17-
final class JitterTrigger implements TriggerInterface
17+
final class JitterTrigger extends AbstractDecoratedTrigger
1818
{
1919
/**
2020
* @param positive-int $maxSeconds
2121
*/
2222
public function __construct(private readonly TriggerInterface $trigger, private readonly int $maxSeconds = 60)
2323
{
24+
parent::__construct($trigger);
2425
}
2526

2627
public function __toString(): string

src/Symfony/Component/Scheduler/Trigger/PeriodicalTrigger.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
/**
1717
* @experimental
1818
*/
19-
class PeriodicalTrigger implements TriggerInterface, \Stringable
19+
class PeriodicalTrigger implements TriggerInterface
2020
{
2121
private float $intervalInSeconds = 0.0;
2222
private \DateTimeImmutable $from;

0 commit comments

Comments
 (0)