Download project

This commit is contained in:
Roman Pyrih
2024-11-20 09:09:44 +01:00
parent 547a138d6a
commit 5ff041757f
40737 changed files with 7766183 additions and 0 deletions

View File

@@ -0,0 +1,19 @@
Copyright (c) 2015 Ross Tuck
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is furnished
to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

View File

@@ -0,0 +1,80 @@
<?php
require_once __DIR__ . '/../vendor/autoload.php';
use League\Container\ReflectionContainer;
use League\Tactician\Container\ContainerLocator;
use League\Container\Container;
use League\Tactician\Handler\CommandHandlerMiddleware;
use League\Tactician\Handler\CommandNameExtractor\ClassNameExtractor;
use League\Tactician\Handler\MethodNameInflector\HandleClassNameInflector;
use League\Tactician\CommandBus;
$mapping = [
'MyCommand' => 'MyCommandHandler',
];
final class Mailer
{
}
final class MyCommand
{
public $name;
public $emailAddress;
public function __construct($name, $emailAddress)
{
$this->name = $name;
$this->emailAddress = $emailAddress;
}
}
final class MyCommandHandler
{
private $mailer;
public function __construct(Mailer $mailer)
{
$this->mailer = $mailer;
}
public function handleMyCommand($command)
{
$format = <<<MSG
Hi %s,
Your email address is %s.
--
Cheers
MSG;
echo sprintf($format, $command->name, $command->emailAddress);
}
}
$containerLocator = new ContainerLocator(
(new Container())->delegate(new ReflectionContainer()),
$mapping
);
$handlerMiddleware = new CommandHandlerMiddleware(
new ClassNameExtractor(),
$containerLocator,
new HandleClassNameInflector()
);
$commandBus = new CommandBus([$handlerMiddleware]);
$command = new MyCommand('Joe Bloggs', 'j.bloggs@theinternet.com');
echo '<pre>';
try {
$commandBus->handle($command);
} catch (\Exception $e) {
echo $e->getMessage();
echo '<pre>';
print_r($e->getTraceAsString());
echo '</pre>';
}

View File

@@ -0,0 +1,89 @@
<?php
namespace League\Tactician\Container;
use Psr\Container\ContainerInterface;
use League\Tactician\Exception\MissingHandlerException;
use League\Tactician\Handler\Locator\HandlerLocator;
/**
* Fetch handler instances from an in-memory collection.
*
* This locator allows you to bind a handler FQCN to receive commands of a
* certain command name.
*/
class ContainerLocator implements HandlerLocator
{
/**
* @var ContainerInterface
*/
protected $container;
/**
* The collection of Command/CommandHandler
*
* @var array
*/
protected $commandNameToHandlerMap = [];
/**
* @param ContainerInterface $container
* @param array $commandNameToHandlerMap
*/
public function __construct(
ContainerInterface $container,
array $commandNameToHandlerMap = []
) {
$this->container = $container;
$this->addHandlers($commandNameToHandlerMap);
}
/**
* Bind a handler instance to receive all commands with a certain class
*
* @param string $handler Handler to receive class
* @param string $commandName Can be a class name or name of a NamedCommand
*/
public function addHandler($handler, $commandName)
{
$this->commandNameToHandlerMap[$commandName] = $handler;
}
/**
* Allows you to add multiple handlers at once.
*
* The map should be an array in the format of:
* [
* 'AddTaskCommand' => 'AddTaskCommandHandler',
* 'CompleteTaskCommand' => 'CompleteTaskCommandHandler',
* ]
*
* @param array $commandNameToHandlerMap
*/
public function addHandlers(array $commandNameToHandlerMap)
{
foreach ($commandNameToHandlerMap as $commandName => $handler) {
$this->addHandler($handler, $commandName);
}
}
/**
* Retrieves the handler for a specified command
*
* @param string $commandName
*
* @return object
*
* @throws MissingHandlerException
*/
public function getHandlerForCommand($commandName)
{
if (!isset($this->commandNameToHandlerMap[$commandName])) {
throw MissingHandlerException::forCommand($commandName);
}
$serviceId = $this->commandNameToHandlerMap[$commandName];
return $this->container->get($serviceId);
}
}