Failed Conditions
Pull Request — experimental/3.1 (#2159)
by Kentaro
88:02
created

PluginGenerator   B

Complexity

Total Complexity 49

Size/Duplication

Total Lines 526
Duplicated Lines 6.27 %

Coupling/Cohesion

Components 2
Dependencies 2

Importance

Changes 0
Metric Value
dl 33
loc 526
rs 8.5454
c 0
b 0
f 0
wmc 49
lcom 2
cbo 2

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getHeader() 8 8 1
B initFieldSet() 0 86 1
A getHookPoints() 0 9 2
C getEvents() 0 47 8
A start() 0 15 2
F createFilesAndFolders() 25 282 30
B createDbRecords() 0 47 5

How to fix   Duplicated Code    Complexity   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like PluginGenerator often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use PluginGenerator, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
/*
4
 * This file is part of EC-CUBE
5
 *
6
 * Copyright(c) 2000-2015 LOCKON CO.,LTD. All Rights Reserved.
7
 *
8
 * http://www.lockon.co.jp/
9
 *
10
 * This program is free software; you can redistribute it and/or
11
 * modify it under the terms of the GNU General Public License
12
 * as published by the Free Software Foundation; either version 2
13
 * of the License, or (at your option) any later version.
14
 *
15
 * This program is distributed in the hope that it will be useful,
16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
 * GNU General Public License for more details.
19
 *
20
 * You should have received a copy of the GNU General Public License
21
 * along with this program; if not, write to the Free Software
22
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
23
 */
24
25
namespace Eccube\Command\GeneratorCommand;
26
27
use Eccube\Command\PluginCommand\AbstractPluginGenerator;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Eccube\Command\Generator...AbstractPluginGenerator.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
28
use Eccube\Common\Constant;
29
use Eccube\Entity\Plugin;
30
use Eccube\Entity\PluginEventHandler;
31
use Symfony\Component\Filesystem\Filesystem;
32
use Symfony\Component\Yaml\Yaml;
33
34
class PluginGenerator extends AbstractPluginGenerator
0 ignored issues
show
introduced by
Missing class doc comment
Loading history...
35
{
36
37
    /**
38
     *
39
     * @var array
40
     */
41
    private $hookPoints = null;
42
43
    /**
44
     *
45
     * @var array
46
     */
47
    private $events = null;
48
49 View Code Duplication
    protected function getHeader()
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...
50
    {
51
        $this->output->writeln('------------------------------------------------------');
52
        $this->output->writeln('---Plugin Generator');
53
        $this->output->writeln('---[*]You can exit from Console Application, by typing '.self::STOP_PROCESS.' instead of typing another word.');
54
        $this->output->writeln('------------------------------------------------------');
55
        $this->output->writeln('');
56
    }
57
58
    protected function initFieldSet()
59
    {
60
        $this->paramList = array(
61
            'pluginName' => array(
0 ignored issues
show
introduced by
Add a comma after each item in a multi-line array
Loading history...
62
                'no' => 1,
63
                'label' => '[+]Plugin Name: ',
64
                'value' => null,
65
                'name' => '[+]Please enter Plugin Name',
66
                'validation' => array(
67
                    'isRequired' => true,
68
                )
69
            ),
70
            'pluginCode' => array(
0 ignored issues
show
introduced by
Add a comma after each item in a multi-line array
Loading history...
71
                'no' => 2,
72
                'label' => '[+]Plugin Code: ',
73
                'value' => null,
74
                'name' => '[+]Please enter Plugin Code (First letter is uppercase alphabet only. alphabet and numbers are allowed.)',
75
                'validation' => array(
76
                    'isRequired' => true,
77
                    'pattern' => '/^[A-Z][0-9a-zA-Z]*$/',
78
                    'isCode' => $this->getPluginCodes(),
79
                )
80
            ),
81
            'version' => array(
0 ignored issues
show
introduced by
Add a comma after each item in a multi-line array
Loading history...
82
                'no' => 3,
83
                'label' => '[+]Version: ',
84
                'value' => null,
85
                'name' => '[+]Please enter version (correct format is x.y.z)',
86
                'validation' => array(
87
                    'isRequired' => true,
88
                    'pattern' => '/^\d+.\d+.\d+$/',
89
                )
90
            ),
91
            'author' => array(
0 ignored issues
show
introduced by
Add a comma after each item in a multi-line array
Loading history...
92
                'no' => 4,
93
                'label' => '[+]Author: ',
94
                'value' => null,
95
                'name' => '[+]Please enter author name or company',
96
                'validation' => array(
97
                    'isRequired' => true,
98
                )
99
            ),
100
            'supportFlag' => array(
0 ignored issues
show
introduced by
Add a comma after each item in a multi-line array
Loading history...
101
                'no' => 5,
102
                'label' => '[+]Old version support: ',
103
                'value' => null,
104
                'name' => '[+]Do you want to support old versions too? [y/n]',
105
                'show' => array(1 => 'Yes', 0 => 'No'),
106
                'validation' => array(
107
                    'isRequired' => true,
108
                    'choice' => array('y' => 1, 'n' => 0),
109
                )
110
            ),
111
            'events' => array(
0 ignored issues
show
introduced by
Add a comma after each item in a multi-line array
Loading history...
112
                'no' => 6,
113
                'label' => '[+]SiteEvents: ',
114
                'value' => array(),
115
                'name' => '[+]Please enter site events(you can find documentation here http://www.ec-cube.net/plugin/)',
116
                'validation' => array(
117
                    'isRequired' => false,
118
                    'inArray' => 'getEvents',
119
                )
120
            ),
121
            'hookPoints' => array(
0 ignored issues
show
introduced by
Add a comma after each item in a multi-line array
Loading history...
122
                'no' => 7,
123
                'label' => '[+]hookpoint: ',
124
                'value' => array(),
125
                'name' => '[+]Please enter hookpoint, sample:front.cart.up.initialize',
126
                'validation' => array(
127
                    'isRequired' => false,
128
                    'inArray' => $this->getHookPoints(),
129
                )
130
            ),
131
            'useOrmPath' => array(
0 ignored issues
show
introduced by
Add a comma after each item in a multi-line array
Loading history...
132
                'no' => 8,
133
                'label' => '[+]Use orm.path: ',
134
                'value' => null,
135
                'name' => '[+]Would you like to use orm.path? [y/n]',
136
                'show' => array(1 => 'Yes', 0 => 'No'),
137
                'validation' => array(
138
                    'isRequired' => true,
139
                    'choice' => array('y' => 1, 'n' => 0),
140
                )
141
            ),
142
        );
143
    }
144
145
    /**
146
     * フックポイント一覧の取得
147
     *
148
     * @return array
149
     */
150
    protected function getHookPoints()
151
    {
152
        if ($this->hookPoints === null) {
153
            $Ref = new \ReflectionClass('\Eccube\Event\EccubeEvents');
154
            $this->hookPoints = array_flip($Ref->getConstants());
155
        }
156
157
        return $this->hookPoints;
158
    }
159
160
    /**
161
     * イベント一覧の取得
162
     *
163
     * @return array|mixed
164
     */
165
    protected function getEvents()
166
    {
167
        if (!isset($this->paramList['supportFlag']['value'])) {
168
            return array();
169
        }
170
        if ($this->events === null) {
171
            $this->events = array();
172
            $routeEvents = array();
173
            if ($this->paramList['supportFlag']['value']) {
174
                $this->events = include $this->app['config']['root_dir'].'/src/Eccube/Command/GeneratorCommand/generatortemplate/eventList.php';
175
                $routeEvents['eccube.event.controller.__route__.before'] = 'Controller__route__Before';
176
                $routeEvents['eccube.event.controller.__route__.after'] = 'Controller__route__After';
177
                $routeEvents['eccube.event.controller.__route__.finish'] = 'Controller__route__Finish';
178
                $routeEvents['eccube.event.render.__route__.before'] = 'Render__route__Before';
179
            }
180
            $this->events += include $this->app['config']['root_dir'].'/src/Eccube/Command/GeneratorCommand/generatortemplate/eventListNew.php';
181
182
            $routeEvents['eccube.event.route.__route__.request'] = 'Route__route__Request';
183
            $routeEvents['eccube.event.route.__route__.controller'] = 'Route__route__Controller';
184
            $routeEvents['eccube.event.route.__route__.response'] = 'Route__route__Response';
185
            $routeEvents['eccube.event.route.__route__.exception'] = 'Route__route__Exception';
186
            $routeEvents['eccube.event.route.__route__.terminate'] = 'Route__route__Terminate';
187
            $allRoutes = array();
188
189
            $controllers = $this->app['controllers'];
190
            $collection = $controllers->flush();
191
            foreach ($collection as $eventName => $dummy) {
192
                $allRoutes[] = $eventName;
193
            }
194
195
            $routes = $this->app['routes']->all();
196
197
            foreach ($routes as $eventName => $dummy) {
198
                $allRoutes[] = $eventName;
199
            }
200
201
            foreach ($allRoutes as $eventName) {
202
                $eventOnFunc = join(array_map('ucfirst', explode('_', strtolower($eventName))));
203
204
                foreach ($routeEvents as $keys => $node) {
205
                    $this->events[str_replace('__route__', $eventName, $keys)] = str_replace('__route__', $eventOnFunc, $node);
206
                }
207
            }
208
        }
209
210
        return $this->events;
211
    }
212
213
    protected function start()
214
    {
215
        $pluginCode = $this->paramList['pluginCode']['value'];
216
217
        $codes = $this->getPluginCodes();
218
        if (in_array($pluginCode, $codes)) {
219
            $this->exitGenerator('<error>Plugin with this code already exists.</error>');
220
221
            return;
222
        }
223
224
        $this->createFilesAndFolders($pluginCode, $this->paramList);
225
        $this->createDbRecords($pluginCode, $this->paramList);
226
        $this->exitGenerator('Plugin was created successfully');
227
    }
228
229
    private function createFilesAndFolders($code, $paramList)
230
    {
231
        $fsList = array(
232
            'dir' => array(),
233
            'file' => array(),
234
        );
235
236
        // config.ymlを作成
237
        $config = array();
238
        $config['name'] = $paramList['pluginName']['value'];
239
        $config['code'] = $code;
240
        $config['version'] = $paramList['version']['value'];
241
        if (!empty($paramList['hookPoints']['value']) || !empty($paramList['events']['value'])) {
242
            $config['event'] = $code.'Event';
243
        }
244
        $config['service'] = array($code.'ServiceProvider');
245
        if ($this->paramList['useOrmPath']['value']) {
246
            $config['orm.path'] = array('/Resource/doctrine');
247
        }
248
249
        $codePath = $this->app['config']['root_dir'].'/app/Plugin/'.$code;
250
251
        $file = new Filesystem();
252
        $file->mkdir($codePath);
253
        if (is_dir($codePath)) {
254
            $fsList['dir'][$codePath] = true;
255
        } else {
256
            $fsList['dir'][$codePath] = false;
257
        }
258
259
        $srcPath = $codePath.'/config.yml';
260
        file_put_contents($srcPath, Yaml::dump($config));
261
        if (is_file($srcPath)) {
262
            $fsList['file'][$srcPath] = true;
263
        } else {
264
            $fsList['file'][$srcPath] = false;
265
        }
266
267
        $author = $paramList['author']['value'];
268
        $year = date('Y');
269
270
        // PluginManager
271
        $pluginFileBefore = file_get_contents($this->app['config']['root_dir'].'/src/Eccube/Command/GeneratorCommand/generatortemplate/PluginManager.php');
272
        $from = '/\[code\]/';
273
        $pluginFileAfter = preg_replace($from, $code, $pluginFileBefore);
274
        $from = '/\[author\]/';
275
        $pluginFileAfter = preg_replace($from, $author, $pluginFileAfter);
276
        $from = '/\[year\]/';
277
        $pluginFileAfter = preg_replace($from, $year, $pluginFileAfter);
278
279
        $srcPath = $codePath.'/PluginManager.php';
280
        file_put_contents($srcPath, $pluginFileAfter);
281
        if (is_file($srcPath)) {
282
            $fsList['file'][$srcPath] = true;
283
        } else {
284
            $fsList['file'][$srcPath] = false;
285
        }
286
287
        // ServiceProvider
288
        $pluginFileBefore = file_get_contents($this->app['config']['root_dir'].'/src/Eccube/Command/GeneratorCommand/generatortemplate/ServiceProvider.php');
289
        $from = '/\[code\]/';
290
        $pluginFileAfter = preg_replace($from, $code, $pluginFileBefore);
291
        $from = '/\[lower_code\]/';
292
        $pluginFileAfter = preg_replace($from, mb_strtolower($code), $pluginFileAfter);
293
        $from = '/\[author\]/';
294
        $pluginFileAfter = preg_replace($from, $author, $pluginFileAfter);
295
        $from = '/\[year\]/';
296
        $pluginFileAfter = preg_replace($from, $year, $pluginFileAfter);
297
298
        $file->mkdir($codePath.'/ServiceProvider');
299 View Code Duplication
        if (is_dir($codePath.'/ServiceProvider')) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
300
            $fsList['dir'][$codePath.'/ServiceProvider'] = true;
301
        } else {
302
            $fsList['dir'][$codePath.'/ServiceProvider'] = false;
303
        }
304
305
        $srcPath = $codePath.'/ServiceProvider/'.$code.'ServiceProvider.php';
306
        file_put_contents($srcPath, $pluginFileAfter);
307
        if (is_file($srcPath)) {
308
            $fsList['file'][$srcPath] = true;
309
        } else {
310
            $fsList['file'][$srcPath] = false;
311
        }
312
313
        // ConfigController
314
        $pluginFileBefore = file_get_contents($this->app['config']['root_dir'].'/src/Eccube/Command/GeneratorCommand/generatortemplate/ConfigController.php');
315
        $from = '/\[code\]/';
316
        $pluginFileAfter = preg_replace($from, $code, $pluginFileBefore);
317
        $from = '/\[author\]/';
318
        $pluginFileAfter = preg_replace($from, $author, $pluginFileAfter);
319
        $from = '/\[year\]/';
320
        $pluginFileAfter = preg_replace($from, $year, $pluginFileAfter);
321
        $from = '/\[code_name\]/';
322
        $pluginFileAfter = preg_replace($from, mb_strtolower($code), $pluginFileAfter);
323
324
        $file->mkdir($codePath.'/Controller');
325 View Code Duplication
        if (is_dir($codePath.'/Controller')) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
326
            $fsList['dir'][$codePath.'/Controller'] = true;
327
        } else {
328
            $fsList['dir'][$codePath.'/Controller'] = false;
329
        }
330
331
        $srcPath = $codePath.'/Controller/ConfigController.php';
332
        file_put_contents($srcPath, $pluginFileAfter);
333
        if (is_file($srcPath)) {
334
            $fsList['file'][$srcPath] = true;
335
        } else {
336
            $fsList['file'][$srcPath] = false;
337
        }
338
339
        // Controller
340
        $pluginFileBefore = file_get_contents($this->app['config']['root_dir'].'/src/Eccube/Command/GeneratorCommand/generatortemplate/Controller.php');
341
        $from = '/\[code\]/';
342
        $pluginFileAfter = preg_replace($from, $code, $pluginFileBefore);
343
        $from = '/\[author\]/';
344
        $pluginFileAfter = preg_replace($from, $author, $pluginFileAfter);
345
        $from = '/\[year\]/';
346
        $pluginFileAfter = preg_replace($from, $year, $pluginFileAfter);
347
        $from = '/\[code_name\]/';
348
        $pluginFileAfter = preg_replace($from, mb_strtolower($code), $pluginFileAfter);
349
350
        $srcPath = $codePath.'/Controller/'.$code.'Controller.php';
351
        file_put_contents($srcPath, $pluginFileAfter);
352
        if (is_file($srcPath)) {
353
            $fsList['file'][$srcPath] = true;
354
        } else {
355
            $fsList['file'][$srcPath] = false;
356
        }
357
358
        // Form
359
        $pluginFileBefore = file_get_contents($this->app['config']['root_dir'].'/src/Eccube/Command/GeneratorCommand/generatortemplate/ConfigType.php');
360
        $from = '/\[code\]/';
361
        $pluginFileAfter = preg_replace($from, $code, $pluginFileBefore);
362
        $from = '/\[author\]/';
363
        $pluginFileAfter = preg_replace($from, $author, $pluginFileAfter);
364
        $from = '/\[year\]/';
365
        $pluginFileAfter = preg_replace($from, $year, $pluginFileAfter);
366
        $from = '/\[code_name\]/';
367
        $pluginFileAfter = preg_replace($from, mb_strtolower($code), $pluginFileAfter);
368
369
        $file->mkdir($codePath.'/Form/Type');
370 View Code Duplication
        if (is_dir($codePath.'/Form/Type')) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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
            $fsList['dir'][$codePath.'/Form/Type'] = true;
372
        } else {
373
            $fsList['dir'][$codePath.'/Form/Type'] = false;
374
        }
