|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Lenevor Framework |
|
5
|
|
|
* |
|
6
|
|
|
* LICENSE |
|
7
|
|
|
* |
|
8
|
|
|
* This source file is subject to the new BSD license that is bundled |
|
9
|
|
|
* with this package in the file license.md. |
|
10
|
|
|
* It is also available through the world-wide-web at this URL: |
|
11
|
|
|
* https://lenevor.com/license |
|
12
|
|
|
* If you did not receive a copy of the license and are unable to |
|
13
|
|
|
* obtain it through the world-wide-web, please send an email |
|
14
|
|
|
* to [email protected] so we can send you a copy immediately. |
|
15
|
|
|
* |
|
16
|
|
|
* @package Lenevor |
|
17
|
|
|
* @subpackage Base |
|
18
|
|
|
* @link https://lenevor.com |
|
19
|
|
|
* @copyright Copyright (c) 2019 - 2021 Alexander Campo <[email protected]> |
|
20
|
|
|
* @license https://opensource.org/licenses/BSD-3-Clause New BSD license or see https://lenevor.com/license or see /license.md |
|
21
|
|
|
* @since 0.1.2 |
|
22
|
|
|
*/ |
|
23
|
|
|
|
|
24
|
|
|
namespace Syscodes\Core\Bootstrap; |
|
25
|
|
|
|
|
26
|
|
|
use Exception; |
|
27
|
|
|
use Syscodes\Dotenv\Dotenv; |
|
28
|
|
|
use Syscodes\Contracts\Core\Application; |
|
29
|
|
|
use Syscodes\Dotenv\Repository\RepositoryCreator; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Initialize boot of a ParseEnv instance. |
|
33
|
|
|
* |
|
34
|
|
|
* @author Alexander Campo <[email protected]> |
|
35
|
|
|
*/ |
|
36
|
|
|
class BootDetectEnvironment |
|
37
|
|
|
{ |
|
38
|
|
|
/** |
|
39
|
|
|
* The application implementation. |
|
40
|
|
|
* |
|
41
|
|
|
* @var \Syscodes\Contracts\Core\Application $app |
|
42
|
|
|
*/ |
|
43
|
|
|
protected $app; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Bootstrap the given application. |
|
47
|
|
|
* |
|
48
|
|
|
* @param \Syscodes\Contracts\Core\Application $app |
|
49
|
|
|
* |
|
50
|
|
|
* @return void |
|
51
|
|
|
*/ |
|
52
|
|
|
public function bootstrap(Application $app) |
|
53
|
|
|
{ |
|
54
|
|
|
$this->detectEnvironmentFile($app); |
|
55
|
|
|
try |
|
56
|
|
|
{ |
|
57
|
|
|
$this->createEnv($app)->load(); |
|
58
|
|
|
} |
|
59
|
|
|
catch (Exception $e) |
|
60
|
|
|
{ |
|
61
|
|
|
// |
|
62
|
|
|
} |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Detect if a custom environment file matching the APP_ENV exists. |
|
67
|
|
|
* |
|
68
|
|
|
* @param \Syscodes\Contracts\Core\Application $app |
|
69
|
|
|
* |
|
70
|
|
|
* @return bool |
|
71
|
|
|
*/ |
|
72
|
|
|
protected function detectEnvironmentFile($app) |
|
73
|
|
|
{ |
|
74
|
|
|
$environment = env('APP_ENV'); |
|
75
|
|
|
|
|
76
|
|
|
if ( ! $environment) { |
|
77
|
|
|
return; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
$this->setEnvironmentFilePath( |
|
81
|
|
|
$app, $app->environmentFile().'.'.$environment |
|
|
|
|
|
|
82
|
|
|
); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Load a custom environment file. |
|
87
|
|
|
* |
|
88
|
|
|
* @param \Syscodes\Contracts\Core\Application $app |
|
89
|
|
|
* @param string $file |
|
90
|
|
|
* |
|
91
|
|
|
* @return bool |
|
92
|
|
|
*/ |
|
93
|
|
|
protected function setEnvironmentFilePath($app, $file) |
|
94
|
|
|
{ |
|
95
|
|
|
if (is_file($app->environmentPath().'/'.$file)) { |
|
|
|
|
|
|
96
|
|
|
$app->setEnvironmentFile($file); |
|
|
|
|
|
|
97
|
|
|
|
|
98
|
|
|
return true; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
return false; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* Create a ParseEnv instance. |
|
106
|
|
|
* |
|
107
|
|
|
* @param \Syscodes\Contracts\Core\Application $app |
|
108
|
|
|
* |
|
109
|
|
|
* @return \Syscodes\Dotenv\Dotenv |
|
110
|
|
|
*/ |
|
111
|
|
|
protected function createEnv($app) |
|
112
|
|
|
{ |
|
113
|
|
|
return Dotenv::create( |
|
114
|
|
|
new RepositoryCreator, |
|
115
|
|
|
$app->environmentPath(), |
|
116
|
|
|
$app->environmentFile() |
|
117
|
|
|
); |
|
118
|
|
|
} |
|
119
|
|
|
} |