-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Close #11: Add AbstractLoaderFactory
All Loaders are now fabricated by an AbstractFactory. The AbstractFactory considers if ServiceBus configuration contains configuration for the requested loader and passes config to it.
- Loading branch information
Showing
9 changed files
with
311 additions
and
46 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
94 changes: 94 additions & 0 deletions
94
src/Prooph/ServiceBus/Service/Factory/AbstractLoaderFactory.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,94 @@ | ||
<?php | ||
/* | ||
* This file is part of the prooph/service-bus. | ||
* (c) Alexander Miertsch <contact@prooph.de> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
* | ||
* Date: 05.07.14 - 21:34 | ||
*/ | ||
|
||
namespace Prooph\ServiceBus\Service\Factory; | ||
|
||
use Codeliner\ArrayReader\ArrayReader; | ||
use Prooph\ServiceBus\Service\Definition; | ||
use Zend\ServiceManager\AbstractFactoryInterface; | ||
use Zend\ServiceManager\Config; | ||
use Zend\ServiceManager\ServiceLocatorInterface; | ||
|
||
/** | ||
* Class AbstractLoaderFactory | ||
* | ||
* @package Prooph\ServiceBus\Service\Factory | ||
* @author Alexander Miertsch <kontakt@codeliner.ws> | ||
*/ | ||
class AbstractLoaderFactory implements AbstractFactoryInterface | ||
{ | ||
/** | ||
* @var array | ||
*/ | ||
protected $servicesMap; | ||
|
||
/** | ||
* @var ArrayReader | ||
*/ | ||
protected $configReader; | ||
|
||
/** | ||
* Determine if we can create a service with name | ||
* | ||
* @param ServiceLocatorInterface $serviceLocator | ||
* @param $name | ||
* @param $requestedName | ||
* @return bool | ||
*/ | ||
public function canCreateServiceWithName(ServiceLocatorInterface $serviceLocator, $name, $requestedName) | ||
{ | ||
return in_array($requestedName, array_keys($this->getServicesMap())); | ||
} | ||
|
||
/** | ||
* Create service with name | ||
* | ||
* @param ServiceLocatorInterface $serviceLocator | ||
* @param $name | ||
* @param $requestedName | ||
* @return mixed | ||
*/ | ||
public function createServiceWithName(ServiceLocatorInterface $serviceLocator, $name, $requestedName) | ||
{ | ||
if (is_null($this->configReader)) { | ||
$this->configReader = new ArrayReader($serviceLocator->get('configuration')); | ||
} | ||
|
||
$config = new Config($this->configReader->arrayValue( | ||
Definition::CONFIG_ROOT_ESCAPED . '.' . $requestedName | ||
)); | ||
|
||
$loaderClass = $this->getServicesMap()[$requestedName]; | ||
|
||
return new $loaderClass($config); | ||
} | ||
|
||
/** | ||
* @return array | ||
*/ | ||
protected function getServicesMap() | ||
{ | ||
if (is_null($this->servicesMap)) { | ||
$this->servicesMap = array( | ||
Definition::COMMAND_BUS_LOADER => 'Prooph\ServiceBus\Service\CommandBusLoader', | ||
Definition::COMMAND_RECEIVER_LOADER => 'Prooph\ServiceBus\Service\CommandReceiverLoader', | ||
Definition::EVENT_BUS_LOADER => 'Prooph\ServiceBus\Service\EventBusLoader', | ||
Definition::EVENT_RECEIVER_LOADER => 'Prooph\ServiceBus\Service\EventReceiverLoader', | ||
Definition::INVOKE_STRATEGY_LOADER => 'Prooph\ServiceBus\Service\InvokeStrategyLoader', | ||
Definition::MESSAGE_DISPATCHER_LOADER => 'Prooph\ServiceBus\Service\MessageDispatcherLoader', | ||
Definition::QUEUE_LOADER => 'Prooph\ServiceBus\Service\QueueLoader' | ||
); | ||
} | ||
|
||
return $this->servicesMap; | ||
} | ||
} | ||
|
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,24 @@ | ||
<?php | ||
/* | ||
* This file is part of the prooph/service-bus. | ||
* (c) Alexander Miertsch <contact@prooph.de> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
* | ||
* Date: 05.07.14 - 21:05 | ||
*/ | ||
|
||
namespace Prooph\ServiceBusTest\Mock\Configtest; | ||
|
||
use Prooph\ServiceBus\Command\CommandBus; | ||
|
||
class CustomBus extends CommandBus | ||
{ | ||
public $message; | ||
|
||
public function __construct($message) { | ||
$this->message = $message; | ||
} | ||
} | ||
|
36 changes: 36 additions & 0 deletions
36
tests/Prooph/ServiceBusTest/Mock/Configtest/CustomBusFactory.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,36 @@ | ||
<?php | ||
/* | ||
* This file is part of the prooph/service-bus. | ||
* (c) Alexander Miertsch <contact@prooph.de> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
* | ||
* Date: 05.07.14 - 21:05 | ||
*/ | ||
|
||
namespace Prooph\ServiceBusTest\Mock\Configtest; | ||
|
||
use Zend\ServiceManager\FactoryInterface; | ||
use Zend\ServiceManager\ServiceLocatorInterface; | ||
|
||
/** | ||
* Class CustomBusFactory | ||
* | ||
* @package Prooph\ServiceBusTest\Mock\Configtest | ||
* @author Alexander Miertsch <kontakt@codeliner.ws> | ||
*/ | ||
class CustomBusFactory implements FactoryInterface | ||
{ | ||
/** | ||
* Create service | ||
* | ||
* @param ServiceLocatorInterface $serviceLocator | ||
* @return mixed | ||
*/ | ||
public function createService(ServiceLocatorInterface $serviceLocator) | ||
{ | ||
return new CustomBus("Created via factory"); | ||
} | ||
} | ||
|
24 changes: 24 additions & 0 deletions
24
tests/Prooph/ServiceBusTest/Mock/Configtest/CustomCommandReceiver.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,24 @@ | ||
<?php | ||
/* | ||
* This file is part of the prooph/service-bus. | ||
* (c) Alexander Miertsch <contact@prooph.de> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
* | ||
* Date: 05.07.14 - 21:27 | ||
*/ | ||
|
||
namespace Prooph\ServiceBusTest\Mock\Configtest; | ||
|
||
use Prooph\ServiceBus\Command\CommandReceiver; | ||
|
||
class CustomCommandReceiver extends CommandReceiver | ||
{ | ||
public $message; | ||
|
||
public function __construct($message) { | ||
$this->message = $message; | ||
} | ||
} | ||
|
31 changes: 31 additions & 0 deletions
31
tests/Prooph/ServiceBusTest/Mock/Configtest/CustomCommandReceiverFactory.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,31 @@ | ||
<?php | ||
/* | ||
* This file is part of the prooph/service-bus. | ||
* (c) Alexander Miertsch <contact@prooph.de> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
* | ||
* Date: 05.07.14 - 21:27 | ||
*/ | ||
|
||
namespace Prooph\ServiceBusTest\Mock\Configtest; | ||
|
||
use Zend\ServiceManager\FactoryInterface; | ||
use Zend\ServiceManager\ServiceLocatorInterface; | ||
|
||
class CustomCommandReceiverFactory implements FactoryInterface | ||
{ | ||
|
||
/** | ||
* Create service | ||
* | ||
* @param ServiceLocatorInterface $serviceLocator | ||
* @return mixed | ||
*/ | ||
public function createService(ServiceLocatorInterface $serviceLocator) | ||
{ | ||
return new CustomCommandReceiver("Created via factory"); | ||
} | ||
} | ||
|
Oops, something went wrong.