Completed
Push — master ( 4ada12...4bd1d2 )
by yuuki
13s
created

ModulePublishCommand::stub()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
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-2016 Yuuki Takezawa
16
 *
17
 */
18
namespace Ytake\LaravelAspect\Console;
19
20
use Illuminate\Support\Str;
21
use Illuminate\Console\Command;
22
use Illuminate\Filesystem\Filesystem;
23
use Symfony\Component\Console\Input\InputArgument;
24
25
/**
26
 * Class ModulePublishCommand
27
 *
28
 * @codeCoverageIgnore
29
 */
30
class ModulePublishCommand extends Command
31
{
32
    /** @var string */
33
    protected $name = 'ytake:aspect-module-publish';
34
35
    /** @var string */
36
    protected $description = 'Publish any aspect modules from ytake/laravel-aspect';
37
38
    /** @var Filesystem */
39
    protected $filesystem;
40
41
    /** @var string */
42
    protected $classPath = 'Modules';
43
44
    /** @var array  package modules */
45
    protected $modules = [
46
        'CacheableModule'     => 'Ytake\LaravelAspect\Modules\CacheableModule',
47
        'CacheEvictModule'    => 'Ytake\LaravelAspect\Modules\CacheEvictModule',
48
        'CachePutModule'      => 'Ytake\LaravelAspect\Modules\CachePutModule',
49
        'TransactionalModule' => 'Ytake\LaravelAspect\Modules\TransactionalModule',
50
        'LoggableModule'      => 'Ytake\LaravelAspect\Modules\LoggableModule',
51
        'LogExceptionsModule' => 'Ytake\LaravelAspect\Modules\LogExceptionsModule',
52
    ];
53
54
    /**
55
     * @param Filesystem $filesystem
56
     */
57
    public function __construct(Filesystem $filesystem)
58
    {
59
        parent::__construct();
60
        $this->filesystem = $filesystem;
61
    }
62
63
    /**
64
     * @return void
65
     */
66
    public function fire()
67
    {
68
        foreach ($this->modules as $className => $module) {
69
            $path = $this->getPath($this->parseClassName($className, $this->argument('module_dir')));
70
71
            if ($this->filesystem->exists($path)) {
72
                continue;
73
            }
74
            $stub = $this->filesystem->get($this->stub());
75
            $extendClassName = $this->getExtendsClassName($module);
76
            $source = str_replace(
77
                [
78
                    'DummyNamespace',
79
                    'DummyClass',
80
                    'DummyAspectModuleClass',
81
                    'DummyModuleClass',
82
                ],
83
                [
84
                    $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...
85
                    $className,
86
                    $module . ' as ' . $extendClassName,
87
                    $extendClassName,
88
                ], $stub);
89
            $this->makeDirectory($path);
90
            $this->filesystem->put($path, $source);
91
            $this->info($path . ' created successfully.');
92
        }
93
    }
94
95
    /**
96
     * @return array
97
     */
98
    protected function getArguments()
99
    {
100
        return [
101
            ['module_dir', InputArgument::OPTIONAL, 'The name of the class directory', $this->classPath],
102
        ];
103
    }
104
105
    /**
106
     * @param string $name
107
     *
108
     * @return string
109
     */
110
    protected function getPath($name)
111
    {
112
        $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...
113
114
        return $this->laravel['path'] . '/' . str_replace('\\', '/', $name) . '.php';
115
    }
116
117
    /**
118
     * Parse the name and format according to the root namespace.
119
     *
120
     * @param  string $name
121
     *
122
     * @return string
123
     */
124
    protected function parseClassName($name, $moduleDirectory = null)
125
    {
126
        $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...
127
128
        if (Str::startsWith($name, $rootNamespace)) {
129
            return $name;
130
        }
131
132
        if (Str::contains($name, '/')) {
133
            $name = str_replace('/', '\\', $name);
134
        }
135
136
        return $this->parseClassName(
137
            trim($rootNamespace, '\\') . '\\' . $moduleDirectory . '\\' . $name
138
        );
139
    }
140
141
    /**
142
     * added custom aspect module, override package modules
143
     *
144
     * @param $module
145
     *
146
     * @return $this
147
     */
148
    protected function addModule($module)
149
    {
150
        $this->modules[$module];
151
152
        return $this;
153
    }
154
155
    /**
156
     * Build the directory for the class if necessary.
157
     *
158
     * @param  string $path
159
     *
160
     * @return string
161
     */
162
    protected function makeDirectory($path)
163
    {
164
        if (!$this->filesystem->isDirectory(dirname($path))) {
165
            $this->filesystem->makeDirectory(dirname($path), 0777, true, true);
166
        }
167
    }
168
169
    /**
170
     * @param $module
171
     *
172
     * @return string
173
     */
174
    protected function getExtendsClassName($module)
175
    {
176
        $shortName = (new \ReflectionClass($module))->getShortName();
177
        $extendClassName = "Package{$shortName}";
178
179
        return $extendClassName;
180
    }
181
182
    /**
183
     * @return string
184
     */
185
    protected function stub()
186
    {
187
        /** module stub file path */
188
        return __DIR__ . '/stub/ModuleStub.stub';
189
    }
190
}
191