Grav Lifecycle
It is often useful to know how Grav processes in order to fully understand how best to extend Grav via plugins. This is the Grav lifecycle:
index.php
- Check PHP version to ensure we're running at least version 5.5.9
- Class loader initialization
- Obtain Grav instances
Grav.php
- No instance exists, so call
load()
- Add
grav
- Initialize the debugger and add it to
debugger
- Register the
log
handler
- Register the
error
handler
- Add
uri
- Add
task
- Add
events
- Add
cache
- Add
session
- Add
plugins
- Add
themes
- Add
twig
- Add
taxonomy
- Add
language
- Add
pages
- Add
assets
- Add
page
- Add
output
- Add
browser
- Add
base_url_absolute
- Add
base_url_relative
- Add
base_url
- Register the
stream
handler
- Register the
config
handler
- call
Grav::process()
Grav.php
- Initialize the configuration
- Initialize the Session
- Initialize the Uri object
- Initialize the error handler
- Initialize the debugger
- Start output buffering
- Initialize the timezone
- Initialize the
plugins
- Fire onPluginsInitialized event
- Initialize the theme
- Fire onThemeInitialized event
- Fire onTask[TASK] event
- Initialize
assets
- Fire onAssetsInitialized event
- Initialize
twig
Twig.php
- Set Twig template paths based on configuration
- Handle language templates if available
- Fire onTwigTemplatePaths event
- Load Twig configuration and loader chain
- Fire onTwigInitialized event
- Load Twig extensions
- Fire onTwigExtensions event
- Set standard Twig variables (config, uri, taxonomy, assets, browser, etc)
- Initialize
pages
Pages.php
- Call
buildPages()
- Check if cache is good
- If cache is good load pages date from cache
- If cache is not good call
recurse()
- Fire onBuildPagesInitialized event in
recurse()
- If a
.md
file is found:
Page.php
- Call
init()
to load the file details
- Set the
filePath
, modified
, id
- Call
header()
to initialize the header variables
- Call
slug()
to set the URL slug
- Call
visible()
to set visible state
- Set
modularTwig()
status based on if folder starts with _
- Fire onPageProcessed event
- If a
folder
found recurse()
the children
- Fire onFolderProcessed event
- Call
buildRoutes()
- Initialize
taxonomy
for all pages
- Build
route
table for fast lookup
- Fire onPagesInitialized event
- Fire onPageInitialized event
- Add the debugger CSS/JS to the assets
- Get Output with Twig's
processSite()
method
Twig.php
- Fire onTwigSiteVariables event
- Get the page output
- Fire onTwigPageVariables, also called for each modular subpage
- If a page is not found or not routable, first fire the onPageFallBackUrl event to see if we have a fallback for a media asset and then fire onPageNotFound if not
- Set all Twig variables on the Twig object
- Set the template name based on file/header/extension information
- Call
render()
method
- Return resulting HTML
- Fire onOutputGenerated event
- Set the HTTP headers
- Echo the output
- Flush the output buffers to the page
- Fire onOutputRendered event
- Connection to client is closed
- Fire onShutdown event
Whenever a page has its content()
method called, the following lifecycle occurs:
Page.php
- If content is NOT cached:
- Fire onPageContentRaw event
- Process the page according to Markdown and Twig settings. Fire onMarkdownInitialized event
- Fire onPageContentProcessed event
- Fire onPageContent event