Skip to content

Instantly share code, notes, and snippets.

@lbialy
Last active January 23, 2018 17:37
Show Gist options
  • Save lbialy/81459de956c20307ecf1 to your computer and use it in GitHub Desktop.
Save lbialy/81459de956c20307ecf1 to your computer and use it in GitHub Desktop.
Laravel multiple implementations via IoC
######################################
<?php
// project_root/app/config/orderrepository.php
return [
'implementation' => 'Voipdeploy\Application\Database\Repositories\OrmOrderRepository'
];
?>
######################################
<?php
// project_root/app/config/app.php
return [
'providers' => array(
// OTHER LARAVEL SERVICE PROVIDERS
'Voipdeploy\Application\ApplicationServiceProvider',
),
];
?>
######################################
<?php namespace Voipdeploy\Application;
// project_root/vendor/voipdeploy/application/src/Voipdeploy/Application/ApplicationServiceProvider.php
use Illuminate\Redis\Database;
use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\Config;
class ApplicationServiceProvider extends ServiceProvider
{
/**
* Indicates if loading of the provider is deferred.
*
* @var bool
*/
protected $defer = false;
/**
* Bootstrap the application events.
*
* @return void
*/
public function boot()
{
$this->package('voipdeploy/application');
}
/**
* Register the service provider.
*
* @return void
*/
public function register()
{
/*
* From configuration
$this->app->bind('Voipdeploy\Application\Database\Repositories\OrderRepositoryInterface', function () {
$implementationClassName = Config::get('orderrepository.implementation');
return new $implementationClassName();
});
*/
$this->app->bind('OrmOrderRepository', function () {
return new \Voipdeploy\Application\Database\Repositories\OrmOrderRepository();
});
$this->app->bind('PlainOrderRepository', function(){
return new \Voipdeploy\Application\Database\Repositories\PlainSqlOrderRepository();
});
$this->app->bind('HomeController', function(){
return new \HomeController($this->app->make('OrmOrderRepository'), $this->app->make('PlainOrderRepository'));
});
}
}
?>
#########################################
<?php
// project_root/vendor/voipdeploy/application\src\Voipdeploy\Application\Database\Repositories\OrderRepositoryInterface.php
namespace Voipdeploy\Application\Database\Repositories;
interface OrderRepositoryInterface
{
public function getHello();
}
?>
##########################################
<?php namespace Voipdeploy\Application\Database\Repositories;
// project_root/vendor/voipdeploy/application/src/Voipdeploy/Application/Database/Repositories/OrmOrderRepository.php
class OrmOrderRepository implements OrderRepositoryInterface
{
public function getHello()
{
return 'Hello from OrmOrderRepository!';
}
}
?>
##########################################
<?php namespace Voipdeploy\Application\Database\Repositories;
// project_root/vendor/voipdeploy/application/src/Voipdeploy/Application/Database/Repositories/PlainSqlOrderRepository.php
class PlainSqlOrderRepository implements OrderRepositoryInterface
{
public function getHello()
{
return "Hello from PlainSqlOrderRepository";
}
}
##########################################
<?php
/*
|--------------------------------------------------------------------------
| Application Routes
|--------------------------------------------------------------------------
|
| Here is where you can register all of the routes for an application.
| It's a breeze. Simply tell Laravel the URIs it should respond to
| and give it the Closure to execute when that URI is requested.
|
*/
Route::controller('/', 'HomeController');
##########################################
<?php
use Illuminate\Routing\Controller as BaseController;
class HomeController extends BaseController
{
protected $ormOrderRepository;
protected $plainSqlOrderRepository;
public function __construct(
\Voipdeploy\Application\Database\Repositories\OrderRepositoryInterface $ormOrderRepository,
\Voipdeploy\Application\Database\Repositories\OrderRepositoryInterface $plainSqlOrderRepository
)
{
$this->ormOrderRepository = $ormOrderRepository;
$this->plainSqlOrderRepository = $plainSqlOrderRepository;
}
public function getIndex()
{
echo $this->ormOrderRepository->getHello();
echo "<br>";
echo $this->plainSqlOrderRepository->getHello();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment