Welcome Guest, Not a member yet? Register   Sign In
Running CI without a web server (e.g., for cron scripts or unit tests)
#1

[eluser]Unknown[/eluser]
I've created a little script which bootstraps the CI superobject and loader, allowing one to use CI outside a cgi/mod_php context and without a Controller object.

This has been especially handy for cronjobs and unit tests.

Since I take a different, more direct approach than what I saw on the wiki, I've posed my code here. Note that my approach does not require a web server or even php-cgi at all.

To use it, modify the CI_REALBASE constant and $system_folder and $application_folder variables to point to your real CI install. Then simply require_once the fake-codeigniter.php file at the top of your script, then use get_instance() to access and start using the CI superobject.

Additional notes:
* The script assumes the use of PHP5. PHP4 will require modifications (you'll have to check the php version and load Base4 instead of Base5).
* As little as possible is loaded, and the autoloader is not run, so you will have to load a lot of things yourself.
* This was written for CI 1.6.3, but will probably work with 1.7.0 with little or no modification.
* It makes some simplifying assumptions, so you may have to tweak things further if you have an unusual install.

Code:
<?php
/**
* Set up required variables for testing CI libraries, etc WITHOUT running codeigniter.
*
* @author Francis Avila
**/

/**
**/
error_reporting(E_ALL);

define('CI_VERSION',    '1.6.3');

define('CI_REALBASE', dirname(__FILE__).'/../htdocs/');

$system_folder = CI_REALBASE."system";

define('EXT', '.php');
define('BASEPATH', $system_folder.'/');

$application_folder = 'application';
define('APPPATH', BASEPATH.$application_folder.'/');

// These are needed just so the URI class doesn't choke.
// Their value does not really matter, but will affect
// what the url helper functions produce.
$_SERVER['SERVER_NAME'] = 'mydomain.net';
$_SERVER['SERVER_PORT'] = '80';

require(BASEPATH.'codeigniter/Common'.EXT);
require(APPPATH.'config/constants'.EXT);
require(BASEPATH.'codeigniter/Base5'.EXT);

$LANG =& load_class('Language');
$CFG =& load_class('Config');
$URI =& load_class('URI');

class CI_Fake extends CI_Base
{
    function __construct()
    {
        parent::CI_Base();
        // This is just to get around the fact that the $instance static var
        // of the CI_Base class object is private.
        $this->load   = & load_class('Loader');
        $this->config = & load_class('Config');
        $this->uri    = & load_class('URI');
    }
}
new CI_Fake();
#2

[eluser]Phil Sturgeon[/eluser]
What are the benefits of using this method over these two:

http://codeigniter.com/wiki/CI_on_the_command_line/

and

http://phpstarter.net/2008/12/run-codeig...-line-ssh/

They both load a pretty normal CI instance and don't encounter any problems. Is this just for extra-light loading?
#3

[eluser]Unknown[/eluser]
Quote:What are the benefits of using this method over these two?

I wasn't able to find those--if I had I probably would have used one of them just to save time.

However, the advantage of this method over those is the following:

* Those still use the entire framework. They will run all hooks and load everything that would normally be loaded--which, depending on what you're doing, might be an advantage, but for unit tests it is a definite bummer.
* Those require you to create controllers for everything you might run on the command line. That means if you have a web project, you will somehow need to restrict access to those controllers from a normal web instance, but make them accessible to the CLI context. (Best way is probably with a check for a constant only defined by the CLI.) This method allows you to use CI like a big library, and doesn't require controllers for anything.
#4

[eluser]awells527[/eluser]
Quote:* Those require you to create controllers for everything you might run on the command line. That means if you have a web project, you will somehow need to restrict access to those controllers from a normal web instance, but make them accessible to the CLI context. (Best way is probably with a check for a constant only defined by the CLI.)

That's the whole idea Smile. The method I posted on PHP Starter was to give all the functionality of CI, but on the command line...which is why I wanted the ability to create controllers for cron jobs, large import jobs, etc.

I believe testing for the presence of $_SERVER['REMOTE_ADDR'] is the best way to know if the script is being called from the web browser or the command line.
#5

[eluser]Kabotyn[/eluser]
WOW!
Thanks a lot!
It's exacly what i need!

I was trying to connect Joomla and CI, and that code helped me!




Theme © iAndrew 2016 - Forum software by © MyBB