375
376
        $srcPath = $codePath.'/Form/Type/'.$code.'ConfigType.php';
377
        file_put_contents($codePath.'/Form/Type/'.$code.'ConfigType.php', $pluginFileAfter);
378
        if (is_file($srcPath)) {
379
            $fsList['file'][$srcPath] = true;
380
        } else {
381
            $fsList['file'][$srcPath] = false;
382
        }
383
384
        // Twig
385
        $pluginFileBefore = file_get_contents($this->app['config']['root_dir'].'/src/Eccube/Command/GeneratorCommand/generatortemplate/config.twig');
386
        $from = '/\[code\]/';
387
        $pluginFileAfter = preg_replace($from, $code, $pluginFileBefore);
388
389
        $file->mkdir($codePath.'/Resource/template/admin');
390 View Code Duplication
        if (is_dir($codePath.'/Resource/template/admin')) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
391
            $fsList['dir'][$codePath.'/Resource/template/admin'] = true;
392
        } else {
393
            $fsList['dir'][$codePath.'/Resource/template/admin'] = false;
394
        }
395
396
        $srcPath = $codePath.'/Resource/template/admin/config.twig';
397
        file_put_contents($srcPath, $pluginFileAfter);
398
        if (is_file($srcPath)) {
399
            $fsList['file'][$srcPath] = true;
400
        } else {
401
            $fsList['file'][$srcPath] = false;
402
        }
