Completed
Push — feature/php-7.1-partial ( 412847 )
by Tom
03:40
created

setCurrentModuleDirectoryWriter()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace N98\Magento\Command\Developer\Console;
4
5
use Magento\Framework\Code\Generator\ClassGenerator;
6
use Magento\Framework\Filesystem\Directory\ReadFactory as DirectoryReadFactory;
7
use Magento\Framework\Filesystem\Directory\WriteFactory as DirectoryWriteFactory;
8
use Magento\Framework\Filesystem\Directory\WriteInterface;
9
use Magento\Framework\Module\Dir as ModuleDir;
10
use N98\Magento\Command\Developer\Console\Structure\ModuleNameStructure;
11
use Symfony\Component\Console\Output\OutputInterface;
12
use Zend\Code\Generator\FileGenerator;
13
use Zend\Filter\Word\SeparatorToSeparator;
14
15
abstract class AbstractGeneratorCommand extends AbstractConsoleCommand
16
{
17
    /**
18
     * @var WriteInterface
19
     */
20
    protected static $currentModuleDirWriter = null;
21
22
    /**
23
     * @param string $type
24
     * @return string
25
     */
26
    public function getCurrentModulePath($type = '')
27
    {
28
        return $this->get(ModuleDir::class)->getDir($this->getCurrentModuleName()->getFullModuleName(), $type);
29
    }
30
31
    /**
32
     * @param string $path
33
     * @return string
34
     */
35
    public function getCurrentModuleFilePath($path)
36
    {
37
        return $this->getCurrentModulePath() . DIRECTORY_SEPARATOR . ltrim($path, DIRECTORY_SEPARATOR);
38
    }
39
40
    /**
41
     * @return string
42
     */
43
    public function getCurrentModuleNamespace()
44
    {
45
        $moduleName = $this->getCurrentModuleName()->getFullModuleName();
46
47
        return $this->getModuleNamespace($moduleName);
48
    }
49
50
    /**
51
     * @param string $moduleName
52
     *
53
     * @return string
54
     */
55
    public function getModuleNamespace($moduleName)
56
    {
57
        list($vendorPrefix, $moduleNamespace) = explode('_', $moduleName);
58
59
        return $vendorPrefix . '\\' . $moduleNamespace;
60
    }
61
62
    /**
63
     * @return ModuleNameStructure
64
     */
65
    public function getCurrentModuleName()
66
    {
67
        try {
68
            $magerunInternal = $this->getScopeVariable('magerunInternal');
69
70
            $currentModuleName = $magerunInternal->currentModule;
71
72
            if (empty($currentModuleName)) {
73
                throw new \InvalidArgumentException('No module defined');
74
            }
75
        } catch (\InvalidArgumentException $e) {
76
            throw new \InvalidArgumentException('Module not defined. Please use "module <name>" command');
77
        }
78
79
        return new ModuleNameStructure($currentModuleName);
80
    }
81
82
    /**
83
     * Returns an id generated by current module name
84
     *
85
     * @return string
86
     */
87
    public function getCurrentModuleId()
88
    {
89
        return lcfirst(str_replace('_', '', $this->getCurrentModuleName()));
90
    }
91
92
    /**
93
     * @param string $name
94
     */
95
    public function setCurrentModuleName($name)
96
    {
97
        try {
98
            $magerunInternal = $this->getScopeVariable('magerunInternal');
99
        } catch (\InvalidArgumentException $e) {
100
            $magerunInternal = new \stdClass();
101
        }
102
        $magerunInternal->currentModule = $name;
103
        $this->setScopeVariable('magerunInternal', $magerunInternal);
104
105
        $this->reset();
106
    }
107
108
    /**
109
     * @param WriteInterface $currentModuleDirWriter
110
     */
111
    public function setCurrentModuleDirectoryWriter(WriteInterface $currentModuleDirWriter)
112
    {
113
        self::$currentModuleDirWriter = $currentModuleDirWriter;
114
    }
115
116
    /**
117
     * @return WriteInterface
118
     */
119
    public function getCurrentModuleDirectoryWriter()
120
    {
121
        if (self::$currentModuleDirWriter === null) {
122
            $directoryWrite = $this->create(DirectoryWriteFactory::class);
123
            /** @var $directoryWrite DirectoryWriteFactory */
124
125
            self::$currentModuleDirWriter = $directoryWrite->create($this->getCurrentModulePath());
126
        }
127
128
        return self::$currentModuleDirWriter;
129
    }
130
131
    /**
132
     * @return \Magento\Framework\Filesystem\Directory\Read
133
     */
134
    public function getCurrentModuleDirectoryReader()
135
    {
136
        $directoryRead = $this->get(DirectoryWriteFactory::class);
137
        /** @var $directoryRead DirectoryReadFactory */
138
        return $directoryRead->create($this->getCurrentModulePath());
139
    }
140
141
    /**
142
     * @param string $pathArgument
143
     * @return string
144
     */
145
    public function getNormalizedPathByArgument($pathArgument)
146
    {
147
        $namespaceFilterDot = $this->create(
148
            SeparatorToSeparator::class,
149
            ['searchSeparator' => '.', 'replacementSeparator' => DIRECTORY_SEPARATOR]
150
        );
151
        $namespaceFilterBackslash = $this->create(
152
            SeparatorToSeparator::class,
153
            ['searchSeparator' => '\\', 'replacementSeparator' => DIRECTORY_SEPARATOR]
154
        );
155
        $pathArgument = $namespaceFilterDot->filter($pathArgument);
156
        $pathArgument = $namespaceFilterBackslash->filter($pathArgument);
157
158
        $parts = explode(DIRECTORY_SEPARATOR, $pathArgument);
159
160
        return implode(DIRECTORY_SEPARATOR, array_map('ucfirst', $parts));
161
    }
162
163
    /**
164
     * @param string $pathArgument
165
     * @return string
166
     */
167
    public function getNormalizedClassnameByArgument($pathArgument)
168
    {
169
        $namespaceFilterDot = $this->create(
170
            SeparatorToSeparator::class,
171
            ['searchSeparator' => '.', 'replacementSeparator' => '\\']
172
        );
173
        $namespaceFilterBackslash = $this->create(
174
            SeparatorToSeparator::class,
175
            ['searchSeparator' => '.', 'replacementSeparator' => '\\']
176
        );
177
178
        $pathArgument = $namespaceFilterDot->filter($pathArgument);
179
        $pathArgument = $namespaceFilterBackslash->filter($pathArgument);
180
181
        $parts = explode('\\', $pathArgument);
182
183
        return implode('\\', array_map('ucfirst', $parts));
184
    }
185
186
    /**
187
     * @param OutputInterface $output
188
     * @param ClassGenerator $classGenerator
189
     * @param string $filePathToGenerate
190
     */
191
    protected function writeClassToFile(
192
        OutputInterface $output,
193
        ClassGenerator $classGenerator,
194
        $filePathToGenerate
195
    ) {
196
        $fileGenerator = FileGenerator::fromArray(
197
            [
198
                'classes' => [$classGenerator],
199
            ]
200
        );
201
202
        $this->getCurrentModuleDirectoryWriter()
203
            ->writeFile($filePathToGenerate, $fileGenerator->generate());
204
205
        $output->writeln('<info>generated </info><comment>' . $filePathToGenerate . '</comment>');
206
    }
207
208
    /**
209
     * Reset internal caches etc.
210
     */
211
    protected function reset()
212
    {
213
        self::$currentModuleDirWriter = null;
214
    }
215
}
216