Status message

Maintenant, vous regardez: Eight Phases of Drupal 7 Bootstrap

Eight Phases of Drupal 7 Bootstrap

The moment a web browser requests a page, Drupal begins running a complex series of steps that result
in a fully rendered page being returned to the browser. With every page request, Drupal has to do those
same calculations,

The web server’s PHP interpreter parses index.php and executes the code. Drupal’s developers have
organized the process of creating a Drupal page into two sequences: bootstrap and execution of the page
callback associated with the current path. This division allows the use of a working Drupal environment
for applications other than generating web pages

All start with index.php file:

  1. /**
  2.  * @file
  3.  * The PHP page that serves all page requests on a Drupal installation.
  4.  *
  5.  * The routines here dispatch control to the appropriate handler, which then
  6.  * prints the appropriate page.
  7.  *
  8.  * All Drupal code is released under the GNU General Public License.
  9.  * See COPYRIGHT.txt and LICENSE.txt.
  10.  */
  11.  
  12. /**
  13.  * Root directory of Drupal installation.
  14.  */
  15. define('DRUPAL_ROOT', getcwd());
  16.  
  17. require_once DRUPAL_ROOT . '/includes/bootstrap.inc';
  18. drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
  19. menu_execute_active_handler();

Now, let's take a look at the bootstrap.inc files,
The task of the bootstrap is to set the stage for business logic and theming to take place by including all
necessary libraries, preparing a database connection, and reading the configuration. It is accomplished
in separate phases, and each phase must be executed only once and in a particular order. This is
enforced by the drupal_bootstrap() function and by having a constant integer assigned to the phases
that represent their processing order , drupal_bootstrap() is called with the bootstrap
phase that should be reached as a parameter.

  1. /**
  2. * First bootstrap phase: initialize configuration.
  3. */
  4. define('DRUPAL_BOOTSTRAP_CONFIGURATION', 0);
  5.  
  6. /**
  7. * Second bootstrap phase: try to serve a cached page.
  8. */
  9. define('DRUPAL_BOOTSTRAP_PAGE_CACHE', 1);
  10.  
  11. /**
  12. * Third bootstrap phase: initialize database layer.
  13. */
  14. define('DRUPAL_BOOTSTRAP_DATABASE', 2);
  15.  
  16. /**
  17. * Fourth bootstrap phase: initialize the variable system.
  18. */
  19. define('DRUPAL_BOOTSTRAP_VARIABLES', 3);
  20.  
  21. /**
  22. * Fifth bootstrap phase: initialize session handling.
  23. */
  24. define('DRUPAL_BOOTSTRAP_SESSION', 4);
  25.  
  26. /**
  27. * Sixth bootstrap phase: set up the page header.
  28. */
  29. define('DRUPAL_BOOTSTRAP_PAGE_HEADER', 5);
  30.  
  31. /**
  32. * Seventh bootstrap phase: find out language of the page.
  33. */
  34. define('DRUPAL_BOOTSTRAP_LANGUAGE', 6);
  35.  
  36. /**
  37. * Final bootstrap phase: Drupal is fully loaded; validate and fix input data.
  38. */
  39. define('DRUPAL_BOOTSTRAP_FULL', 7);
  40.  
  41. /**
  42. * Ensures Drupal is bootstrapped to the specified phase.
  43. *
  44. * In order to bootstrap Drupal from another PHP script, you can use this code:
  45. * @code
  46. * define('DRUPAL_ROOT', '/path/to/drupal');
  47. * require_once DRUPAL_ROOT . '/includes/bootstrap.inc';
  48. * drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
  49. * @endcode
  50. *
  51. * @param int $phase
  52. * A constant telling which phase to bootstrap to. When you bootstrap to a
  53. * particular phase, all earlier phases are run automatically. Possible
  54. * values:
  55. * - DRUPAL_BOOTSTRAP_CONFIGURATION: Initializes configuration.
  56. * - DRUPAL_BOOTSTRAP_PAGE_CACHE: Tries to serve a cached page.
  57. * - DRUPAL_BOOTSTRAP_DATABASE: Initializes the database layer.
  58. * - DRUPAL_BOOTSTRAP_VARIABLES: Initializes the variable system.
  59. * - DRUPAL_BOOTSTRAP_SESSION: Initializes session handling.
  60. * - DRUPAL_BOOTSTRAP_PAGE_HEADER: Sets up the page header.
  61. * - DRUPAL_BOOTSTRAP_LANGUAGE: Finds out the language of the page.
  62. * - DRUPAL_BOOTSTRAP_FULL: Fully loads Drupal. Validates and fixes input
  63. * data.
  64. * @param boolean $new_phase
  65. * A boolean, set to FALSE if calling drupal_bootstrap from inside a
  66. * function called from drupal_bootstrap (recursion).
  67. *
  68. * @return int
  69. * The most recently completed phase.
  70. */
  71. function drupal_bootstrap($phase = NULL, $new_phase = TRUE) {
  72. // Not drupal_static(), because does not depend on any run-time information.
  73. static $phases = array(
  74. DRUPAL_BOOTSTRAP_CONFIGURATION,
  75. DRUPAL_BOOTSTRAP_PAGE_CACHE,
  76. DRUPAL_BOOTSTRAP_DATABASE,
  77. DRUPAL_BOOTSTRAP_VARIABLES,
  78. DRUPAL_BOOTSTRAP_SESSION,
  79. DRUPAL_BOOTSTRAP_PAGE_HEADER,
  80. DRUPAL_BOOTSTRAP_LANGUAGE,
  81. DRUPAL_BOOTSTRAP_FULL,
  82. );
  83. // Not drupal_static(), because the only legitimate API to control this is to
  84. // call drupal_bootstrap() with a new phase parameter.
  85. static $final_phase;
  86. // Not drupal_static(), because it's impossible to roll back to an earlier
  87. // bootstrap state.
  88. static $stored_phase = -1;
  89.  
  90. if (isset($phase)) {
  91. // When not recursing, store the phase name so it's not forgotten while
  92. // recursing but take care of not going backwards.
  93. if ($new_phase && $phase >= $stored_phase) {
  94. $final_phase = $phase;
  95. }
  96.  
  97. // Call a phase if it has not been called before and is below the requested
  98. // phase.
  99. while ($phases && $phase > $stored_phase && $final_phase > $stored_phase) {
  100. $current_phase = array_shift($phases);
  101.  
  102. // This function is re-entrant. Only update the completed phase when the
  103. // current call actually resulted in a progress in the bootstrap process.
  104. if ($current_phase > $stored_phase) {
  105. $stored_phase = $current_phase;
  106. }
  107.  
  108. switch ($current_phase) {
  109. case DRUPAL_BOOTSTRAP_CONFIGURATION:
  110. _drupal_bootstrap_configuration();
  111. break;
  112.  
  113. case DRUPAL_BOOTSTRAP_PAGE_CACHE:
  114. _drupal_bootstrap_page_cache();
  115. break;
  116.  
  117. case DRUPAL_BOOTSTRAP_DATABASE:
  118. _drupal_bootstrap_database();
  119. break;
  120.  
  121. case DRUPAL_BOOTSTRAP_VARIABLES:
  122. _drupal_bootstrap_variables();
  123. break;
  124.  
  125. case DRUPAL_BOOTSTRAP_SESSION:
  126. require_once DRUPAL_ROOT . '/' . variable_get('session_inc', 'includes/session.inc');
  127. drupal_session_initialize();
  128. break;
  129.  
  130. case DRUPAL_BOOTSTRAP_PAGE_HEADER:
  131. _drupal_bootstrap_page_header();
  132. break;
  133.  
  134. case DRUPAL_BOOTSTRAP_LANGUAGE:
  135. drupal_language_initialize();
  136. break;
  137.  
  138. case DRUPAL_BOOTSTRAP_FULL:
  139. require_once DRUPAL_ROOT . '/includes/common.inc';
  140. _drupal_bootstrap_full();
  141. break;
  142. }
  143. }
  144. }
  145. return $stored_phase;
  146. }

