Grav - Lifecycle

In this chapter, we will understand the lifecycle of Grav. Grav Lifecycle determines how Grav processes in order to extend the Grav via Plugins. The following diagram shows the flow of Grav lifecycle process.

Grav Lifecycle

The following four steps from the Grav lifecycle −

  • PHP Version

  • Loader class is initialized

  • Obtain Grav Instance

  • Call Grav Process

PHP Version

It checks the version of PHP to make sure that we are running the PHP version above 5.4.0.

Loader class is initialized

In the second step, the class loader gets initialized.

Obtain Grav Instance

  • If no instance exists, then it calls the load() method and adds Grav.

  • It initializes the debugger value and adds to the debugger.

  • It registers the log and the error handler.

  • It adds the uri, task, events, cache, session, plugins, themes, twig, taxonomy, language, pages, assets and base url.

  • It registers the stream and the config handler.

Call Grav Process

  • It initializes the configuration, Uri object, error handler, debugger and session.

  • After initializing, it starts buffering the output.

  • It initializes the timezone and the plugins and fires the onPluginsInitialized event.

  • Next it initializes the theme and fires the onThemeInitialized and onTask[TASK] events.

  • It initializes the assets and fires the onAssetsInitialized event.

  • It initializes the twig with the following actions −

    • Based on the configuration, it sets the twig template paths.

    • Handles the available language templates.

    • Next it fires the onTwigTemplatePaths event.

    • Loader chain and twig configuration is loaded.

    • Fires onTwigInitialized event.

    • It loads the twig extensions and fires onTwigExtensions event.

    • It sets the standard twig variables.

  • Next it initializes the pages.

    • It calls the buildPages() method.

    • If cache is good, then it loads pages from cache. If cache is not good then the recurse() method is called.

    • onBuildPagesInitialized event is fired in the recurse() method.

    • If a file is found as .md, the following actions are performed −

      • To load the file details, the init() method is called.

      • It sets the filePath, modified and id.

      • The header() method is called to initialize header variables.

      • The slug() method is called to set the URL slug.

      • The visible() method is called to set the visible state.

      • Based on the folder that starts with _(underscore), modularTwig() is set.

    • It later fires the onPageProcessed event.

    • recurse() the children if a folder is found.

    • It fires the onFolderProcessed event.

    • calls the buildRoutes() method.

    • For all pages the taxonomy is initialized

    • The route table is built for fast lookup.

  • The events onPagesInitialized and onPageInitialized gets fired.

  • Debugger CSS/JS is added to the assets.

  • Using Twig's processSite() method, we get the output.

    • The event onTwigSiteVariables is fired.

    • Gets the output of the page.

    • When page is not found or not routable then the event onPageNotFound is fired.

    • All Twig variables are set on twig object.

    • Template name is set depending upon the file/header/extension information.

    • render() method is called.

    • Returns the file format in HTML

  • It fires the onOutputGenerated event.

  • Set the HTTP headers.

  • Displays the output

  • The output buffer is flushed to the page.

  • The event onOutputRendered will get fire.

  • Closes the connection to client.

  • Lastly, it fires the onShutDown event.

When the content() method is called on page, then the following lifecycle occurs.

  • The event onPageContentRaw will get fire.

  • According to the Markdown and Twig settings, it processes the page.

  • It fires the onPageContentProcessed event.