Issues (43)

app/controllers/AddonXml.php (1 issue)

Labels
Severity
1
<?php
2
3
namespace controllers;
4
5
use generators\AddonXml\AddonXmlGenerator;
6
use generators\Language\LanguageGenerator;
7
use generators\MultipleFileGenerator;
8
use terminal\Terminal;
9
use filesystem\Filesystem;
10
use autocomplete\Autocomplete;
11
use mediators\GeneratorMediator;
12
use \Config;
13
14
class AddonXml extends AbstractController
15
{
16
    private $mfGenerator;
17
    private static $allowedMethods = [
18
        'help',
19
        'update'
20
    ];
21
22
    use HelpTrait;
23
24
    function __construct(
25
        Config              $config,
26
        Terminal            $terminal,
27
        Filesystem          $filesystem,
28
        Autocomplete        $autocomplete
29
    )
30
    {
31
        $this->config               = $config;
32
        $this->terminal             = $terminal;
33
        $this->filesystem           = $filesystem;
34
        $this->autocomplete         = $autocomplete;
35
36
        $addonXmlGenerator      = new AddonXmlGenerator($this->config);
37
        $generatorMediator      = new GeneratorMediator();
38
39
        $generatorMediator->addGenerator($addonXmlGenerator);
40
41
        $this->mfGenerator = new MultipleFileGenerator($this->filesystem);
42
        $this->mfGenerator->addGenerator($addonXmlGenerator);
43
        
44
        // handle all supported languages
45
        $supported_languages = $this->config->get('addon.supported_languages');
46
        if ($supported_languages) {
47
            $supported_languages_list = explode(',', $supported_languages);
48
        }
49
50
        $self = $this;
51
        array_walk($supported_languages_list, function($language) use ($self, $generatorMediator) {
52
            $languageGenerator = new LanguageGenerator($self->config, $language);
53
            $generatorMediator->addGenerator($languageGenerator);
54
            $self->mfGenerator->addGenerator($languageGenerator);
55
        });
56
    }
57
58
    /**
59
     * @inheritdoc
60
     */
61
    public static function getAllowedMethods(): array
62
    {
63
        return self::$allowedMethods;
64
    }
65
66
    /**
67
     * help:
68
     * addon-xml/update --addon.id <addon_id> --set <item> [...args]
69
     * Sets additional field to addon xml file
70
     * addon.id - id of the addon
71
     * ---
72
     *      --set
73
     *          settings-item - <item id="date">...</item>
74
     *              args: --section <section_id> --type <type> --id <id> [--default_value <default_value>] [--variants "<variant1,variant2>"]
75
     *                  section         - id for the settings section
76
     *                  type            - type of the item id: input, textarea, password, checkbox, selectbox, multiple select, multiple checkboxes, countries list, states list, file, info, header, template
77
     *                  id              - id of the setting item
78
     *                  default_value   - default value for setting item
79
     *                  variants        - list of item value variants comma separated and quote wrapped
80
     * ---
81
     * 
82
     * see more @link [https://www.cs-cart.ru/docs/4.9.x/developer_guide/addons/scheme/scheme3.0_structure.html]
83
     * @throws Exception if file doesn't exists
84
     */
85
    public function update()
86
    {
87
        $this->mfGenerator
88
            ->throwIfNotExists('Some addon file not found.')
89
            ->read();
90
91
        $old_content = [];
92
        $this->mfGenerator->each(function($generator) use (&$old_content) {
93
            $old_content[$generator->extract()->getKey()] = $generator->extract()->toString();
94
        });
95
        
96
        $addonXmlGenerator = $this->mfGenerator
97
            ->find(AddonXmlGenerator::class)
98
                ->extract();
0 ignored issues
show
The method extract() does not exist on generators\AbstractFileGenerator. Since it exists in all sub-types, consider adding an abstract or default implementation to generators\AbstractFileGenerator. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

98
                ->/** @scrutinizer ignore-call */ extract();
Loading history...
99
100
        $method     = $this->getMethodName();
101
        $class_name = get_class($this);
102
        if (method_exists($class_name, $method)) {
103
            $this->{$method}($addonXmlGenerator);
104
        } else {
105
            throw new \BadMethodCallException('There is no such command');
106
        }
107
108
        $this->mfGenerator
109
            ->write()
110
            ->throwIfNotExists();
111
        
112
        $self = $this;
113
        $this->mfGenerator->each(function($generator) use ($self, $old_content) {
114
            $self->terminal->success($generator->extract()->getPath() . ' was changed');
115
            $self->terminal->diff(
116
                \Diff::toString(\Diff::compare($old_content[$generator->extract()->getKey()], $generator->extract()->toString()))
117
            );
118
        });
119
    }
120
121
    /**
122
     * 
123
     */
124
    public function setSettingsItem($addonXmlGenerator)
125
    {
126
        $addonXmlGenerator->setSetting(
127
            $this->config->get('section'),
128
            $this->config->get('type'),
129
            $this->config->get('id'),
130
            $this->config->get('default_value') ?: '',
131
            (function($config) {
132
                $variants = $config->get('variants');
133
134
                return $variants ? explode(',', $variants) : [];
135
            })($this->config)
136
        );
137
    }
138
139
    public function removeSettingsItem($addonXmlGenerator)
140
    {
141
        $addonXmlGenerator->removeSetting(
142
            $this->config->get('id')
143
        );
144
    }
145
146
    public function updateAutocomplete()
147
    {
148
        $autocomplete   = $this->autocomplete;
149
        $arguments      = $this->terminal->getArguments();
150
        
151
        if (!empty($arguments['set']) && $arguments['set'] === 'settings-item') {
152
            return $this->setSettingsItemAutocomplete();
153
        }
154
155
        return $this->autocomplete->combineQueueParam(
156
            $this->autocomplete->queueArgument('addon.id', function() use ($autocomplete) {
157
                return $autocomplete->getAddonsList();
158
            }),
159
            $this->autocomplete->queueArgument('set', ['settings-item'])
160
        );
161
    }
162
163
    public function setSettingsItemAutocomplete()
164
    {
165
        $generator      = $this->mfGenerator
166
            ->find(AddonXmlGenerator::class)
167
                ->extract();
168
169
        return $this->autocomplete->combineQueueParam(
170
            $this->autocomplete->queueArgument('type', function() use ($generator) {
171
                return $generator->getVariants('item');
172
            }),
173
            $this->autocomplete->queueArgument('section'),
174
            $this->autocomplete->queueArgument('id')
175
        );
176
    }
177
}
178