     1: <?php
     2: /*************************************************************************************/
     3: /*                                                                                   */
     4: /*      Thelia                                                                       */
     5: /*                                                                                   */
     6: /*      Copyright (c) OpenStudio                                                     */
     7: /*  email : info@thelia.net                                                      */
     8: /*      web : http://www.thelia.net                                                  */
     9: /*                                                                                   */
    10: /*      This program is free software; you can redistribute it and/or modify         */
    11: /*      it under the terms of the GNU General Public License as published by         */
    12: /*      the Free Software Foundation; either version 3 of the License                */
    13: /*                                                                                   */
    14: /*      This program is distributed in the hope that it will be useful,              */
    15: /*      but WITHOUT ANY WARRANTY; without even the implied warranty of               */
    16: /*      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                */
    17: /*      GNU General Public License for more details.                                 */
    18: /*                                                                                   */
    19: /*      You should have received a copy of the GNU General Public License            */
    20: /*      along with this program. If not, see <http://www.gnu.org/licenses/>.     */
    21: /*                                                                                   */
    22: /*************************************************************************************/
    23:
    24: namespace Thelia\Core\DependencyInjection\Compiler;
    25:
    26: use Symfony\Component\DependencyInjection\ContainerBuilder;
    27: use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
    28:
    29: /**
    30:  * Class RegisterListenersPass
    31:  * @package Thelia\Core\DependencyInjection\Compiler
    32:  *
    33:  * Source code come from Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\RegisterKernelListenersPass class
    34:  */
    35: class RegisterListenersPass implements CompilerPassInterface
    36: {
    37:     public function process(ContainerBuilder $container)
    38:     {
    39:         if (!$container->hasDefinition('event_dispatcher')) {
    40:             return;
    41:         }
    42:
    43:         $definition = $container->getDefinition('event_dispatcher');
    44:
    45:         foreach ($container->findTaggedServiceIds('kernel.event_listener') as $id => $events) {
    46:             foreach ($events as $event) {
    47:                 $priority = isset($event['priority']) ? $event['priority'] : 0;
    48:
    49:                 if (!isset($event['event'])) {
    50:                     throw new \InvalidArgumentException(sprintf('Service "%s" must define the "event" attribute on "kernel.event_listener" tags.', $id));
    51:                 }
    52:
    53:                 if (!isset($event['method'])) {
    54:                     $event['method'] = 'on'.preg_replace(array(
    55:                         '/(?<=\b)[a-z]/ie',
    56:                         '/[^a-z0-9]/i'
    57:                     ), array('strtoupper("\\0")', ''), $event['event']);
    58:                 }
    59:
    60:                 $definition->addMethodCall('addListenerService', array($event['event'], array($id, $event['method']), $priority));
    61:             }
    62:         }
    63:
    64:         foreach ($container->findTaggedServiceIds('kernel.event_subscriber') as $id => $attributes) {
    65:             // We must assume that the class value has been correctly filled, even if the service is created by a factory
    66:             $class = $container->getDefinition($id)->getClass();
    67:
    68:             $refClass = new \ReflectionClass($class);
    69:             $interface = 'Symfony\Component\EventDispatcher\EventSubscriberInterface';
    70:             if (!$refClass->implementsInterface($interface)) {
    71:                 throw new \InvalidArgumentException(sprintf('Service "%s" must implement interface "%s".', $id, $interface));
    72:             }
    73:
    74:             $definition->addMethodCall('addSubscriberService', array($id, $class));
    75:         }
    76:     }
    77: }