PHP Framework CodeIgniter Framework Source code Analysis, annotation culture, Class library analysis (i)

Source: Internet
Author: User
Tags definition benchmark error handling get ip variables php file php and php framework


The last few days decided to look at the CI Framework source code, because it is lightweight, the structure of the code file is clearer, and index.php as a portal, loading all the base classes in the codeigniter.php file, so I looked at each class drop, and looked at the time to analyze each step Key process, and the original English annotation was translated, now the main document has been translated, and later will be translated and analyzed some other important classes, please look forward to



Here is its detailed execution process, from the entrance to the end, the complete record, we first look at, the other analysis, for example, CI superclass structure, how to implement the MVC pattern, database class implementation, XSS filter class detailed analysis and so on will be landing



Source: Http://www.cnblogs.com/mier



Download source file From here: Http://files.cnblogs.com/mier/ci execution process. txt



After downloading, the file suffix is changed to. php can be viewed normally in the IDE


1 <?php
2//Contract: variable in parentheses after the meaning of the variable, square brackets for the value of the variable
3 1.index.php.
4 (1), initialize some of the system variables, including:
5 $system _folder (Systems folder [System]),
6 $application _folder (Application folder [application])
7 (2). Define constants, including:
8 EXT (file name extension [PathInfo (__file__, Pathinfo_extension)]),
9 Fcpath (full pathname of file in server [__file__]),
SELF (file name containing extension [PathInfo (__file__, Pathinfo_basename)]),
One basepath (the path to the system folder [$system _folder. /']),
AppPath (program folder [$application _folder. /']),
13 (3). Directed to
BasePath ' Codeigniter/codeigniter '. EXT;
15 (4). End
?>
<?php
2.codeigniter.php (System Front Controller, loading basic classes and processing requests)
19 (1), load global functions, which play a role in later loading classes and acquiring configuration items.
Require (basepath. ' Codeigniter/common '). EXT);
Common.php has the following functions:
Is_really_writable ($file);//Determine if the file is writable
&load_class ($class, $instantiate = TRUE)//The function uses a single piece mode to load the class when the requested class does not exist, and then set it to a static variable, if This class has already been loaded, the installation variables for this class are returned
24//The second parameter indicates whether this class is instantiated, and if set to false, only is a file that contains this class without instantiating it, the value returned is true/false just whether the load succeeded
25//Load class library from libraries
&get_config ()//Returns an array of configurations in the config.php file
Config_item ($item)//Get a configuration item
Show_error ($message)//Display Error
show_404 ($page = ')//display 404 error
Log_message ($level = ' error ', $message, $php _error = FALSE)//write information to log
_exception_handler ($severity, $message, $filepath, $line)//error handling function to intercept the error and write to the specified file
32 (2), compatibility function, handling some functions that do not exist in some versions
Require (basepath. ' Codeigniter/compat '). EXT);
The following contents exist in compat.php:
35 defines the level of error display: Define (' e_strict ', 2048);
36 defines a function: Ctype_digit ($STR)//string contains only numbers, otherwise returns false
37 defines a function: Ctype_alnum ($STR);//string contains only numbers or letters, otherwise return false
38 (3). Load Frame Constants
Require (AppPath. ' Config/constants '). EXT);
40 the constants defined in this file are specified in the file, omitted here
41 (4). Set error handling, disable Magic_quotes_runtime
42 (5). Import the benchmark class, set breakpoints, and debug breakpoints, this class allows you to set breakpoints to calculate elapsed time and memory consumption
$BM =& load_class (' Benchmark ');
The following properties and methods are available in the benchmark class:
' var $marker = array ();
function mark ($name)/Set a breakpoint, assign the current time in microseconds to this variable
function Elapsed_time ($point 1 = ', $point 2 = ', $decimals = 4)//calculates the interval of two breakpoints, or returns if the value of the first breakpoint is null. A label {elapsed_time}, which will be processed in the output class
The function memory_usage ()//returns a {memory_usage} label, which is processed in the output class because it makes sense to display the amount of memory consumed by the entire script.
49 (6). Loading the hook class provides a mechanism to extend the operation of the basic system by inserting a script at some point that you want to run, including a variety of points to
$EXT =& load_class (' Hooks ');
51 This class contains the following properties and methods:
var $enabled = false;//Whether hooks are enabled
var $hooks = array ();//Hook arrays
The var $in _progress = false;//identifies whether the hook is running
The function _initialize ()//Determines whether hooks is enabled, loads hooks in config/hooks.php if enabled, and assigns the $hooks property
The function _call_hook ($which = ')/call hook, can set the hook is limited, see the Class library description in this file
The function _run_hook ($data)//Runs the hook, contains the file according to the set array of hooks, instantiates the class, runs its method, and attaches the property
58 (7). Load and instantiate the Config class, load the configuration file from the Config folder, which allows you to create your own profile in the Config folder, but you must define the configuration items in this file as a $config[""] Array
$CFG =& load_class (' Config ');
60 This class contains the following properties and methods:
The var $config = array ();//config array
var $is _loaded = Array ()//Loaded configuration file
The function ci_config ()///constructor Gets the basic Config item in config.php and assigns it to $config property
The $ function load ($file = ', $use _sections = False, $fail _gracefully = false)//load the configuration file from the Config folder ( Load successfully added to $is_loaded property) and load configuration entry to $config property
The-function item ($item, $index = ')//Returns a configuration item $index allows configuration to occur as a two-dimensional array, with $config[when configured as a two-dimensional array $index] [$item]
The Slash_item function ($item)//Gets a configuration item and then adds a "/" symbol
The function site_url ($uri = ')//Converts the URI to the full URL form according to the config setting
The function System_url ()//returns the full path to the system folder
The Set_item function ($item, $value)//sets the value of a setting item, except that it is set in the property and does not change the file's settings
70 (8). Loads and instantiates the URI class, which intercepts the parameter portion of the current URL and decomposes it into a controller as well as a method and a parameter for the main function call
$URI =& load_class (' URI ');
72 This class contains the following properties and methods:
var $keyval = array ();
The string that follows the _string;//index.php var $uri
var $segments = array ();
The var $rsegments = array ();
The index.php function _fetch_uri_string ()//Gets the string followed by the character and assigns it to the property $uri _string.
The _parse_request_uri function ()//handles the harassing characters in the URI to work properly
The function _filter_uri ($STR)//filter string to escape special characters
_remove_url_suffix ()//If the name of the pseudo suffix is set in the configuration, remove the suffix
Bayi function _explode_segments ()//to decompose the string "/" Into the $segments
The $segments function _reindex_segments ()//uses ingenious methods to change the index of an array of indexes so that its index starts at 1, which is more intuitive
The function segment ($n, $no _result = FALSE)//Returns the parameter of the specified index, that is, the value of the $segments[$n]
The function rsegment ($n, $no _result = FALSE)//is the same as the basic function of the function above, except that it allows you to enter when the URI routing function of the CodeIgniter is opened Row detail Split and redistribute URI information
The function Uri_to_assoc ($n = 3, $default = Array ()) returns the argument after the third argument as an associative array the third parameter is the first key to return the array , the fourth argument is the value of the first returned array
86//etc.
The function Ruri_to_assoc ($n = 3, $default = Array ())//IBID., is only applied to routing functions
The function _uri_to_assoc ($n = 3, $default = Array (), $which = ' segment ')/IBID.
The function Assoc_to_uri ($array)//is contrary to the functionality of the above method
The function _slash_segment ($n, $where = ' trailing ', $which = ' segment ')//Slash, when $where= "trailing" Add at the end, = "leading" at the beginning
The Segment_array function ()//returns $segments
Total number of total_segments ()//$segments
93 (9). Load and instantiate the router class, which handles the URI to load the controller and Controller methods
$RTR =& load_class (' Router ');
95 This class contains the following properties and methods:
var $config;
The var $routes = array ();//routing rules, defined in routes.php
The var $error _routes = Array ();
var $class = ';
var $method = ' index ';
The var $directory = ';
102 var $uri _protocol = ' auto ';
The _controller;//var $default default Controller
var $scaffolding _request = FALSE; Must is set to FALSE
The system function _set_routing ()//sets the default routing information and, if no controller information exists, loads the default controller according to the routes.php settings.
The function _set_request ($segments = Array ())//The method invokes Set_class and Set_method with an array input to send the request
The _validate_request function ($segments)//checks whether the folder information is included in the URI, and if it is included, assigns its value to the property $directory
108 function _parse_routes ()//processing routes, based on routing settings to
109 function Set_class ($class)/Set Controller class
The function Fetch_class ()
function Set_method ($method)//Set Controller method
112 function Fetch_method ()
113 function Set_directory ($dir)/Set folder
114 (10). Load and instantiate output class
$OUT =& load_class (' Output ');
116 This class contains the following methods and properties:
117 Var $final _output;
118 var $cache _expiration = 0;
119 var $headers = array ();
The var $enable _profiler = FALSE;
121 function Enable_profiler ($val = TRUE)//allows you to turn the profiler on or off, and the profiler will display the Test datum points at the bottom of the page, or debug and optimize the other data needed
122 function Get_output ()
123 function Set_output ($output)
124 function Append_output ($output)//The contents of the attached output
The Set_header function ($header, $replace = TRUE)//sets the header to array properties to send $headers
126 function Set_status_header ($code = ', $text = ')/Set status code
127 function Cache ($time)/Set caching and cache time, in minutes
128 function _display ($output = ')//This method will generate and output to the client, and if the profiler is turned on, add the profiling results at the end of the page, and if the cache is set, call the cached file
129 function _write_cache ($output)//write cache file based on cached time set
130 function _display_cache (& $CFG, & $URI)//display cache files, find files based on URL (MD5 encrypted file name), if present, and if it is expired, delete Except
131 (11). Determine if a cached file exists, display if present, and exit
132 (12). Load input class, this class is used to process filter user input, prevent various attacks
$IN =& load_class (' Input ');
134 var $use _xss_clean = FALSE;
135 var $xss _hash = ';
136 var $ip _address = FALSE;
137 var $user _agent = FALSE;
138 var $allow _get_array = FALSE;
139 var $never _allowed_str = array ()/filtered string, detailed in the class file definition
140 var $never _allowed_regex =array ()//With regular filter, see definition of class file in detail
The Ci_input function ()///constructor, which determines whether security is turned on from the configuration file, and then calls the following method
The _sanitize_globals ()//Clears the Globals variable, which, when the Globals_register is turned on, is the equivalent of shutting down this configuration item
143 function _clean_input_data ($STR)//Filter input data
144 function _clean_input_keys ($STR)//filter Key value
145 function _fetch_from_array (& $array, $index = ', $xss _clean = FALSE)//Get value from $array if XS is set The S_clean filters the
146 function Get ($index = ', $xss _clean = FALSE)//Fetch filtered gets array
147 function post ($index = ', $xss _clean = FALSE)//Get filtered post array
The 148 function get_post ($index = ', $xss _clean = FALSE)//Gets the post array, if not, from the Get array
149 function Cookie ($index = ', $xss _clean = FALSE)//Get cookie Array
_clean function Server ($index = ', $XSS = FALSE)//Get server variable array
151 function IP_Address ()//Get IP Address
152 function Valid_ip ($IP)//Verify IP Address
153 function Filename_security ($STR)//filter for hidden trouble in file name
154 function Xss_clean ($str, $is _image = FALSE)//perform various XSS filtering
The _remove_invisible_characters function ($STR)//Remove an invisible symbol
156 omits the callback function in a series of Xss_clean and performs various security filtering on the input
157 (13). Loading language class
158 $LANG =& Load_class (' Language ');
159 omit the description of this class
160 (14). Loading Controller class
161 Load_class (' Controller ', FALSE);
162 contains only this file and does not instantiate this class
163 load Loader Class if PHP version is less than 5
164 (15). Find the Controller class and method in the controller folder according to the URI, and exit if it is not found, and find the containing file
165 $class = $RTR->fetch_class ();//Get Requested Class
166 $method = $RTR->fetch_method ()//method of obtaining the request
167 (16).
$CI = new $class ();//load superclass, superclass is created based on class in controller
169 to determine whether the request is a scaffold
170 (a) Controller class structure: This class inherits from the Ci_base class
171 var $_ci_scaffolding = FALSE;
172 var $_ci_scaff_table = FALSE;
173 function Controller ()//constructor, execute Parent::ci_base (); $this->_ci_initialize ();
174 function _ci_initialize ()//This adds all the base classes to the variables for this class, makes this class A superclass, and loads the load class to load the views and models.
175 function _ci_scaffolding ()//Execute Scaffold
176 (19). Close the database if you use a database
177
178
179

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.