Issues (72)

src/AutomaticServiceProvider.php (9 issues)

1
<?php
2
3
namespace MedianetDev\BackpackForm;
4
5
/**
6
 * This trait automatically loads package stuff, if they're present
7
 * in the expected directory. Stick to the conventions and
8
 * your package will "just work". Feel free to override
9
 * any of the methods below in your ServiceProvider
10
 * if you need to change the paths.
11
 */
12
trait AutomaticServiceProvider
13
{
14
    public function __construct($app)
15
    {
16
        $this->app = $app;
0 ignored issues
show
Bug Best Practice introduced by
The property app does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
17
        $this->path = __DIR__.'/..';
0 ignored issues
show
Bug Best Practice introduced by
The property path does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
18
    }
19
20
    /**
21
     * -------------------------
22
     * SERVICE PROVIDER DEFAULTS
23
     * -------------------------
24
     */
25
26
    /**
27
     * Perform post-registration booting of services.
28
     *
29
     * @return void
30
     */
31
    public function boot(): void
32
    {
33
        if ($this->packageDirectoryExistsAndIsNotEmpty('bootstrap') &&
34
            file_exists($helpers = $this->packageHelpersFile())) {
35
            require $helpers;
36
        }
37
38
        if ($this->packageDirectoryExistsAndIsNotEmpty('resources/lang')) {
39
            $this->loadTranslationsFrom($this->packageLangsPath(), $this->vendorNameDotPackageName());
0 ignored issues
show
It seems like loadTranslationsFrom() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

39
            $this->/** @scrutinizer ignore-call */ 
40
                   loadTranslationsFrom($this->packageLangsPath(), $this->vendorNameDotPackageName());
Loading history...
40
        }
41
42
        if ($this->packageDirectoryExistsAndIsNotEmpty('resources/views')) {
43
            // Load published views
44
            $this->loadViewsFrom($this->publishedViewsPath(), $this->vendorNameDotPackageName());
0 ignored issues
show
It seems like loadViewsFrom() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

44
            $this->/** @scrutinizer ignore-call */ 
45
                   loadViewsFrom($this->publishedViewsPath(), $this->vendorNameDotPackageName());
Loading history...
45
46
            // Fallback to package views
47
            $this->loadViewsFrom($this->packageViewsPath(), $this->vendorNameDotPackageName());
48
        }
49
50
        if ($this->packageDirectoryExistsAndIsNotEmpty('database/migrations')) {
51
            $this->loadMigrationsFrom($this->packageMigrationsPath());
0 ignored issues
show
It seems like loadMigrationsFrom() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

51
            $this->/** @scrutinizer ignore-call */ 
52
                   loadMigrationsFrom($this->packageMigrationsPath());
Loading history...
52
        }
53
54
        if ($this->packageDirectoryExistsAndIsNotEmpty('routes')) {
55
            $this->loadRoutesFrom($this->packageRoutesFile());
0 ignored issues
show
It seems like loadRoutesFrom() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

55
            $this->/** @scrutinizer ignore-call */ 
56
                   loadRoutesFrom($this->packageRoutesFile());
Loading history...
56
        }
57
58
        // Publishing is only necessary when using the CLI.
59
        if ($this->app->runningInConsole()) {
60
            $this->bootForConsole();
61
        }
62
    }
63
64
    /**
65
     * Register any package services.
66
     *
67
     * @return void
68
     */
69
    public function register(): void
70
    {
71
        if ($this->packageDirectoryExistsAndIsNotEmpty('config')) {
72
            $this->mergeConfigFrom($this->packageConfigFile(), $this->vendorNameDotPackageName());
0 ignored issues
show
It seems like mergeConfigFrom() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

72
            $this->/** @scrutinizer ignore-call */ 
73
                   mergeConfigFrom($this->packageConfigFile(), $this->vendorNameDotPackageName());
Loading history...
73
        }
74
    }
75
76
    /**
77
     * Console-specific booting.
78
     *
79
     * @return void
80
     */
81
    protected function bootForConsole(): void
82
    {
83
        // Publishing the configuration file.
84
        if ($this->packageDirectoryExistsAndIsNotEmpty('config')) {
85
            $this->publishes([
0 ignored issues
show
The method publishes() does not exist on MedianetDev\BackpackForm\AutomaticServiceProvider. Did you maybe mean publishedLangsPath()? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

85
            $this->/** @scrutinizer ignore-call */ 
86
                   publishes([

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...
86
                $this->packageConfigFile() => $this->publishedConfigFile(),
87
            ], 'config');
88
        }
89
90
        // Publishing the views.
91
        if ($this->packageDirectoryExistsAndIsNotEmpty('resources/views')) {
92
            $this->publishes([
93
                $this->packageViewsPath() => $this->publishedViewsPath(),
94
            ], 'views');
95
        }
96
97
        // Publishing assets.
98
        if ($this->packageDirectoryExistsAndIsNotEmpty('resources/assets')) {
99
            $this->publishes([
100
                $this->packageAssetsPath() => $this->publishedAssetsPath(),
101
            ], 'assets');
102
        }
103
104
        // Publishing the translation files.
105
        if ($this->packageDirectoryExistsAndIsNotEmpty('resources/lang')) {
106
            $this->publishes([
107
                $this->packageLangsPath() => $this->publishedLangsPath(),
108
            ], 'lang');
109
        }
110
111
        // Registering package commands.
112
        if (!empty($this->commands)) {
113
            $this->commands($this->commands);
0 ignored issues
show
It seems like commands() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

113
            $this->/** @scrutinizer ignore-call */ 
114
                   commands($this->commands);
Loading history...
114
        }
115
    }
116
117
    /**
118
     * -------------------
119
     * CONVENIENCE METHODS
120
     * -------------------
121
     */
122
123
    protected function vendorNameDotPackageName()
124
    {
125
        return $this->vendorName.'.'.$this->packageName;
126
    }
127
128
    protected function vendorNameSlashPackageName()
129
    {
130
        return $this->vendorName.'/'.$this->packageName;
131
    }
132
133
    // -------------
134
    // Package paths
135
    // -------------
136
137
    protected function packageViewsPath() {
138
        return $this->path.'/resources/views';
139
    }
140
141
    protected function packageLangsPath() {
142
        return $this->path.'/resources/lang';
143
    }
144
145
    protected function packageAssetsPath() {
146
        return $this->path.'/resources/assets';
147
    }
148
149
    protected function packageMigrationsPath() {
150
        return $this->path.'/database/migrations';
151
    }
152
153
    protected function packageConfigFile() {
154
        return $this->path.'/config/'.$this->packageName.'.php';
155
    }
156
157
    protected function packageRoutesFile(): string
158
    {
159
        $packageRouteFile = $this->path . '/routes/' . $this->packageName . '.php';
160
161
        $projectRouteFile = base_path('routes/' . $this->packageName . '.php');
162
163
        $routeFilePathInUse = file_exists($projectRouteFile)
164
            ? $projectRouteFile
165
            : $packageRouteFile;
166
167
        if (!file_exists($routeFilePathInUse)) {
168
            throw new \RuntimeException(
169
                sprintf(
170
                    'No route file found for package "%s". Checked locations: %s and %s',
171
                    $this->packageName,
172
                    $packageRouteFile,
173
                    $projectRouteFile
174
                )
175
            );
176
        }
177
178
        return $routeFilePathInUse;
179
    }
180
181
    protected function packageHelpersFile() {
182
        return $this->path.'/bootstrap/helpers.php';
183
    }
184
185
    // ---------------
186
    // Published paths
187
    // ---------------
188
189
    protected function publishedViewsPath() {
190
        return base_path('resources/views/vendor/'.$this->vendorName.'/'.$this->packageName);
191
    }
192
193
    protected function publishedConfigFile() {
194
        return config_path($this->packageName.'.php');
195
    }
196
197
    protected function publishedAssetsPath() {
198
        return public_path('vendor/'.$this->vendorNameSlashPackageName());
199
    }
200
201
    protected function publishedLangsPath() {
202
        return resource_path('lang/vendor/'.$this->vendorName);
203
    }
204
205
    // -------------
206
    // Miscellaneous
207
    // -------------
208
209
    protected function packageDirectoryExistsAndIsNotEmpty($name)
210
    {
211
        // check if directory exists
212
        if (!is_dir($this->path.'/'.$name)) {
213
            return false;
214
        }
215
216
        // check if directory has files
217
        foreach (scandir($this->path.'/'.$name) as $file) {
218
            if ($file != '.' && $file != '..' && $file != '.gitkeep') {
219
                return true;
220
            }
221
        }
222
223
        return false;
224
    }
225
}
226