Bootstrap Source Code for CI Explained

I’ve stopped work on this series to start a new one for CI 3.

The front controller source code is usually found in a file named index.php found in the root folder of a CI installation. You can have more than one front controller per CI installation. However, in that case it’s considered to be a state of having multiple applications.

Introduction

This post is part of a series which explains the CodeIgniter (CI) source code. This post explains the CI 2.1.2 bootstrap. Its name is CodeIgniter.php.

In a nutshell the bootstrap script loads the base classes and executes the request.

Location

Typical location is ./system/core/CodeIgniter.php

Code

<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

This line is found in just about every CI script. It’s a security trick. Basically, it stops execution of the PHP code if BASEPATH has not been defined.

Comment

/**
 * CodeIgniter
 *
 * An open source application development framework for PHP 5.1.6 or newer
 *
 * @package		CodeIgniter
 * @author		ExpressionEngine Dev Team
 * @copyright	Copyright (c) 2008 - 2011, EllisLab, Inc.
 * @license		http://codeigniter.com/user_guide/license.html
 * @link		http://codeigniter.com
 * @since		Version 1.0
 * @filesource
 */

// ------------------------------------------------------------------------

This is a typical CI file header comment. This comment section will be one of two file header comments for this file. The other one follows.

You will see this particular file header comment at the top of all scripts found in the system folder.

Comment

/**
 * System Initialization File
 *
 * Loads the base classes and executes the request.
 *
 * @package		CodeIgniter
 * @subpackage	codeigniter
 * @category	Front-controller
 * @author		ExpressionEngine Dev Team
 * @link		http://codeigniter.com/user_guide/
 */

This is the header comment which relates directly to this script.

Code

/**
 * CodeIgniter Version
 *
 * @var string
 *
 */
	define('CI_VERSION', '2.1.2');

If you’ve been reading this series of posts THEN you don’t need me to tell you what this code does.

Code

/**
 * CodeIgniter Branch (Core = TRUE, Reactor = FALSE)
 *
 * @var boolean
 *
 */
	define('CI_CORE', FALSE);

This is an EllisLab Reactor application. Reactor is the community branch of CodeIgniter. On the other hand — the version of CodeIgniter used in ExpressionEngine is the Core CodeIgniter.

Comment

/*
 * ------------------------------------------------------
 *  Load the global functions
 * ------------------------------------------------------
 */
	require(BASEPATH.'core/Common.php');

Load the global functions by running Common.php .

Cross reference

Code

/*
 * ------------------------------------------------------
 *  Load the framework constants
 * ------------------------------------------------------
 */
	if (defined('ENVIRONMENT') AND file_exists(APPPATH.'config/'.ENVIRONMENT.'/constants.php'))
	{
		require(APPPATH.'config/'.ENVIRONMENT.'/constants.php');
	}
	else
	{
		require(APPPATH.'config/constants.php');
	}

This code causes the constants found in constants.php to be defined. The code will execute constants.php. The location of the particular constants.php to execute depends on the constant ENVIRONMENT.

Cross reference

Code

/*
 * ------------------------------------------------------
 *  Define a custom error handler so we can log PHP errors
 * ------------------------------------------------------
 */
	set_error_handler('_exception_handler');

_exception_handler is a function we defined earlier. This code sets it up as the error handler. See my notes on Common.php for this and other functions we’ve defined.

Code

	if ( ! is_php('5.3'))
	{
		@set_magic_quotes_runtime(0); // Kill magic quotes
	}

This code kills magic quotes if the current version of PHP is less than version 5.3. Again, is_php() was defined by us earlier.

The @ symbol tells PHP not to generate errors on this function call.

See Error Control Operators.

Code

/*
 * ------------------------------------------------------
 *  Set the subclass_prefix
 * ------------------------------------------------------
 *
 * Normally the "subclass_prefix" is set in the config file.
 * The subclass prefix allows CI to know if a core class is
 * being extended via a library in the local application
 * "libraries" folder. Since CI allows config items to be
 * overriden via data set in the main index. php file,
 * before proceeding we need to know if a subclass_prefix
 * override exists.  If so, we will set this value now,
 * before any classes are loaded
 * Note: Since the config file data is cached it doesn't
 * hurt to load it here.
 */
	if (isset($assign_to_config['subclass_prefix']) AND $assign_to_config['subclass_prefix'] != '')
	{
		get_config(array('subclass_prefix' => $assign_to_config['subclass_prefix']));
	}

