| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | declare(strict_types=1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | namespace Yiisoft\Profiler; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Yiisoft\Strings\WildcardPattern; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * Target is the base class for all profiling target classes. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  * A profile target object will filter the messages stored by {@see Profiler} according | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  * to its {@see include} and {@see exclude} properties. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  * For more details and usage information on Target, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  * see the [guide article on profiling & targets](guide:runtime-profiling). | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | abstract class Target | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |      * @var array list of message categories that this target is interested in. Defaults to empty, meaning all | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |      * categories. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |      * You can use an asterisk at the end of a category so that the category may be used to | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |      * match those categories sharing the same common prefix. For example, 'Yiisoft\Db\*' will match | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |      * categories starting with 'Yiisoft\Db\', such as `Yiisoft\Db\Connection`. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     private array $include = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |      * @var array list of message categories that this target is NOT interested in. Defaults to empty, meaning no | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |      * uninteresting messages. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |      * If this property is not empty, then any category listed here will be excluded from {@see include}. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |      * You can use an asterisk at the end of a category so that the category can be used to | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |      * match those categories sharing the same common prefix. For example, 'Yiisoft\Db\*' will match | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |      * categories starting with 'Yiisoft\Db\', such as `Yiisoft\Db\Connection`. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |      * {@see include} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     private array $exclude = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |      * @var bool whether to enable this log target. Defaults to true. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |     private bool $enabled = true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |      * Processes the given log messages. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |      * This method will filter the given messages with {@see levels} and {@see categories}. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |      * And if requested, it will also export the filtering result to specific medium (e.g. email). | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |      * @param array $messages profiling messages to be processed. See {@see Profiler::$messages} for the structure | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 | 2 |  |      * of each message. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 | 2 |  |     public function collect(array $messages): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 | 1 |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |         if (!$this->enabled) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |             return; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 | 2 |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 | 2 |  |         $messages = $this->filterMessages($messages); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 | 2 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |         if (count($messages) > 0) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 | 2 |  |             $this->export($messages); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 69 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 70 |  |  |     public function include(array $include): self | 
            
                                                                        
                            
            
                                    
            
            
                | 71 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 72 |  |  |         $this->include = $include; | 
            
                                                                        
                            
            
                                    
            
            
                | 73 |  |  |         return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |     public function exclude(array $exclude): self | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |         $this->exclude = $exclude; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |         return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |     public function enable(): self | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |         $this->enabled = true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 | 7 |  |         return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 | 7 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 | 7 |  |     public function disable(): self | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 | 7 |  |         $this->enabled = false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 | 2 |  |         return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 | 1 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 | 2 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 | 1 |  |     public function isEnabled(): bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 | 1 |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |         return $this->enabled; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 | 7 |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 | 6 |  |      * Exports profiling messages to a specific destination. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 | 2 |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 | 2 |  |      * Child classes must implement this method. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 | 2 |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 | 1 |  |      * @param Message[] $messages profiling messages to be exported. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 | 1 |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |     abstract public function export(array $messages); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |      * Filters the given messages according to their categories. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 | 7 |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 | 2 |  |      * @param Message[] $messages messages to be filtered. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |      * The message structure follows that in {@see Profiler::$messages}. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 | 7 |  |      * @return array the filtered messages. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |     protected function filterMessages(array $messages): array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |         foreach ($messages as $i => $message) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |             if (!$this->isCategoryMatched($message->level())) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |                 unset($messages[$i]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |         return $messages; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |     private function isCategoryMatched($category): bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |         $matched = empty($this->include); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |         foreach ($this->include as $pattern) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |             if ((new WildcardPattern($pattern))->match($category)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |                 $matched = true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |                 break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |         if ($matched) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |             foreach ($this->exclude as $pattern) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |                 if ((new WildcardPattern($pattern))->match($category)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |                     $matched = false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |                     break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |         return $matched; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 147 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 148 |  |  |  |