src/DependencyInjection/M6WebStatsdExtension.php
protected function loadClient($container, $alias, array $config, array $servers, $baseEvents)
    {
        $usedServers    = [];
        $events         = $config['events'];
        $matchedServers = [];

        if ($config['servers'][0] == 'all') {
            // Use all servers
            $matchedServers = array_keys($servers);
        } else {
            // Use only declared servers
            foreach ($config['servers'] as $serverAlias) {

                // Named server
                if (array_key_exists($serverAlias, $servers)) {
                    $matchedServers[] = $serverAlias;
                    continue;
                }

                // Search matchning server config name
                $found = false;
                foreach (array_keys($servers) as $key) {
                    if (fnmatch($serverAlias, $key)) {
                        $matchedServers[] = $key;
                        $found            = true;
                    }
                }

                // No server found
                if (!$found) {
                    throw new InvalidConfigurationException(sprintf(
                        'M6WebStatsd client %s used server %s which is not defined in the servers section',
                        $alias,
                        $serverAlias
                    ));
                }
            }
        }

        // Matched server congurations
        foreach ($matchedServers as $serverAlias) {
            $usedServers[] = [
                'address' => $servers[$serverAlias]['address'],
                'port'    => $servers[$serverAlias]['port']
            ];
        }

        // Add the statsd client configured
        $serviceId  = ($alias == 'default') ? 'm6_statsd' : 'm6_statsd.'.$alias;
        $definition = new Definition('M6Web\Bundle\StatsdBundle\Client\Client');
        $definition->setPublic(true);
        $definition->addArgument($usedServers);

        if (isset($config['to_send_limit'])) {
            $definition->addMethodCall('setToSendLimit', [$config['to_send_limit']]);
        }

        foreach ($events as $eventName => $eventConfig) {
            $definition->addTag('kernel.event_listener', ['event' => $eventName, 'method' => 'handleEvent']);
            $definition->addMethodCall('addEventToListen', [$eventName, $eventConfig]);
        }

        $definition->addMethodCall('setPropertyAccessor', [new Reference('property_accessor_statsdbundle')]);

        $container->setDefinition($serviceId, $definition);

        // Add the statsd client listener
        $serviceListenerId = $serviceId.'.listener';
        $definition = new Definition('M6Web\Bundle\StatsdBundle\Statsd\Listener');
        $definition->setPublic(true);
        $definition->addArgument(new Reference($serviceId));
        $definition->addArgument(new Reference('event_dispatcher'));
        $definition->addTag('kernel.event_listener', [
            'event'    => 'kernel.terminate',
            'method'   => 'onKernelTerminate',
            'priority' => -100
        ]);
        $definition->addTag('kernel.event_listener', [
            'event'    => 'console.terminate',
            'method'   => 'onConsoleTerminate',
            'priority' => -100
        ]);

        if ($baseEvents) {
            $definition->addTag('kernel.event_listener', [
                'event' => 'kernel.terminate',
                'method' => 'dispatchBaseEvents',
                'priority' => 0
            ]);
            $definition->addTag('kernel.event_listener', [
                'event' => 'kernel.exception',
                'method' => 'onKernelException',
                'priority' => 0
            ]);
        }
        $container->setDefinition($serviceListenerId, $definition);

        return $serviceId;
    }
protected function loadClient($container, $alias, array $config, array $servers, $baseEvents)
    {
        $usedServers    = [];
        $events         = $config['events'];
        $matchedServers = [];

        if ($config['servers'][0] == 'all') {
            // Use all servers
            $matchedServers = array_keys($servers);
        } else {
            // Use only declared servers
            foreach ($config['servers'] as $serverAlias) {

                // Named server
                if (array_key_exists($serverAlias, $servers)) {
                    $matchedServers[] = $serverAlias;
                    continue;
                }

                // Search matchning server config name
                $found = false;
                foreach (array_keys($servers) as $key) {
                    if (fnmatch($serverAlias, $key)) {
                        $matchedServers[] = $key;
                        $found            = true;
                    }
                }

                // No server found
                if (!$found) {
                    throw new InvalidConfigurationException(sprintf(
                        'M6WebStatsd client %s used server %s which is not defined in the servers section',
                        $alias,
                        $serverAlias
                    ));
                }
            }
        }

        // Matched server congurations
        foreach ($matchedServers as $serverAlias) {
            $usedServers[] = [
                'address' => $servers[$serverAlias]['address'],
                'port'    => $servers[$serverAlias]['port']
            ];
        }

        // Add the statsd client configured
        $serviceId  = ($alias == 'default') ? 'm6_statsd' : 'm6_statsd.'.$alias;
        $definition = new Definition('M6Web\Bundle\StatsdBundle\Client\Client');
        $definition->setPublic(true);
        $definition->addArgument($usedServers);

        if (isset($config['to_send_limit'])) {
            $definition->addMethodCall('setToSendLimit', [$config['to_send_limit']]);
        }

        foreach ($events as $eventName => $eventConfig) {
            $definition->addTag('kernel.event_listener', ['event' => $eventName, 'method' => 'handleEvent']);
            $definition->addMethodCall('addEventToListen', [$eventName, $eventConfig]);
        }

        $definition->addMethodCall('setPropertyAccessor', [new Reference('property_accessor_statsdbundle')]);

        $container->setDefinition($serviceId, $definition);

        // Add the statsd client listener
        $serviceListenerId = $serviceId.'.listener';
        $definition = new Definition('M6Web\Bundle\StatsdBundle\Statsd\Listener');
        $definition->setPublic(true);
        $definition->addArgument(new Reference($serviceId));
        $definition->addArgument(new Reference('event_dispatcher'));
        $definition->addTag('kernel.event_listener', [
            'event'    => 'kernel.terminate',
            'method'   => 'onKernelTerminate',
            'priority' => -100
        ]);
        $definition->addTag('kernel.event_listener', [
            'event'    => 'console.terminate',
            'method'   => 'onConsoleTerminate',
            'priority' => -100
        ]);

        if ($baseEvents) {
            $definition->addTag('kernel.event_listener', [
                'event' => 'kernel.terminate',
                'method' => 'dispatchBaseEvents',
                'priority' => 0
            ]);
            $definition->addTag('kernel.event_listener', [
                'event' => 'kernel.exception',
                'method' => 'onKernelException',
                'priority' => 0
            ]);
        }
        $container->setDefinition($serviceListenerId, $definition);

        return $serviceId;
    }
Load a client configuration as a service in the container. A client can use multiple servers

@param ContainerInterface $container  The container
@param string             $alias      Alias of the client
@param array              $config     Base config of the client
@param array              $servers    List of available servers as describe in the config file
@param boolean            $baseEvents Register base events

@throws InvalidConfigurationException
@return string the service name