| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace controllers; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use generators\AddonXml\AddonXmlGenerator; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use generators\Language\LanguageGenerator; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use generators\Readme\ReadmeGenerator; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use generators\MultipleFileGenerator; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use generators\FileGenerator; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use terminal\Terminal; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use filesystem\Filesystem; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use mediators\GeneratorMediator; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use \Config; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | class AddonXml extends AbstractController | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |     private $mfGenerator; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |     private static $allowedMethods = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |         'help', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |         'create', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |         'remove', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |         'update' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |     ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     use HelpTrait; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     function __construct( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |         Config              $config, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |         Terminal            $terminal, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |         Filesystem          $filesystem | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |         $this->config               = $config; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |         $this->terminal             = $terminal; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |         $this->filesystem           = $filesystem; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |         $addonXmlGenerator      = new AddonXmlGenerator($this->config); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |         $languageGenerator      = new LanguageGenerator($this->config); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |         $generatorMediator      = new GeneratorMediator(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |         $generatorMediator | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |             ->addGenerator($addonXmlGenerator) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |             ->addGenerator($languageGenerator); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |         $this->mfGenerator = new MultipleFileGenerator($this->filesystem); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |         $this->mfGenerator | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |             ->addGenerator($addonXmlGenerator) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |             ->addGenerator($languageGenerator); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |      * @inheritdoc | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |     public static function getAllowedMethods(): array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |         return self::$allowedMethods; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |      * help: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |      * addon-xml create | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |      * creates addonXml structure and write it to file | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |      * @throws Exception if file already exists | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |     public function create() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |         $addonXmlFileGenerator = $this->mfGenerator | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |             ->find(AddonXmlGenerator::class); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |          | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |         $addonXmlGenerator = $addonXmlFileGenerator | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |                 ->throwIfExists('Such addon.xml already exists. Remove it first if you want to replace it.') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |                 ->extract(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |                  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |         $addonXmlGenerator | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |             ->create(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |         $addonXmlFileGenerator     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |             ->write() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |             ->throwIfNotExists($addonXmlGenerator->getPath() . ' cannot be created.'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |         /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |          * results | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |          */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |         $this->terminal->success($addonXmlGenerator->getPath() . ' was created'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |         $this->terminal->diff( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |             \Diff::toString(\Diff::compare('', $addonXmlGenerator->toString())) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |      * help: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |      * addon-xml remove | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |      * removes file addon.xml | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |      * @throws Exception if file doesn't exists | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |     public function remove() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |         $addonXmlGenerator = $this->mfGenerator | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |             ->find(AddonXmlGenerator::class) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |                 ->read() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |                 ->remove() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |                 ->throwIfExists('File cannot be removed.') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |                 ->extract(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |         $this->terminal->success($addonXmlGenerator->getPath() . ' was removed'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |         $this->terminal->diff( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |             \Diff::toString(\Diff::compare($addonXmlGenerator->toString(), '')) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |      * help: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |      * addon-xml/update --addon.id <addon_id> <item> [remove] [...args] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |      * Sets additional field to addon xml file | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |      * addon.id - id of the addon | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |      * --- | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |      *      --settings-item - <item id="date">...</item> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |      *           args: section=<section_id> type=<type> id=<id> [dv=<default_value>] [v=<variants>] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |      *              section         - id for the settings section | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |      *              type            - type of the item id: input, textarea, password, checkbox, selectbox, multiple select, multiple checkboxes, countries list, states list, file, info, header, template | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |      *              id              - id of the setting item | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |      *              default_value   - (df) - default value for setting item | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |      *              variants        - (v) - list of item value variants comma separated | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |      * --- | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |      *  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |      * see more @link [https://www.cs-cart.ru/docs/4.9.x/developer_guide/addons/scheme/scheme3.0_structure.html] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |      * @throws Exception if file doesn't exists | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |     public function update() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |         $this->mfGenerator | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |             ->throwIfNotExists('Some addon file not found.') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |             ->read(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |         $old_content = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |         $this->mfGenerator->each(function($generator) use (&$old_content) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |             $old_content[get_class($generator->extract())] = $generator->extract()->toString(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |         }); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |          | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |         $addonXmlGenerator = $this->mfGenerator | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |             ->find(AddonXmlGenerator::class) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |                 ->extract(); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |         $method     = $this->getMethodName(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |         $class_name = get_class($this); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |         if (method_exists($class_name, $method)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |             $this->{$method}($addonXmlGenerator); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |             throw new \BadMethodCallException('There is no such command'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |         $this->mfGenerator | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |             ->write() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |             ->throwIfNotExists(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |          | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |         $this->mfGenerator->each(function($generator) use ($old_content) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |             $this->terminal->diff( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  |                 \Diff::toString(\Diff::compare($old_content[get_class($generator->extract())], $generator->extract()->toString())) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  |             ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  |         }); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  |      *  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 165 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 166 |  |  |     public function setSettingsItem($addonXmlGenerator) | 
            
                                                                        
                            
            
                                    
            
            
                | 167 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 168 |  |  |         $addonXmlGenerator->setSetting( | 
            
                                                                        
                            
            
                                    
            
            
                | 169 |  |  |             $this->config->get('section'), | 
            
                                                                        
                            
            
                                    
            
            
                | 170 |  |  |             $this->config->get('type'), | 
            
                                                                        
                            
            
                                    
            
            
                | 171 |  |  |             $this->config->get('id'), | 
            
                                                                        
                            
            
                                    
            
            
                | 172 |  |  |             $this->config->getOr('default_value', 'dv') ?: '', | 
            
                                                                        
                            
            
                                    
            
            
                | 173 |  |  |             (function($config) { | 
            
                                                                        
                            
            
                                    
            
            
                | 174 |  |  |                 $variants = $config->getOr('variants', 'v'); | 
            
                                                                        
                            
            
                                    
            
            
                | 175 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 176 |  |  |                 return $variants ? explode(',', $variants) : []; | 
            
                                                                        
                            
            
                                    
            
            
                | 177 |  |  |             })($this->config) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 178 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 179 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 180 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 181 |  |  |     public function removeSettingsItem($addonXmlGenerator) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 182 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 183 |  |  |         $addonXmlGenerator->removeSetting( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 184 |  |  |             $this->config->get('id') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 185 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 186 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 187 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 188 |  |  |     public function updateAutocomplete() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 189 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 190 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 191 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 192 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 193 |  |  |     public function updateSettingsItemAutocomplete() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 194 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 195 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 196 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 197 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 198 |  |  |  |