/** * Create service with name * * @param ServiceLocatorInterface $serviceLocator * @param string $name * @param string $requestedName * @throws \LogicException * @return mixed */ public function createServiceWithName(ServiceLocatorInterface $serviceLocator, $name, $requestedName) { /** @var $env Environment */ $env = $serviceLocator->get(Definition::SERVICE_ENVIRONMENT); $nameParts = explode('.', $requestedName); Assertion::min(count($nameParts), 3, sprintf("Given service bus alias %s is invalid. Format should be processing.(command|event)_bus.[target]", $requestedName)); $busType = $nameParts[1]; unset($nameParts[0]); unset($nameParts[1]); $address = implode('.', $nameParts); $busConfig = $this->getChannelConfigFor($env, $address); $target = $this->getTargetFromAddress($address); $bus = $busType === "command_bus" ? new CommandBus() : new EventBus(); $bus->utilize($this->getForwardToMessageDispatcher()); $bus->utilize($this->getToMessageTranslator()); $bus->utilize($this->getHandleWorkflowMessageStrategy()); $bus->utilize($this->getInvokeProcessorStrategy()); $bus->utilize(new ServiceLocatorProxy(Zf2ServiceManagerProxy::proxy($serviceLocator))); $messageHandler = $busConfig->stringValue('message_dispatcher', $target); if (!empty($messageHandler)) { $bus->utilize(new SingleTargetMessageRouter($messageHandler)); } else { throw new \LogicException("Missing a message handler for the bus " . $requestedName); } foreach ($busConfig->arrayValue('utils') as $busUtil) { if (is_string($busUtil)) { $busUtil = $serviceLocator->get($busUtil); } $bus->utilize($busUtil); } return $bus; }
/** * Create service * * @param ServiceLocatorInterface $serviceLocator * @throws \InvalidArgumentException * @return mixed */ public function createService(ServiceLocatorInterface $serviceLocator) { //Set up is ported from https://github.com/prooph/ProophEventStoreModule/blob/master/src/ProophEventStoreModule/Factory/EventStoreFactory.php $config = $serviceLocator->get("configuration"); if (!isset($config['prooph.event_store'])) { throw new \InvalidArgumentException("Missing key prooph.event_store in application configuration"); } $config = $config['prooph.event_store']; if (!isset($config['adapter'])) { throw new \InvalidArgumentException("Missing adapter configuration in prooph.event_store configuration"); } $adapterType = isset($config['adapter']["type"]) ? $config['adapter']["type"] : 'Prooph\\EventStore\\Adapter\\InMemoryAdapter'; $adapterOptions = isset($config['adapter']["options"]) ? $config['adapter']["options"] : []; if ($adapterType == 'Prooph\\EventStore\\Adapter\\Zf2\\Zf2EventStoreAdapter' && isset($adapterOptions['zend_db_adapter']) && is_string($adapterOptions['zend_db_adapter'])) { $config['adapter']['options']['zend_db_adapter'] = $serviceLocator->get($adapterOptions['zend_db_adapter']); } $featureManagerConfig = null; if (isset($config['feature_manager'])) { $featureManagerConfig = new Config($config['feature_manager']); unset($config['feature_manager']); } $esConfiguration = new Configuration($config); $featureManager = new ZF2FeatureManager($featureManagerConfig); $featureManager->setServiceLocator($serviceLocator); $esConfiguration->setFeatureManager(Zf2ServiceManagerProxy::proxy($featureManager)); return new EventStore($esConfiguration); }
/** * @param null|array|ServiceManager $configurationOrServices * @throws \RuntimeException * @throws \InvalidArgumentException * @return \Prooph\Processing\Environment\Environment */ public static function setUp($configurationOrServices = null) { $env = null; //If neither a config array nor a ServiceManager is given, we initialize the variable with an empty array //Later in the set up the default config is merged with this empty array if (is_null($configurationOrServices)) { $configurationOrServices = []; } //Initialize the Environment if ($configurationOrServices instanceof ServiceManager) { //We assume that when the workflow processor service definition is missing //then the other services related to the workflow environment are also missing if (!$configurationOrServices->has(Definition::SERVICE_WORKFLOW_PROCESSOR)) { $servicesConfig = new Config(self::$defaultServicesConfig); $servicesConfig->configureServiceManager($configurationOrServices); } $env = new self(Zf2ServiceManagerProxy::proxy($configurationOrServices)); //Check if the provided ServiceManager already has a configuration service available //so we make sure that we won't override it later but just merge it with the default environment config if ($env->services()->has('configuration')) { $configurationOrServices = $env->services()->get('configuration'); } else { $configurationOrServices = []; } } elseif (is_array($configurationOrServices)) { //No external ServiceManager given, so we set up a new one $servicesConfig = []; if (isset($configurationOrServices['services'])) { $servicesConfig = $configurationOrServices['services']; unset($configurationOrServices['services']); } $servicesConfig = new Config(ArrayUtils::merge(self::$defaultServicesConfig, $servicesConfig)); $env = new self(Zf2ServiceManagerProxy::proxy(new ServiceManager($servicesConfig))); } //This should never happen, but if for whatever reason a wrong $configurationOrServices was passed to the set up //we stop the process here if (is_null($env)) { throw new \InvalidArgumentException("Processing set up requires either a config array or a ready to use Zend\\ServiceManager"); } //We proceed with merging and preparing the environment configuration $envConfig = ArrayUtils::merge(self::$defaultEnvConfig, $configurationOrServices); //The environment node name is used as target for the local channel, the config needs to be adapted accordingly. $envConfigReader = new ArrayReader($envConfig); $nodeName = $envConfigReader->stringValue('processing.node_name', Definition::DEFAULT_NODE_NAME); $localChannelTargets = $envConfigReader->arrayValue('processing.channels.local.targets'); if (!in_array($nodeName, $localChannelTargets)) { $envConfig = ArrayUtils::merge($envConfig, ['processing' => ['channels' => ['local' => ['targets' => [$nodeName]]]]]); } $env->services()->set('configuration', $envConfig, true); if (!$env->services()->has('config')) { $env->services()->setAlias('config', 'configuration'); } $env->services()->set(Definition::SERVICE_ENVIRONMENT, $env, true); //The node name is used as message bus target to address the workflow processor of the current environment //We alias the workflow processor service with the node name to ensure that the target can be resolved $env->services()->setAlias($nodeName, Definition::SERVICE_WORKFLOW_PROCESSOR); //The environment component ships with an own workflow engine implementation that uses the ServiceManager //to resolve the required message buses for the various targets $env->workflowEngine = new ServicesAwareWorkflowEngine($env->services()); //Add an initializer which injects the local processor message buses whenever a workflow message handler //is requested from the ServiceManager $env->services()->addInitializer(new WorkflowProcessorBusesProvider()); //After the set up routine is finished the plugin mechanism can be triggered foreach ($env->getConfig()->arrayValue('plugins') as $plugin) { if (!$plugin instanceof Plugin && !is_string($plugin)) { throw new \RuntimeException(sprintf("Invalid plugin detected: %s. Plugins should be instance of Prooph\\Processing\\Environment\\Plugin or a ServiceManager alias that resolves to a plugin", is_object($plugin) ? get_class($plugin) : gettype($plugin))); } if (is_string($plugin)) { $plugin = $env->services->get($plugin); if (!$plugin instanceof Plugin) { throw new \RuntimeException(sprintf("Resolved plugin for alias %s does not implement Prooph\\Processing\\Environment\\Plugin")); } } $env->register($plugin); } return $env; }