mjamilasfihani / conquer-container
Containerized Dependency Injection for CodeIgniter 4 Controller.
Fund package maintenance!
mjamilasfihani
Requires
- php: ^7.4 || ^8.0 || ^8.1
Requires (Dev)
- codeigniter4/devkit: ^1.1
- codeigniter4/framework: ^4.2.0
- rector/rector: 0.15.18
README
Containerized Dependency Injection for CodeIgniter 4 Controller.
Warning! This library crack the core point of CodeIgniter 4, consider to use it at your own risk!
Prerequisites
Usage of Conquer\Container requires the following:
- A CodeIgniter 4.2.0+ based project
- Composer for package management
- PHP 7.4+
Installation
Use the package manager composer to install.
composer require mjamilasfihani/conquer-container
Usage
Let's say you have app/Libraries/ExampleLibrary.php
file, and you want to load in __construct()
function
in your controller without initializing it manually. Than this library is yours.
Can I imagine your controller? Thank you :
namespace App\Controllers; use App\Controllers\BaseController; use App\Libraries\ExampleLibrary; class Home extends BaseController { /** * @var \App\Libraries\ExampleLibrary */ protected ExampleLibrary $exampleLibrary; // This is your old constructor isn't? // // /** // * Constructor // */ // public function __construct() // { // $this->exampleLibrary = new ExampleLibrary(); // } /** * This will be your new Constructor * * @param \App\Libraries\ExampleLibrary $exampleLibrary */ public function __construct(ExampleLibrary $exampleLibrary) { $this->exampleLibrary = $exampleLibrary; }; /** * Display Homepage * * @return string */ public function index(): string { // even it has equal result, depend how like you call your library :) $this->exampleLibrary; return view('welcome_message'); } }
If you have AnotherExampleLibrary.php
and it need ExampleLibrary
class in the constructor,
feel free to add it. Because it has been supported in since v2.0.0
Here is what I mean :
namespace App\Libraries; use App\Libraries\ExampleLibrary; class AnotherExampleLibrary { protected ExampleLibrary $exampleLibrary; /** * Constructor * * @param \App\Libraries\ExampleLibrary $exampleLibrary */ public function __construct(ExampleLibrary $exampleLibrary) { $this->exampleLibrary = $exampleLibrary; } public function anotherExampleMethod() { // you have a power from your parent class $exampleLibrary = $this->exampleLibrary; ... } }
Now you can call it with no worries from your controller :
public function __construct(AnotherExampleLibrary $anotherExampleLibrary) { // this use case is very help full for implement the repository pattern $this->anotherExampleLibrary = $anotherExampleLibrary; }
Notes
Remember one thing! Doing container like this is not officially supported by CodeIgniter 4,
since it has different structure do not judge me if you got an error for calling
the CodeIgniter 4 library use this method. (Do It By Your Own Risk)
Contributing
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
Please make sure to update tests as appropriate.