Completed
Push — master ( df10c6...b81a63 )
by CodexShaper
02:54
created

Application::loadRoutes()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
nc 4
nop 1
dl 0
loc 21
ccs 0
cts 10
cp 0
crap 12
rs 9.584
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;
44
        
45
        $this->app = $container;
46
47
        if (is_null($this->app)) {
48
            $this->app = new Container();
49
            Facade::setFacadeApplication($this->app);
50
            $this->app->instance(ContainerInterface::class, $this->app);
51
        }
52
53
        $this->app['app'] = $this->app;
54
55
        $this->root = __DIR__ . '/../../../../';
56
57
        if (! empty($this->options) && isset($this->options['paths']['root'])) {
58
            $this->root = rtrim($this->options['paths']['root'], "/") . '/';
59
        }
60
61
        if (!isset($this->app['root'])) {
62
            $this->app['root'] = $this->root;
63
        }
64
65
        $this->config = new Config($this->options);
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([
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) {
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)
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
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected EOF, expecting T_FUNCTION or T_CONST
Loading history...