GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( 18d867...283b71 )
by
unknown
11:46
created

ThemeMakeCommand::makeControllerClass()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 19
Code Lines 12

Duplication

Lines 19
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 19
loc 19
rs 9.4285
cc 2
eloc 12
nc 2
nop 0
1
<?php
2
3
namespace App\Console\Commands;
4
5
use Illuminate\Console\Command;
6
use Illuminate\Filesystem\Filesystem;
7
use ReflectionClass;
8
use Symfony\Component\Console\Input\InputArgument;
9
use Symfony\Component\Process\Process;
10
use Xpressengine\Plugin\PluginEntity;
11
12
class ThemeMakeCommand extends Command
13
{
14
15
    protected $signature = 'make:theme
16
                        {plugin : The name of target plugin}
17
                        {class : The class name of theme. except namespace}
18
                        {title : The title of the theme}
19
                        {--id= : The path of theme class file}
20
                        {--description= : The description of the theme}';
21
22
    /**
23
     * The console command description.
24
     *
25
     * @var string
26
     */
27
    protected $description = 'Create a new theme of XpressEngine';
28
29
    /**
30
     * The filesystem instance.
31
     *
32
     * @var \Illuminate\Filesystem\Filesystem
33
     */
34
    protected $files;
35
36
    protected $originComposerStr = null;
37
38
    protected $srcDir;
39
    protected $attr;
40
41
    /**
42
     * Create a new controller creator command instance.
43
     *
44
     * @param  \Illuminate\Filesystem\Filesystem $files
45
     *
46
     * @return void
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
47
     */
48
    public function __construct(Filesystem $files)
49
    {
50
        parent::__construct();
51
52
        $this->files = $files;
53
    }
54
55
    /**
56
     * Execute the console command.
57
     *
58
     * @return bool|null
59
     */
60
    public function fire()
61
    {
62
        // get plugin info
63
        $plugin = $this->getPlugin();
64
        $pluginClass = new ReflectionClass($plugin->getClass());
65
        $namespace = $pluginClass->getNamespaceName();
66
        $this->resolvePluginSrcDir($plugin, $namespace);
67
68
        // get theme info
69
        $themeClass = ucfirst($this->argument('class')); // Basic
70
        $themeFile = $this->getThemeFile($plugin, $themeClass); // src/Theme/Basic.php
71
        $themeId = $this->getThemeId($plugin, $themeClass); // myplugin@basic
72
        $themeTitle = $this->getThemeTitle();
73
        $description = $this->getThemeDescription($themeId, $plugin);
74
75
        // other files info
76
        $view = strtolower($themeClass); // basic
77
        $templateFile = $this->getTemplateFile($view, $plugin); // views/theme/basic.blade.php
78
        $cssFile = $this->getCssFile($view, $plugin); // assets/theme/basic.css
79
        $controllerFile = $this->getControllerFile($plugin); // src/Controllers/Theme/ConfigController.php
80
        $configTemplateFile = $this->getConfigTemplateFile($plugin);
81
82
        $configId = $this->getConfigId($plugin, $themeId);
83
84
        $this->attr = compact(
85
            'plugin',
86
            'pluginClass',
87
            'namespace',
88
            'themeClass',
89
            'themeFile',
90
            'themeId',
91
            'themeTitle',
92
            'description',
93
            'view',
94
            'templateFile',
95
            'cssFile',
96
            'controllerFile',
97
            'configTemplateFile',
98
            'configId'
99
        );
100
101
102
        // print and confirm the information of theme
103
        if($this->confirmInfo() === false){
104
            return false;
105
        }
106
107
        try {
108
            $this->makeThemeClass();
109
            $this->makeTemplate();
110
            $this->makeCss();
111
112
            // for config
113
            $this->makeControllerClass();
114
            $this->makeConfigTemplate();
115
116
            // composer.json 파일 수정
117
            if($this->registerTheme() === false) {
118
                throw new \Exception('Writing to composer.json file was failed.');
119
            }
120
121
        } catch (\Exception $e) {
122
            $this->clean();
123
            throw $e;
124
        }
125
126
        $this->info("Theme is created successfully.");
127
128
        //$this->info("See ./plugins/$name directory. And open $url in your browser.");
0 ignored issues
show
Unused Code Comprehensibility introduced by
86% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
129
        //$this->info("Input and modify your plugin information in ./plugins/$name/composer.json file.");
0 ignored issues
show
Unused Code Comprehensibility introduced by
86% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
130
    }
131
132
    /**
133
     * makeDirectory
134
     *
135
     * @param $path
136
     *
137
     * @return void
138
     */
139
    protected function makeDirectory($path)
140
    {
141
        if (!$this->files->isDirectory($path)) {
142
            $this->files->makeDirectory($path, 0777, true, true);
143
        }
144
    }
145
146
    /**
147
     * buildCode
148
     *
149
     * @param $stub
150
     *
151
     * @return string
152
     * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
153
     */
154
    protected function buildCode($stub)
155
    {
156
        $code = $this->files->get($this->getStub($stub));
157
158
        /*
159
         * DummyNamespace
160
         * DummyPluginNamespace
161
         * DummyPlugin
162
         * DummyClass
163
         * DummyTemplateFile
164
         * DummyTemplateView
165
         * DummyCssFile
166
         * DummyConfigId
167
         * DummyTitle
168
        */
169
170
        $this->replaceCode($code, 'DummyNamespace', $this->attr('namespace').'\\Theme')
171
            ->replaceCode($code, 'DummyClass', $this->attr('themeClass'))
172
            ->replaceCode($code, 'DummyPluginNamespace', $this->attr('pluginClass')->getNamespaceName())
173
            ->replaceCode($code, 'DummyPluginId', $this->attr('plugin')->getId())
174
            ->replaceCode($code, 'DummyPluginClass', $this->attr('pluginClass')->getShortName())
175
            ->replaceCode($code, 'DummyTemplateFile', $this->attr('templateFile'))
176
            ->replaceCode($code, 'DummyTemplateView', 'views.theme.'.$this->attr('view'))
177
            ->replaceCode($code, 'DummyCssFile', $this->attr('cssFile'))
178
            ->replaceCode($code, 'DummyConfigId', $this->attr('configId'))
179
            ->replaceCode($code, 'DummyTitle', $this->attr('themeTitle'));
180
181
        return $code;
182
    }
183
184
    /**
185
     * replaceCode
186
     *
187
     * @param $stub
188
     * @param $search
189
     * @param $replace
190
     *
191
     * @return $this
192
     */
193
    protected function replaceCode(&$stub, $search, $replace)
194
    {
195
        $stub = str_replace($search, $replace, $stub);
196
        return $this;
197
    }
198
199
    /**
200
     * Get the stub file for the generator.
201
     *
202
     * @return string
203
     */
204
    protected function getStub($filename)
205
    {
206
        return __DIR__.'/stubs/'.$filename;
207
    }
208
209
    /**
210
     * getThemeId
211
     *
212
     * @param PluginEntity $plugin
213
     * @param              $class
214
     *
215
     * @return array|string
216
     * @throws \Exception
217
     * @internal param $file
218
     *
219
     */
220
    protected function getThemeId(PluginEntity $plugin, $class)
221
    {
222
        $id = $this->option('id');
223
224
        if(!$id) {
225
            $id = $plugin->getId().'@'.strtolower($class);
226
        } else {
227
            if(strpos('theme/', $id) === 0) {
228
                $id = substr($id, 6);
229
            }
230
231
            if(strpos($id, '@') === false) {
232
                $id = $plugin->getId().'@'.$id;
233
            }
234
        }
235
236
        $theme = \App::make('xe.theme')->getTheme('theme/'.$id);
237
238
        if($theme !== null) {
239
            throw new \Exception("Theme[$theme] already exists.");
240
        }
241
242
        return $id;
243
    }
244
245
    /**
246
     * getTargetPlugin
247
     *
248
     * @param $file
249
     *
250
     * @return PluginEntity
251
     */
252
    protected function getPlugin()
253
    {
254
        $plugin = $this->argument('plugin');
255
256
        $plugin = app('xe.plugin')->getPlugin($plugin);
257
        if($plugin === null) {
258
            throw new \Exception("Unable to find a plugin to locate the theme file. plugin[$plugin] is not found.");
259
        }
260
261
        return $plugin;
262
    }
263
264
    /**
265
     * getThemeTitle
266
     *
267
     * @return array|string
268
     */
269
    protected function getThemeTitle()
270
    {
271
        return $this->argument('title');
272
    }
273
274
    /**
275
     * getThemeDescription
276
     *
277
     * @param $id
278
     * @param $plugin
279
     *
280
     * @return array|string
281
     */
282
    protected function getThemeDescription($id, PluginEntity $plugin)
283
    {
284
        $description = $this->option('description');
285
        if(!$description) {
286
            $description = sprintf(
287
                '%s Theme supported by %s plugin.',
288
                ucfirst(last(explode('@', $id))), ucfirst($plugin->getId())
289
            );
290
        }
291
        return $description;
292
    }
293
294
    /**
295
     * getThemeFile
296
     *
297
     * @return array|string
298
     * @throws \Exception
299
     */
300
    protected function getThemeFile(PluginEntity $plugin, $themeClass)
301
    {
302
        $path = $this->srcDir."/Theme/$themeClass.php";
303
        if(file_exists($plugin->getPath($path))) {
304
            throw new \Exception("file[$path] already exists.");
305
        }
306
        return $path;
307
    }
308
309
    /**
310
     * getControllerFile
311
     *
312
     * @param PluginEntity $plugin
313
     *
314
     * @return string
315
     * @throws \Exception
316
     */
317
    protected function getControllerFile(PluginEntity $plugin)
318
    {
319
        $path = $this->srcDir."/Controllers/Theme/ConfigController.php";
320
        if(file_exists($plugin->getPath($path))) {
321
            throw new \Exception("file[$path] already exists.");
322
        }
323
        return $path;
324
    }
325
326
    /**
327
     * getTemplatePath
328
     *
329
     * @param $id
330
     * @param $plugin
331
     *
332
     * @return string
333
     * @throws \Exception
334
     */
335
    protected function getTemplateFile($view, PluginEntity $plugin)
336
    {
337
        $viewFile = 'views/theme/'.$view.'.blade.php';
338
        if(file_exists($plugin->getPath($viewFile))) {
339
            throw new \Exception("template file[$viewFile] already exists.");
340
        }
341
        return $viewFile;
342
    }
343
344
    /**
345
     * getConfigTemplateFile
346
     *
347
     * @param PluginEntity $plugin
348
     *
349
     * @return string
350
     * @throws \Exception
351
     */
352 View Code Duplication
    protected function getConfigTemplateFile(PluginEntity $plugin)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
353
    {
354
        $viewFile = 'views/theme/config.blade.php';
355
        if(file_exists($plugin->getPath($viewFile))) {
356
            throw new \Exception("template file[$viewFile] already exists.");
357
        }
358
        return $viewFile;
359
    }
360
361
    /**
362
     * getCssPath
363
     *
364
     * @param $id
365
     * @param $plugin
366
     *
367
     * @return string
368
     * @throws \Exception
369
     */
370 View Code Duplication
    protected function getCssFile($view, PluginEntity $plugin)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
371
    {
372
        $cssFile = 'assets/theme/'.$view.'.css';
373
        if(file_exists($plugin->getPath($cssFile))) {
374
            throw new \Exception("css file[$cssFile] already exists.");
375
        }
376
        return $cssFile;
377
    }
378
379
    /**
380
     * confirmInfo
381
     *
382
     * @param $plugin
383
     * @param $namespace
384
     * @param $themeClass
385
     * @param $themeFile
386
     * @param $themeId
387
     * @param $themeTitle
388
     * @param $description
389
     * @param $templateFile
390
     * @param $cssFile
391
     * @param $controllerFile
392
     * @param $configId
393
     *
394
     * @return bool
395
     */
396
    protected function confirmInfo() {
397
        $this->info(
398
            sprintf(
399
                "[New theme info]
400
  plugin: %s
401
  class name: %s
402
  class file: %s
403
  id: %s
404
  title: %s
405
  description: %s
406
  template file: %s
407
  css file: %s,
408
  config id: %s
409
  config controller file: %s",
410
                $this->attr['plugin']->getId(),
411
                $this->attr['namespace'].'\\'.$this->attr['themeClass'],
412
                $this->attr['themeFile'],
413
                $this->attr['themeId'],
414
                $this->attr['themeTitle'],
415
                $this->attr['description'],
416
                $this->attr['templateFile'],
417
                $this->attr['cssFile'],
418
                $this->attr['configId'],
419
                $this->attr['controllerFile']
420
            )
421
        );
422
423
        while ($confirm = $this->ask('Do you want to add theme? [yes|no]')) {
424
            if ($confirm === 'yes') {
425
                return true;
426
            } else {
427
                return false;
428
            }
429
        }
430
    }
431
432
    /**
433
     * makeThemeClass
434
     *
435
     * @throws \Exception
436
     */
437 View Code Duplication
    protected function makeThemeClass()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
438
    {
439
        $plugin = $this->attr('plugin');
440
        $themeFile = $this->attr('themeFile');
441
        $namespace = $this->attr('namespace');
442
        $class = $this->attr('themeClass');
443
        $path = $plugin->getPath($themeFile);
444
445
        $code = $this->buildCode('theme.stub');
446
447
        $this->makeDirectory(dirname($path));
448
449
        $this->files->put($path, $code);
450
451
        if(!class_exists($namespace.'\\Theme\\'.$class)) {
452
            $this->files->delete($path);
453
            throw new \Exception("Unable to load the theme class file[$path]. please check autoload setting of composer.json");
454
        }
455
    }
456
457 View Code Duplication
    protected function makeControllerClass()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
458
    {
459
        $plugin = $this->attr('plugin');
460
        $controllerFile = $this->attr('controllerFile');
461
        $namespace = $this->attr('namespace');
462
        $class = 'ConfigController';
463
        $path = $plugin->getPath($controllerFile);
464
465
        $code = $this->buildCode('theme.config.controller.stub');
466
467
        $this->makeDirectory(dirname($path));
468
469
        $this->files->put($path, $code);
470
471
        if(!class_exists($namespace.'\\Controllers\\Theme\\'.$class)) {
472
            $this->files->delete($path);
473
            throw new \Exception("Unable to load the theme class file[$path]. please check autoload setting of composer.json");
474
        }
475
    }
476
477
    protected function makeConfigTemplate()
478
    {
479
        $plugin = $this->attr('plugin');
480
        $configTemplateFile = $this->attr('configTemplateFile');
481
        $path = $plugin->getPath($configTemplateFile);
482
483
        $code = $this->buildCode('theme.config.blade.stub');
484
485
        $this->makeDirectory(dirname($path));
486
487
        $this->files->put($path, $code);
488
    }
489
490
    /**
491
     * formatJson
492
     *
493
     * @param      $json
494
     * @param bool $unescapeUnicode
495
     * @param bool $unescapeSlashes
496
     *
497
     * @return string
498
     */
499
    public function formatJson($json, $unescapeUnicode = true, $unescapeSlashes = true)
500
    {
501
        $result = '';
502
        $pos = 0;
503
        $strLen = strlen($json);
504
        $indentStr = '    ';
505
        $newLine = "\n";
506
        $outOfQuotes = true;
507
        $buffer = '';
508
        $noescape = true;
509
510
        for ($i = 0; $i < $strLen; $i++) {
511
            // Grab the next character in the string
512
            $char = substr($json, $i, 1);
513
514
            // Are we inside a quoted string?
515
            if ('"' === $char && $noescape) {
516
                $outOfQuotes = !$outOfQuotes;
517
            }
518
519
            if (!$outOfQuotes) {
520
                $buffer .= $char;
521
                $noescape = '\\' === $char ? !$noescape : true;
522
                continue;
523
            } elseif ('' !== $buffer) {
524
                if ($unescapeSlashes) {
525
                    $buffer = str_replace('\\/', '/', $buffer);
526
                }
527
528
                if ($unescapeUnicode && function_exists('mb_convert_encoding')) {
529
                    // https://stackoverflow.com/questions/2934563/how-to-decode-unicode-escape-sequences-like-u00ed-to-proper-utf-8-encoded-cha
530
                    $buffer = preg_replace_callback('/(\\\\+)u([0-9a-f]{4})/i', function ($match) {
531
                        $l = strlen($match[1]);
532
533
                        if ($l % 2) {
534
                            return str_repeat('\\', $l - 1) . mb_convert_encoding(
535
                                pack('H*', $match[2]),
536
                                'UTF-8',
537
                                'UCS-2BE'
538
                            );
539
                        }
540
541
                        return $match[0];
542
                    }, $buffer);
543
                }
544
545
                $result .= $buffer.$char;
546
                $buffer = '';
547
                continue;
548
            }
549
550
            if (':' === $char) {
551
                // Add a space after the : character
552
                $char .= ' ';
553
            } elseif (('}' === $char || ']' === $char)) {
554
                $pos--;
555
                $prevChar = substr($json, $i - 1, 1);
556
557
                if ('{' !== $prevChar && '[' !== $prevChar) {
558
                    // If this character is the end of an element,
559
                    // output a new line and indent the next line
560
                    $result .= $newLine;
561
                    for ($j = 0; $j < $pos; $j++) {
562
                        $result .= $indentStr;
563
                    }
564
                } else {
565
                    // Collapse empty {} and []
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
566
                    $result = rtrim($result);
567
                }
568
            }
569
570
            $result .= $char;
571
572
            // If the last character was the beginning of an element,
573
            // output a new line and indent the next line
574
            if (',' === $char || '{' === $char || '[' === $char) {
575
                $result .= $newLine;
576
577
                if ('{' === $char || '[' === $char) {
578
                    $pos++;
579
                }
580
581
                for ($j = 0; $j < $pos; $j++) {
582
                    $result .= $indentStr;
583
                }
584
            }
585
        }
586
587
        return $result;
588
    }
589
590
    /**
591
     * registerTheme
592
     *
593
     * @param $plugin
594
     * @param $id
595
     * @param $class
596
     * @param $title
597
     * @param $description
598
     *
599
     * @return int
600
     * @throws \Exception
601
     * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
602
     */
603
    protected function registerTheme()
604
    {
605
        $plugin = $this->attr('plugin');
606
        $id = 'theme/'.$this->attr('themeId');
607
        $class = $this->attr('namespace').'\\Theme\\'.$this->attr('themeClass');
608
        $title = $this->attr('themeTitle');
609
        $description = $this->attr('description');
610
611
        $composerStr = $this->files->get($plugin->getPath('composer.json'));
612
        $this->originComposerStr = $composerStr;
613
        $json = json_decode($composerStr, true);
614
        $component = data_get($json, 'extra.xpressengine.component');
615
        if(isset($component[$id])) {
616
            throw new \Exception(sprintf('component[%s] already exists.', $id));
617
        }
618
        $component[$id] = [];
619
        $component[$id]['class'] = $class;
620
        $component[$id]['name'] = $title;
621
        $component[$id]['description'] = $description;
622
        array_set($json, 'extra.xpressengine.component', $component);
623
624
        $json = $this->formatJson(json_encode($json));
625
626
        return $this->files->put($plugin->getPath('composer.json'), $json);
627
    }
628
629
    /**
630
     * makeTemplate
631
     *
632
     * @param $plugin
633
     * @param $template
634
     *
635
     * @return bool
636
     */
637 View Code Duplication
    protected function makeTemplate()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
638
    {
639
        $plugin = $this->attr('plugin');
640
        $file = $plugin->getPath($this->attr('templateFile'));
641
        $this->makeDirectory(dirname($file));
642
643
        return $this->files->copy($this->getStub('theme.blade.stub'), $file);
644
    }
645
646
    /**
647
     * makeCss
648
     *
649
     * @param $plugin
650
     * @param $css
651
     *
652
     * @return bool
653
     */
654 View Code Duplication
    protected function makeCss()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
655
    {
656
        $plugin = $this->attr('plugin');
657
        $css = $this->attr('cssFile');
658
        $file = $plugin->getPath($css);
659
        $this->makeDirectory(dirname($file));
660
        return $this->files->copy($this->getStub('theme.css.stub'), $file);
661
    }
662
663
    /**
664
     * clean
665
     *
666
     * @param $file
667
     * @param $plugin
668
     * @param $template
669
     * @param $css
670
     *
671
     * @return void
672
     */
673
    protected function clean()
674
    {
675
        $plugin = $this->attr('plugin');
676
        $themeFile  = $this->attr('themeFile');
677
        $template = $this->attr('templateFile');
678
        $css = $this->attr('css');
679
        $controller = $this->attr('controllerFile');
680
        $configTemplate = $this->attr('configTemplateFile');
681
682
        // delete theme class file
683
        if(is_writable($plugin->getPath($themeFile))) {
684
            $this->files->delete($plugin->getPath($themeFile));
685
        }
686
687
        // unregister component from composer.json
688
        $composerFile = $plugin->getPath('composer.json');
689
        if($this->originComposerStr !== null && is_writable($composerFile)) {
690
            $this->files->put($composerFile, $this->originComposerStr);
691
        }
692
693
        // delete template file
694
        $templateFile = $plugin->getPath($template);
695
        if(is_writable($templateFile)) {
696
            $this->files->delete($templateFile);
697
        }
698
699
        // delete css file
700
        $cssFile = $plugin->getPath($css);
701
        if(is_writable($cssFile)) {
702
            $this->files->delete($cssFile);
703
        }
704
705
        // delete controller file
706
        $controllerFile = $plugin->getPath($controller);
707
        if(is_writable($controllerFile)) {
708
            $this->files->delete($controllerFile);
709
        }
710
711
        // delete config template file
712
        $configTemplateFile = $plugin->getPath($configTemplate);
713
        if(is_writable($configTemplateFile)) {
714
            $this->files->delete($configTemplateFile);
715
        }
716
717
    }
718
719
    /**
720
     * getConfigId
721
     *
722
     * @param $plugin
723
     * @param $id
724
     *
725
     * @return string
726
     */
727
    protected function getConfigId(PluginEntity $plugin, $id)
728
    {
729
        return $plugin->getId().'@'.$id;
730
    }
731
732
    /**
733
     * resolvePluginSrcDir
734
     *
735
     * @param $plugin
736
     * @param $namespace
737
     *
738
     * @return string
739
     * @throws \Exception
740
     */
741
    private function resolvePluginSrcDir($plugin, $namespace)
742
    {
743
        $autoloads = $plugin->getMetaData('autoload.psr-4');
744
        $srcDir = false;
745
        foreach ($autoloads as $key => $dir) {
746
            if (trim($key, '\\') === trim($namespace, '\\')) {
747
                $srcDir = trim($dir, '/');
748
            }
749
        }
750
        if ($srcDir === false) {
751
            throw new \Exception(sprintf("Plugin format is wrong. Can not add theme to %s plugin", $plugin->getId()));
752
        }
753
754
        return $this->srcDir = $srcDir;
755
    }
756
757
    /**
758
     * attr
759
     *
760
     * @param $key
761
     *
762
     * @return mixed
763
     */
764
    private function attr($key)
765
    {
766
        return array_get($this->attr, $key);
767
    }
768
769
770
}
771