ingeniasoftware / luthier-ci

Improved routing, middleware support, authentication tools and more for CodeIgniter 3 framework
https://luthier.ingenia.me/ci/en/
MIT License
151 stars 39 forks source link

Override Package Classes #47

Closed giorgosstab closed 4 years ago

giorgosstab commented 4 years ago

hey mate..nice work about your package..but i am curious if i can ovveride some of your controller and add mine logic without edit vendor/luthier/luthier/src

andersonsalas commented 4 years ago

Hi @giorgosstab,

What kind of logic/files do you want to extend? Editing vendor files is a bad idea because you will lose any changes when update the package with composer.

giorgosstab commented 4 years ago

thanks for your quick reply! i wanna extend cli mingrate template file file path: luthier/luthier/src/Cli.php

andersonsalas commented 4 years ago

I have done some tests and unfortunately there is no way to extend the Luthier\Cli class (without doing bad practices) due CodeIgniter limitations. Even without that limitations, the base class contains only private methods, so no inheritance are available.

The good news: in this case there is no problem if you write your own Cli controller and copy the code of Luthier\Cli within your custom methods, since the original methods has no dependences but the get_instance() function (as ci())

<?php
# application/routes/cli.php

Luthier\Cli::maker();
Luthier\Cli::migrations();

// luthier make custom-controller [name]
Route::cli('luthier/make/custom-controller/{(.+):name}', 'MyCliController@makeController');

// luthier make custom-migration [name] [type?]
Route::cli('luthier/make/custom-migration/{name}/{((sequential|timestamp)):type?}', 'MyCliController@makeMigration');

And the controller:

<?php

defined('BASEPATH') OR exit('No direct script access allowed');

class MyCliController extends CI_Controller
{
    public function __construct()
    {
        parent::__construct();
    }

    public function makeController($name)
    {
        echo 'Command "luthier make custom-controller" executed!';
        /* Original code here */
    }

    public function makeMigration($name, $type = 'timestamp')
    {
        echo 'Command "luthier make custom-migration" executed!';
        /* Original code here */
    }
}

Sorry about that limitation, CI 3 is an old-school framework and some OOP features such namespaces are missing. Hope this helps you, happy codding! :smiley:

giorgosstab commented 4 years ago

Thank you mate..i think i can make my work with that changes.. happy coding too! :D