Page MenuHomeIn-Portal Phabricator

unit_config_cloner.php
No OneTemporary

File Metadata

Created
Sat, Sep 27, 10:04 PM

unit_config_cloner.php

<?php
/**
* @version $Id: unit_config_cloner.php 16252 2015-09-27 06:34:45Z alex $
* @package In-Portal
* @copyright Copyright (C) 1997 - 2013 Intechnic. All rights reserved.
* @license GNU/GPL
* In-Portal is Open Source software.
* This means that this software may have been modified pursuant
* the GNU General Public License, and as distributed it includes
* or is derivative of works licensed under the GNU General Public License
* or other free or open source software licenses.
* See http://www.in-portal.org/license for copyright notices and details.
*/
defined('FULL_PATH') or die('restricted access!');
class kUnitConfigCloner extends kBase implements kiCacheable
{
/**
* Unit config reader.
*
* @var kUnitConfigReader
*/
protected $reader;
/**
* Cloned unit config difference compared to original unit config.
*
* @var array
*/
protected $clones = array();
/**
* Creates cloner.
*/
public function __construct(kUnitConfigReader $reader)
{
parent::__construct();
$this->reader = $reader;
}
/**
* Sets data from cache to object
*
* @param Array $data
*/
public function setFromCache(&$data)
{
$this->clones = $data['ConfigCloner.clones'];
}
/**
* Gets object data for caching
*
* @return Array
*/
public function getToCache()
{
return array(
'ConfigCloner.clones' => $this->clones,
);
}
/**
* Creates unit configs, based on 'Clones' option.
*
* @param string $prefix Unit config prefix.
* @param boolean $with_cached Process also cached clones.
*
* @return array
*/
public function extrude($prefix, $with_cached = true)
{
$main_config = $this->reader->getUnitConfig($prefix);
$sub_configs = $main_config->getSetting('Clones', array());
if ( $with_cached && isset($this->clones[$prefix]) ) {
$sub_configs = array_merge($sub_configs, $this->clones[$prefix]);
}
if ( !$sub_configs ) {
return array();
}
$processed = array();
$main_config->setSetting('Clones', null);
unset($this->clones[$prefix]);
if ( !isset($this->clones[$prefix]) ) {
$this->clones[$prefix] = array();
}
foreach ( $sub_configs as $sub_prefix => $sub_config_data ) {
$this->clones[$prefix][$sub_prefix] = $sub_config_data;
$sub_config_data['Prefix'] = $sub_prefix;
$sub_config = $this->prepareClone($main_config, $sub_config_data);
$this->reader->add($sub_config, $this->reader->getPrefixFile($prefix));
array_push($processed, $sub_prefix);
}
if ( !$prefix ) {
// configs, that used only for cloning & not used itself
$this->reader->remove($prefix);
}
return array_unique($processed);
}
/**
* Prepares data for of cloned unit config.
*
* @param kUnitConfig $main_config Main unit config.
* @param array $sub_config_data Sub config data.
*
* @return kUnitConfig
*/
protected function prepareClone(kUnitConfig $main_config, $sub_config_data)
{
$ret = kUtil::array_merge_recursive($main_config->getRaw(), $sub_config_data);
foreach ($sub_config_data as $key => $value) {
if ( !$value && is_array($value) ) {
unset($ret[$key]);
}
}
return new kUnitConfig($sub_config_data['Prefix'], $ret);
}
/**
* Creates clones (where needed) and parses them.
*
* @param array $prefixes Unit config prefixes.
*
* @return array
*/
public function extrudeAndParse(array $prefixes)
{
$cloned_prefixes = array();
foreach ( $prefixes as $prefix ) {
$cloned_prefixes = array_merge($cloned_prefixes, $this->extrude($prefix));
}
foreach ( $cloned_prefixes as $cloned_prefix ) {
$this->reader->getUnitConfig($cloned_prefix)->parse();
}
return array_unique($cloned_prefixes);
}
/**
* Process clones, that were defined via OnAfterConfigRead event
*
* @return void
*/
public function processDynamicallyAdded()
{
$new_clones = $this->extrudeAndParse($this->reader->getPrefixes());
// execute delayed methods for cloned unit configs
$this->Application->cacheManager->applyDelayedUnitProcessing();
// call OnAfterConfigRead for cloned configs
foreach ( $new_clones as $prefix ) {
$this->reader->runAfterConfigRead($prefix);
}
}
}

Event Timeline