Skip to content

Commit

Permalink
Add default db prefix to getDataConnector function calls (#30)
Browse files Browse the repository at this point in the history
Add two config options for
* setting which Laravel database connection should be used
* overwriting DataConnector completely with your own custom implementation

Fixes bug where DataConnector was not reading the table-name prefix and passing it on to the Celtic library.

Much appreciation to the contributors and bug-reporters that drove this release.

workon #28

---------

Co-authored-by: Jeroen de Bruijn <jeroen.de.bruijn@brightalley.nl>
  • Loading branch information
chrispittman and Jeroen de Bruijn authored Apr 25, 2024
1 parent 6e20db3 commit 6999a2b
Show file tree
Hide file tree
Showing 8 changed files with 77 additions and 8 deletions.
23 changes: 23 additions & 0 deletions config/lti.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,27 @@
]
],

/*
|--------------------------------------------------------------------------
| Miscellaneous config
|--------------------------------------------------------------------------
*/

/*
The name of the database connection (in config/databasee.php) which will be used
as a data source. By default, this is the same connection that's your 'default'
in config/database.php.
*/

// 'connection_name' => 'mysql'

/*
The Celtic LTI library requires a DataConnector class which provides access to the
database. We provide a default implementation of this class which uses the same database
that you've configured for your Laravel app. You can write your own custom class that
implements the DataConnectorProvider interface and set it here.
*/

// 'data_connector_provider' => LonghornOpen\LaravelCelticLTI\DataConnector\MyCustomDataConnectorProvider::class,

];
4 changes: 2 additions & 2 deletions src/Commands/AddLti1p2Platform.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Exception;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
use LonghornOpen\LaravelCelticLTI\DataConnector\DataConnectorProviderFactory;
use LonghornOpen\LaravelCelticLTI\PlatformCreator;

class AddLti1p2Platform extends Command
Expand Down Expand Up @@ -41,8 +42,7 @@ public function __construct()
*/
public function handle()
{
$pdo = DB::connection()->getPdo();
$dataConnector = LTI\DataConnector\DataConnector::getDataConnector($pdo, '', 'pdo');
$dataConnector = DataConnectorProviderFactory::getDataConnectorProvider()->getDataConnector();

$name = $this->argument('name');
$consKey = $this->argument('key');
Expand Down
4 changes: 2 additions & 2 deletions src/Commands/AddLti1p3Platform.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Exception;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
use LonghornOpen\LaravelCelticLTI\DataConnector\DataConnectorProviderFactory;
use LonghornOpen\LaravelCelticLTI\PlatformCreator;

class AddLti1p3Platform extends Command
Expand Down Expand Up @@ -56,8 +57,7 @@ public function handle()
return 0;
}

$pdo = DB::connection()->getPdo();
$dataConnector = LTI\DataConnector\DataConnector::getDataConnector($pdo, '', 'pdo');
$dataConnector = DataConnectorProviderFactory::getDataConnectorProvider()->getDataConnector();
$deployment_id = $this->option('deployment_id');
$client_id = $this->option('client_id');

Expand Down
7 changes: 7 additions & 0 deletions src/DataConnector/DataConnectorProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace LonghornOpen\LaravelCelticLTI\DataConnector;

interface DataConnectorProvider {
public function getDataConnector();
}
11 changes: 11 additions & 0 deletions src/DataConnector/DataConnectorProviderFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace LonghornOpen\LaravelCelticLTI\DataConnector;

class DataConnectorProviderFactory {
public static function getDataConnectorProvider()
{
$dc_class = config('lti.data_connector_provider', LaravelDataConnectorProvider::class);
return new $dc_class();
}
}
23 changes: 23 additions & 0 deletions src/DataConnector/LaravelDataConnectorProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace LonghornOpen\LaravelCelticLTI\DataConnector;

use ceLTIc\LTI\DataConnector\DataConnector;
use Illuminate\Support\Facades\DB;
use LonghornOpen\LaravelCelticLTI\DataConnector\DataConnectorProvider;

class LaravelDataConnectorProvider implements DataConnectorProvider
{
public function getDataConnector()
{
$connection_name = config('lti.connection_name', config('database.default'));
return $this->getDataConnectorForConnection($connection_name);
}

public function getDataConnectorForConnection($connection_name)
{
$pdo = DB::connection($connection_name)->getPdo();
$dbTableNamePrefix = config('database.connections.' . $connection_name . '.prefix', '');
return DataConnector::getDataConnector($pdo, $dbTableNamePrefix, 'pdo');
}
}
4 changes: 3 additions & 1 deletion src/LtiServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use LonghornOpen\LaravelCelticLTI\Commands\AddLti1p2Platform;
use LonghornOpen\LaravelCelticLTI\Commands\AddLti1p3Platform;
use LonghornOpen\LaravelCelticLTI\Commands\UpdateConsumerSetting;
use LonghornOpen\LaravelCelticLTI\DataConnector\DataConnectorProviderFactory;

class LtiServiceProvider extends ServiceProvider
{
Expand All @@ -27,7 +28,8 @@ public function boot() : void
]);

try {
Tool::$defaultTool = LtiTool::getLtiTool();
$dataConnector = DataConnectorProviderFactory::getDataConnectorProvider()->getDataConnector();
Tool::$defaultTool = LtiTool::getLtiTool($dataConnector);
} catch (\PDOException $e) {
// LtiTool tries to connect to the DB. Can't do that?
// Not worth stopping boot() for...any real DB connection
Expand Down
9 changes: 6 additions & 3 deletions src/LtiTool.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use ceLTIc\LTI\ResourceLink;
use ceLTIc\LTI\UserResult;
use Illuminate\Support\Facades\DB;
use LonghornOpen\LaravelCelticLTI\DataConnector\DataConnectorProviderFactory;

class LtiTool extends LTI\Tool
{
Expand All @@ -31,12 +32,14 @@ public static function getLtiTool($dataConnector = null) {
}

// Generally, you shouldn't construct this object yourself; use the
// singleton provided by LtiTool::getLtiTool() instead.
// singleton provided by LtiTool::getLtiTool() instead. Celtic's
// LTI code processes LTI nonces (one-time-use tokens) and invalidates
// them after processing, so creating a second LtiTool in the middle
// of a request will often fail due to nonce problems.
public function __construct($dataConnector = null)
{
if ($dataConnector === null) {
$pdo = DB::connection()->getPdo();
$dataConnector = DataConnector::getDataConnector($pdo, '', 'pdo');
$dataConnector = DataConnectorProviderFactory::getDataConnectorProvider()->getDataConnector();
}
parent::__construct($dataConnector);

Expand Down

0 comments on commit 6999a2b

Please sign in to comment.