mjamilasfihani/conquer-container

There is no license information available for the latest version (v2.2.5) of this package.

Containerized Dependency Injection for CodeIgniter 4 Controller.

v2.2.5 2023-02-25 02:15 UTC

This package is auto-updated.

Last update: 2024-03-30 00:42:09 UTC


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!

Coverage Status

Prerequisites

Usage of Conquer\Container requires the following:

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.

License

MIT