Skip to content

Commit

Permalink
Added php function calls namespace prefixing
Browse files Browse the repository at this point in the history
  • Loading branch information
gquemener committed Oct 4, 2019
1 parent 4423c7b commit 1341e59
Show file tree
Hide file tree
Showing 12 changed files with 86 additions and 86 deletions.
6 changes: 3 additions & 3 deletions src/DependencyInjection/Compiler/PluginsPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ public function process(ContainerBuilder $container)

foreach ($buses as $name => $bus) {
$globalPlugins = $container->findTaggedServiceIds('prooph_service_bus.plugin');
$typePlugins = $container->findTaggedServiceIds(sprintf('prooph_service_bus.%s.plugin', $type . '_bus'));
$localPlugins = $container->findTaggedServiceIds(sprintf('prooph_service_bus.%s.plugin', $name));
$typePlugins = $container->findTaggedServiceIds(\sprintf('prooph_service_bus.%s.plugin', $type . '_bus'));
$localPlugins = $container->findTaggedServiceIds(\sprintf('prooph_service_bus.%s.plugin', $name));

$plugins = array_merge(array_keys($globalPlugins), array_keys($typePlugins), array_keys($localPlugins));
$plugins = \array_merge(\array_keys($globalPlugins), \array_keys($typePlugins), \array_keys($localPlugins));

$busDefinition = $container->getDefinition($bus);

Expand Down
30 changes: 15 additions & 15 deletions src/DependencyInjection/Compiler/RoutePass.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,22 +41,22 @@ public function process(ContainerBuilder $container)
$buses = $container->getParameter('prooph_service_bus.' . $type . '_buses');

foreach ($buses as $name => $bus) {
$routerServiceId = sprintf('prooph_service_bus.%s.decorated_router', $name);
$routerServiceId = \sprintf('prooph_service_bus.%s.decorated_router', $name);
if (! $container->has($routerServiceId)) {
$routerServiceId = sprintf('prooph_service_bus.%s.router', $name);
$routerServiceId = \sprintf('prooph_service_bus.%s.router', $name);
}
$router = $container->findDefinition($routerServiceId);

$routerArguments = $router->getArguments();
$serviceLocator = $container->findDefinition(sprintf('%s.plugin.service_locator.locator', $name));
$serviceLocator = $container->findDefinition(\sprintf('%s.plugin.service_locator.locator', $name));
$serviceLocatorServices = $serviceLocator->getArgument(0);

$handlers = $container->findTaggedServiceIds(sprintf('prooph_service_bus.%s.route_target', $name));
$handlers = $container->findTaggedServiceIds(\sprintf('prooph_service_bus.%s.route_target', $name));

foreach ($handlers as $id => $args) {
$serviceLocatorServices[$id] = new Reference($id);
// Safeguard to have only one tag per command / query
if ($type !== 'event' && count($args) > 1) {
if ($type !== 'event' && \count($args) > 1) {
throw CompilerPassException::tagCountExceeded($type, $id, $bus);
}
foreach ($args as $eachArgs) {
Expand All @@ -69,15 +69,15 @@ public function process(ContainerBuilder $container)
: $this->recognizeMessageNames($container, $container->getDefinition($id), $id, $type);

if ($type === 'event') {
$routerArguments[0] = array_merge_recursive(
$routerArguments[0] = \array_merge_recursive(
$routerArguments[0],
array_combine($messageNames, array_fill(0, count($messageNames), [$id]))
\array_combine($messageNames, \array_fill(0, \count($messageNames), [$id]))
);
$routerArguments[0] = array_map('array_unique', $routerArguments[0]);
$routerArguments[0] = \array_map('array_unique', $routerArguments[0]);
} else {
$routerArguments[0] = array_merge(
$routerArguments[0] = \array_merge(
$routerArguments[0],
array_combine($messageNames, array_fill(0, count($messageNames), $id))
\array_combine($messageNames, \array_fill(0, \count($messageNames), $id))
);
}
}
Expand All @@ -86,10 +86,10 @@ public function process(ContainerBuilder $container)
$serviceLocator->setArgument(0, $serviceLocatorServices);

// Update route configuration parameter
$configId = sprintf('prooph_service_bus.%s.configuration', $name);
$configId = \sprintf('prooph_service_bus.%s.configuration', $name);

$routes = is_array($routerArguments[0]) ? $routerArguments[0] : [];
$config = array_replace($container->getParameter($configId), ['router' => ['routes' => $routes]]);
$routes = \is_array($routerArguments[0]) ? $routerArguments[0] : [];
$config = \array_replace($container->getParameter($configId), ['router' => ['routes' => $routes]]);

$container->setParameter($configId, $config);
}
Expand All @@ -109,7 +109,7 @@ private function recognizeMessageNames(
throw CompilerPassException::unknownHandlerClass($routeClass, $routeId, $busType);
}

$methodsWithMessageParameter = array_filter(
$methodsWithMessageParameter = \array_filter(
$handlerReflection->getMethods(ReflectionMethod::IS_PUBLIC),
function (ReflectionMethod $method) {
return ($method->getNumberOfRequiredParameters() === 1 || $method->getNumberOfRequiredParameters() === 2)
Expand All @@ -122,7 +122,7 @@ function (ReflectionMethod $method) {
}
);

return array_unique(array_map(function (ReflectionMethod $method) {
return \array_unique(\array_map(function (ReflectionMethod $method) {
return self::detectMessageName($method->getParameters()[0]->getClass());
}, $methodsWithMessageParameter));
}
Expand Down
32 changes: 16 additions & 16 deletions src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,21 +66,21 @@ private function addServiceBusSection(string $type, ArrayNodeDefinition $node):
->prototype('scalar')
->beforeNormalization()
->ifTrue(function ($v) {
return strpos($v, '@') === 0;
return \strpos($v, '@') === 0;
})
->then(function ($v) {
return substr($v, 1);
return \substr($v, 1);
})
->end()
->end();
} else {
$handlerNode
->beforeNormalization()
->ifTrue(function ($v) {
return strpos($v, '@') === 0;
return \strpos($v, '@') === 0;
})
->then(function ($v) {
return substr($v, 1);
return \substr($v, 1);
})
->end();
}
Expand All @@ -98,32 +98,32 @@ private function addServiceBusSection(string $type, ArrayNodeDefinition $node):
->scalarNode('message_factory')
->beforeNormalization()
->ifTrue(function ($v) {
return strpos($v, '@') === 0;
return \strpos($v, '@') === 0;
})
->then(function ($v) {
return substr($v, 1);
return \substr($v, 1);
})
->end()
->defaultValue('prooph_service_bus.message_factory')
->end()
->scalarNode('message_data_converter')
->beforeNormalization()
->ifTrue(function ($v) {
return strpos($v, '@') === 0;
return \strpos($v, '@') === 0;
})
->then(function ($v) {
return substr($v, 1);
return \substr($v, 1);
})
->end()
->defaultValue('prooph_service_bus.message_data_converter.' . $type)
->end()
->scalarNode('message_converter')
->beforeNormalization()
->ifTrue(function ($v) {
return strpos($v, '@') === 0;
return \strpos($v, '@') === 0;
})
->then(function ($v) {
return substr($v, 1);
return \substr($v, 1);
})
->end()
->defaultValue('prooph_service_bus.message_converter')
Expand All @@ -138,10 +138,10 @@ private function addServiceBusSection(string $type, ArrayNodeDefinition $node):
->prototype('scalar')
->beforeNormalization()
->ifTrue(function ($v) {
return strpos($v, '@') === 0;
return \strpos($v, '@') === 0;
})
->then(function ($v) {
return substr($v, 1);
return \substr($v, 1);
})
->end()
->end()
Expand All @@ -153,21 +153,21 @@ private function addServiceBusSection(string $type, ArrayNodeDefinition $node):
->scalarNode('type')
->beforeNormalization()
->ifTrue(function ($v) {
return strpos($v, '@') === 0;
return \strpos($v, '@') === 0;
})
->then(function ($v) {
return substr($v, 1);
return \substr($v, 1);
})
->end()
->defaultValue('prooph_service_bus.' . $type . '_bus_router')
->end()
->scalarNode('async_switch')
->beforeNormalization()
->ifTrue(function ($v) {
return strpos($v, '@') === 0;
return \strpos($v, '@') === 0;
})
->then(function ($v) {
return substr($v, 1);
return \substr($v, 1);
})
->end()
->end()
Expand Down
46 changes: 23 additions & 23 deletions src/DependencyInjection/ProophServiceBusExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,21 +76,21 @@ private function busLoad(
$loader->load($type . '_bus.xml');

$serviceBuses = [];
foreach (array_keys($config) as $name) {
foreach (\array_keys($config) as $name) {
$serviceBuses[$name] = 'prooph_service_bus.' . $name;
}
$container->setParameter("prooph_service_bus.{$type}_buses", $serviceBuses);

// Add DataCollector
if ($type !== 'query' && $container->getParameter('kernel.debug') && class_exists(Stopwatch::class)) {
if ($type !== 'query' && $container->getParameter('kernel.debug') && \class_exists(Stopwatch::class)) {
$container
->setDefinition(
sprintf('prooph_service_bus.plugin.symfony_data_collector.%s_bus', $type),
\sprintf('prooph_service_bus.plugin.symfony_data_collector.%s_bus', $type),
new ChildDefinition('prooph_service_bus.plugin.symfony_data_collector')
)
->addArgument($type)
->addTag('data_collector', [
'id' => sprintf('prooph.%s_bus', $type),
'id' => \sprintf('prooph.%s_bus', $type),
'template' => '@ProophServiceBus/Collector/debug_view.html.twig',
]);
}
Expand Down Expand Up @@ -120,7 +120,7 @@ private function loadBus(string $type, string $name, array $options, ContainerBu
new ChildDefinition('prooph_service_bus.' . $type . '_bus')
);
$serviceBusDefinition->setPublic(true);
if (in_array(NamedMessageBus::class, class_implements($container->getDefinition('prooph_service_bus.'.$type.'_bus')->getClass()))) {
if (\in_array(NamedMessageBus::class, \class_implements($container->getDefinition('prooph_service_bus.'.$type.'_bus')->getClass()))) {
$serviceBusDefinition->addMethodCall('setBusName', [$name]);
$serviceBusDefinition->addMethodCall('setBusType', [$type]);
}
Expand All @@ -138,35 +138,35 @@ private function loadBus(string $type, string $name, array $options, ContainerBu
)
->addArgument(new Reference($options['message_converter']));

$contextFactoryId = sprintf('prooph_service_bus.message_context_factory.%s', $serviceBusId);
$contextFactoryId = \sprintf('prooph_service_bus.message_context_factory.%s', $serviceBusId);
$container
->setDefinition($contextFactoryId, new ChildDefinition('prooph_service_bus.message_context_factory'))
->addArgument(new Reference($options['message_data_converter']));

// Collecting data for each configured service bus
if ($type !== 'query' && $container->getParameter('kernel.debug') && class_exists(Stopwatch::class)) {
if ($type !== 'query' && $container->getParameter('kernel.debug') && \class_exists(Stopwatch::class)) {
$container
->setDefinition(
sprintf('%s.plugin.data_collector', $serviceBusId),
\sprintf('%s.plugin.data_collector', $serviceBusId),
new ChildDefinition('prooph_service_bus.plugin.data_collector')
)
->addArgument(new Reference($contextFactoryId))
->addArgument(new Reference(sprintf('prooph_service_bus.plugin.symfony_data_collector.%s_bus', $type)))
->addArgument(new Reference(\sprintf('prooph_service_bus.plugin.symfony_data_collector.%s_bus', $type)))
->addTag("prooph_service_bus.{$type}_bus.plugin");
}

// Logging for each configured service bus
$container
->setDefinition(
sprintf('%s.plugin.psr_logger', $serviceBusId),
\sprintf('%s.plugin.psr_logger', $serviceBusId),
new ChildDefinition('prooph_service_bus.plugin.psr_logger')
)
->setArguments([
new Reference($contextFactoryId),
new Reference('logger', ContainerInterface::NULL_ON_INVALID_REFERENCE),
])
->addTag('monolog.logger', ['channel' => sprintf('%s_bus.%s', $type, $name)])
->addTag(sprintf('prooph_service_bus.%s.plugin', $name));
->addTag('monolog.logger', ['channel' => \sprintf('%s_bus.%s', $type, $name)])
->addTag(\sprintf('prooph_service_bus.%s.plugin', $name));

// define message factory
$messageFactoryId = 'prooph_service_bus.message_factory.' . $name;
Expand All @@ -176,7 +176,7 @@ private function loadBus(string $type, string $name, array $options, ContainerBu
$messageFactoryPluginId = 'prooph_service_bus.message_factory_plugin.' . $name;
$messageFactoryPluginDefinition = new ChildDefinition('prooph_service_bus.message_factory_plugin');
$messageFactoryPluginDefinition->setArguments([new Reference($messageFactoryId)]);
$messageFactoryPluginDefinition->addTag(sprintf('prooph_service_bus.%s.plugin', $name));
$messageFactoryPluginDefinition->addTag(\sprintf('prooph_service_bus.%s.plugin', $name));

$container->setDefinition($messageFactoryPluginId, $messageFactoryPluginDefinition);

Expand All @@ -198,30 +198,30 @@ private function loadBus(string $type, string $name, array $options, ContainerBu
new Reference($options['router']['async_switch']),
]);
}
$routerDefinition->addTag(sprintf('prooph_service_bus.%s.plugin', $name));
$routerDefinition->addTag(\sprintf('prooph_service_bus.%s.plugin', $name));

$container->setDefinition($routerId, $routerDefinition);
}

// define service locator
$routeTargets = array_values($options['router']['routes'] ?? []);
$routeTargets = \array_values($options['router']['routes'] ?? []);
if ($type === 'event') {
$routeTargets = array_merge([], ...$routeTargets);
$routeTargets = \array_merge([], ...$routeTargets);
}
$routeTargets = array_unique($routeTargets);
$serviceLocatorId = sprintf('%s.plugin.service_locator.locator', $name);
$routeTargets = \array_unique($routeTargets);
$serviceLocatorId = \sprintf('%s.plugin.service_locator.locator', $name);
$serviceLocator = $container->register($serviceLocatorId, ServiceLocator::class);
$serviceLocator->addTag('container.service_locator');
$serviceLocator->setArgument(0, array_map(function (string $id) {
$serviceLocator->setArgument(0, \array_map(function (string $id) {
return new Reference($id);
}, array_combine($routeTargets, $routeTargets)));
}, \array_combine($routeTargets, $routeTargets)));

// and the plugin for it
$serviceLocatorPlugin = new ChildDefinition('prooph_service_bus.plugin.service_locator');
$serviceLocatorPlugin->setArgument(0, new Reference($serviceLocatorId));
$serviceLocatorPlugin->addTag(sprintf('prooph_service_bus.%s.plugin', $name));
$container->setDefinition(sprintf('%s.plugin.service_locator', $name), $serviceLocatorPlugin);
$serviceLocatorPlugin->addTag(\sprintf('prooph_service_bus.%s.plugin', $name));
$container->setDefinition(\sprintf('%s.plugin.service_locator', $name), $serviceLocatorPlugin);

$container->setParameter(sprintf('prooph_service_bus.%s.configuration', $name), $options);
$container->setParameter(\sprintf('prooph_service_bus.%s.configuration', $name), $options);
}
}
6 changes: 3 additions & 3 deletions src/Exception/CompilerPassException.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class CompilerPassException extends RuntimeException
{
public static function messageTagMissing(string $serviceId): self
{
return new self(sprintf(
return new self(\sprintf(
'The "message" tag key is missing from tag. '
. 'Either provide a "message" tag or enable "message_detection" for service "%s"',
$serviceId
Expand All @@ -17,7 +17,7 @@ public static function messageTagMissing(string $serviceId): self

public static function tagCountExceeded(string $type, string $serviceId, string $busName): self
{
return new self(sprintf(
return new self(\sprintf(
'More than 1 %s handler tagged on service "%s" with tag "%s". Only events can have multiple handlers',
$type,
$serviceId,
Expand All @@ -27,7 +27,7 @@ public static function tagCountExceeded(string $type, string $serviceId, string

public static function unknownHandlerClass(string $className, string $serviceId, string $busName): self
{
return new self(sprintf(
return new self(\sprintf(
'Service %s has been tagged as %s handler, but its class %s does not exist',
$serviceId,
$busName,
Expand Down
Loading

0 comments on commit 1341e59

Please sign in to comment.