Issues (156)

src/Application.php (6 issues)

1
<?php
2
/**
3
 * The bootstrap file.
4
 *
5
 * @link       https://github.com/maab16
6
 * @since      1.0.0
7
 */
8
9
namespace WPB;
10
11
use CodexShaper\Database\Database;
12
use Illuminate\Container\Container;
13
use Illuminate\Contracts\Container\Container as ContainerInterface;
14
use Illuminate\Http\Request;
15
use Illuminate\Support\Facades\Facade;
16
use WPB\App\User;
17
use WPB\Support\Facades\Config;
18
19
/**
20
 * The Application class.
21
 *
22
 * @since      1.0.0
23
 *
24
 * @author     Md Abu Ahsan basir <[email protected]>
25
 */
26
class Application
27
{
28
    /**
29
     * The app container.
30
     *
31
     * @since    1.0.0
32
     *
33
     * @var \Illuminate\Contracts\Container\Container The app container.
34
     */
35
    protected $app = null;
36
37
    /**
38
     * The config.
39
     *
40
     * @since    1.0.0
41
     *
42
     * @var \WPB\Support\Facades\Config The config.
43
     */
44
    protected $config;
45
46
    /**
47
     * The database manager.
48
     *
49
     * @since    1.0.0
50
     *
51
     * @var \CodexShaper\Database\Database The database manager.
52
     */
53
    protected $db;
54
55
    /**
56
     * The default options.
57
     *
58
     * @since    1.0.0
59
     *
60
     * @var array The default options.
61
     */
62
    protected $options;
63
64
    /**
65
     * This string unique root path.
66
     *
67
     * @since    1.0.0
68
     *
69
     * @var string This string unique root path.
70
     */
71
    protected $root;
72
73
    /**
74
     * The application factory.
75
     *
76
     * @since    1.0.0
77
     *
78
     * @param array                                                           $options   The site options.
79
     * @param \Illuminate\Contracts\Container\Container as ContainerInterface $container The app container.
0 ignored issues
show
The type WPB\as was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
80
     *
81
     * @return void
82
     */
83
    public function __construct($options = [], ContainerInterface $container = null)
84
    {
85
        $this->options = $options;
86
87
        $this->app = $container;
88
89
        if (is_null($this->app)) {
90
            $this->app = new Container();
91
            Facade::setFacadeApplication($this->app);
0 ignored issues
show
$this->app of type Illuminate\Container\Container is incompatible with the type Illuminate\Contracts\Foundation\Application expected by parameter $app of Illuminate\Support\Facad...:setFacadeApplication(). ( Ignorable by Annotation )

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

91
            Facade::setFacadeApplication(/** @scrutinizer ignore-type */ $this->app);
Loading history...
92
            $this->app->instance(ContainerInterface::class, $this->app);
93
        }
94
95
        $this->app['app'] = $this->app;
96
97
        $this->root = __DIR__.'/../../../../';
98
99
        if (!empty($this->options) && isset($this->options['paths']['root'])) {
100
            $this->root = rtrim($this->options['paths']['root'], '/').'/';
101
        }
102
103
        if (!isset($this->app['root'])) {
104
            $this->app['root'] = $this->root;
105
        }
106
107
        $this->config = new Config($this->options);
108
109
        $this->setup_env();
110
        $this->register_config();
111
        $this->setup_database();
112
        $this->register_providers();
113
        $this->register_request();
114
        $this->register_instances();
115
        $this->load_routes($this->app['router']);
116
    }
117
118
    /**
119
     * Get the app container.
120
     *
121
     * @since    1.0.0
122
     *
123
     * @return \Illuminate\Container\Container
124
     */
125
    public function get_instance()
126
    {
127
        if (!$this->app) {
128
            new self();
129
        }
130
131
        return $this->app;
132
    }
133
134
    /**
135
     * Setup the env.
136
     *
137
     * @since    1.0.0
138
     *
139
     * @return void
140
     */
141
    protected function setup_env()
142
    {
143
        $this->app['env'] = $this->config->get('app.env');
144
    }
145
146
    /**
147
     * Register config.
148
     *
149
     * @since    1.0.0
150
     *
151
     * @return void
152
     */
153
    protected function register_config()
154
    {
155
        $this->app->bind(
156
            'config',
157
            function () {
158
                return [
159
                    'app'           => $this->config->get('app'),
160
                    'view.paths'    => $this->config->get('view.paths'),
161
                    'view.compiled' => $this->config->get('view.compiled'),
162
                ];
163
            },
164
            true
165
        );
166
    }
167
168
    /**
169
     * Setup the database.
170
     *
171
     * @since    1.0.0
172
     *
173
     * @return void
174
     */
175
    protected function setup_database()
176
    {
177
        global $wpdb;
178
179
        $this->db = new Database(
180
            [
181
                'driver'    => 'mysql',
182
                'host'      => $wpdb->dbhost,
183
                'database'  => $wpdb->dbname,
184
                'username'  => $wpdb->dbuser,
185
                'password'  => $wpdb->dbpassword,
186
                'prefix'    => $wpdb->prefix,
187
                'charset'   => $wpdb->charset,
188
                'collation' => $wpdb->collate,
189
            ]
190
        );
191
192
        $this->db->run();
193
194
        $this->app->singleton(
195
            'db',
196
            function () {
197
                return $this->db;
198
            }
199
        );
200
    }
201
202
    /**
203
     * Register providers.
204
     *
205
     * @since    1.0.0
206
     *
207
     * @return void
208
     */
209
    protected function register_providers()
210
    {
211
        $providers = $this->config->get('app.providers');
212
213
        if ($providers && count($providers) > 0) {
0 ignored issues
show
$providers of type string is incompatible with the type Countable|array expected by parameter $var of count(). ( Ignorable by Annotation )

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

213
        if ($providers && count(/** @scrutinizer ignore-type */ $providers) > 0) {
Loading history...
214
            foreach ($providers as $provider) {
0 ignored issues
show
The expression $providers of type string is not traversable.
Loading history...
215
                with(new $provider($this->app))->register();
216
            }
217
        }
218
    }
219
220
    /**
221
     * Register request.
222
     *
223
     * @since    1.0.0
224
     *
225
     * @return void
226
     */
227
    protected function register_request()
228
    {
229
        $this->app->bind(
230
            'request',
231
            function ($app) {
0 ignored issues
show
The parameter $app is not used and could be removed. ( Ignorable by Annotation )

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

231
            function (/** @scrutinizer ignore-unused */ $app) {

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

Loading history...
232
                $request = Request::capture();
233
                $wp_user = wp_get_current_user();
0 ignored issues
show
The function wp_get_current_user was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

233
                $wp_user = /** @scrutinizer ignore-call */ wp_get_current_user();
Loading history...
234
                if ($wp_user) {
235
                    $user = User::find($wp_user->ID);
236
                    $request->merge(['user' => $user]);
237
                    $request->setUserResolver(
238
                        function () use ($user) {
239
                            return $user;
240
                        }
241
                    );
242
                }
243
244
                return $request;
245
            }
246
        );
247
    }
248
249
    /**
250
     * Register router.
251
     *
252
     * @since    1.0.0
253
     *
254
     * @return void
255
     */
256
    protected function register_instances()
257
    {
258
        $this->app->instance(\Illuminate\Http\Request::class, $this->app['request']);
259
        $this->app->instance(\Illuminate\Routing\Router::class, $this->app['router']);
260
        $this->app->instance(\WPB\Router::class, $this->app['router']);
261
        $this->app->instance(\Illuminate\Contracts\View\Factory::class, $this->app['view']);
262
        $this->app->instance(\Illuminate\Contracts\Routing\UrlGenerator::class, $this->app['url']);
263
        $this->app->alias('Route', \WPB\Support\Facades\Route::class);
264
    }
265
266
    /**
267
     * Get the config value.
268
     *
269
     * @since    1.0.0
270
     *
271
     * @param \Illuminate\Routing\Router $router The app router.
272
     * @param string                     $dir    The custom routes directory.
273
     *
274
     * @return void
275
     */
276
    public function load_routes($router, $dir = null)
277
    {
278
        if (!$dir) {
279
            $dir = __DIR__.'/../routes/';
280
        }
281
282
        require $dir.'web.php';
283
284
        $router->group(
285
            ['prefix' => 'api'],
286
            function () use ($dir, $router) {
287
                require $dir.'api.php';
288
            }
289
        );
290
    }
291
}
292