ConsoleTools::publishesResources()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
nc 3
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Rinvex\Support\Traits;
6
7
use Illuminate\Support\Str;
8
9
trait ConsoleTools
10
{
11
    /**
12
     * Publish package migrations.
13
     *
14
     * @return void
15
     */
16
    protected function publishesMigrations(string $package, bool $isModule = false): void
17
    {
18
        if (! $this->publishesResources()) {
19
            return;
20
        }
21
22
        $namespace = str_replace('laravel-', '', $package);
23
        $basePath = $isModule ? $this->app->path($package)
0 ignored issues
show
Bug introduced by
The property app does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
24
            : $this->app->basePath('vendor/'.$package);
25
26
        if (file_exists($path = $basePath.'/database/migrations')) {
27
            $stubs = $this->app['files']->glob($path.'/*.php');
28
            $existing = $this->app['files']->glob($this->app->databasePath('migrations/'.$package.'/*.php'));
29
30
            $migrations = collect($stubs)->flatMap(function ($migration) use ($existing, $package) {
31
                $sequence = mb_substr(basename($migration), 0, 17);
32
                $match = collect($existing)->first(function ($item, $key) use ($migration, $sequence) {
0 ignored issues
show
Unused Code introduced by
The parameter $key is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
33
                    return mb_strpos($item, str_replace($sequence, '', basename($migration))) !== false;
34
                });
35
36
                return [$migration => $this->app->databasePath('migrations/'.$package.'/'.($match ? basename($match) : date('Y_m_d_His', time() + mb_substr($sequence, -6)).str_replace($sequence, '', basename($migration))))];
37
            })->toArray();
38
39
            $this->publishes($migrations, $namespace.'::migrations');
0 ignored issues
show
Bug introduced by
The method publishes() does not exist on Rinvex\Support\Traits\ConsoleTools. Did you maybe mean publishesMigrations()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
40
        }
41
    }
42
43
    /**
44
     * Publish package config.
45
     *
46
     * @return void
47
     */
48
    protected function publishesConfig(string $package, bool $isModule = false): void
49
    {
50
        if (! $this->publishesResources()) {
51
            return;
52
        }
53
54
        $namespace = str_replace('laravel-', '', $package);
55
        $basePath = $isModule ? $this->app->path($package)
56
            : $this->app->basePath('vendor/'.$package);
57
58
        if (file_exists($path = $basePath.'/config/config.php')) {
59
            $this->publishes([$path => $this->app->configPath(str_replace('/', '.', $namespace).'.php')], $namespace.'::config');
0 ignored issues
show
Bug introduced by
The method publishes() does not exist on Rinvex\Support\Traits\ConsoleTools. Did you maybe mean publishesMigrations()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
60
        }
61
    }
62
63
    /**
64
     * Publish package views.
65
     *
66
     * @return void
67
     */
68
    protected function publishesViews(string $package, bool $isModule = false): void
69
    {
70
        if (! $this->publishesResources()) {
71
            return;
72
        }
73
74
        $namespace = str_replace('laravel-', '', $package);
75
        $basePath = $isModule ? $this->app->path($package)
76
            : $this->app->basePath('vendor/'.$package);
77
78
        if (file_exists($path = $basePath.'/resources/views')) {
79
            $this->publishes([$path => $this->app->resourcePath('views/vendor/'.$package)], $namespace.'::views');
0 ignored issues
show
Bug introduced by
The method publishes() does not exist on Rinvex\Support\Traits\ConsoleTools. Did you maybe mean publishesMigrations()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
80
        }
81
    }
82
83
    /**
84
     * Publish package lang.
85
     *
86
     * @return void
87
     */
88
    protected function publishesLang(string $package, bool $isModule = false): void
89
    {
90
        if (! $this->publishesResources()) {
91
            return;
92
        }
93
94
        $namespace = str_replace('laravel-', '', $package);
95
        $basePath = $isModule ? $this->app->path($package)
96
            : $this->app->basePath('vendor/'.$package);
97
98
        if (file_exists($path = $basePath.'/resources/lang')) {
99
            $this->publishes([$path => $this->app->resourcePath('lang/vendor/'.$package)], $namespace.'::lang');
0 ignored issues
show
Bug introduced by
The method publishes() does not exist on Rinvex\Support\Traits\ConsoleTools. Did you maybe mean publishesMigrations()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
100
        }
101
    }
102
103
    /**
104
     * Determine if the application is running in the console.
105
     *
106
     * @TODO: Implement this method to detect if we're in active dev zone or not!
107
     *        Ex: running inside cortex/console action
108
     *
109
     * @return bool
110
     */
111
    public function runningInDevzone()
112
    {
113
        return true;
114
    }
115
116
    /**
117
     * Register console commands.
118
     *
119
     * @param array $commands
120
     *
121
     * @return void
122
     */
123
    protected function registerCommands(array $commands): void
124
    {
125
        if (! $this->app->runningInConsole() && ! $this->runningInDevzone()) {
126
            return;
127
        }
128
129
        foreach ($commands as $key => $value) {
130
            $this->app->singleton($value, $key);
131
        }
132
133
        $this->commands(array_values($commands));
0 ignored issues
show
Bug introduced by
The method commands() does not exist on Rinvex\Support\Traits\ConsoleTools. Did you maybe mean registerCommands()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
134
    }
135
136
    /**
137
     * Register models into IoC.
138
     *
139
     * @param array $models
140
     *
141
     * @return void
142
     */
143
    protected function registerModels(array $models): void
144
    {
145
        foreach ($models as $service => $class) {
146
            $this->app->singleton($service, $model = $this->app['config'][Str::replaceLast('.', '.models.', $service)]);
147
            $model === $class || $this->app->alias($service, $class);
148
        }
149
    }
150
151
    /**
152
     * Can publish resources.
153
     *
154
     * @return bool
155
     */
156
    protected function publishesResources(): bool
157
    {
158
        return ! $this->app->environment('production') || $this->app->runningInConsole() || $this->runningInDevzone();
159
    }
160
161
    /**
162
     * Can autoload migrations.
163
     *
164
     * @param string $module
165
     *
166
     * @return bool
167
     */
168
    protected function autoloadMigrations(string $module): bool
169
    {
170
        return $this->publishesResources() && $this->app['config'][str_replace(['laravel-', '/'], ['', '.'], $module).'.autoload_migrations'];
171
    }
172
}
173