1. First Bootstrap Phase: Initialize Configuration
In phase one, settings.php is read from the sites/default folder and the most important global variables
are set, either directly from settings.php like $databases or by computing their value based on the server
environment. Here are three you will need in your daily site developer’s life:
$base_url: The base URL all your Drupal pages share. Each path is appended to it.
It must be a valid URL without a trailing slash. This only needs to be set if Drupal
does not determine it correctly.
$base_url = '<a href="http://www.example.com/drupal';">http://www.example.com/drupal';</a> // NO trailing slash!1
$base_path: The base URL’s path component (either ‘/’ or anything following the
domain part) with a trailing slash appended. It is derived from $base_url and can
be handy on its own.
$base_path = '/drupal/';
$base_root: Contains the protocol and domain parts of the URL. It is either the
base URL or derived from it by removing the base path if there is any.
$base_root = '<a href="http://www.example.com';">http://www.example.com';[/geshifilter-ruby]

2. Second Bootstrap Phase: Try to Serve a Cached Page
During the second bootstrap phase Drupal tries to deliver the whole page from its cache in case page
caching is enabled in the Performance section of the configuration interface and the visitor is not logged
in. If a cached version of the page can be found and is not expired, it is sent between invoking hook_boot() and hook_exit().
If the cache back end requires a database connection (determined by
$conf['page_cache_without_database'] in settings.php), the third and fourth bootstrap phases are
executed before fetching the cached page.
Debugging page caching is eased by an additional HTTP header the Drupal developers have
introduced for this purpose: if the page is actually served from cache, the X-Drupal-Cache HTTP header
is set to HIT; otherwise, its value is set to MISS

