| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * This file is part of the `tvi/monitor-bundle` project. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * (c) https://github.com/turnaev/monitor-bundle/graphs/contributors | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * For the full copyright and license information, please view the LICENSE.md | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * file that was distributed with this source code. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  */ | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                            
                                                                                            
                                                                                            
                                                                                            
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | namespace Tvi\MonitorBundle\DependencyInjection\Compiler; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use Symfony\Component\DependencyInjection\ContainerBuilder; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | use Symfony\Component\DependencyInjection\Definition; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | use Tvi\MonitorBundle\DependencyInjection\DiTags; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | /** | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  * @author Vladimir Turnaev <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  */ | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                            
                                                                                            
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | class AddCheckPluginsCompilerPass implements CompilerPassInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     public const SERVICE_ID_FORMAT = 'tvi_monitor.check.%s'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     /** | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |      * @var array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     protected $checkServiceConfs = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 | 47 |  |     public function process(ContainerBuilder $container) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 | 47 |  |         $this->processChecks($container); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 | 47 |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 35 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 36 | 47 |  |     protected function processChecks(ContainerBuilder $container) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 38 | 47 |  |         $checkConfigs = $container->getParameter('tvi_monitor.conf.checks'); | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 40 | 47 |  |         $container->setParameter('tvi_monitor.checks.conf', null); | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 42 | 47 |  |         $checkServiceIds = $container->findTaggedServiceIds(DiTags::CHECK_PLUGIN); | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 44 | 47 |  |         foreach ($checkServiceIds as $checkServiceId => $null) { | 
            
                                                                        
                            
            
                                    
            
            
                | 45 | 39 |  |             $checkDefinitionTpl = $container->getDefinition($checkServiceId); | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 47 | 39 |  |             $container->removeDefinition($checkServiceId); | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 49 | 39 |  |             $checkTag = $checkDefinitionTpl->getTag(DiTags::CHECK_PLUGIN); | 
            
                                                                        
                            
            
                                    
            
            
                | 50 | 39 |  |             $checkPluginAlias = $checkTag[0]['alias']; | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 52 | 39 |  |             $checkConfig = $checkConfigs[$checkPluginAlias]; | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 55 | 39 |  |             if (isset($checkConfig['_singl'])) { | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 57 | 37 |  |                 $this->addCheckPlugin($container, $checkDefinitionTpl, $checkConfig['_singl'], $checkPluginAlias); | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  |             } | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 60 | 39 |  |             if (isset($checkConfig['_multi'])) { | 
            
                                                                        
                            
            
                                    
            
            
                | 61 | 37 |  |                 foreach ($checkConfig['_multi'] as $pref => $conf) { | 
            
                                                                        
                            
            
                                    
            
            
                | 62 | 39 |  |                     $this->addCheckPlugin($container, $checkDefinitionTpl, $conf, $checkPluginAlias, $pref); | 
            
                                                                        
                            
            
                                    
            
            
                | 63 |  |  |                 } | 
            
                                                                        
                            
            
                                    
            
            
                | 64 |  |  |             } | 
            
                                                                        
                            
            
                                    
            
            
                | 65 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 66 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 67 | 47 |  |         $registryDefinition = $container->getDefinition('tvi_monitor.checks.manager'); | 
            
                                                                        
                            
            
                                    
            
            
                | 68 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 69 | 47 |  |         $tagConfs = $container->getParameter('tvi_monitor.conf.tags'); | 
            
                                                                        
                            
            
                                    
            
            
                | 70 | 47 |  |         $container->setParameter('tvi_monitor.tags', null); | 
            
                                                                        
                            
            
                                    
            
            
                | 71 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 72 | 47 |  |         $groupConfs = $container->getParameter('tvi_monitor.conf.groups'); | 
            
                                                                        
                            
            
                                    
            
            
                | 73 | 47 |  |         $container->setParameter('tvi_monitor.conf.groups', null); | 
            
                                                                        
                            
            
                                    
            
            
                | 74 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 75 | 47 |  |         $registryDefinition->addMethodCall('add', [$this->checkServiceConfs, $tagConfs, $groupConfs]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 | 47 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 | 39 |  |     protected function addCheckPlugin(ContainerBuilder $container, Definition $checkPluginDefinitionTpl, array $conf, string $checkPluginAlias, string $checkPluginPref = null) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 | 39 |  |         $checkPluginDefinition = clone $checkPluginDefinitionTpl; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 | 39 |  |         if (null !== $checkPluginPref) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 | 37 |  |             $checkPluginAlias .= '.'.$checkPluginPref; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 | 39 |  |         if(isset($conf['importance'])) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 | 1 |  |             $conf['tags'][] = $conf['importance']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 | 39 |  |         foreach ($checkPluginDefinition->getArguments() as $argumentIndex => $argument) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 | 30 |  |             $argument = str_replace('%%', '', $argument); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 | 30 |  |             if (array_key_exists($argument, $conf['check'])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 | 30 |  |                 $checkPluginDefinition->replaceArgument($argumentIndex, $conf['check'][$argument]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 | 39 |  |         $methodCalls = $checkPluginDefinition->getMethodCalls(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 | 39 |  |         foreach ($methodCalls as &$methodCall) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 | 39 |  |             if ('setAdditionParams' === $methodCall[0]) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 | 39 |  |                 $conf['id'] = $checkPluginAlias; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 | 39 |  |                 $methodCall[1][0] = $conf; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 | 39 |  |         $checkPluginDefinition->setMethodCalls($methodCalls); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 | 39 |  |         $checkServiceId = sprintf(self::SERVICE_ID_FORMAT, sha1($checkPluginAlias)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 | 39 |  |         $container->setDefinition($checkServiceId, $checkPluginDefinition); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 | 39 |  |         $this->checkServiceConfs[$checkPluginAlias] = ['serviceId' => $checkServiceId, 'group' => $conf['group'], 'tags' => $conf['tags']]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 | 39 |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 116 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 117 |  |  |  |