403
404
        // index.twig
405
        $pluginFileBefore = file_get_contents($this->app['config']['root_dir'].'/src/Eccube/Command/GeneratorCommand/generatortemplate/index.twig');
406
        $from = '/\[code\]/';
407
        $pluginFileAfter = preg_replace($from, mb_strtolower($code), $pluginFileBefore);
408
409
        $file->mkdir($codePath.'/Resource/template/admin');
410 View Code Duplication
        if (is_dir($codePath.'/Resource/template/admin')) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
411
            $fsList['dir'][$codePath.'/Resource/template/admin'] = true;
412
        } else {
413
            $fsList['dir'][$codePath.'/Resource/template/admin'] = false;
414
        }
415
416
        $srcPath = $codePath.'/Resource/template/index.twig';
417
        file_put_contents($srcPath, $pluginFileAfter);
418
        if (is_file($srcPath)) {
419
            $fsList['file'][$srcPath] = true;
420
        } else {
421
            $fsList['file'][$srcPath] = false;
422
        }
423
424
        $onFunctions = array();
425
        $eventKeys = array();
426
        $onEvents = array();
427
428
        // イベント
429
        $events = $paramList['events']['value'];
430
        if (count($events) > 0) {
431
            foreach ($events as $eventKey => $eventConst) {
432
                $onEvents[$eventKey] = array(array('on'.$eventConst.', NORMAL'));
433
                $onFunctions[$eventKey] = 'on'.$eventConst;
434
                $eventKeys[] = $eventKey;
435
            }
436
        }