3.Third Bootstrap Phase: Initialize the Database Layer
The database abstraction layer is set up in this phase. Because there’s no need to open a connection yet,
only the base classes and utility functions (db_query, et al) are included. In addition, callbacks for
autoloading classes and interfaces are registered with the Standard PHP Library (SPL) autoload stack3.
The files containing classes and interfaces are declared by modules’ .info files or live in the main include
folder, and Drupal maintains a registry to track them. The first time a class or interface is needed during
execution, the callbacks use this registry to include the necessary file.

4. Fourth Bootstrap Phase: Initialize the Variable System
In the fourth bootstrap phase, Drupal fetches all values from the variable database table (which includes
both configuration settings and persistent variables) and merges them with those defined in
settings.php in the global variable $conf. Values set in the file via $conf['variable_name'] take
precedence over those stored in the database; in other words, you can prevent variables from being
overridden through the UI by defining them in the settings.php file.

The $conf variable takes the form of a giant associative array. Its values can be obtained by calling
variable_get('key_name', 'a default value'). Variables can be persisted by calling
variable_set('key_name', 'value') in your code

In addition to the variables, all modules required during bootstrap are loaded, implementing hooks
called during bootstrap, hook_boot(), hook_exit(), hook_language_init(), and hook_watchdog().

5.Fifth Bootstrap Phase: Initialize Session Handling
Things covered so far: database, settings, variables, some common Drupal functions and constants,
global variables, and bootstrap modules.
In this phase, Drupal registers its session handler and a session is associated for already
authenticated users. Usually, anonymous users won’t get a session at all unless something needs to be
stored in $_SESSION; for this case, a session ID is pregenerated. This allows HTTP proxy caching for
anonymous visitors. If Drupal can’t detect a logged in user, a dummy user object is created during this
phase that represents the anonymous visitor with a user ID 0 in Drupal’s database.
If you need fancier session handling than Drupal’s own database solution, you can include an
alternative by pointing $['conf']['session_inc'] in settings.php to a file containing the functions that
need to be implemented. Sites with many authenticated visitors can benefit from a more efficient
session storage.

6.Sixth Bootstrap Phase: Set up the Page Header
After all that nice setup, the first output is generated to be sent to the site’s visitor: the HTTP headers.
The default headers Drupal sends to the client only affect caching. To be precise: No byte is sent on its
way to the visitor because Drupal operates in output buffering mode. In other words, nothing leaves the
server until the buffer is flushed, which happens at the final stage of the cycle. Wait! Something happens
before: hook_boot() is invoked, giving modules a first opportunity to intervene in the page creation cycle.
Note that this hook must be disabled to support external caching mechanisms

7. Seventh Bootstrap Phase: Find out the Language of the Page
The next-to-last step in the bootstrap process deals with language selection for the current visitor if the
site is multilingual. Once the language is determined, implementations of hook_language_init() can
react, for instance by setting language dependent variables.

Language Negotiation Algorithms
Negotiation algorithms that determine which language to use can be provided by
hook_language_negotiation_info() and existing ones can be modified by
hook_language_negotiation_info_alter(). The providers will have a chance to determine a language in
the order they are set by the site admin at the language configuration page
(admin/config/regional/language/configure). Each defined negotiation algorithm must provide a
callback to determine the language. Language selection stops as soon as a provider returns a valid
language.
Here is an example of a language provider:

  1. $providers[LOCALE_LANGUAGE_NEGOTIATION_URL] = array(
  2. 'types' => array(LANGUAGE_TYPE_CONTENT, LANGUAGE_TYPE_INTERFACE, LANGUAGE_TYPE_URL),
  3. 'callbacks' => array(
  4. 'language' => 'locale_language_from_url',
  5. 'switcher' => 'locale_language_switcher_url',
  6. 'url_rewrite' => 'locale_language_url_rewrite_url',
  7. ),
  8. 'file' => $file,
  9. 'weight' => -8,
  10. 'name' => t('URL'),
  11. 'description' => t('Determine the language from the URL (Path prefix or domain).'),
  12. 'config' => 'admin/config/regional/language/configure/url',
  13. );

