Completed
Push — feature/merge-build-stop-step ( d59daf )
by Tom
8s
created

MakeModuleCommand::createEtcModuleFile()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 7

Duplication

Lines 17
Ratio 100 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 17
loc 17
rs 9.4285
cc 1
eloc 7
nc 1
nop 2
1
<?php
2
3
namespace N98\Magento\Command\Developer\Console;
4
5
use Magento\Framework\App\Cache;
6
use Magento\Framework\App\Filesystem\DirectoryList;
7
use Magento\Framework\App\State\CleanupFiles;
8
use Magento\Framework\Filesystem;
9
use Magento\Framework\Filesystem\Directory\ReadInterface;
10
use Magento\Framework\Filesystem\Directory\WriteInterface;
11
use Magento\Framework\Module\Status;
12
use N98\Magento\Command\Developer\Console\Structure\ModuleNameStructure;
13
use Symfony\Component\Console\Input\ArrayInput;
14
use Symfony\Component\Console\Input\InputArgument;
15
use Symfony\Component\Console\Input\InputInterface;
16
use Symfony\Component\Console\Output\OutputInterface;
17
18
class MakeModuleCommand extends AbstractGeneratorCommand
19
{
20
    protected function configure()
21
    {
22
        $this
23
            ->setName('make:module')
24
            ->addArgument('modulename', InputArgument::REQUIRED)
25
            ->setDescription('Creates a new module');
26
    }
27
28
    /**
29
     * @param InputInterface $input
30
     * @param OutputInterface $output
31
     *
32
     * @return int|void
33
     */
34
    protected function execute(InputInterface $input, OutputInterface $output)
35
    {
36
        $moduleName = new ModuleNameStructure($input->getArgument('modulename'));
37
        
38
        $filesystem = $this->get(Filesystem::class);
39
        /** @var $filesystem Filesystem */
40
        $appDirectoryWriter = $filesystem->getDirectoryWrite(DirectoryList::APP);
41
        $appDirectoryReader = $filesystem->getDirectoryRead(DirectoryList::APP);
42
43
        $this->createRegistrationFile($moduleName, $appDirectoryWriter);
44
        $this->createComposerFile($moduleName, $appDirectoryWriter);
45
        $this->createEtcModuleFile($moduleName, $appDirectoryWriter);
46
        $this->createTestDirectories($moduleName, $appDirectoryWriter);
47
        $this->includeRegistrationFile($moduleName, $appDirectoryReader);
48
49
        $output->writeln('<info>created new module </info><comment>' . $moduleName->getFullModuleName() . '</comment>');
50
51
        $this->activateNewModuleInSystem($output, $moduleName);
52
        $this->cleanClassCache();
53
54
        $this->changeToNewModule($output, $moduleName);
55
    }
56
57
    /**
58
     * @param OutputInterface $output
59
     * @param ModuleNameStructure $moduleName
60
     * @return int
61
     */
62
    private function changeToNewModule(OutputInterface $output, ModuleNameStructure $moduleName)
63
    {
64
        $command = $this->getApplication()->find('module');
65
        $arguments = [
66
            'module' => $moduleName->getFullModuleName(),
67
        ];
68
        $input = new ArrayInput($arguments);
69
70
        return $command->run($input, $output);
71
    }
72
73
    /**
74
     * @param ModuleNameStructure $moduleName
75
     * @param WriteInterface $appDirectoryWriter
76
     */
77 View Code Duplication
    private function createRegistrationFile(ModuleNameStructure $moduleName, WriteInterface $appDirectoryWriter)
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...
78
    {
79
        $registrationFileBody = <<<FILE_BODY
80
<?php
81
82
\Magento\Framework\Component\ComponentRegistrar::register(
83
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
84
    '{$moduleName->getFullModuleName()}',
85
    __DIR__
86
);
87
88
FILE_BODY;
89
        $appDirectoryWriter->writeFile(
90
            'code/' . $moduleName->getVendorName() . '/' . $moduleName->getShortModuleName() . '/registration.php',
91
            $registrationFileBody
92
        );
93
    }
94
95
    /**
96
     * @param ModuleNameStructure $moduleName
97
     * @param WriteInterface $appDirectoryWriter
98
     */
99
    private function createComposerFile(ModuleNameStructure $moduleName, WriteInterface $appDirectoryWriter)
100
    {
101
        $composerFileBody = $this->getHelper('twig')->render(
102
            'dev/console/make/module/composer.json.twig',
103
            [
104
                'vendor' => $moduleName->getVendorName(),
105
                'module' => $moduleName->getShortModuleName(),
106
                'namespace' => str_replace('\\', '\\\\', $this->getModuleNamespace($moduleName->getFullModuleName())),
107
            ]
108
        );
109
110
        $appDirectoryWriter->writeFile(
111
            'code/' . $moduleName->getVendorName() . '/' . $moduleName->getShortModuleName() . '/composer.json',
112
            $composerFileBody
113
        );
114
    }
115
116
    /**
117
     * @param ModuleNameStructure $moduleName
118
     * @param WriteInterface $appDirectoryWriter
119
     */
120 View Code Duplication
    private function createEtcModuleFile(ModuleNameStructure $moduleName, WriteInterface $appDirectoryWriter)
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...
121
    {
122
        $moduleFileBody = <<<FILE_BODY
123
<?xml version="1.0"?>
124
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 138 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
125
    <module name="{$moduleName->getFullModuleName()}" setup_version="1.0.0">
126
        <sequence>
127
        </sequence>
128
    </module>
129
</config>
130
131
FILE_BODY;
132
133
        $appDirectoryWriter->writeFile(
134
            'code/' . $moduleName->getVendorName() . '/' . $moduleName->getShortModuleName() . '/etc/module.xml', $moduleFileBody
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 129 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
135
        );
136
    }
137
138
    /**
139
     * @param ModuleNameStructure $moduleName
140
     * @param WriteInterface $appDirectoryWriter
141
     */
142
    private function createTestDirectories(ModuleNameStructure $moduleName, WriteInterface $appDirectoryWriter)
143
    {
144
        $appDirectoryWriter->create(
145
            'code/' . $moduleName->getVendorName() . '/' . $moduleName->getShortModuleName() . '/Test/Unit'
146
        );
147
    }
148
149
    /**
150
     * @param ModuleNameStructure $moduleName
151
     * @param ReadInterface $appDirectoryReader
152
     */
153
    private function includeRegistrationFile(ModuleNameStructure $moduleName, ReadInterface $appDirectoryReader)
154
    {
155
        $moduleRegistrationFile = $appDirectoryReader->getAbsolutePath(
156
            'code/' . $moduleName->getVendorName() . '/' . $moduleName->getShortModuleName() . '/registration.php'
157
        );
158
159
        include($moduleRegistrationFile);
160
    }
161
162
    /**
163
     * @return voic
0 ignored issues
show
Documentation introduced by
Should the return type not be voic|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
164
     */
165
    private function cleanClassCache()
166
    {
167
        $applicationCache = $this->get(Cache::class);
168
        $applicationCache->clean();
169
170
        $cleanupFiles = $this->get(CleanupFiles::class);
171
        $cleanupFiles->clearCodeGeneratedClasses();
172
    }
173
174
    /**
175
     * @param OutputInterface $output
176
     * @param ModuleNameStructure $moduleName
177
     */
178
    private function activateNewModuleInSystem(OutputInterface $output, ModuleNameStructure $moduleName)
179
    {
180
        $moduleStatus = $this->get(Status::class);
181
        /** @var $moduleStatus Status */
182
        $moduleStatus->setIsEnabled(true, [$moduleName->getFullModuleName()]);
183
184
        $output->writeln('<info>activated new module </info><comment>' . $moduleName->getFullModuleName() . '</comment>');
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 122 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
185
    }
186
}
187