Last active
July 8, 2021 06:57
-
-
Save einnar82/d8eb7588bc25c01a2248717644972370 to your computer and use it in GitHub Desktop.
Override OCI_CRED_EXT error in laravel-oci8 by yajra
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Providers; | |
use Illuminate\Database\Connection; | |
use Yajra\Oci8\Oci8ServiceProvider as ServiceProvider; | |
use App\Connectors\OracleConnector as Connector; | |
use Yajra\Oci8\Oci8Connection; | |
class Oci8ServiceProvider extends ServiceProvider | |
{ | |
/** | |
* Register the service provider. | |
* | |
* @return void | |
*/ | |
public function register() | |
{ | |
if (file_exists(config_path('oracle.php'))) { | |
$this->mergeConfigFrom(config_path('oracle.php'), 'database.connections'); | |
} else { | |
$this->mergeConfigFrom(__DIR__ . '/../config/oracle.php', 'database.connections'); | |
} | |
Connection::resolverFor('oracle', function ($connection, $database, $prefix, $config) { | |
$connector = new Connector(); | |
$connection = $connector->connect($config); | |
$db = new Oci8Connection($connection, $database, $prefix, $config); | |
if (!empty($config['skip_session_vars'])) { | |
return $db; | |
} | |
// set oracle session variables | |
$sessionVars = [ | |
'NLS_TIME_FORMAT' => 'HH24:MI:SS', | |
'NLS_DATE_FORMAT' => 'YYYY-MM-DD HH24:MI:SS', | |
'NLS_TIMESTAMP_FORMAT' => 'YYYY-MM-DD HH24:MI:SS', | |
'NLS_TIMESTAMP_TZ_FORMAT' => 'YYYY-MM-DD HH24:MI:SS TZH:TZM', | |
'NLS_NUMERIC_CHARACTERS' => '.,', | |
]; | |
// Like Postgres, Oracle allows the concept of "schema" | |
if (isset($config['schema'])) { | |
$sessionVars['CURRENT_SCHEMA'] = $config['schema']; | |
} | |
if (isset($config['session'])) { | |
$sessionVars = array_merge($sessionVars, $config['session']); | |
} | |
if (isset($config['edition'])) { | |
$sessionVars = array_merge( | |
$sessionVars, | |
['EDITION' => $config['edition']] | |
); | |
} | |
$db->setSessionVars($sessionVars); | |
return $db; | |
}); | |
} | |
/** | |
* Bootstrap services. | |
* | |
* @return void | |
*/ | |
public function boot() | |
{ | |
// | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Connectors; | |
use Illuminate\Support\Arr; | |
use Yajra\Oci8\Connectors\OracleConnector as Connector; | |
class OracleConnector extends Connector | |
{ | |
/** | |
* Establish a database connection. | |
* | |
* @param array $config | |
* @return PDO | |
*/ | |
public function connect(array $config) | |
{ | |
$tns = ! empty($config['tns']) ? $config['tns'] : $this->getDsn($config); | |
$options = $this->getOptions($config); | |
if (defined('OCI_CRED_EXT') && Arr::get($options, 'session_mode') === OCI_CRED_EXT) { | |
// External connections can only be used with user / and an empty password | |
$config['username'] = '/'; | |
$config['password'] = null; | |
} | |
$connection = $this->createConnection($tns, $config, $options); | |
return $connection; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment