Welcome Guest, Not a member yet? Register   Sign In
Best way to integrate composer with CodeIgniter 3.0
#1

[eluser]Aphax[/eluser]
Hello everyone,

It's the first time i'm using composer and I would like to use it with my app who runs on Codeigniter 3.0 Wink
I was wondering what would be the best way to integrate Codeigniter as a vendor package in the application, because I doesn't seem obvious at first sight, between application files with a customisation purpose and codeigniter's core files.
My concerns are about index.php configuration variables (system_path,application_folder,view_folder) and files loaded in application folder, how can I avoid conflicts between this files in codeigniter's package and thoses in my application ?

Example :

I have to set up my application path folder, once edited, how can I avoid any conflicts when updating CodeIgniter's package ? I could create an custom index.php file requiring CodeIgniter's original file, but I have no proper way to assure the values of my path variables, if I define them before including original index file, they will be overwritten, if I set up them after, they will not be loaded in the application Smile

Thank you
#2

[eluser]ivantcholakov[/eluser]
CodeIgniter 3-dev has a setting allowing Composer packages usage. But why do you wish to make CodeIgniter itself to be a package?

Edit: Who do you think will support and update this package? You?
#3

[eluser]ivantcholakov[/eluser]
Since you start using Composer, here is what I can recommend. Forget for now about CodeIgniter as a Composer package. It won't happen soon, or maybe it would never happen.

Using Composer will require at least PHP 5.3.2, probably it is not a problem for you. Let me show an example how this could be done in CodeIgniter:

0. Install Composer on your dev computer, find their installation instructions.
1. You can download CodeIgniter 3 dev and install it on your dev computer. After you see the "welcome" page within the browser, create the folder application/vendor/
2. Open application/config/config.php, find the setting $config['composer_autoload']. Set it to TRUE.
3. Create the file application/composer.json with some simple content, for example, require some package:
Code:
{
    "require": {
        "fg/multiplayer": "1.1.0"
    }
}

4. Open the terminal and with cd commands go to the folder application/ (let it be the current terminal folder). Type the command composer install (or php composer.phar install , it depends on how you installed Composer). After that you will see that the directory application/vendor/ is no longer empty, and you have got one package installed.

Next, there are some optional things to be done. I very often see that developers use component APIs directly, this may be not very convenient. It would be better if some class with its interface to be placed between the component and your application. Then you can switch to other component in the future, to make some hacks, and to use more convenient API for your taste. So...

5. Make a CodeIgniter library that exploits the installer component - within application/libraries/ make the file Multiplayer.php with the following content:

Code:
<?php defined('BASEPATH') OR exit('No direct script access allowed.');

/**
* @author Ivan Tcholakov <[email protected]>, 2014
* @license The MIT License, http://opensource.org/licenses/MIT
*/

class Multiplayer extends \Multiplayer\Multiplayer {

    public function __construct($config = array()) {

        if (!is_array($config)) {
            $config = array();
        }

        parent::__construct($config);
    }

}

The \Multiplayer\Multiplayer class can be used as a singleton (this a case by coincidence) and our library does that. We can inject configuration options to it once. Here is how:

6. Under the directory application/config/ create the file multiplayer.php with the following content:

Code:
&lt;?php defined('BASEPATH') OR exit('No direct script access allowed');

/**
* @author Ivan Tcholakov <[email protected]>, 2014
* @license The MIT License, http://opensource.org/licenses/MIT
*/

$config['vbox7'] = array(
    'id' => '#vbox7\.com/play\:(?<id>[a-z0-9_-]+)#i',
    'url' => 'http://vbox7.com/emb/external.php?vid=%s',
    'map' => array(),
);

\Multiplayer\Multiplayer component supports only 3 video sources, by this configuration file I am adding one more. By using the configuration file I am extending a component's feature.

7. Testing all this: Within the welcome page view, place this code for testing:

