Drupal line by line : part 3

I'd just finished explaining that Drupal was aware that it neededto do a full bootstrap and it was just about to start the firstphase of that process.

Drupal calls the function _drupal_bootstrap_configuration().

Error Handling

The very first thing that happens in the first phase of thebootstrap process is Drupal defines its own error handlingfunctions. Up until this point if an error had occurred in the codephp would have used its own error handling to report the problem.Luckily very few lines of code have been executed to thispoint.

Drupal sets an error handler as well as an exception handler.The exception handler is new to Drupal since Drupal 7 now includesobject oriented code which when called could generateexceptions.

What happens inside of these error handling functions is bestleft to its own discussion. Just be aware that they exist and theyare defined very early on. Also note the error handling code foundin the error.inc file isn't loaded until the first error occurs.Our first encounter of lazy loading inDrupal 7.

drupal_environment_initialize().

<?php
function drupal_environment_initialize() {?>

In this function Drupal first cleans up parts of the$_SERVER super global. It makessure that there is a valid server protocol variable set. It alsodoes a security check on the http host variable.

<?php
 
// Whenclean URLs are enabled, emulate ?q=foo/bar using REQUEST_URI. Itis
  // not possible to append the query string usingmod_rewrite without the B
  // flag (this was added in Apache 2.2.8),because mod_rewrite s the
  // path before passing it on to PHP. This is aproblem when the path contains
  // e.g. "&" or "%" that havespecial meanings in URLs and must be encoded.
 
$_GET['q'] =request_path();
?>

The request_path() functioncleans up the default $_GET['q']value as per the code comments above.1

The rest of drupal_environment_inistialize is literallyself explanatory.

<?php
 
// EnforceE_ALL, but allow users to set levels not part of E_ALL.
 
error_reporting(E_ALL| error_reporting());

 

// Override PHP settings required forDrupal to work properly.
  // sites/default/default.settings.php containsmore runtime settings.
  // The .htaccess file contains settings thatcannot be changed at runtime.

  // Don't escapequotes when reading files from the database, disk, etc.
 

ini_set ( 'magic_quotes_runtime' , '0' );
 
// Usesession cookies, not transparent sessions that puts the session idin
  // the query string.
 
ini_set ( 'session.use_cookies' , '1' );
 
ini_set ( 'session.use_only_cookies' , '1' );
 
ini_set ( 'session.use_trans_sid' , '0' );
 
// Don'tsend HTTP headers using PHP's session handler.
 
ini_set ( 'session.cache_limiter' , 'none' );
 
// Usehttponly session cookies.
 
ini_set ( 'session.cookie_httponly' , '1' );

 

// Set sane locale settings, to ensureconsistent string, dates, times and
  // numbers handling.
 
setlocale ( LC_ALL , 'C' );
?>

With drupal_environment_initialize() complete jumpback to _drupal_bootstrap_configuration().

Its at this stage that Drupal, completely out of the blue,decides to start a timer function (to keep tack of page executiontime). This is rather unexpected since you might not have knownthat Drupal has timer functions at all - let alone ones that arecalled on every single page load. Just something to keep in mindwhen developing your next killer module - you have access totimer_start,timer_read, and timer_stop functions.

drupal_settings_initialize();

Finally _drupal_bootstrap_configuration callsdrupal_settings_initialize().

<?php
function drupal_settings_initialize() {
  global
$base_url, $base_path, $base_root;

 

// Export the following settings.phpvariables to the global namespace
 
global $databases , $cookie_domain , $conf , $installed_profile , $update_free_access , $db_url , $db_prefix , $drupal_hash_salt , $is_https , $base_secure_url , $base_insecure_url ;
?>

It starts off by defining a long list of global variables thatget used throughout Drupal.

Drupal then looks for and includes the settings.php file. Thisfile is often found in /sites/default/settings.php, but if you havea multi-site install of Drupal it could be in different directoriesunder /sites/ depending on which site you are currently looking at.Drupal figures out which directory settings.php is in with a callto conf_path().

I won't go through conf_path() line by line, butit is worth reading the documentation for that function if you everwondered how Drupal just knew where to load settings from2.

An entire post could be dedicated to settings.php, but what youneed to know here is that your site's configuration is loaded inthe first bootstrap phase. It is very much worthwhile to read thecode comments in settings.php.

In the rest of the function, variables for base URL, cookiedomain, and session name are all set correctly.

And with that, the first phase bootstrap phase is complete.

Summary

The first bootstrap phase is all about laying the foundation forthe rest of page execution. Custom error handling code is prepared,php settings are modified, settings.php gets loaded and some keyvariables used later on are initialized.

Next time I'll look at phase 2 of the Drupal bootstrapprocess.

Footnotes

Whenyou have clean urls enabled, you are hiding 'ugly' urls from yoursite's visitors. The ugly url is in the form http://example.com/index.php?q=foo/bar/baz.Its easy to forget that Drupal paths are really a single parameterpassed via HTTP GET.

Thecode is fun with gems like: $server =explode('.', implode('.', array_reverse(explode(':',rtrim($_SERVER['HTTP_HOST'], '.')))));
. It justgives you the feeling that there has got to be a better way toaccomplish the same thing - but that voodoo gets the jobdone.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值