/
Runtime context switching for url building [5.2.2-B2]

Runtime context switching for url building [5.2.2-B2]

The runtime context is state, that is calculated once per page, which affects:

  • URL generation logic for given TPL file
  • location of TPL files used to display page content for given URL

Known runtime contexts are:

Runtime ContextSetting NameConfigured From
Front-EndIs Front-End?/index.php
Theme NameURL
Mod-Rewrite UrlsSystem Setting
Admin ConsoleIs Admin Console?/admin/index.php

Typical URL examples:

URLWhere
http://www.website.tld/index.php?env=-platform%2Flogin%2Fregister%3Am0-1-1-1-s-
to the "platform/login/register" template in "advanced" theme on Front-End when Mod-Rewrite is disabled
http://www.website.tld/platform/login/register.html
to the "platform/login/register" template in "advanced" theme on Front-End when Mod-Rewrite is enabled
http://www.website.tld/admin/index.php?env=-languages%2Fphrase_list%3Am0--1--r-
to the "languages/phrase_list" template in Admin Console

If a need arises to build url to Admin Console from Front-End or vice versa then this can be done, to the certain extent, using this code:

While in Admin Console Only
// URL to Front-End page:
$url = $this->Application->HREF(
	'path/to/template',
	'_FRONT_END_', // To remove "/admin" from URL.
	array('__MOD_REWRITE__' => 1) // Mod-rewrite not used in Admin Console, so needs to be specified.
);
 
// Path to Front-End template from "advanced" theme:
$this->Application->InitParser('advanced');
$path1 = $this->Application->TemplatesCache->GetRealFilename('path/to/template1');
$path2 = $this->Application->TemplatesCache->GetRealFilename('path/to/template2');
 
// Path to Front-End template from given theme:
$this->Application->InitParser(true);
$path1 = $this->Application->TemplatesCache->GetRealFilename('theme:advanced/path/to/template1');
$path2 = $this->Application->TemplatesCache->GetRealFilename('theme:simple/path/to/template2');

Above examples demonstrate, that:

  • it's hard to use because the need of deep system understanding
  • some stuff needs be hardcoded for it to work

It can't be seen from code, but there are couple of other issues with it:

  • works only when building Front-End url from Admin Console, but not other way around
  • the built URL in edge cases will different from same URLs built on Front-End directly (e.g. impossible to built url using "use_section" parameter)

Solution

Allow to replace all variables (e.g. runtime context) that are imposed by the way how page was opened at once.

// Use Admin context.
$backup = $this->Application->applyRuntimeContext(new AdminRuntimeContext());
// do something
$this->Application->applyRuntimeContext($backup);
 
// Use Front-End context from current theme.
$backup = $this->Application->applyRuntimeContext(new FrontEndRuntimeContext());
// do something
$this->Application->applyRuntimeContext($backup);
 
// Use Front-End context from primary theme.
$backup = $this->Application->applyRuntimeContext(new FrontEndRuntimeContext('default'));
// do something
$this->Application->applyRuntimeContext($backup);

// Use Front-End context from given theme.
$backup = $this->Application->applyRuntimeContext(new FrontEndRuntimeContext(5));
// do something
$this->Application->applyRuntimeContext($backup);

Plan:

  1. create "kApplication::themePath" public property and use it instead of THEMES_PATH constant (keep constant for BC)
  2. create "kApplication::modRewrite" public property and use it instead of MOD_REWRITE constant (keep constant for BC)
  3. add "FrontEndRuntimeContext" class, that will be able to calculate values for "isAdmin", "themePath" and "modRewrite" application properties for a given theme
  4. add "AdminRuntimeContext" class, that will be able to calculate values for "isAdmin", "themePath" and "modRewrite" application properties for Admin Console
  5. change "kUrlManager::LoadStructureTemplateMapping" method to allow to be called several times in a row
  6. extract configuration code of "TemplatesCache" class, that depends on values changed by runtime context into separate "configure" method
  7. create "kApplication::applyRuntimeContext(AbstractRuntimeContext $runtime_context)" method, that will:
    1. set values of kApplication properties from matching properties of runtime context
    2. initialize "theme.current" object (front-end only)
    3. set "m_theme" request variable
    4. call "TemplatesCache::configure" method
    5. call "kUrlManager::LoadStructureTemplateMapping" method
    6. return runtime context that was used prior to this method was called

Related Tasks