| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Knp\Bundle\SnappyBundle\Snappy\Generator; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Knp\Snappy\GeneratorInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Psr\Log\LoggerInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * Wraps a GeneratorInterface instance to log the media generations using the | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * configured logger. | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 11 |  |  |  */ | 
            
                                                                        
                            
            
                                    
            
            
                | 12 |  |  | class LoggableGenerator implements GeneratorInterface | 
            
                                                                        
                            
            
                                    
            
            
                | 13 |  |  | { | 
            
                                                                        
                            
            
                                    
            
            
                | 14 |  |  |     private $generator; | 
            
                                                                        
                            
            
                                    
            
            
                | 15 |  |  |     private $logger; | 
            
                                                                        
                            
            
                                    
            
            
                | 16 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 17 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 18 |  |  |      * Constructor. | 
            
                                                                        
                            
            
                                    
            
            
                | 19 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 20 |  |  |      * @param GeneratorInterface $generator | 
            
                                                                        
                            
            
                                    
            
            
                | 21 |  |  |      * @param LoggerInterface    $logger | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 22 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |     public function __construct(GeneratorInterface $generator, LoggerInterface $logger = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |         $this->generator = $generator; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |         $this->logger = $logger; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 27 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 28 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  |      * Returns the underlying generator instance. | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  |      * @return GeneratorInterface | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     public function getInternalGenerator() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |         return $this->generator; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 37 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 38 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  |      * {@inheritdoc} | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |     public function generate($input, $output, array $options = [], $overwrite = false) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |         if (is_array($input)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |             $debug_input = implode(', ', $input); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |             $debug_input = $input; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |         $this->logDebug(sprintf('Generate from file (%s) to file (%s).', $debug_input, $output)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |         $this->generator->generate($input, $output, $options, $overwrite); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 52 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  |      * {@inheritdoc} | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |     public function generateFromHtml($html, $output, array $options = [], $overwrite = false) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |         $debugHtml = is_array($html) ? implode(', ', $html) : $html; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |         $this->logDebug(sprintf('Generate from HTML (%s) to file (%s).', substr($debugHtml, 0, 100), $output)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |         $this->generator->generateFromHtml($html, $output, $options, $overwrite); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 64 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 65 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 66 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 67 |  |  |      * {@inheritdoc} | 
            
                                                                        
                            
            
                                    
            
            
                | 68 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |     public function getOutput($input, array $options = []) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |         if (is_array($input)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |             $debug_input = implode(', ', $input); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |             $debug_input = $input; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |         $this->logDebug(sprintf('Output from file (%s).', $debug_input)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |         return $this->generator->getOutput($input, $options); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 79 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 80 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 81 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 82 |  |  |      * {@inheritdoc} | 
            
                                                                        
                            
            
                                    
            
            
                | 83 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |     public function getOutputFromHtml($html, array $options = []) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |         $debugHtml = is_array($html) ? implode(', ', $html) : $html; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |         $this->logDebug(sprintf('Output from HTML (%s).', substr($debugHtml, 0, 100))); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |         return $this->generator->getOutputFromHtml($html, $options); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 91 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 92 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 93 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 94 |  |  |      * {@inheritdoc} | 
            
                                                                        
                            
            
                                    
            
            
                | 95 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |     public function setOption($name, $value) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |         $this->logDebug(sprintf('Set option %s = %s.', $name, var_export($value, true))); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |         return $this->generator->setOption($name, $value); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 101 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 102 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 103 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 104 |  |  |      * Logs the given debug message if the logger is configured or do nothing | 
            
                                                                        
                            
            
                                    
            
            
                | 105 |  |  |      * otherwise. | 
            
                                                                        
                            
            
                                    
            
            
                | 106 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 107 |  |  |      * @param string $message | 
            
                                                                        
                            
            
                                    
            
            
                | 108 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |     private function logDebug($message) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |         if (null === $this->logger) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |             return; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |         $this->logger->debug($message); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 116 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 117 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 118 |  |  |  | 
            
                        
This check looks for
@paramannotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive.
Most often this is a case of a parameter that can be null in addition to its declared types.