File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ <?php namespace Illuminate \Config ;
2+
3+ /**
4+ * PHP $_ENV loader for protecting sensitive configuration options.
5+ *
6+ * Inspired by the wonderful "Dotenv" library by Vance Lucas.
7+ */
8+ class EnvironmentVariables {
9+
10+ /**
11+ * The environment loader implementation.
12+ *
13+ * @var \Illuminate\Config\EnvironmentLoaderInterface $loader
14+ */
15+ protected $ loader ;
16+
17+ /**
18+ * The server environment instance.
19+ *
20+ * @param \Illuminate\Config\EnvironmentLoaderInterface $loader
21+ * @return void
22+ */
23+ public function __construct (EnvironmentVariablesLoaderInterface $ loader )
24+ {
25+ $ this ->loader = $ loader ;
26+ }
27+
28+ /**
29+ * Load the server variables for a given environment.
30+ *
31+ * @param string $environment
32+ */
33+ public function load ($ environment = null )
34+ {
35+ foreach ($ this ->loader ->load ($ environment ) as $ key => $ value )
36+ {
37+ $ _ENV [$ key ] = $ value ;
38+
39+ $ _SERVER [$ key ] = $ value ;
40+ }
41+ }
42+
43+ }
Original file line number Diff line number Diff line change 1+ <?php namespace Illuminate \Config ;
2+
3+ interface EnvironmentVariablesLoaderInterface {
4+
5+ /**
6+ * Load the environment variables for the given environment.
7+ *
8+ * @param string $environment
9+ * @return array
10+ */
11+ public function load ($ environment = null );
12+
13+ }
Original file line number Diff line number Diff line change 1+ <?php namespace Illuminate \Config ;
2+
3+ use Illuminate \Filesystem \Filesystem ;
4+
5+ class FileEnvironmentVariablesLoader implements EnvironmentVariablesLoaderInterface {
6+
7+ /**
8+ * The filesystem instance.
9+ *
10+ * @var \Illuminate\Filesystem\Filesystem
11+ */
12+ protected $ files ;
13+
14+ /**
15+ * The path to the configuration files.
16+ *
17+ * @var string
18+ */
19+ protected $ path ;
20+
21+ /**
22+ * Create a new file environment loader instance.
23+ *
24+ * @param \Illumiante\Filesystem\Filesystem $files
25+ * @return void
26+ */
27+ public function __construct (Filesystem $ files , $ path = null )
28+ {
29+ $ this ->files = $ files ;
30+ $ this ->path = $ path ?: base_path ();
31+ }
32+
33+ /**
34+ * Load the environment variables for the given environment.
35+ *
36+ * @param string $environment
37+ * @return array
38+ */
39+ public function load ($ environment = null )
40+ {
41+ if ($ environment == 'production ' ) $ environment = null ;
42+
43+ if ( ! $ this ->files ->exists ($ path = $ this ->getFile ($ environment )))
44+ {
45+ return array ();
46+ }
47+ else
48+ {
49+ return $ this ->files ->getRequire ($ path );
50+ }
51+ }
52+
53+ /**
54+ * Get the file for the given environment.
55+ *
56+ * @param string $environment
57+ * @return string
58+ */
59+ protected function getFile ($ environment )
60+ {
61+ if ($ environment )
62+ {
63+ return $ this ->path .'/.env. ' .$ environment .'.php ' ;
64+ }
65+ else
66+ {
67+ return $ this ->path .'/.env.php ' ;
68+ }
69+ }
70+
71+ }
Original file line number Diff line number Diff line change 1010use Illuminate \Events \EventServiceProvider ;
1111use Illuminate \Routing \RoutingServiceProvider ;
1212use Illuminate \Exception \ExceptionServiceProvider ;
13+ use Illuminate \Config \FileEnvironmentVariablesLoader ;
1314use Symfony \Component \HttpKernel \HttpKernelInterface ;
1415use Symfony \Component \HttpKernel \TerminableInterface ;
1516use Symfony \Component \HttpKernel \Exception \HttpException ;
@@ -914,6 +915,16 @@ public function getConfigLoader()
914915 return new FileLoader (new Filesystem , $ this ['path ' ].'/config ' );
915916 }
916917
918+ /**
919+ * Get the environment variables loader instance.
920+ *
921+ * @return \Illuminate\Config\EnvironmentVariablesLoaderInterface
922+ */
923+ public function getEnvironmentVariablesLoader ()
924+ {
925+ return new FileEnvironmentVariablesLoader (new Filesystem , $ this ['path.base ' ]);
926+ }
927+
917928 /**
918929 * Get the service provider repository instance.
919930 *
Original file line number Diff line number Diff line change 3737 $ basePath .'/vendor/laravel/framework/src/Illuminate/Support/NamespacedItemResolver.php ' ,
3838 $ basePath .'/vendor/laravel/framework/src/Illuminate/Config/FileLoader.php ' ,
3939 $ basePath .'/vendor/laravel/framework/src/Illuminate/Config/LoaderInterface.php ' ,
40+ $ basePath .'/vendor/laravel/framework/src/Illuminate/Config/EnvironmentVariablesLoaderInterface.php ' ,
41+ $ basePath .'/vendor/laravel/framework/src/Illuminate/Config/FileEnvironmentVariablesLoader.php ' ,
42+ $ basePath .'/vendor/laravel/framework/src/Illuminate/Config/EnvironmentVariables.php ' ,
4043 $ basePath .'/vendor/laravel/framework/src/Illuminate/Filesystem/Filesystem.php ' ,
4144 $ basePath .'/vendor/laravel/framework/src/Illuminate/Foundation/AliasLoader.php ' ,
4245 $ basePath .'/vendor/laravel/framework/src/Illuminate/Foundation/ProviderRepository.php ' ,
Original file line number Diff line number Diff line change 4545use Illuminate \Http \Request ;
4646use Illuminate \Support \Facades \Facade ;
4747use Illuminate \Foundation \AliasLoader ;
48+ use Illuminate \Config \EnvironmentVariables ;
4849use Illuminate \Config \Repository as Config ;
4950
5051/*
104105
105106$ app ->registerCoreContainerAliases ();
106107
108+ /*
109+ |--------------------------------------------------------------------------
110+ | Register The Environment Variables
111+ |--------------------------------------------------------------------------
112+ |
113+ | Here we will register all of the $_ENV and $_SERVER variables into the
114+ | process so that they're globally available configuration options so
115+ | sensitive configuration information can be swept out of the code.
116+ |
117+ */
118+
119+ with ($ envVariables = new EnvironmentVariables (
120+ $ app ->getEnvironmentVariablesLoader ()))->load ($ env );
121+
107122/*
108123|--------------------------------------------------------------------------
109124| Register The Configuration Repository
You can’t perform that action at this time.
0 commit comments