Completed
Push — master ( 27f4df...9b17c0 )
by Biao
04:49
created

Laravel::getRawGlobals()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 0
1
<?php
2
3
namespace Hhxsv5\LaravelS\Illuminate;
4
5
use Illuminate\Container\Container;
6
use Illuminate\Contracts\Console\Kernel as ConsoleKernel;
7
use Illuminate\Contracts\Http\Kernel as HttpKernel;
8
use Illuminate\Http\Request as IlluminateRequest;
9
use Symfony\Component\HttpFoundation\BinaryFileResponse;
10
use Symfony\Component\HttpFoundation\Response as SymfonyResponse;
11
12
class Laravel
13
{
14
    /**@var Container */
15
    protected $app;
16
17
    /**@var Container */
18
    protected $snapshotApp;
19
20
    /**@var ReflectionApp */
21
    protected $reflectionApp;
22
23
    /**@var HttpKernel */
24
    protected $kernel;
25
26
    /**@var array */
27
    protected $conf = [];
28
29
    /**@var array */
30
    protected static $staticBlackList = [
31
        '/index.php'  => 1,
32
        '/.htaccess'  => 1,
33
        '/web.config' => 1,
34
    ];
35
36
    /**@var array */
37
    private $rawGlobals = [];
38
39
    /**@var CleanerManager */
40
    protected $cleanerManager;
41
42
    public function __construct(array $conf = [])
43
    {
44
        $this->conf = $conf;
45
46
        // Merge $_ENV $_SERVER
47
        $server = isset($this->conf['_SERVER']) ? $this->conf['_SERVER'] : [];
48
        $env = isset($this->conf['_ENV']) ? $this->conf['_ENV'] : [];
49
        $this->rawGlobals['_SERVER'] = array_merge($_SERVER, $server);
50
        $this->rawGlobals['_ENV'] = array_merge($_ENV, $env);
51
    }
52
53
    public function prepareLaravel()
54
    {
55
        list($this->app, $this->kernel) = $this->createAppKernel();
56
57
        $this->reflectionApp = new ReflectionApp($this->app);
58
59
        $this->saveSnapshot();
60
61
        // Create cleaner manager
62
        $this->cleanerManager = new CleanerManager($this->app, $this->conf);
63
    }
64
65
    protected function saveSnapshot()
66
    {
67
        $this->snapshotApp = clone $this->app;
68
69
        $instances = $this->reflectionApp->instances();
70
71
        foreach ($instances as $key => $value) {
72
            $this->snapshotApp->offsetSet($key, is_object($value) ? clone $value : $value);
73
        }
74
    }
75
76
    protected function createAppKernel()
77
    {
78
        // Register autoload
79
        self::autoload($this->conf['root_path']);
80
81
        // Make kernel for Laravel
82
        $app = require $this->conf['root_path'] . '/bootstrap/app.php';
83
        $kernel = $this->isLumen() ? null : $app->make(HttpKernel::class);
84
85
        // Load all Configurations for Lumen
86
        if ($this->isLumen()) {
87
            $this->configureLumen($app);
88
        }
89
90
        // Boot
91
        if ($this->isLumen()) {
92
            if (method_exists($app, 'boot')) {
93
                $app->boot();
94
            }
95
        } else {
96
            $app->make(ConsoleKernel::class)->bootstrap();
97
        }
98
99
        return [$app, $kernel];
100
    }
101
102
    protected function configureLumen(Container $app)
103
    {
104
        $cfgPaths = [
105
            // Framework default configuration
106
            $this->conf['root_path'] . '/vendor/laravel/lumen-framework/config/',
107
            // App configuration
108
            $this->conf['root_path'] . '/config/',
109
        ];
110
111
        $keys = [];
112
        foreach ($cfgPaths as $cfgPath) {
113
            $configs = (array)glob($cfgPath . '*.php');
114
            foreach ($configs as $config) {
115
                $config = substr(basename($config), 0, -4);
116
                $keys[$config] = $config;
117
            }
118
        }
119
120
        foreach ($keys as $key) {
121
            $app->configure($key);
0 ignored issues
show
Bug introduced by
The method configure() does not exist on Illuminate\Container\Container. ( Ignorable by Annotation )

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

121
            $app->/** @scrutinizer ignore-call */ 
122
                  configure($key);

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...
122
        }
123
    }
124
125
    public static function autoload($rootPath)
126
    {
127
        $autoload = $rootPath . '/bootstrap/autoload.php';
128
        if (file_exists($autoload)) {
129
            require_once $autoload;
130
        } else {
131
            require_once $rootPath . '/vendor/autoload.php';
132
        }
133
    }
134
135
    public function getRawGlobals()
136
    {
137
        return $this->rawGlobals;
138
    }
139
140
    public function handleDynamic(IlluminateRequest $request)
141
    {
142
        ob_start();
143
144
        if ($this->isLumen()) {
145
            $response = $this->app->dispatch($request);
0 ignored issues
show
Bug introduced by
The method dispatch() does not exist on Illuminate\Container\Container. ( Ignorable by Annotation )

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

145
            /** @scrutinizer ignore-call */ 
146
            $response = $this->app->dispatch($request);

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...
146
            if ($response instanceof SymfonyResponse) {
147
                $content = $response->getContent();
148
            } else {
149
                $content = (string)$response;
150
            }
151
152
            $this->reflectionApp->callTerminableMiddleware($response);
153
        } else {
154
            $response = $this->kernel->handle($request);
155
            $content = $response->getContent();
156
            $this->kernel->terminate($request, $response);
157
        }
158
159
        // prefer content in response, secondly ob
160
        if (strlen($content) === 0 && ob_get_length() > 0) {
161
            $response->setContent(ob_get_contents());
162
        }
163
164
        ob_end_clean();
165
166
        return $response;
167
    }
168
169
    public function handleStatic(IlluminateRequest $request)
170
    {
171
        $uri = $request->getRequestUri();
172
        if (isset(self::$staticBlackList[$uri])) {
173
            return false;
174
        }
175
176
        $publicPath = $this->conf['static_path'];
177
        $requestFile = $publicPath . $uri;
178
        if (is_file($requestFile)) {
179
            return $this->createStaticResponse($requestFile, $request);
180
        } elseif (is_dir($requestFile)) {
181
            $indexFile = $this->lookupIndex($requestFile);
182
            if ($indexFile === false) {
183
                return false;
184
            } else {
185
                return $this->createStaticResponse($indexFile, $request);
186
            }
187
        } else {
188
            return false;
189
        }
190
    }
191
192
    protected function lookupIndex($folder)
193
    {
194
        $folder = rtrim($folder, '/') . '/';
195
        foreach (['index.html', 'index.htm'] as $index) {
196
            $tmpFile = $folder . $index;
197
            if (is_file($tmpFile)) {
198
                return $tmpFile;
199
            }
200
        }
201
        return false;
202
    }
203
204
    public function createStaticResponse($requestFile, IlluminateRequest $request)
205
    {
206
        $response = new BinaryFileResponse($requestFile);
207
        $response->prepare($request);
208
        $response->isNotModified($request);
209
        return $response;
210
    }
211
212
    public function clean()
213
    {
214
        $this->cleanerManager->clean($this->snapshotApp, $this->reflectionApp);
215
    }
216
217
    public function fireEvent($name, array $params = [])
218
    {
219
        $params[] = $this->app;
220
        return method_exists($this->app['events'], 'dispatch') ?
221
            $this->app['events']->dispatch($name, $params) : $this->app['events']->fire($name, $params);
0 ignored issues
show
Bug introduced by
The method dispatch() does not exist on Illuminate\Events\Dispatcher. ( Ignorable by Annotation )

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

221
            $this->app['events']->/** @scrutinizer ignore-call */ 
222
                                  dispatch($name, $params) : $this->app['events']->fire($name, $params);

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...
222
    }
223
224
    public function bindRequest(IlluminateRequest $request)
225
    {
226
        $this->app->instance('request', $request);
227
    }
228
229
    public function bindSwoole($swoole)
230
    {
231
        $this->app->singleton('swoole', function () use ($swoole) {
232
            return $swoole;
233
        });
234
    }
235
236
    public function saveSession()
237
    {
238
        if ($this->app->offsetExists('session')) {
239
            $this->app['session']->save();
240
        }
241
    }
242
243
    protected function isLumen()
244
    {
245
        return $this->conf['is_lumen'];
246
    }
247
}
248