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 terminal\Terminal; |
||||||||
10 | use filesystem\Filesystem; |
||||||||
11 | use autocomplete\Autocomplete; |
||||||||
12 | use mediators\GeneratorMediator; |
||||||||
13 | use \Config; |
||||||||
14 | |||||||||
15 | class Addon extends AbstractController |
||||||||
16 | { |
||||||||
17 | private $mfGenerator; |
||||||||
18 | private static $allowedMethods = [ |
||||||||
19 | 'help', |
||||||||
20 | 'create', |
||||||||
21 | 'remove' |
||||||||
22 | ]; |
||||||||
23 | |||||||||
24 | use HelpTrait; |
||||||||
25 | |||||||||
26 | function __construct( |
||||||||
27 | Config $config, |
||||||||
28 | Terminal $terminal, |
||||||||
29 | Filesystem $filesystem, |
||||||||
30 | Autocomplete $autocomplete |
||||||||
31 | ) |
||||||||
32 | { |
||||||||
33 | $this->config = $config; |
||||||||
34 | $this->terminal = $terminal; |
||||||||
35 | $this->filesystem = $filesystem; |
||||||||
36 | $this->autocomplete = $autocomplete; |
||||||||
37 | |||||||||
38 | $addonXmlGenerator = new AddonXmlGenerator($this->config); |
||||||||
39 | $readmeGenerator = new ReadmeGenerator($this->config); |
||||||||
40 | |||||||||
41 | $generatorMediator = new GeneratorMediator(); |
||||||||
42 | |||||||||
43 | $generatorMediator |
||||||||
44 | ->addGenerator($addonXmlGenerator) |
||||||||
45 | ->addGenerator($readmeGenerator); |
||||||||
46 | |||||||||
47 | $this->mfGenerator = new MultipleFileGenerator($this->filesystem); |
||||||||
48 | $this->mfGenerator |
||||||||
49 | ->addGenerator($addonXmlGenerator) |
||||||||
50 | ->addGenerator($readmeGenerator); |
||||||||
51 | |||||||||
52 | // handle all supported languages |
||||||||
53 | $supported_languages = $this->config->get('addon.supported_languages'); |
||||||||
54 | if ($supported_languages) { |
||||||||
55 | $supported_languages_list = explode(',', $supported_languages); |
||||||||
56 | } |
||||||||
57 | |||||||||
58 | $self = $this; |
||||||||
59 | array_walk($supported_languages_list, function($language) use ($self, $generatorMediator) { |
||||||||
60 | $languageGenerator = new LanguageGenerator($self->config, $language); |
||||||||
61 | $generatorMediator->addGenerator($languageGenerator); |
||||||||
62 | $self->mfGenerator->addGenerator($languageGenerator); |
||||||||
63 | }); |
||||||||
64 | } |
||||||||
65 | |||||||||
66 | /** |
||||||||
67 | * @inheritdoc |
||||||||
68 | */ |
||||||||
69 | public static function getAllowedMethods(): array |
||||||||
70 | { |
||||||||
71 | return self::$allowedMethods; |
||||||||
72 | } |
||||||||
73 | |||||||||
74 | /** |
||||||||
75 | * help: |
||||||||
76 | * addon/create |
||||||||
77 | * creates addon.xml, language file, readme and other |
||||||||
78 | * @throws Exception if file already exists |
||||||||
79 | */ |
||||||||
80 | public function create() |
||||||||
81 | { |
||||||||
82 | $this->mfGenerator |
||||||||
83 | ->throwIfExists('File already exists. Remove it first if you want to replace it.') |
||||||||
84 | ->find(AddonXmlGenerator::class) |
||||||||
85 | ->extract() |
||||||||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||||||||
86 | ->create(); |
||||||||
87 | |||||||||
88 | $this->mfGenerator |
||||||||
89 | ->excluding(ReadmeGenerator::class) |
||||||||
90 | ->write() |
||||||||
91 | ->throwIfNotExists('File cannot be created.'); |
||||||||
92 | |||||||||
93 | $this->mfGenerator |
||||||||
94 | ->find(ReadmeGenerator::class) |
||||||||
95 | ->readFromTemplate() |
||||||||
96 | ->write(); |
||||||||
97 | |||||||||
98 | $self = $this; |
||||||||
99 | $this->mfGenerator->each(function($generator) use ($self) { |
||||||||
100 | $self->terminal->success($generator->extract()->getPath() . ' was created'); |
||||||||
101 | $self->terminal->diff( |
||||||||
102 | \Diff::toString(\Diff::compare('', $generator->extract()->toString())) |
||||||||
103 | ); |
||||||||
104 | }); |
||||||||
105 | } |
||||||||
106 | |||||||||
107 | /** |
||||||||
108 | * help: |
||||||||
109 | * addon remove |
||||||||
110 | * removes entire path of the addon |
||||||||
111 | */ |
||||||||
112 | public function remove() |
||||||||
113 | { |
||||||||
114 | $self = $this; |
||||||||
115 | $this->terminal->confirm( |
||||||||
116 | function() use ($self) { |
||||||||
117 | $addonPath = $self->config->get('filesystem.output_path'); |
||||||||
118 | |||||||||
119 | $self->filesystem->delete($addonPath); |
||||||||
120 | |||||||||
121 | if ($self->filesystem->exists($addonPath)) { |
||||||||
122 | throw new \Exception($addonPath . ' cannot be removed'); |
||||||||
123 | } |
||||||||
124 | |||||||||
125 | $this->terminal->warning('Addon was removed'); |
||||||||
126 | } |
||||||||
127 | ); |
||||||||
128 | } |
||||||||
129 | |||||||||
130 | /** |
||||||||
131 | * Autocomplete addon param |
||||||||
132 | */ |
||||||||
133 | public function createAutocomplete($prev = null, $cur = null, $arguments = []) |
||||||||
0 ignored issues
–
show
The parameter
$arguments is not used and could be removed.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for parameters that have been defined for a function or method, but which are not used in the method body. ![]() The parameter
$prev is not used and could be removed.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for parameters that have been defined for a function or method, but which are not used in the method body. ![]() The parameter
$cur is not used and could be removed.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for parameters that have been defined for a function or method, but which are not used in the method body. ![]() |
|||||||||
134 | { |
||||||||
135 | $generator = $this->mfGenerator |
||||||||
136 | ->find(AddonXmlGenerator::class) |
||||||||
137 | ->extract(); |
||||||||
138 | |||||||||
139 | return $this->autocomplete->combineQueueParam( |
||||||||
140 | $this->autocomplete->queueArgument('addon.id'), |
||||||||
141 | $this->autocomplete->queueArgument('addon.scheme', function() use ($generator) { |
||||||||
142 | return $generator->getVariants('scheme'); |
||||||||
143 | }), |
||||||||
144 | $this->autocomplete->queueArgument('addon.status', function() use ($generator) { |
||||||||
145 | return $generator->getVariants('status'); |
||||||||
146 | }), |
||||||||
147 | $this->autocomplete->queueArgument('addon.edition_type'), |
||||||||
148 | $this->autocomplete->queueArgument('addon.priority'), |
||||||||
149 | $this->autocomplete->queueArgument('addon.position') |
||||||||
150 | ); |
||||||||
151 | } |
||||||||
152 | |||||||||
153 | /** |
||||||||
154 | * Autocomplete addon name to be removed |
||||||||
155 | */ |
||||||||
156 | public function removeAutocomplete($prev = null, $cur = null, $arguments = []) |
||||||||
0 ignored issues
–
show
The parameter
$prev is not used and could be removed.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for parameters that have been defined for a function or method, but which are not used in the method body. ![]() The parameter
$arguments is not used and could be removed.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for parameters that have been defined for a function or method, but which are not used in the method body. ![]() The parameter
$cur is not used and could be removed.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for parameters that have been defined for a function or method, but which are not used in the method body. ![]() |
|||||||||
157 | { |
||||||||
158 | $self = $this; |
||||||||
159 | |||||||||
160 | return $this->autocomplete->combineQueueParam( |
||||||||
161 | $this->autocomplete->queueArgument('addon.id', function() use ($self) { |
||||||||
162 | return $self->autocomplete->getAddonsList(); |
||||||||
163 | }) |
||||||||
164 | ); |
||||||||
165 | } |
||||||||
166 | } |
||||||||
167 |