Completed
Push — master ( 36a006...f90c70 )
by CodexShaper
04:12
created

Application::setup_database()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 22
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 16
nc 1
nop 0
dl 0
loc 22
rs 9.7333
c 0
b 0
f 0
1
<?php
2
/**
3
 * The bootstrap file.
4
 *
5
 * @link       https://github.com/maab16
6
 * @since      1.0.0
7
 *
8
 * @package    WPB
9
 * @subpackage WPB/src
10
 */
11
12
namespace WPB;
13
14
use WPB\App\User;
15
use CodexShaper\Database\Database;
16
use CodexShaper\Database\Facades\DB;
17
use WPB\Support\Facades\Config;
18
use Illuminate\Container\Container;
19
use Illuminate\Contracts\Container\Container as ContainerInterface;
20
use Illuminate\Http\Request;
21
use Illuminate\Support\Facades\Facade;
22
23
/**
24
 * The Application class.
25
 *
26
 * @since      1.0.0
27
 * @package    WPB
28
 * @subpackage WPB/src
29
 * @author     Md Abu Ahsan basir <[email protected]>
30
 */
31
class Application {
32
33
	/**
34
	 * The app container.
35
	 *
36
	 * @since    1.0.0
37
	 * @access   protected
38
	 * @var      \Illuminate\Contracts\Container\Container    $app    The app container.
39
	 */
40
	protected $app = null;
41
42
	/**
43
	 * The config.
44
	 *
45
	 * @since    1.0.0
46
	 * @access   protected
47
	 * @var      \WPB\Support\Facades\Config    $config    The config.
48
	 */
49
	protected $config;
50
51
	/**
52
	 * The database manager.
53
	 *
54
	 * @since    1.0.0
55
	 * @access   protected
56
	 * @var      \CodexShaper\Database\Database    $db    The database manager.
57
	 */
58
	protected $db;
59
60
	/**
61
	 * The default options.
62
	 *
63
	 * @since    1.0.0
64
	 * @access   protected
65
	 * @var      array    $options    The default options.
66
	 */
67
	protected $options;
68
69
	/**
70
	 * This string unique root path.
71
	 *
72
	 * @since    1.0.0
73
	 * @access   protected
74
	 * @var      string    $root    This string unique root path.
75
	 */
76
	protected $root;
77
78
	/**
79
	 * The application factory.
80
	 *
81
	 * @since    1.0.0
82
	 * @param array                                                           $options The site options.
83
	 * @param \Illuminate\Contracts\Container\Container as ContainerInterface $container The app container.
0 ignored issues
show
Bug introduced by
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...
84
	 *
85
	 * @return void
86
	 */
87
	public function __construct( $options = array(), ContainerInterface $container = null ) {
88
		$this->options = $options;
89
90
		$this->app = $container;
91
92
		if ( is_null( $this->app ) ) {
93
			$this->app = new Container();
94
			Facade::setFacadeApplication( $this->app );
0 ignored issues
show
Bug introduced by
$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

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

211
		if ( $providers && count( /** @scrutinizer ignore-type */ $providers ) > 0 ) {
Loading history...
212
			foreach ( $providers as $provider ) {
0 ignored issues
show
Bug introduced by
The expression $providers of type string is not traversable.
Loading history...
213
				with( new $provider( $this->app ) )->register();
214
			}
215
		}
216
	}
217
218
	/**
219
	 * Register request.
220
	 *
221
	 * @since    1.0.0
222
	 *
223
	 * @return void
224
	 */
225
	protected function register_request() {
226
		$this->app->bind(
227
			Request::class,
228
			function ( $app ) {
0 ignored issues
show
Unused Code introduced by
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

228
			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...
229
				$request = Request::capture();
230
				$wp_user = wp_get_current_user();
0 ignored issues
show
Bug introduced by
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

230
				$wp_user = /** @scrutinizer ignore-call */ wp_get_current_user();
Loading history...
231
				if ( $wp_user ) {
232
					$user = User::find( $wp_user->ID );
233
					$request->merge( array( 'user' => $user ) );
234
					$request->setUserResolver(
235
						function () use ( $user ) {
236
							return $user;
237
						}
238
					);
239
				}
240
241
				return $request;
242
			}
243
		);
244
	}
245
246
	/**
247
	 * Register router.
248
	 *
249
	 * @since    1.0.0
250
	 *
251
	 * @return void
252
	 */
253
	protected function register_router() {
254
		$this->app->instance( \Illuminate\Routing\Router::class, $this->app['router'] );
255
		$this->app->instance( \WPB\Router::class, $this->app['router'] );
0 ignored issues
show
Bug introduced by
The type WPB\Router 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...
256
		$this->app->alias( 'Route', \WPB\Support\Facades\Route::class );
257
	}
258
259
	/**
260
	 * Get the config value.
261
	 *
262
	 * @since    1.0.0
263
	 * @param \Illuminate\Routing\Router $router The app router.
264
	 * @param string                     $dir The custom routes directory.
265
	 *
266
	 * @return void
267
	 */
268
	public function load_routes( $router, $dir = null ) {
269
		if ( ! $dir ) {
270
			$dir = __DIR__ . '/../routes/';
271
		}
272
273
		require $dir . 'web.php';
274
275
		$router->group(
276
			array( 'prefix' => 'api' ),
277
			function () use ( $dir, $router ) {
278
				require $dir . 'api.php';
279
			}
280
		);
281
	}
282
}
283