8. Final Bootstrap Phase: Load Modules and Initialize Theme
Finally drupal_bootstrap_full() is executed. All files containing the Drupal utility functions are
included. Enabled modules are loaded (in other words, the .module files are included). Modules get a
chance to register stream wrappers4 by means of hook_stream_wrappers() and modify existing ones with
hook_stream_wrapper_alter().The path in $_GET['q'] is normalized and the theme is initialized

Note: You can replace all or some of the original functions of Drupal’s menu system and path handling functions
by providing an alternative menu.inc or path.inc in settings.php. You might want to do this to improve the
performance of your Drupal site.

Tags:

Comments

https://canadianhpharmacy.com/ pharmacy canada plus
online pharmacies tech school https://canadianhpharmacy.com/
canadian pharmacies shipping to usa https://canadianhpharmacy.com/

That is very interesting, You are a very skilled blogger.

I have joined your rss feed and sit up for seeking more of your great post.

Also, I have shared your web site in my social networks

Here is my website; magazin pescuit

https://canadianlpharmacy.com/ how safe are canadian online pharmacies
canadian online pharmacies rated https://canadianlpharmacy.com/
canada online pharmacies medication https://canadianlpharmacy.com/

https://viagrawwithoutdoctor.com/ canada online pharmacies
canadian discount pharmacies in canada https://viagrawwithoutdoctor.com/
online pharmacies of canada https://viagrawwithoutdoctor.com/

https://viagracwithoutdoctor.com/ top rated canadian pharmacies online
canada medications cheap https://viagracwithoutdoctor.com/
canadian pharmacies stendra https://viagracwithoutdoctor.com/

After looking over a few of the blog posts on your

web site, I honestly appreciate your technique of blogging.

I added it to my bookmark site list and will be checking back

in the near future. Take a look at my web site as well and let me

know your opinion.

my website v bucks free generator

https://viagracwithoutdoctor.com/ aarp recommended canadian pharmacies
Canadian Pharmacy USA https://viagracwithoutdoctor.com/
canadian prescription drugstore https://viagracwithoutdoctor.com/

https://canadianlpharmacy.com/ safe canadian online pharmacies
good canadian online pharmacies https://canadianlpharmacy.com/
aarp recommended canadian pharmacies https://canadianlpharmacy.com/

Greetings from Colorado! I'm bored at work so I decided to check out your blog on my

iphone during lunch break. I really like the info you

provide here and can't wait to take a look when I get home.

I'm surprised at how fast your blog loaded on my

mobile .. I'm not even using WIFI, just 3G .. Anyhow, superb site!

Feel free to visit my blog post ... free vbucks

Hi! Someone in my Myspace group shared this site with us so I came to give it a look.

I'm definitely enjoying the information. I'm book-marking and will be

tweeting this to my followers! Wonderful blog and amazing design.

Here is my webpage v bucks free

https://canadianhpharmacy.com/ canadian pharmaceuticals nafta
canada online pharmacies https://canadianhpharmacy.com/
northwest pharmacies in canada https://canadianhpharmacy.com/

great publish, very informative. I wonder why the opposite specialists

of this sector do not realize this. You must continue your writing.

I'm sure, you have a great readers' base already!

Check out my web blog ... free vbucks

https://viagracwithoutdoctor.com/ pharmacy canada 24
legitimate canadian mail order pharmacies https://viagracwithoutdoctor.com/
drugstore online canada https://viagracwithoutdoctor.com/

https://canadianhpharmacy.com/ canada medication cost
canadian discount pharmacies in ocala fl https://canadianhpharmacy.com/
canadian medications 247 https://canadianhpharmacy.com/

https://canadianhpharmacy.com/ canada online pharmacies legitimate
pharmacy onesource https://canadianhpharmacy.com/
canadian medications 247 https://canadianhpharmacy.com/