437
438
        // フックポイント
439
        $hookPoints = $paramList['hookPoints']['value'];
440
        if (count($hookPoints)) {
441
            foreach ($hookPoints as $hookKey => $hookConst) {
442
                $onName = 'on'.join(array_map('ucfirst', explode('_', strtolower($hookConst))));
443
                $onEvents[$hookKey] = array(array($onName.', NORMAL'));
444
                $onFunctions[$hookKey] = $onName;
445
            }
446
        }
447
448
        if (count($onEvents)) {
449
            $srcPath = $codePath.'/event.yml';
450
            file_put_contents($srcPath, str_replace('\'', '', Yaml::dump($onEvents)));
451
            if (is_file($srcPath)) {
452
                $fsList['file'][$srcPath] = true;
453
            } else {
454
                $fsList['file'][$srcPath] = false;
455
            }
456
457
            $pluginFileBefore = file_get_contents($this->app['config']['root_dir'].'/src/Eccube/Command/GeneratorCommand/generatortemplate/Event.php');
458
459
            // Event
460
            $from = '/\[code\]/';
461
            $pluginFileAfter = preg_replace($from, $code, $pluginFileBefore);
462
            $from = '/\[author\]/';
463
            $pluginFileAfter = preg_replace($from, $author, $pluginFileAfter);
464
            $from = '/\[year\]/';
465
            $pluginFileAfter = preg_replace($from, $year, $pluginFileAfter);
466
467
            $functions = '';
468
            $args = include $this->app['config']['root_dir'].'/src/Eccube/Command/GeneratorCommand/generatortemplate/eventArguments.php';
469
            foreach ($onFunctions as $key => $name) {
470
                if (in_array($key, $eventKeys)) {
471
                    // 共通イベントは引数の型を利用するイベントにより変更
472
                    $ext = pathinfo($key, PATHINFO_EXTENSION);
473
                    if (array_key_exists($ext, $args)) {
474
                        $functions .= "    /**\n     * @param {$args[$ext]} \$event\n     */\n    public function {$name}({$args[$ext]} \$event)\n    {\n    }\n\n";
475
                    } else {
476
                        // 旧イベントの場合、引数は「eccube.event.render」のみ可能
477
                        if (preg_match("/^eccube.event.render\./", $key)) {
478
                            $functions .= "    /**\n     * @param {$args['eccube.event.render']} \$event\n     */\n    public function {$name}({$args['eccube.event.render']} \$event)\n    {\n    }\n\n";
479
                        } else {
480
                            $functions .= "    /**\n     *\n     */\n    public function {$name}()\n    {\n    }\n\n";
481
                        }
482
                    }
483
                } else {
484
                    // HookPointイベントの引数はEventArgs共通
485
                    $functions .= "    /**\n     * @param EventArgs \$event\n     */\n    public function {$name}(EventArgs \$event)\n    {\n    }\n\n";
486
                }
487
            }
488
            $from = '/\[hookpoint_function\]/';
489
            $pluginFileAfter = preg_replace($from, $functions, $pluginFileAfter);
490
            $srcPath = $codePath.'/'.$code.'Event.php';
491
            file_put_contents($srcPath, $pluginFileAfter);
492
            if (is_file($srcPath)) {
493
                $fsList['file'][$srcPath] = true;
494
            } else {
495
                $fsList['file'][$srcPath] = false;
496
            }
497
        }
