Completed
Push — master ( b81a63...5c5ce9 )
by CodexShaper
03:29
created

Application::getInstance()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 0
dl 0
loc 8
ccs 0
cts 4
cp 0
crap 6
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace CodexShaper\WP;
4
5
use CodexShaper\App\User;
6
use CodexShaper\Database\Database;
7
use CodexShaper\Database\Facades\DB;
8
use CodexShaper\WP\Support\Facades\Config;
9
use Illuminate\Container\Container;
10
use Illuminate\Contracts\Container\Container as ContainerInterface;
11
use Illuminate\Http\Request;
12
use Illuminate\Support\Facades\Facade;
13
14
class Application
15
{
16
    /**
17
     * @var app
18
     */
19
    protected $app = null;
20
21
    /**
22
     * @var config
23
     */
24
    protected $config;
25
26
    /**
27
     * @var db
28
     */
29
    protected $db;
30
31
    /**
32
     * @var options
33
     */
34
    protected $options;
35
36
    /**
37
     * @var root
38
     */
39
    protected $root;
40
41
    public function __construct($options = [], ContainerInterface $container = null)
42
    {
43
        $this->options = $options;
0 ignored issues
show
Documentation Bug introduced by
It seems like $options of type array is incompatible with the declared type object<CodexShaper\WP\options> of property $options.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
44
        
45
        $this->app = $container;
0 ignored issues
show
Documentation Bug introduced by
It seems like $container can also be of type object<Illuminate\Contracts\Container\Container>. However, the property $app is declared as type object<CodexShaper\WP\app>. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
46
47
        if (is_null($this->app)) {
48
            $this->app = new Container();
0 ignored issues
show
Documentation Bug introduced by
It seems like new \Illuminate\Container\Container() of type object<Illuminate\Container\Container> is incompatible with the declared type object<CodexShaper\WP\app> of property $app.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
49
            Facade::setFacadeApplication($this->app);
0 ignored issues
show
Documentation introduced by
$this->app is of type object<Illuminate\Container\Container>, but the function expects a object<Illuminate\Contra...Foundation\Application>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
50
            $this->app->instance(ContainerInterface::class, $this->app);
51
        }
52
53
        $this->app['app'] = $this->app;
54
55
        $this->root = __DIR__ . '/../../../../';
0 ignored issues
show
Documentation Bug introduced by
It seems like __DIR__ . '/../../../../' of type string is incompatible with the declared type object<CodexShaper\WP\root> of property $root.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
56
57
        if (! empty($this->options) && isset($this->options['paths']['root'])) {
58
            $this->root = rtrim($this->options['paths']['root'], "/") . '/';
0 ignored issues
show
Documentation Bug introduced by
It seems like rtrim($this->options['paths']['root'], '/') . '/' of type string is incompatible with the declared type object<CodexShaper\WP\root> of property $root.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
59
        }
60
61
        if (!isset($this->app['root'])) {
62
            $this->app['root'] = $this->root;
63
        }
64
65
        $this->config = new Config($this->options);
0 ignored issues
show
Documentation Bug introduced by
It seems like new \CodexShaper\WP\Supp...\Config($this->options) of type object<CodexShaper\WP\Support\Facades\Config> is incompatible with the declared type object<CodexShaper\WP\config> of property $config.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
66
67
        $this->setupEnv();
68
        $this->registerConfig();
69
        $this->setupDatabase();
70
        $this->registerProviders();
71
        $this->registerRequest();
72
        $this->registerRouter();
73
        $this->loadRoutes();
74
    }
75
76
    public function getInstance()
77
    {
78
        if (!$this->app) {
79
            return new self();
80
        }
81
82
        return $this->app;
83
    }
84
85
    protected function setupEnv()
86
    {
87
        $this->app['env'] = $this->config->get('app.env');
88
    }
89
90
    protected function registerConfig()
91
    {
92
        $this->app->bind('config', function () {
93
            return [
94
                'app'           => $this->config->get('app'),
95
                'view.paths'    => $this->config->get('view.paths'),
96
                'view.compiled' => $this->config->get('view.compiled'),
97
            ];
98
        }, true);
99
    }
100
101
    protected function setupDatabase()
102
    {
103
        global $wpdb;
104
105
        $this->db = new Database([
0 ignored issues
show
Documentation Bug introduced by
It seems like new \CodexShaper\Databas...on' => $wpdb->collate)) of type object<CodexShaper\Database\Database> is incompatible with the declared type object<CodexShaper\WP\db> of property $db.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
106
            'driver'            => 'mysql',
107
            'host'               => $wpdb->dbhost,
108
            'database'        => $wpdb->dbname,
109
            'username'        => $wpdb->dbuser,
110
            'password'        => $wpdb->dbpassword,
111
            'prefix'          => $wpdb->prefix,
112
            'charset'            => $wpdb->charset,
113
            'collation'     => $wpdb->collate,
114
        ]);
115
116
        $this->db->run();
117
118
        $this->app->singleton('db', function () {
119
            return $this->db;
120
        });
121
    }
122
123
    protected function registerProviders()
124
    {
125
        $providers = $this->config->get('app.providers');
126
127
        if( $providers && count($providers) > 0) {
128
            foreach ($providers as $provider) {
129
                with(new $provider($this->app))->register();
130
            }
131
        }
132
    }
133
134
    protected function registerRequest()
135
    {
136
        $this->app->bind(Request::class, function ($app) {
0 ignored issues
show
Unused Code introduced by
The parameter $app 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...
137
            $request = Request::capture();
138
139
            if ($wp_user = wp_get_current_user()) {
140
                $user = User::find($wp_user->ID);
141
                $request->merge(['user' => $user]);
142
                $request->setUserResolver(function () use ($user) {
143
                    return $user;
144
                });
145
            }
146
147
            return $request;
148
        });
149
    }
150
151
    protected function registerRouter()
152
    {
153
        if(isset($this->app['router'])) {
154
            $this->app->instance(\Illuminate\Routing\Router::class, $this->app['router']);
155
        }  
156
        $this->app->alias('Route', \CodexShaper\WP\Support\Facades\Route::class);
157
    }
158
159
    protected function loadRoutes($dir = null)
0 ignored issues
show
Unused Code introduced by
The parameter $dir 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...
160
    {
161
        foreach ( get_option('active_plugins') as $activate_plugin) {
162
            $dir = $this->root.'../'.dirname($activate_plugin);
163
           if(is_dir($dir.'/routes')) {
164
                foreach (glob($dir.'/routes/*.php') as $route) {
165
                    require_once $route;
166
                }
167
            }
168
        }
169
    //     if (!$dir) {
170
    //         $dir = $this->root . 'routes/';
171
    //     }
172
173
    //     if(isset($this->app['router'])) {
174
            
175
    //         // $app['router']->group(['middleware' => ['web']], function(){
176
    //         require $dir.'web.php';
177
    //         // });
178
179
    //         $this->app['router']->group(['prefix' => 'api'], function () use ($dir) {
180
    //             require $dir.'api.php';
181
    //         });
182
183
    //         $this->app['router']->group(['prefix' => 'wp-admin'], function () use ($dir) {
184
    //             require $dir.'admin.php';
185
    //         });
186
    //     }
187
    }
188
}
189