https://canadianlpharmacy.com/ top rated canadian pharmacies online
prescriptions from canada without https://canadianlpharmacy.com/
canadian online pharmacies reviews https://canadianlpharmacy.com/

https://canadianhpharmacy.com/ cialis canadian pharmacy
good canadian online pharmacies https://canadianhpharmacy.com/
pharmacy onesource https://canadianhpharmacy.com/

https://canadianlpharmacy.com/ canada online pharmacies for men
canadian pharmacies without an rx https://canadianlpharmacy.com/
canadian mail order pharmacies https://canadianlpharmacy.com/

https://viagracwithoutdoctor.com/ canadian pharcharmy online24
canadian pharmacies that ship to us https://viagracwithoutdoctor.com/
canadianpharmacyusa24h https://viagracwithoutdoctor.com/

https://viagrawwithoutdoctor.com/ canadian pharmaceuticals
legitimate canadian mail order pharmacies https://viagrawwithoutdoctor.com/
pharmacy canada best https://viagrawwithoutdoctor.com/

It's remarkable designed for me to have a site, which is helpful

for my experience. thanks admin

Also visit my blog post: free the vbucks

https://canadianlpharmacy.com/ canadian pharmacy no prescription
canadian drug store https://canadianlpharmacy.com/
canadian pharmacy world https://canadianlpharmacy.com/

https://canadianhpharmacy.com/ legitimate canadian mail order pharmacies
canadian drug store https://canadianhpharmacy.com/
canada medication https://canadianhpharmacy.com/

I need to to thank you for this excellent read!! I certainly enjoyed

every little bit of it. I've got you saved as a favorite

to look at new stuff you post…

Here is my blog free vbucks

https://canadianlpharmacy.com/ canada online pharmacies reviews
are canadian online pharmacies safe https://canadianlpharmacy.com/
pharmacy canada reviews https://canadianlpharmacy.com/

https://canadianhpharmacy.com/ viagra canadiense
discount canadian pharmacies https://canadianhpharmacy.com/
canadian pharmacy viagra https://canadianhpharmacy.com/

Unquestionably believe that that you said. Your favorite justification seemed to be at the web the easiest thing to bear in mind of.

I say to you, I definitely get annoyed while other folks think about

concerns that they plainly don't recognize about. You managed

to hit the nail upon the top as smartly as outlined out the entire thing with no need

side effect , people could take a signal. Will likely be again to get more.

Thanks

My blog :: free the vbucks

https://canadianlpharmacy.com/ canadian pharmacy no prescription
best canadian pharmacy https://canadianlpharmacy.com/
canadianpharmacyusa24h https://canadianlpharmacy.com/

https://viagrawwithoutdoctor.com/ canadian mail order pharmacies
pharmacy canada online prescriptions https://viagrawwithoutdoctor.com/
canadian pharmaceuticals for usa sales https://viagrawwithoutdoctor.com/

hi!,I really like your writing very much! proportion we keep up a correspondence extra about your post on AOL?

I require a specialist on this area to resolve my problem.

Maybe that is you! Looking forward to look you.

My site: v bucks free

https://viagracwithoutdoctor.com/ canadian pharmacies shipping to usa
trusted pharmacy canada https://viagracwithoutdoctor.com/
online canadian discount pharmacies https://viagracwithoutdoctor.com/

Nice post. I learn something totally new and challenging on blogs I stumbleupon every day.

It's always interesting to read content from other authors and use

a little something from their web sites.

Here is my web page ... free v bucks

https://canadianlpharmacy.com/ online pharmacy canada
canadian medications by mail https://canadianlpharmacy.com/
canadian pharmacies that ship to us https://canadianlpharmacy.com/

Greetings from Florida! I'm bored to death at work so I decided

to check out your site on my iphone during lunch break.

I enjoy the knowledge you present here and can't wait to take a look when I get home.

I'm amazed at how quick your blog loaded on my phone ..

I'm not even using WIFI, just 3G .. Anyhow, awesome blog!

Feel free to visit my webpage; free vbucks

https://viagrawwithoutdoctor.com/ canadian drug
north west pharmacies canada https://viagrawwithoutdoctor.com/
aarp recommended canadian pharmacies https://viagrawwithoutdoctor.com/

I visited various blogs except the audio feature for audio songs existing at this web page is really

marvelous.

Also visit my web site; free the vbucks

Your method of describing everything in this article is in fact pleasant, all can without difficulty understand it, Thanks a lot.

My web-site; free the vbucks

Pages

Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.
2 + 6 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.