| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | /* | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  * This program is free software: you can redistribute it and/or modify | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * it under the terms of the GNU General Public License as published by | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * the Free Software Foundation, either version 3 of the License, or | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * (at your option) any later version. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * This program is distributed in the hope that it will be useful, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * but WITHOUT ANY WARRANTY; without even the implied warranty of | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * GNU General Public License for more details. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  * You should have received a copy of the GNU General Public License | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  * along with this program.  If not, see <http://www.gnu.org/licenses/>. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | namespace Romm\Formz\DataCollectors; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | use Konafets\TYPO3DebugBar\DataCollectors\BaseCollector; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | use Romm\Formz\Form\FormObject; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | use Romm\Formz\Form\FormObjectFactory; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | use TYPO3\CMS\Core\Utility\GeneralUtility; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | use TYPO3\CMS\Extbase\Utility\DebuggerUtility; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | class FormzCollector extends BaseCollector | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |      * @var FormObject[] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     private $formzInstances = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |      * Called by the DebugBar when data needs to be collected | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |      * @return array Collected data | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |     public function collect() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |         $formzInstances = $this->getFormzInstances(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |         $debug = ''; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |         foreach ($formzInstances as $instance) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |             $debug .= DebuggerUtility::var_dump($instance, $instance->getName(), 10, false, true, true); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |             $forms[] = [ | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |                 'name' => $instance->getName(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |                 'params' => [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |                     'Class Name' => $instance->getClassName(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |                     'Fields' => $instance->getProperties(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |                     'Hash' => $instance->getHash() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |                 ] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |             ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |         $formsCount = \count($formzInstances); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |         $output = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |             'formsCount' => $formsCount, // Number of formz instance in the page | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |             'debug' => $debug, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |             'status' => '<b>' . $formsCount . '</b> Formz instance(s) in this page', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |             'forms' => $forms, | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |             'console' => '' // div for output console javascript | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |         return $output; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |      * @return FormObject[] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |     public function getFormzInstances() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |         $formObjFactory = GeneralUtility::makeInstance(FormObjectFactory::class); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |         foreach ($formObjFactory->getInstances() as $instance) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |             $this->formzInstances[$instance->getName()] = $instance; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |         return $this->formzInstances; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |      * Returns a hash where keys are control names and their values | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |      * an array of options as defined in {@see DebugBar\JavascriptRenderer::addControl()} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |      * @return array | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 86 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 87 |  |  |     public function getWidgets() | 
            
                                                                        
                            
            
                                    
            
            
                | 88 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 89 |  |  |         $name = $this->getName(); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 90 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 91 |  |  |         return [ | 
            
                                                                        
                            
            
                                    
            
            
                | 92 |  |  |             (string) $name => [ | 
            
                                                                        
                            
            
                                    
            
            
                | 93 |  |  |                 'icon' => $name, | 
            
                                                                        
                            
            
                                    
            
            
                | 94 |  |  |                 'widget' => 'PhpDebugBar.Widgets.FormzWidget', | 
            
                                                                        
                            
            
                                    
            
            
                | 95 |  |  |                 'map' => $name, | 
            
                                                                        
                            
            
                                    
            
            
                | 96 |  |  |                 'default' => '[]', | 
            
                                                                        
                            
            
                                    
            
            
                | 97 |  |  |             ], | 
            
                                                                        
                            
            
                                    
            
            
                | 98 |  |  |             "$name:badge" => [ | 
            
                                                                        
                            
            
                                    
            
            
                | 99 |  |  |                 'map' => 'formz.formsCount', | 
            
                                                                        
                            
            
                                    
            
            
                | 100 |  |  |                 'default' => 0, | 
            
                                                                        
                            
            
                                    
            
            
                | 101 |  |  |             ], | 
            
                                                                        
                            
            
                                    
            
            
                | 102 |  |  |         ]; | 
            
                                                                        
                            
            
                                    
            
            
                | 103 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |      * Returns the unique name of the collector | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |      * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |     public function getName() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |         return 'formz'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 114 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 115 |  |  |  | 
            
                        
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArrayis initialized the first time when the foreach loop is entered. You can also see that the value of thebarkey is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.