Completed
Push — master ( ce0f45...969a46 )
by yuuki
01:36
created

ModulePublishCommand::makeDirectory()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 3
nc 2
nop 1
1
<?php
2
3
/**
4
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
5
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
6
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
7
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
8
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
9
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
10
 * THE SOFTWARE.
11
 *
12
 * This software consists of voluntary contributions made by many individuals
13
 * and is licensed under the MIT license.
14
 *
15
 * Copyright (c) 2015-2017 Yuuki Takezawa
16
 *
17
 */
18
19
namespace Ytake\LaravelAspect\Console;
20
21
use Illuminate\Support\Str;
22
use Illuminate\Console\Command;
23
use Illuminate\Filesystem\Filesystem;
24
use Symfony\Component\Console\Input\InputArgument;
25
26
/**
27
 * Class ModulePublishCommand
28
 *
29
 * @codeCoverageIgnore
30
 */
31
class ModulePublishCommand extends Command
32
{
33
    /** @var string */
34
    protected $name = 'ytake:aspect-module-publish';
35
36
    /** @var string */
37
    protected $description = 'Publish any aspect modules from ytake/laravel-aspect';
38
39
    /** @var Filesystem */
40
    protected $filesystem;
41
42
    /** @var string */
43
    protected $classPath = 'Modules';
44
45
    /** @var array  package modules */
46
    protected $modules = [
47
        'CacheableModule'      => 'Ytake\LaravelAspect\Modules\CacheableModule',
48
        'CacheEvictModule'     => 'Ytake\LaravelAspect\Modules\CacheEvictModule',
49
        'CachePutModule'       => 'Ytake\LaravelAspect\Modules\CachePutModule',
50
        'TransactionalModule'  => 'Ytake\LaravelAspect\Modules\TransactionalModule',
51
        'LoggableModule'       => 'Ytake\LaravelAspect\Modules\LoggableModule',
52
        'LogExceptionsModule'  => 'Ytake\LaravelAspect\Modules\LogExceptionsModule',
53
        'PostConstructModule'  => 'Ytake\LaravelAspect\Modules\PostConstructModule',
54
        'RetryOnFailureModule' => 'Ytake\LaravelAspect\Modules\RetryOnFailureModule',
55
        'MessageDrivenModule'  => 'Ytake\LaravelAspect\Modules\MessageDrivenModule',
56
        'QueryLogModule'       => 'Ytake\LaravelAspect\Modules\QueryLogModule',
57
    ];
58
59
    /**
60
     * @param Filesystem $filesystem
61
     */
62
    public function __construct(Filesystem $filesystem)
63
    {
64
        parent::__construct();
65
        $this->filesystem = $filesystem;
66
    }
67
68
    /**
69
     * @return void
70
     */
71
    public function handle()
72
    {
73
        foreach ($this->modules as $className => $module) {
74
            $path = $this->getPath($this->parseClassName($className, $this->argument('module_dir')));
75
76
            if ($this->filesystem->exists($path)) {
77
                continue;
78
            }
79
            $stub = $this->filesystem->get($this->stub());
80
            $extendClassName = $this->getExtendsClassName($module);
81
            $source = str_replace(
82
                [
83
                    'DummyNamespace',
84
                    'DummyClass',
85
                    'DummyAspectModuleClass',
86
                    'DummyModuleClass',
87
                ],
88
                [
89
                    $this->laravel->getNamespace() . $this->argument('module_dir'),
0 ignored issues
show
Bug introduced by
The method getNamespace() does not seem to exist on object<Illuminate\Contra...Foundation\Application>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
90
                    $className,
91
                    $module . ' as ' . $extendClassName,
92
                    $extendClassName,
93
                ], $stub);
94
            $this->makeDirectory($path);
95
            $this->filesystem->put($path, $source);
96
            $this->info($path . ' created successfully.');
97
        }
98
    }
99
100
    /**
101
     * @return array
102
     */
103
    protected function getArguments()
104
    {
105
        return [
106
            ['module_dir', InputArgument::OPTIONAL, 'The name of the class directory', $this->classPath],
107
        ];
108
    }
109
110
    /**
111
     * @param string $name
112
     *
113
     * @return string
114
     */
115
    protected function getPath($name)
116
    {
117
        $name = str_replace($this->laravel->getNamespace(), '', $name);
0 ignored issues
show
Bug introduced by
The method getNamespace() does not seem to exist on object<Illuminate\Contra...Foundation\Application>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
118
119
        return $this->laravel['path'] . '/' . str_replace('\\', '/', $name) . '.php';
120
    }
121
122
    /**
123
     * Parse the name and format according to the root namespace.
124
     *
125
     * @param  string $name
126
     *
127
     * @return string
128
     */
129
    protected function parseClassName($name, $moduleDirectory = null)
130
    {
131
        $rootNamespace = $this->laravel->getNamespace();
0 ignored issues
show
Bug introduced by
The method getNamespace() does not seem to exist on object<Illuminate\Contra...Foundation\Application>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
132
133
        if (Str::startsWith($name, $rootNamespace)) {
134
            return $name;
135
        }
136
137
        if (Str::contains($name, '/')) {
138
            $name = str_replace('/', '\\', $name);
139
        }
140
141
        return $this->parseClassName(
142
            trim($rootNamespace, '\\') . '\\' . $moduleDirectory . '\\' . $name
143
        );
144
    }
145
146
    /**
147
     * added custom aspect module, override package modules
148
     *
149
     * @param $module
150
     *
151
     * @return $this
152
     */
153
    protected function addModule($module)
154
    {
155
        $this->modules[$module];
156
157
        return $this;
158
    }
159
160
    /**
161
     * Build the directory for the class if necessary.
162
     *
163
     * @param  string $path
164
     *
165
     * @return string
166
     */
167
    protected function makeDirectory($path)
168
    {
169
        if (!$this->filesystem->isDirectory(dirname($path))) {
170
            $this->filesystem->makeDirectory(dirname($path), 0777, true, true);
171
        }
172
    }
173
174
    /**
175
     * @param $module
176
     *
177
     * @return string
178
     */
179
    protected function getExtendsClassName($module)
180
    {
181
        $shortName = (new \ReflectionClass($module))->getShortName();
182
        $extendClassName = "Package{$shortName}";
183
184
        return $extendClassName;
185
    }
186
187
    /**
188
     * @return string
189
     */
190
    protected function stub()
191
    {
192
        /** module stub file path */
193
        return __DIR__ . '/stub/ModuleStub.stub';
194
    }
195
}
196