| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace WebinoDev\Factory; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use BjyProfiler\Db\Adapter\ProfilingAdapter; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use BjyProfiler\Db\Profiler\LoggingProfiler; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use BjyProfiler\Db\Profiler\Profiler; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Zend\Db\Adapter\Adapter; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use Zend\Log; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use Zend\ServiceManager\FactoryInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use Zend\ServiceManager\ServiceLocatorInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  * Class ProfilingAdapterFactory | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | class ProfilingAdapterFactory implements FactoryInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |      * @param ServiceLocatorInterface $services | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |      * @return ProfilingAdapter|bool|Adapter | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 21 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 22 |  |  |     public function createService(ServiceLocatorInterface $services) | 
            
                                                                        
                            
            
                                    
            
            
                | 23 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 24 |  |  |         $config = $services->get('Config'); | 
            
                                                                        
                            
            
                                    
            
            
                | 25 |  |  |         if (empty($config['db'])) { | 
            
                                                                        
                            
            
                                    
            
            
                | 26 |  |  |             return false; | 
            
                                                                        
                            
            
                                    
            
            
                | 27 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 28 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  |         if (!class_exists(ProfilingAdapter::class)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  |             return new Adapter($config['db']); | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  |         $adapter = new ProfilingAdapter($config['db']); | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  |         $adapter->setProfiler($this->createProfiler()); | 
            
                                                                        
                            
            
                                    
            
            
                | 35 |  |  |         $adapter->injectProfilingStatementPrototype(!empty($config['db']['options']) ? $config['db'] : []); | 
            
                                                                        
                            
            
                                    
            
            
                | 36 |  |  |         return $adapter; | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |      * @return Profiler|LoggingProfiler | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |     protected function createProfiler() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |         if ('cli' === PHP_SAPI) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |             // In CLI mode write queries | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |             // profiling info to stdout | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |             $logger = new Log\Logger; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |             $logger->addWriter(new Log\Writer\Stream('php://output'), Log\Logger::DEBUG); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |             return new LoggingProfiler($logger); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |         return new Profiler; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 54 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 55 |  |  |  |