Changeset View
Changeset View
Standalone View
Standalone View
branches/5.3.x/core/kernel/utility/unit_config_reader.php
Show First 20 Lines • Show All 152 Lines • ▼ Show 20 Line(s) | |||||
$this->Application->Debugger->appendHTML('UnitConfigReader: Generating Cache'); | $this->Application->Debugger->appendHTML('UnitConfigReader: Generating Cache'); | ||||
} | } | ||||
// === lines below would only executed on cold start (no unit config cache) === | // === lines below would only executed on cold start (no unit config cache) === | ||||
// no cache found -> include all unit configs to create it ! | // no cache found -> include all unit configs to create it ! | ||||
$this->includeConfigFiles($folder_path, $cache); | $this->includeConfigFiles($folder_path, $cache); | ||||
$this->parseConfigs(); | $this->parseConfigs(); | ||||
$this->sortRouters(); | |||||
// tell AfterConfigRead to store cache if needed | // tell AfterConfigRead to store cache if needed | ||||
// can't store it here because AfterConfigRead needs ability to change config data | // can't store it here because AfterConfigRead needs ability to change config data | ||||
$this->storeCache = $cache; | $this->storeCache = $cache; | ||||
if ( !$this->Application->InitDone ) { | if ( !$this->Application->InitDone ) { | ||||
// scanModules is called multiple times during installation process | // scanModules is called multiple times during installation process | ||||
$this->Application->InitManagers(); | $this->Application->InitManagers(); | ||||
// get build-in rewrite listeners ONLY to be able to parse mod-rewrite url when unit config cache is missing | |||||
$this->retrieveCollections(); | |||||
$this->sortRewriteListeners(); | |||||
} | } | ||||
$this->Application->cacheManager->applyDelayedUnitProcessing(); | $this->Application->cacheManager->applyDelayedUnitProcessing(); | ||||
} | } | ||||
/** | /** | ||||
* Locates (recursively) and reads all unit configs at given path. | * Locates (recursively) and reads all unit configs at given path. | ||||
* | * | ||||
▲ Show 20 Lines • Show All 174 Lines • ▼ Show 20 Line(s) | |||||
if ( $store_cache || (defined('IS_INSTALL') && IS_INSTALL) ) { | if ( $store_cache || (defined('IS_INSTALL') && IS_INSTALL) ) { | ||||
// cache is not stored during install, but dynamic clones should be processed in any case | // cache is not stored during install, but dynamic clones should be processed in any case | ||||
$this->cloner->processDynamicallyAdded(); | $this->cloner->processDynamicallyAdded(); | ||||
$this->retrieveCollections(); | $this->retrieveCollections(); | ||||
} | } | ||||
if ( $store_cache ) { | if ( $store_cache ) { | ||||
$this->sortRewriteListeners(); | |||||
$this->Application->HandleEvent(new kEvent('adm:OnAfterCacheRebuild')); | $this->Application->HandleEvent(new kEvent('adm:OnAfterCacheRebuild')); | ||||
$this->Application->cacheManager->UpdateUnitCache(); | $this->Application->cacheManager->UpdateUnitCache(); | ||||
if ( defined('DEBUG_MODE') && DEBUG_MODE && defined('DBG_VALIDATE_CONFIGS') && DBG_VALIDATE_CONFIGS ) { | if ( defined('DEBUG_MODE') && DEBUG_MODE && defined('DBG_VALIDATE_CONFIGS') && DBG_VALIDATE_CONFIGS ) { | ||||
// validate configs here to have changes from OnAfterConfigRead hooks to prefixes | // validate configs here to have changes from OnAfterConfigRead hooks to prefixes | ||||
foreach ( $this->configData as $config ) { | foreach ( $this->configData as $config ) { | ||||
if ( !$config->getTableName() ) { | if ( !$config->getTableName() ) { | ||||
continue; | continue; | ||||
} | } | ||||
$config->validate(); | $config->validate(); | ||||
} | } | ||||
} | } | ||||
} | } | ||||
} | } | ||||
/** | /** | ||||
* Sort rewrite listeners according to RewritePriority (non-prioritized listeners goes first) | * Sort routers according to their weight (non-prioritized routers goes first). | ||||
* | * | ||||
* @return void | * @return void | ||||
*/ | */ | ||||
protected function sortRewriteListeners() | protected function sortRouters() | ||||
{ | { | ||||
$listeners = array(); | $sorted_routers = array(); | ||||
$prioritized_listeners = array(); | $prioritized_routers = array(); | ||||
$routers = $this->collectRouters(); | |||||
// process non-prioritized listeners | |||||
foreach ( $this->Application->RewriteListeners as $prefix => $listener_data ) { | // Process non-prioritized routers. | ||||
if ( $listener_data['priority'] === false ) { | foreach ( $routers as $prefix => $router_data ) { | ||||
$listeners[$prefix] = $listener_data; | if ( $router_data['priority'] === false ) { | ||||
$sorted_routers[$prefix] = $router_data; | |||||
} | } | ||||
else { | else { | ||||
$prioritized_listeners[$prefix] = $listener_data['priority']; | $prioritized_routers[$prefix] = $router_data['priority']; | ||||
} | |||||
} | |||||
// Process prioritized routers. | |||||
asort($prioritized_routers, SORT_NUMERIC); | |||||
foreach ( $prioritized_routers as $prefix => $priority ) { | |||||
$sorted_routers[$prefix] = $routers[$prefix]; | |||||
} | } | ||||
$this->Application->routers = $sorted_routers; | |||||
} | } | ||||
// process prioritized listeners | /** | ||||
asort($prioritized_listeners, SORT_NUMERIC); | * Collects routers. | ||||
* | |||||
* @return array | |||||
*/ | |||||
protected function collectRouters() | |||||
{ | |||||
$routers = array(); | |||||
$router_classes = $this->Application->getSubClasses('AbstractRouter'); | |||||
foreach ( $router_classes as $router_class ) { | |||||
if ( !class_exists($router_class) ) { | |||||
// This can happen, when: | |||||
// - router class (coming from cache) was renamed; | |||||
// - new cache is built based on outdated class map. | |||||
continue; | |||||
} | |||||
foreach ( $prioritized_listeners as $prefix => $priority ) { | /** @var AbstractRouter $router */ | ||||
$listeners[$prefix] = $this->Application->RewriteListeners[$prefix]; | $router = new $router_class(); | ||||
$routers[$router->getPrefix()] = array( | |||||
'class' => $router_class, | |||||
'priority' => $router->getWeight(), | |||||
); | |||||
} | } | ||||
$this->Application->RewriteListeners = $listeners; | return $routers; | ||||
} | } | ||||
/** | /** | ||||
* Re-reads all configs. | * Re-reads all configs. | ||||
* | * | ||||
* @return void | * @return void | ||||
*/ | */ | ||||
public function ReReadConfigs() | public function ReReadConfigs() | ||||
{ | { | ||||
// don't reset prefix file, since file scanning could slow down the process | // don't reset prefix file, since file scanning could slow down the process | ||||
$prefix_files_backup = $this->prefixFiles; | $prefix_files_backup = $this->prefixFiles; | ||||
$this->Application->cacheManager->EmptyUnitCache(); | $this->Application->cacheManager->EmptyUnitCache(); | ||||
$this->prefixFiles = $prefix_files_backup; | $this->prefixFiles = $prefix_files_backup; | ||||
// parse all configs | // parse all configs | ||||
$this->afterConfigProcessed = array(); | $this->afterConfigProcessed = array(); | ||||
$this->includeConfigFiles(MODULES_PATH, false); | $this->includeConfigFiles(MODULES_PATH, false); | ||||
$this->parseConfigs(); | $this->parseConfigs(); | ||||
$this->sortRouters(); | |||||
$this->AfterConfigRead(false); | $this->AfterConfigRead(false); | ||||
$this->cloner->processDynamicallyAdded(); | $this->cloner->processDynamicallyAdded(); | ||||
$this->retrieveCollections(); | $this->retrieveCollections(); | ||||
} | } | ||||
/** | /** | ||||
* Process all collectible unit config options here to also catch ones, defined from OnAfterConfigRead events | * Process all collectible unit config options here to also catch ones, defined from OnAfterConfigRead events | ||||
* | * | ||||
*/ | */ | ||||
protected function retrieveCollections() | protected function retrieveCollections() | ||||
{ | { | ||||
foreach ( $this->configData as $prefix => $config ) { | foreach ( $this->configData as $prefix => $config ) { | ||||
// collect replacement templates | // collect replacement templates | ||||
if ( $config->getReplacementTemplates() ) { | if ( $config->getReplacementTemplates() ) { | ||||
$this->Application->ReplacementTemplates = array_merge($this->Application->ReplacementTemplates, $config->getReplacementTemplates()); | $this->Application->ReplacementTemplates = array_merge($this->Application->ReplacementTemplates, $config->getReplacementTemplates()); | ||||
} | } | ||||
// collect rewrite listeners | |||||
if ( $config->getRewriteListener() ) { | |||||
$rewrite_listeners = $config->getRewriteListener(); | |||||
if ( !is_array($rewrite_listeners) ) { | |||||
// when one method is used to build and parse url | |||||
$rewrite_listeners = array($rewrite_listeners, $rewrite_listeners); | |||||
} | |||||
foreach ( $rewrite_listeners as $index => $rewrite_listener ) { | |||||
if ( strpos($rewrite_listener, ':') === false ) { | |||||
$rewrite_listeners[$index] = $prefix . '_EventHandler:' . $rewrite_listener; | |||||
} | |||||
} | |||||
$rewrite_priority = $config->getRewritePriority(); | |||||
$this->Application->RewriteListeners[$prefix] = array('listener' => $rewrite_listeners, 'priority' => $rewrite_priority); | |||||
} | |||||
} | } | ||||
} | } | ||||
public function loadConfig($prefix) | public function loadConfig($prefix) | ||||
{ | { | ||||
$preloaded_prefix = $this->PreloadConfigFile($this->getPrefixFile($prefix)); | $preloaded_prefix = $this->PreloadConfigFile($this->getPrefixFile($prefix)); | ||||
if ( $this->finalStage ) { | if ( $this->finalStage ) { | ||||
▲ Show 20 Lines • Show All 244 Lines • ▼ Show 20 Line(s) | |||||
{ | { | ||||
if ( !isset($this->prefixFiles[$prefix]) ) { | if ( !isset($this->prefixFiles[$prefix]) ) { | ||||
throw new Exception('Configuration file for prefix "<strong>' . $prefix . '</strong>" is unknown'); | throw new Exception('Configuration file for prefix "<strong>' . $prefix . '</strong>" is unknown'); | ||||
} | } | ||||
return $this->prefixFiles[$prefix]; | return $this->prefixFiles[$prefix]; | ||||
} | } | ||||
} | } | ||||
No newline at end of file |