| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | /* | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * This file is a part of Sculpin. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * (c) Dragonfly Development Inc. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * For the full copyright and license information, please view the LICENSE | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * file that was distributed with this source code. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | namespace Symplify\PHP7_Sculpin\ProxySourceCollection; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | //use Doctrine\Common\Inflector\Inflector; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use Symplify\PHP7_Sculpin\DataProvider\DataProviderInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | use Symplify\PHP7_Sculpin\Event\ConvertEvent; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | use Symplify\PHP7_Sculpin\Event\SculpinEvents; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | use Symplify\PHP7_Sculpin\Event\SourceSetEvent; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | use Symplify\PHP7_Sculpin\Formatter\FormatterManager; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | use Symplify\PHP7_Sculpin\Source\Filter\FilterInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | use Symplify\PHP7_Sculpin\Source\Map\MapInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | use Symfony\Component\EventDispatcher\EventSubscriberInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | final class ProxySourceCollectionDataProvider implements DataProviderInterface, EventSubscriberInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     private $formatterManager; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     private $dataProviderName; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     private $collection = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     private $filter; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     private $map; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     private $factory; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |     public function __construct( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |         FormatterManager $formatterManager, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |         string $dataProviderName, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |         ProxySourceCollection $collection, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |         FilterInterface $filter, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |         MapInterface $map, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |         SimpleProxySourceItemFactory $factory | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |     ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |         $this->formatterManager = $formatterManager; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |         $this->dataProviderName = $dataProviderName; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |         $this->collection = $collection; // ?: new ProxySourceCollection(); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |         $this->filter = $filter; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |         $this->map = $map; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |         $this->factory = $factory; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |      * {@inheritdoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |     public function provideData() : ProxySourceCollection | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |         return $this->collection; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |     public static function getSubscribedEvents() : array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |         return [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |             SculpinEvents::EVENT_BEFORE_RUN => [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |                 ['beforeRun', 0], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |                 ['beforeRunPost', -100], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |             ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |             SculpinEvents::EVENT_AFTER_CONVERT => 'afterConvert', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |     public function beforeRun(SourceSetEvent $sourceSetEvent) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |         foreach ($sourceSetEvent->updatedSources() as $source) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |             if ($source->isGenerated()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |                 // We want to skip generated sources in case someone is | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |                 // doing something like a redirect where virtual sources are | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |                 // created like a redirect plugin. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |                 // NOTE: This means that a generator cannot create proxy | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |                 // source collection items. This could be limiting in the | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |                 // future... | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |                 continue; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |             if ($this->filter->match($source)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |                 $this->map->process($source); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |                 $this->collection[$source->sourceId()] = $this->factory->createProxySourceItem($source); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |         $foudAtLeastOne = false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |         foreach ($sourceSetEvent->allSources() as $source) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |             if ($this->filter->match($source)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |                 $foudAtLeastOne = true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |                 break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |         if (!$foudAtLeastOne) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |             echo 'Didnt find at least one of this type : '.$this->dataProviderName.PHP_EOL; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |         $this->collection->init(); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |     public function beforeRunPost(SourceSetEvent $sourceSetEvent) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |         $anItemHasChanged = false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |         foreach ($this->collection as $item) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |             if ($item->hasChanged()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |                 $anItemHasChanged = true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |                 $this->collection->init(); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |                 break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |         if ($anItemHasChanged) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |             foreach ($sourceSetEvent->allSources() as $source) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |                 if ($source->data()->get('use') and in_array($this->dataProviderName, $source->data()->get('use'))) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |                     $source->forceReprocess(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  | //        foreach ($this->collection as $item) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  | //            $item->data()->set('next_'.$this->dataSingularName, $item->nextItem()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  | //            $item->data()->set('previous_'.$this->dataSingularName, $item->previousItem()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  | //        } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 124 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 125 |  |  |     public function afterConvert(ConvertEvent $convertEvent) | 
            
                                                        
            
                                    
            
            
                | 126 |  |  |     { | 
            
                                                        
            
                                    
            
            
                | 127 |  |  |         $sourceId = $convertEvent->source()->sourceId(); | 
            
                                                        
            
                                    
            
            
                | 128 |  |  |         if (isset($this->collection[$sourceId])) { | 
            
                                                        
            
                                    
            
            
                | 129 |  |  |             $item = $this->collection[$sourceId]; | 
            
                                                        
            
                                    
            
            
                | 130 |  |  |             $item->setBlocks($this->formatterManager->formatSourceBlocks($convertEvent->source())); | 
            
                                                        
            
                                    
            
            
                | 131 |  |  |         } | 
            
                                                        
            
                                    
            
            
                | 132 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 133 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 134 |  |  |  | 
            
                        
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.