If you go back and look at my post about the front controller you will see that there was an array called $assign_to_config. This array is useful in case where we have custom front controllers which require on-the-fly configurations of some items. Well, in this code is where we actually set one of those on-the-fly configuration items. If this doesn’t make sense then go back and read up on the common functions which cache and read configuration items.

Good example of how get_config() can reassign config values.

Code

/*
 * ------------------------------------------------------
 *  Set a liberal script execution time limit
 * ------------------------------------------------------
 */
	if (function_exists("set_time_limit") == TRUE AND @ini_get("safe_mode") == 0)
	{
		@set_time_limit(300);
	}

set_time_limit() sets the number of seconds a script is allowed to run. If this is reached, the script returns a fatal error. The default limit is 30 seconds or, if it exists, the max_execution_time value defined in the php.ini. See set_time_limit…

Code

/*
 * ------------------------------------------------------
 *  Start the timer... tick tock tick tock...
 * ------------------------------------------------------
 */
	$BM =& load_class('Benchmark', 'core');
	$BM->mark('total_execution_time_start');
	$BM->mark('loading_time:_base_classes_start');

Loads the Benchmark class — which is actually CI_Benchmark. This class is in ./system/core/Benchmark.php .

Marks total_execution_time_start and loading_time:_base_classes_start .

Code

/*
 * ------------------------------------------------------
 *  Instantiate the hooks class
 * ------------------------------------------------------
 */
	$EXT =& load_class('Hooks', 'core');

Loads the Hooks class — which is actually CI_Hooks. This class is in ./system/core/Hooks.php .

Hooks allow me (the developer) to add my own code at hook points within the executing framework code. At each hook point all hooks related to the hook point will execute.

Code

/*
 * ------------------------------------------------------
 *  Is there a "pre_system" hook?
 * ------------------------------------------------------
 */
	$EXT->_call_hook('pre_system');

Cross reference.

Call the hooks associated with the pre_system hook point name. _call_hook is for running my own framework code (if that code exists.) This code of mine (which is associated with label pre_system) would be intended as pre_system (i.e. before framework action) code.

Code

/*
 * ------------------------------------------------------
 *  Instantiate the config class
 * ------------------------------------------------------
 */
	$CFG =& load_class('Config', 'core');

Loads the Config class — which is actually CI_Config. This class is in ./system/core/Config.php .

Code

	// Do we have any manually set config items in the index.php file?
	if (isset($assign_to_config))
	{
		$CFG->_assign_to_config($assign_to_config);
	}

_assign_to_config() method of CI_Config class is applied on $assign_to_config array. See earlier explanations of what this array is for.

Code

/*
 * ------------------------------------------------------
 *  Instantiate the UTF-8 class
 * ------------------------------------------------------
 *
 * Note: Order here is rather important as the UTF-8
 * class needs to be used very early on, but it cannot
 * properly determine if UTf-8 can be supported until
 * after the Config class is instantiated.
 *
 */

	$UNI =& load_class('Utf8', 'core');

Loads the Utf8 class — which is actually CI_Utf8. This class is in ./system/core/Utf8.php .

The comment which precedes this code is significant; so, read it!

Code

/*
 * ------------------------------------------------------
 *  Instantiate the URI class
 * ------------------------------------------------------
 */
	$URI =& load_class('URI', 'core');

Loads the URI class — which is actually CI_URI. This class is in ./system/core/URI.php .

Code

/*
 * ------------------------------------------------------
 *  Instantiate the routing class and set the routing
 * ------------------------------------------------------
 */
	$RTR =& load_class('Router', 'core');
	$RTR->_set_routing();

	// Set any routing overrides that may exist in the main index file
	if (isset($routing))
	{
		$RTR->_set_overrides($routing);
	}

Self explanatory.

This series (of posts) builds on itself. Starting now I’ll assume you’ve learned the previous posts. It also builds on my series which annotates the CodeIgniter documentation.

Code

/*
 * ------------------------------------------------------
 *  Instantiate the output class
 * ------------------------------------------------------
 */
	$OUT =& load_class('Output', 'core');

Self explanatory.

Code

/*
 * ------------------------------------------------------
 *	Is there a valid cache file?  If so, we're done...
 * ------------------------------------------------------
 */
	if ($EXT->_call_hook('cache_override') === FALSE)
	{
		if ($OUT->_display_cache($CFG, $URI) == TRUE)
		{
			exit;
		}
	}

? ? ?

Advertisements

About samehramzylabib

See About on https://samehramzylabib.wordpress.com
This entry was posted in CI Source Code Explained. Bookmark the permalink.

One Response to Bootstrap Source Code for CI Explained

  1. Pingback: Front Controller Source Code for CI Explained | Sam's PHP

Comment

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s