Code:
$this->load->library('multiplayer);
echo $this->multiplayer->html('https://www.youtube.com/watch?v=NRhVcTTMlrM');

Reload the welcome page in the browser. You should see a video player. This is it.

If you commit your project within a version control system (Git, Mercurial, SVN, ...), among other things, commit the whole folder application/vendor/, your file application/composer.json and the file application/composer.lock (Created by Composer).
#4

[eluser]Aphax[/eluser]
@ivantcholakov, thank you for that very clear answer, I understand the way you integrate composer in CodeIgniter, but that doesn't integrate CodeIgniter itself in the composer logic (at least system/core files), and that's the problem I'm trying to solve, why I want that ? Just to separate CI's core file from my application files and avoiding "CI package" (when I mean package I mean the whole CI brick as a standalone framework with core file I will never edit) storage in my repos.

[quote author="ivantcholakov" date="1411334327"]CodeIgniter 3-dev has a setting allowing Composer packages usage. But why do you wish to make CodeIgniter itself to be a package?
[/quote]
Because I'm participating to a small open-source project where we compare PHP frameworks between each other over a blog sample, the goal is to provide the composer.json file with the framework package in it, avoiding all the framework system file in the git repo. (It's better if the user can get all the framework up-to-date core files with a single composer install command)

[quote author="ivantcholakov" date="1411334327"]
Edit: Who do you think will support and update this package? You?
[/quote]
The actual developers of CI ? I don't get your point, sorry.

[quote author="ivantcholakov" date="1411334327"]Since you start using Composer, here is what I can recommend. Forget for now about CodeIgniter as a Composer package. It won't happen soon, or maybe it would never happen.[/quote]

I believe you but... Then why is it avaible on packagist as a package ? ("ellislab/codeigniter": "dev-develop") Maybe i'm missunderstanding something obvious Smile

[quote author="ivantcholakov" date="1411334327"]
If you commit your project within a version control system (Git, Mercurial, SVN, ...), among other things, commit the whole folder application/vendor/, your file application/composer.json and the file application/composer.lock (Created by Composer).[/quote]
Really ? I don't get this, why including vendor files into the repo whereas the main purpose of composer is to avoid code duplication/storage ?

According to you, what would be the best solution for storing CI-based applications in a repo without storing the whole CI application ?

Thanks for your help !



#5

[eluser]ivantcholakov[/eluser]
Quote:... but that doesn’t integrate CodeIgniter itself in the composer logic (at least system/core files), and that’s the problem I’m trying to solve, ...

I am a little distant to this idea, here is some explanation that I found: https://github.com/EllisLab/CodeIgniter/issues/2262

Quote:Because I’m participating to a small open-source project where we compare PHP frameworks between each other over a blog sample, the goal is to provide the composer.json file with the framework package in it, avoiding all the framework system file in the git repo. (It’s better if the user can get all the framework up-to-date core files with a single composer install command)

Sounds like an experiment. Well, the next quotes are related to the link I've given.

Quote:Really ? I don’t get this, why including vendor files into the repo whereas the main purpose of composer is to avoid code duplication/storage ?

Why not? I see Composer as a tool for building my project efficiently by code reuse. I don't see Composer as a deployment tool. It is you to use Composer, let the end adopters download the whole your application, don't force them to deal with Composer. Give them everything with just one download.
Also, if your installed Composer packages are part of your source, your IDE's autocomplete feature will respond better.

Quote:According to you, what would be the best solution for storing CI-based applications in a repo without storing the whole CI application ?

I don't see such a solution. And I think, I don't need it.
#6

[eluser]Aphax[/eluser]
Quote:I am a little distant to this idea, here is some explanation that I found: https://github.com/EllisLab/CodeIgniter/issues/2262
I see, it's true that the constraints imposed by packagist avoid any valid source on the long term for CodeIgniter actually...

Quote:Why not? I see Composer as a tool for building my project efficiently by code reuse. I don’t see Composer as a deployment tool. It is you to use Composer, let the end adopters download the whole your application, don’t force them to deal with Composer. Give them everything with just one download.
Also, if your installed Composer packages are part of your source, your IDE’s autocomplete feature will respond better.
I must agree with that ! But composer will quickly become a must-have for every developer I guess, we should adopt It with no excuses Smile

Anyway it's brighter in my mind now, thanks for your time @ivantcholakov Smile

PS : I will share my solution if I find a good one





Theme © iAndrew 2016 - Forum software by © MyBB