498
499
        // LICENSE
500
        $srcPath = $codePath.'/LICENSE';
501
        $file->copy($this->app['config']['root_dir'].'/src/Eccube/Command/GeneratorCommand/generatortemplate/LICENSE', $srcPath);
502
        if (is_file($srcPath)) {
503
            $fsList['file'][$srcPath] = true;
504
        } else {
505
            $fsList['file'][$srcPath] = false;
506
        }
507
508
        $this->completeMessage($fsList);
509
510
    }
511
512
    private function createDbRecords($code, $paramList)
513
    {
514
        // DB登録
515
        $Plugin = new Plugin();
516
        $Plugin->setName($paramList['pluginName']['value']);
517
        $Plugin->setCode($code);
518
        $Plugin->setClassName('');
519
        $Plugin->setVersion($paramList['version']['value']);
520
        $Plugin->setEnable(Constant::DISABLED);
521
        $Plugin->setSource(0);
522
        $Plugin->setDelFlg(Constant::DISABLED);
523
524
        $this->app['orm.em']->persist($Plugin);
525
        $this->app['orm.em']->flush($Plugin);
526
527
        $this->output->writeln('');
528
        $this->output->writeln('[+]Database');
529
        if ($Plugin->getId()) {
530
            $this->output->writeln('<info> Plugin information was added to table [DB.Plugin] (id='.$Plugin->getId().')</info>');
531
        } else {
532
            $this->output->writeln('<error> there was a problem inserting plugin information to table [DB.Plugin] (id='.$Plugin->getId().')</error>');
533
        }
534
535
        $hookPoints = $paramList['hookPoints']['value'];
536
        if (empty($hookPoints)) {
537
            return;
538
        }
539
540
        $eventCount = 0;
541
        foreach ($hookPoints as $hookKey => $hookConst) {
542
            $PluginEventHandler = new PluginEventHandler();
543
            $functionName = 'on'.join(array_map('ucfirst', explode('_', strtolower($hookConst))));
544
            $PluginEventHandler->setPlugin($Plugin)
545
                ->setEvent($hookKey)
546
                ->setPriority($this->app['eccube.repository.plugin_event_handler']->calcNewPriority($hookKey, $functionName))
547
                ->setHandler($functionName)
548
                ->setHandlerType('NORMAL')
549
                ->setDelFlg(Constant::DISABLED);
550
            $this->app['orm.em']->persist($PluginEventHandler);
551
            $eventCount++;
552
        }
553
        $this->app['orm.em']->flush();
554
        if ($eventCount) {
555
            $this->output->writeln('');
556
            $this->output->writeln('<info> Plugin information was added to table [DB.PluginEventHandler] (inserts number='.$eventCount.') </info>');
557
        }
558
    }
559
}
560