Thursday, April 23

Drupal 7 Bootstrap Process ( drupal_bootstrap ) - Part 1



This is the beginning of what I hope will be a series of posts that take you through a Drupal 7 page load line by line.

Here is the code I dove into: index.php - as it was in the days of Drupal 4.4:
<?php// $Id: index.php,v 1.76 2003/11/25 19:26:20 dries Exp $ include_once "includes/bootstrap.inc";drupal_page_header();
include_once
"includes/common.inc"; fix_gpc_magic(); menu_build("system"); if (menu_active_handler_exists()) {
 
menu_execute_active_handler();
}
else {
 
drupal_not_found();
}
drupal_page_footer(); ?>
And that's how I became a Drupal developer. I went down the rabbit hole. I began stepping through the code (without a deubugger!) trying to sort out what it was that Drupal was doing so that I could find what I needed to change to make my site look the way I wanted to. By the time I found my answer I had learned more about Drupal than I needed or wanted to know at the time. I was also hooked.
And the rest, as they say, was history.
With the release of Drupal 7 I thought I would repeat the exercise. This time however I will document the experience to share with you. We can learn something new together.

Here is index.php - as it is today:

<?php// $Id: index.php,v 1.99 2009/10/15 14:07:25 dries Exp $ /**
* @file
* The PHP page that serves all page requests on a Drupal installation.
*
* The routines here dispatch control to the appropriate handler, which then
* prints the appropriate page.
*
* All Drupal code is released under the GNU General Public License.
* See COPYRIGHT.txt and LICENSE.txt.
*/
/**
* Root directory of Drupal installation.
*/
define('DRUPAL_ROOT', getcwd()); require_once DRUPAL_ROOT . '/includes/bootstrap.inc';drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);menu_execute_active_handler();?>
The first thing you'll notice is that Drupal has a lot more documentation inside the code than it did 6 versions ago. This will make stepping through and explaining the code line by line a little easier (since much of the work is already done). Its worth noting that the code comments are in phpDoc format and are used to generate the code documentation found at http://api.drupal.org/
As you can see in the code comments index.php serves (nearly1) all page requests on your website. In other words, index.php is where code execution begins whether you request http://example.com/home or http://example.com/news/january/headlines or http://example.com/admin/rule/the/world.
Yes. Four (4) lines of code return every single page on your Drupal site! That's pretty amazing. So what's going on here.
A call is made to getcwd() which returns the current working directory which gets assigned to the DRUPAL_ROOT constant.
Basically Drupal is telling itself which directory in the server's file system it is installed and assigning that information to a constant that will be used elsewhere in the code.
Using that constant Drupal then includes the file bootstrap.inc from the includes directory using require_once.
<?php// $Id: bootstrap.inc,v 1.459 2010/12/30 04:35:00 webchick Exp $
/**
* @file
* Functions that need to be loaded on every Drupal request.
*/
//3000 or so more lines of code
?>
Bootstrap.inc contains roughly 80 functions that "need to be loaded on every Drupal request". As soon as bootstrap.inc gets included 37 new constants get defined.
From those 80 or so functions and 37 or so new constants only the drupal_bootstrap function is called with the value of the DRUPAL_BOOTSTRAP_FULL constant as an argument.
The short version of what is happening in this one function call is Drupal is told to start up (bootstrap) all of its mechanisms required to handle the page request. It loads configuration information, page caching system(s), database(s), session handling, translation system, modules, and everything else you can imagine (and things you might not have imagined) that are required to handle a page request.
The long version of what happens in this one function call is what many follow up posts in this series will be about. You'll have to come back for those.
Finally, the last line in index.php calls the function menu_execute_active_handler(). The short version of what this function does is to act like a telephone operator and direct or route your call to the appropriate function that can answer you.
It takes a look at the path of the requested URL (i.e. the parts after the domain name e.g. news/headlines/december) and figure out which function is responsible for handling requests for that path. Once menu_execute_active_handler() finds the function to call, it calls it. What happens at the point depends entirely on which function was called, but in most cases an html page is sent to the browser.
Once again the long version of what happens is left for another time.

Summary

And there you have it: Four lines of code and only two function calls. All you need to get a page out of Drupal. Sounds easy.
Next time I'll start looking in depth at the bootstrap process in bootstrap.inc and the drupal_bootstrap() function.
If you have any questions, suggestions or corrections just leave a comment. I truly appreciate the feedback.

No comments:

Post a Comment