Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Messenger] Add handled & sent stamps
- Loading branch information
Showing
16 changed files
with
325 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Messenger\Stamp; | ||
|
||
/** | ||
* Stamp identifying a message handled by the `HandleMessageMiddleware` middleware | ||
* and storing the handler returned value. | ||
* | ||
* @see \Symfony\Component\Messenger\Middleware\HandleMessageMiddleware | ||
* | ||
* @author Maxime Steinhausser <maxime.steinhausser@gmail.com> | ||
* | ||
* @experimental in 4.2 | ||
*/ | ||
final class HandledStamp implements StampInterface | ||
{ | ||
private $result; | ||
private $callableName; | ||
private $handlerAlias; | ||
|
||
/** | ||
* @param mixed $result The returned value of the message handler | ||
*/ | ||
public function __construct($result, string $callableName, string $handlerAlias = null) | ||
{ | ||
$this->result = $result; | ||
$this->callableName = $callableName; | ||
$this->handlerAlias = $handlerAlias; | ||
} | ||
|
||
/** | ||
* @param mixed $result The returned value of the message handler | ||
*/ | ||
public static function fromCallable(callable $handler, $result, string $handlerAlias = null): self | ||
{ | ||
if (\is_array($handler)) { | ||
if (\is_object($handler[0])) { | ||
return new self($result, \get_class($handler[0]).'::'.$handler[1], $handlerAlias); | ||
} | ||
|
||
return new self($result, $handler[0].'::'.$handler[1], $handlerAlias); | ||
} | ||
|
||
if (\is_string($handler)) { | ||
return new self($result, $handler, $handlerAlias); | ||
} | ||
|
||
if ($handler instanceof \Closure) { | ||
$r = new \ReflectionFunction($handler); | ||
if (false !== strpos($r->name, '{closure}')) { | ||
return new self($result, 'Closure', $handlerAlias); | ||
} | ||
if ($class = $r->getClosureScopeClass()) { | ||
return new self($result, $class->name.'::'.$r->name, $handlerAlias); | ||
} | ||
|
||
return new self($result, $r->name, $handlerAlias); | ||
} | ||
|
||
return new self($result, \get_class($handler).'::__invoke', $handlerAlias); | ||
} | ||
|
||
/** | ||
* @return mixed | ||
*/ | ||
public function getResult() | ||
{ | ||
return $this->result; | ||
} | ||
|
||
public function getCallableName(): string | ||
{ | ||
return $this->callableName; | ||
} | ||
|
||
public function getHandlerAlias(): ?string | ||
{ | ||
return $this->handlerAlias; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Messenger\Stamp; | ||
|
||
/** | ||
* Marker stamp identifying a message sent by the `SendMessageMiddleware`. | ||
* | ||
* @see \Symfony\Component\Messenger\Middleware\SendMessageMiddleware | ||
* | ||
* @author Maxime Steinhausser <maxime.steinhausser@gmail.com> | ||
* | ||
* @experimental in 4.2 | ||
*/ | ||
final class SentStamp implements StampInterface | ||
{ | ||
private $senderClass; | ||
private $senderAlias; | ||
|
||
public function __construct(string $senderClass, string $senderAlias = null) | ||
{ | ||
$this->senderAlias = $senderAlias; | ||
$this->senderClass = $senderClass; | ||
} | ||
|
||
public function getSenderClass(): string | ||
{ | ||
return $this->senderClass; | ||
} | ||
|
||
public function getSenderAlias(): ?string | ||
{ | ||
return $this->senderAlias; | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
src/Symfony/Component/Messenger/Tests/Handler/HandlersLocatorTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Messenger\Tests\Handler; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Symfony\Component\Messenger\Envelope; | ||
use Symfony\Component\Messenger\Handler\HandlersLocator; | ||
use Symfony\Component\Messenger\Tests\Fixtures\DummyMessage; | ||
|
||
class HandlersLocatorTest extends TestCase | ||
{ | ||
public function testItYieldsProvidedAliasAsKey() | ||
{ | ||
$handler = $this->createPartialMock(\stdClass::class, array('__invoke')); | ||
$locator = new HandlersLocator(array( | ||
DummyMessage::class => array('dummy' => $handler), | ||
)); | ||
|
||
$this->assertSame(array('dummy' => $handler), iterator_to_array($locator->getHandlers(new Envelope(new DummyMessage('a'))))); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.