/**
  * Bootstrap the application services.
  *
  * @return void
  */
 public function boot()
 {
     $configPath = __DIR__ . '/config/connectionloader.php';
     $migrationPath = __DIR__ . '/migration/2016_05_02_000000_create_connection_loader_table.php';
     /**
      * Copy the default configuration file and migration file when user runs php artisan vendor:publish
      */
     $this->publishes([$configPath => config_path('/connectionloader.php')], 'config');
     $this->publishes([$migrationPath => database_path('/migrations/2016_05_02_000000_create_connection_loader_table.php')], 'migration');
     if ($this->app['config']->get('connectionloader.enabled')) {
         if (!$this->app['config']->get('connectionloader.enabled')) {
             return;
         }
         $connection = $this->app['config']->get('connectionloader.connection');
         $table = $this->app['config']->get('connectionloader.table');
         $check = $this->app['config']->get('connectionloader.check_enabled');
         if (!(isset($connection) && isset($table) && isset($check))) {
             \error_log('Invalid connection or table specified in configuration file');
             return;
         }
         /**
          * Function to gather database connections from database and table provided
          * in configuration file. Compiles into file that returns an array.
          * Function returns path to the temporary file.
          */
         $fileName = ConnectionLoader::getConnections($connection, $table);
         if ($fileName == null) {
             \error_log('Error in returned file name value');
             return;
         }
         $file_path = storage_path('app/' . $fileName);
         /**
          * Merge the returned configuration array into the existing database.connections
          * configuration key.
          */
         $key = 'database.connections';
         $config = $this->app['config']->get($key, []);
         $configSet = $this->app['config']->set($key, array_merge(require $file_path, $config));
         /**
          * Now to delete the temporary file created during the process
          */
         $result = Storage::delete($fileName);
         if ($result === false) {
             \error_log('Failed to delete ' . storage_path() . $fileName);
             \error_log('Trying once more');
             $result = Storage::delete($fileName);
             if ($result === true) {
                 \error_log(storage_path() . $fileName . ' Deleted successfully');
                 return;
             }
             \error_log('Failed to delete twice, delete manually ' . storage_path() . $fileName);
             return;
         }
         ConnectionLoader::checkConnections($connection, $table, $check);
     }
 }
 /**
  * getConnections() function fetches all connections from
  * connection and table specified. Then returns a path
  * to a configuration file which gets imported and
  * deleted.
  *
  * @param $connection
  * @param $table
  * @return null|string
  */
 public static function getConnections($connection, $table)
 {
     $connections = DB::connection($connection)->table($table)->get();
     if (!empty($connections)) {
         /**
          * Create an empty array which will get populated with the data from
          * $connections in the correct format within the foreach statement
          * below file generation.
          */
         $config = array();
         /**
          * Generate a random string for the filename as it is temporary
          * and more secure.
          */
         $random_string = ConnectionLoader::random_string(35);
         /**
          * Append the .php extension to the file name and store here
          * storage_path/app/$tempFile with some starting content.
          */
         $tempFileName = $random_string . '.php';
         $tempFile = Storage::disk('local')->put($tempFileName, '<?php return ');
         if ($tempFile === true) {
             /**
              * Loops through the connections checking the driver and pushes
              * correctly formatted array into the $config array.
              */
             foreach ($connections as $connection) {
                 $name = $connection->name;
                 $driver = $connection->driver;
                 if ($driver == 'sqlite') {
                     $connection_config = array('driver' => $driver, 'database' => database_path($connection->database), 'prefix' => $connection->prefix);
                     $config[$name] = $connection_config;
                 } elseif ($driver == 'mysql') {
                     $strict = $connection->strict;
                     if ($strict == 1) {
                         $strict = true;
                     } else {
                         $strict = false;
                     }
                     $password = $connection->password;
                     if (empty($password)) {
                         $connection_config = array('driver' => $driver, 'host' => $connection->host, 'database' => $connection->database, 'username' => $connection->username, 'password' => $password, 'charset' => $connection->charset, 'collation' => $connection->collation, 'prefix' => $connection->prefix, 'strict' => $strict);
                     } else {
                         $connection_config = array('driver' => $driver, 'host' => $connection->host, 'database' => $connection->database, 'username' => $connection->username, 'password' => Crypt::decrypt($password), 'charset' => $connection->charset, 'collation' => $connection->collation, 'prefix' => $connection->prefix, 'strict' => $strict);
                     }
                     $config[$name] = $connection_config;
                 } elseif ($driver == 'pgsql') {
                     $password = $connection->password;
                     if (empty($password)) {
                         $connection_config = array('driver' => $driver, 'host' => $connection->host, 'port' => $connection->port, 'database' => $connection->database, 'username' => $connection->username, 'password' => $password, 'charset' => $connection->charset, 'prefix' => $connection->prefix, 'schema' => $connection->schema);
                     } else {
                         $connection_config = array('driver' => $driver, 'host' => $connection->host, 'port' => $connection->port, 'database' => $connection->database, 'username' => $connection->username, 'password' => Crypt::decrypt($password), 'charset' => $connection->charset, 'prefix' => $connection->prefix, 'schema' => $connection->schema);
                     }
                     $config[$name] = $connection_config;
                 }
             }
             /**
              * Append a var_export of $config to the temporary file and
              * append the semicolon.
              *
              * Returns the temporary filename.
              */
             $endFile = ';';
             Storage::append($tempFileName, var_export($config, true));
             Storage::append($tempFileName, $endFile);
             return $tempFileName;
         } else {
             \error_log('Unable to create temporary file');
         }
     } else {
         \error_log('Configuration File Connection Invalid.');
         return null;
     }
 }