Skip to content

Instantly share code, notes, and snippets.

@kajnelissen
Last active April 23, 2022 09:15
Show Gist options
  • Save kajnelissen/d1b795f7dbd03f30bd8e to your computer and use it in GitHub Desktop.
Save kajnelissen/d1b795f7dbd03f30bd8e to your computer and use it in GitHub Desktop.
Constructor Injection in CodeIgniter controllers. Also resolves nested dependencies.
<?php
use Nelissen\LooseCI\Services\BookingService;
class Booking extends CI_Controller
{
/**
* @var \Nelissen\LooseCI\Services\BookingService
*/
private $booking;
/**
* @param BookingService $booking
*/
public function __construct(BookingService $booking)
{
parent::__construct();
$this->booking = $booking;
}
// etc
}
<?php namespace Nelissen\LooseCI\Services;
use Nelissen\LooseCI\Repositories\Room\RoomRepositoryInterface;
use Nelissen\LooseCI\Repositories\Booking\BookingRepositoryInterface;
use Nelissen\LooseCI\Repositories\Booker\BookerRepositoryInterface;
use Nelissen\LooseCI\Models\Room;
use Nelissen\LooseCI\Models\Booker;
use Nelissen\LooseCI\Exceptions\BookingExceedsRoomCapacityException;
/**
* Class BookingService
*
* @author Kaj Nelissen
* @package Nelissen\LooseCI\Services
*/
class BookingService
{
/**
* @var
*/
private $bookingRepo;
/**
* @var
*/
private $roomRepo;
/**
* @var
*/
private $bookerRepo;
/**
* @param BookingRepositoryInterface $bookingRepo
* @param RoomRepositoryInterface $roomRepo
* @param BookerRepositoryInterface $bookerRepo
*/
public function __construct(BookingRepositoryInterface $bookingRepo,
RoomRepositoryInterface $roomRepo,
BookerRepositoryInterface $bookerRepo)
{
$this->bookingRepo = $bookingRepo;
$this->bookerRepo = $bookerRepo;
$this->roomRepo = $roomRepo;
}
// etc
}
<?php
// CI code
// insert following before the line $CI = new $class();
$ioc = new \Illuminate\Container\Container();
// bind interfaces to concrete implementation in order to resolve
$ioc->bind('Nelissen\LooseCI\Repositories\Booking\BookingRepositoryInterface', 'Nelissen\LooseCI\Repositories\Booking\CIBookingRepository');
$ioc->bind('Nelissen\LooseCI\Repositories\Booker\BookerRepositoryInterface', 'Nelissen\LooseCI\Repositories\Booker\CIBookerRepository');
$ioc->bind('Nelissen\LooseCI\Repositories\Room\RoomRepositoryInterface', 'Nelissen\LooseCI\Repositories\Room\CIRoomRepository');
// then replace $CI = new $class(); with
$CI = $ioc->build($class);
// $CI is now an instantiation of the requested controller
// however, all constructor parameters are automatically resolved out of the IoC container
// thus, improving testability
{
"name": "Nelissen/LooseCI",
"description": "Just a proof of concept demonstrating some tricks to decouple and test CodeIgniter.",
"keywords": ["codeigniter"],
"license": "GPL-2.0",
"authors": [
{
"name": "Kaj Nelissen"
}
],
"require": {
"php": ">=5.3.0",
"illuminate/container": "4.2.*",
"filp/whoops": "~1.0"
},
"require-dev": {
"phpunit/phpunit": "4.1.4",
"mockery/mockery": "0.9.1",
"phpdocumentor/phpdocumentor": "2.6.*",
"phpspec/phpspec": "~2.0",
"behat/behat": "2.4.*@stable",
"behat/mink": "1.4@stable"
},
"autoload": {
"psr-4": {
"Nelissen\\LooseCI\\": "src/"
},
"classmap": [
"application/core/",
"application/libraries/",
"application/models/",
"application/controllers/"
]
},
"minimum-stability": "stable"
}
@eavMarshall
Copy link

Wouldn't you be better off creating a way to allow people to override the creation of the controller? This way they could choose the container they want.

@aryadiahmad4689
Copy link

CodeIgniter.php? where i write tht in my project?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment