public function testConstruct() { $config = new Mage_Core_Model_Config_Base(<<<XML <?xml version="1.0"?> <root><key>value</key></root> XML ); $this->assertInstanceOf('Mage_Core_Model_Config_Element', $config->getNode('key')); }
public function loadModules(array $files) { $this->loadString('<config/>'); $loadedModules = array(); foreach ($files as $file) { $this->_magento2config->loadFile($file); $moduleName = $this->_magento2config->getNode('module')->getAttribute('name'); if ($this->isModuleLoaded($moduleName)) { $loadedModules[] = $moduleName; continue; } $loadedModules[] = $moduleName; $version = $this->_magento2config->getNode('module')->getAttribute('schema_version'); $modules = Mage::getConfig()->getNode('modules'); $child = $modules->addChild($moduleName); $child->addChild('active', 'true'); $child->addChild('codePool', 'community'); $child->addChild('version', $version); $this->loadString('<config/>'); } return $loadedModules; }
/** * Load dependencies for active & allowed modules into an array structure * * @param Mage_Core_Model_Config_Base $modulesConfig * @param array $allowedModules * @return array */ protected function _loadModuleDependencies(Mage_Core_Model_Config_Base $modulesConfig, array $allowedModules) { $result = array(); foreach ($modulesConfig->getNode('modules')->children() as $moduleName => $moduleNode) { $isModuleActive = 'true' === (string) $moduleNode->active; $isModuleAllowed = empty($allowedModules) || in_array($moduleName, $allowedModules); if (!$isModuleActive || !$isModuleAllowed) { continue; } $dependencies = array(); if ($moduleNode->depends) { /** @var $dependencyNode Varien_Simplexml_Element */ foreach ($moduleNode->depends->children() as $dependencyNode) { $dependencyModuleName = $dependencyNode->getName(); $dependencies[$dependencyModuleName] = $this->_getDependencyType($dependencyNode); } } $result[$moduleName] = array('module' => $moduleName, 'dependencies' => $dependencies); } return $result; }
/** * Returns node found by the $path and scope info * * @param string $path * @param string $scope * @param string|int $scopeCode * @return Mage_Core_Model_Config_Element */ public function getNode($path = null, $scope = '', $scopeCode = null) { if ($scope !== '') { if ('store' === $scope || 'website' === $scope) { $scope .= 's'; } if ('default' !== $scope && is_int($scopeCode)) { if ('stores' == $scope) { $scopeCode = Mage::app()->getStore($scopeCode)->getCode(); } elseif ('websites' == $scope) { $scopeCode = Mage::app()->getWebsite($scopeCode)->getCode(); } else { Mage::throwException(Mage::helper('Mage_Core_Helper_Data')->__('Unknown scope "%s".', $scope)); } } $path = $scope . ($scopeCode ? '/' . $scopeCode : '') . (empty($path) ? '' : '/' . $path); } /** * Check path cache loading */ if ($this->_useCache && $path !== null) { $path = explode('/', $path); $section = $path[0]; if (isset($this->_cacheSections[$section])) { $res = $this->getSectionNode($path); if ($res !== false) { return $res; } } } return parent::getNode($path); }
/** * Check base url settings, if not set it rises an exception * * @param Mage_Core_Model_Config_Base $original * @param Mage_Core_Model_Config_Base $test * @return EcomDev_PHPUnit_Model_Config * @throws RuntimeException */ protected function _checkBaseUrl($original, $test) { $baseUrlSecure = (string) $test->getNode(self::XML_PATH_SECURE_BASE_URL); $baseUrlUnsecure = (string) $test->getNode(self::XML_PATH_UNSECURE_BASE_URL); if (empty($baseUrlSecure) || empty($baseUrlUnsecure) || $baseUrlSecure == self::CHANGE_ME || $baseUrlUnsecure == self::CHANGE_ME) { throw new RuntimeException('The base url is not set for proper controller tests. ' . 'Please run ecomdev-phpunit.php with magento-config action.'); } }
/** * @param $type * @param Mage_Core_Model_Config_Base $moduleConfig * @return Mage_Core_Model_Config_Element */ protected function _getClassConfig($type, Mage_Core_Model_Config_Base $moduleConfig) { $xpath = "global/{$type}s/*[class]"; $classConfigs = $moduleConfig->getNode()->xpath($xpath); if ($classConfigs) { return $classConfigs[0]; } return false; }
protected function isRedisEnabled() { $fileConfig = new Mage_Core_Model_Config_Base(); $fileConfig->loadFile(Mage::getBaseDir('etc') . DS . 'modules' . DS . 'Cm_RedisSession.xml'); $isActive = $fileConfig->getNode('modules/Cm_RedisSession/active'); if (!$isActive || !in_array((string) $isActive, array('true', '1'))) { return false; } return true; }
public function mergexmlAction() { /*@davidselo: Action para entender como funciona el merge de los archivos XML*/ /*$first = new Mage_Core_Model_Config_Base; $first->loadString('<config> <one></one> <two></two> <three></three> </config>'); $second = new Mage_Core_Model_Config_Base; $second->loadString('<config> <four></four> <five></five> </config>'); $first->extend($second); echo $first->getNode()->asNiceXml();*/ /*SALIDA POR PANTALLA: * <config> * <one/> * <two/> * <three/> * <four/> * <five/> * </config> * * */ /* $first = new Mage_Core_Model_Config_Base; $first->loadString('<config> <one></one> <two></two> <three></three> </config>'); $second = new Mage_Core_Model_Config_Base; $second->loadString('<config> <one>Hello</one> <two>Goodby</two> </config>'); $first->extend($second); echo $first->getNode()->asNiceXml();*/ /*SALIDA POR PANTALLA: * <config> * <three/> * <one>Hello</one> * <two>Goodby</two> * </config>*/ /*$first = new Mage_Core_Model_Config_Base; $first->loadString('<config> <one> <two> <three></three> </two> </one> </config>'); $second = new Mage_Core_Model_Config_Base; $second->loadString('<config> <one> <two> <four></four> </two> </one> </config>'); $first->extend($second); echo $first->getNode()->asNiceXml(); */ $first = new Mage_Core_Model_Config_Base(); $first->loadString('<config> <one> <two> <three>Original Value</three> </two> </one> </config>'); $second = new Mage_Core_Model_Config_Base(); $second->loadString('<config> <one> <two> <four></four> <three>New Value</three> </two> </one> </config>'); $first->extend($second, false); echo $first->getNode()->asNiceXml(); }
/** * Makes all events to lower-case * * @param string $area * @param Mage_Core_Model_Config_Base $mergeModel */ protected function _makeEventsLowerCase($area, Mage_Core_Model_Config_Base $mergeModel) { $events = $mergeModel->getNode($area . "/" . Mage_Core_Model_App_Area::PART_EVENTS); if ($events !== false) { $children = clone $events->children(); /** @var Mage_Core_Model_Config_Element $event */ foreach ($children as $event) { if ($this->_isNodeNameHasUpperCase($event)) { $oldName = $event->getName(); $newEventName = strtolower($oldName); if (!isset($events->{$newEventName})) { /** @var Mage_Core_Model_Config_Element $newNode */ $newNode = $events->addChild($newEventName, $event); $newNode->extend($event); } unset($events->{$oldName}); } } } }
/** * Init fieldset fields * * @param Varien_Data_Form_Element_Fieldset $fieldset * @param Varien_Simplexml_Element $group * @param Varien_Simplexml_Element $section * @param string $fieldPrefix * @param string $labelPrefix * @return Soon_StockReleaser_Block_Adminhtml_System_Config_Form */ public function initFields($fieldset, $group, $section, $fieldPrefix = '', $labelPrefix = '') { if (!$group->is('use_custom_form', 1)) { return parent::initFields($fieldset, $group, $section, $fieldPrefix = '', $labelPrefix = ''); } if (!$this->_configDataObject) { $this->_initObjects(); } // Extends for config data $configDataAdditionalGroups = array(); $paymentMethods = Mage::helper('payment')->getPaymentMethods(); $xmlString = "<config><fields>"; $sort_order = 0; foreach ($paymentMethods as $code => $paymentMethod) { if (!isset($paymentMethod['active']) || $paymentMethod['active'] == 0) { continue; } ++$sort_order; $xmlString .= ' <' . $code . ' translate="label"> <label>' . $paymentMethod['title'] . '</label> <frontend_type>text</frontend_type> <sort_order>' . $sort_order . '</sort_order> <validate>validate-number</validate> <show_in_default>1</show_in_default> <show_in_website>1</show_in_website> <show_in_store>1</show_in_store> </' . $code . '>'; ++$sort_order; $xmlString .= ' <' . $code . '-unit translate="label"> <frontend_type>select</frontend_type> <source_model>stockreleaser/system_config_source_unit</source_model> <sort_order>' . $sort_order . '</sort_order> <show_in_default>1</show_in_default> <show_in_website>1</show_in_website> <show_in_store>1</show_in_store> </' . $code . '-unit>'; } $xmlString .= "</fields></config>"; $element = new Mage_Core_Model_Config_Base(); $element->loadString($xmlString); foreach ($element->getNode('fields') as $elements) { $elements = (array) $elements; // sort either by sort_order or by child node values bypassing the sort_order if ($group->sort_fields && $group->sort_fields->by) { $fieldset->setSortElementsByAttribute((string) $group->sort_fields->by, $group->sort_fields->direction_desc ? SORT_DESC : SORT_ASC); } else { usort($elements, array($this, '_sortForm')); } foreach ($elements as $e) { if (!$this->_canShowField($e)) { continue; } /** * Look for custom defined field path */ $path = (string) $e->config_path; if (empty($path)) { $path = $section->getName() . '/' . $group->getName() . '/' . $fieldPrefix . $e->getName(); } elseif (strrpos($path, '/') > 0) { // Extend config data with new section group $groupPath = substr($path, 0, strrpos($path, '/')); if (!isset($configDataAdditionalGroups[$groupPath])) { $this->_configData = $this->_configDataObject->extendConfig($groupPath, false, $this->_configData); $configDataAdditionalGroups[$groupPath] = true; } } $id = $section->getName() . '_' . $group->getName() . '_' . $fieldPrefix . $e->getName(); if (isset($this->_configData[$path])) { $data = $this->_configData[$path]; $inherit = false; } else { $data = $this->_configRoot->descend($path); $inherit = true; } if ($e->frontend_model) { $fieldRenderer = Mage::getBlockSingleton((string) $e->frontend_model); } else { $fieldRenderer = $this->_defaultFieldRenderer; } $fieldRenderer->setForm($this); $fieldRenderer->setConfigData($this->_configData); $helperName = $this->_configFields->getAttributeModule($section, $group, $e); $fieldType = (string) $e->frontend_type ? (string) $e->frontend_type : 'text'; $name = 'groups[' . $group->getName() . '][fields][' . $fieldPrefix . $e->getName() . '][value]'; $label = Mage::helper($helperName)->__($labelPrefix) . ' ' . Mage::helper($helperName)->__((string) $e->label); $hint = (string) $e->hint ? Mage::helper($helperName)->__((string) $e->hint) : ''; if ($e->backend_model) { $model = Mage::getModel((string) $e->backend_model); if (!$model instanceof Mage_Core_Model_Config_Data) { Mage::throwException('Invalid config field backend model: ' . (string) $e->backend_model); } $model->setPath($path)->setValue($data)->setWebsite($this->getWebsiteCode())->setStore($this->getStoreCode())->afterLoad(); $data = $model->getValue(); } $comment = $this->_prepareFieldComment($e, $helperName, $data); $tooltip = $this->_prepareFieldTooltip($e, $helperName); if ($e->depends) { foreach ($e->depends->children() as $dependent) { $dependentId = $section->getName() . '_' . $group->getName() . '_' . $fieldPrefix . $dependent->getName(); $shouldBeAddedDependence = true; $dependentValue = (string) $dependent; $dependentFieldName = $fieldPrefix . $dependent->getName(); $dependentField = $group->fields->{$dependentFieldName}; /* * If dependent field can't be shown in current scope and real dependent config value * is not equal to preferred one, then hide dependence fields by adding dependence * based on not shown field (not rendered field) */ if (!$this->_canShowField($dependentField)) { $dependentFullPath = $section->getName() . '/' . $group->getName() . '/' . $fieldPrefix . $dependent->getName(); $shouldBeAddedDependence = $dependentValue != Mage::getStoreConfig($dependentFullPath, $this->getStoreCode()); } if ($shouldBeAddedDependence) { $this->_getDependence()->addFieldMap($id, $id)->addFieldMap($dependentId, $dependentId)->addFieldDependence($id, $dependentId, $dependentValue); } } } $field = $fieldset->addField($id, $fieldType, array('name' => $name, 'label' => $label, 'comment' => $comment, 'tooltip' => $tooltip, 'hint' => $hint, 'value' => $data, 'inherit' => $inherit, 'class' => $e->frontend_class, 'field_config' => $e, 'scope' => $this->getScope(), 'scope_id' => $this->getScopeId(), 'scope_label' => $this->getScopeLabel($e), 'can_use_default_value' => $this->canUseDefaultValue((int) $e->show_in_default), 'can_use_website_value' => $this->canUseWebsiteValue((int) $e->show_in_website))); $this->_prepareFieldOriginalData($field, $e); if (isset($e->validate)) { $field->addClass($e->validate); } if (isset($e->frontend_type) && 'multiselect' === (string) $e->frontend_type && isset($e->can_be_empty)) { $field->setCanBeEmpty(true); } $field->setRenderer($fieldRenderer); if ($e->source_model) { // determine callback for the source model $factoryName = (string) $e->source_model; $method = false; if (preg_match('/^([^:]+?)::([^:]+?)$/', $factoryName, $matches)) { array_shift($matches); list($factoryName, $method) = array_values($matches); } $sourceModel = Mage::getSingleton($factoryName); if ($sourceModel instanceof Varien_Object) { $sourceModel->setPath($path); } if ($method) { if ($fieldType == 'multiselect') { $optionArray = $sourceModel->{$method}(); } else { $optionArray = array(); foreach ($sourceModel->{$method}() as $value => $label) { $optionArray[] = array('label' => $label, 'value' => $value); } } } else { $optionArray = $sourceModel->toOptionArray($fieldType == 'multiselect'); } $field->setValues($optionArray); } } } return $this; }
protected function _checkRewrites() { $this->section('Rewrites Information'); $types = array('models' => 'Models', 'blocks' => 'Blocks', 'helpers' => 'Helpers'); $this->output('/!\\ Only active modules are considered and core modules are ignored'); $this->output('The script will also try to find possible conflicts between classes', 4); $this->br(); $rewrites = array(); $modules = $this->_config->getNode('modules')->children(); foreach ($modules as $moduleName => $module) { if ($module->is('active') && $module->codePool != 'core') { $mergeModel = new Mage_Core_Model_Config_Base(); $configFile = $this->_config->getModuleDir('etc', $moduleName) . DS . 'config.xml'; if ($data = $mergeModel->loadFile($configFile)) { foreach ($types as $type => $label) { $list = $mergeModel->getNode()->global->{$type}; if ($list) { foreach ($list->children() as $key => $data) { if ($data->rewrite) { foreach ($data->rewrite->children() as $path => $value) { $rewrites[$type]["{$key}/{$path}"][$moduleName] = (string) $value; } } } } } } } } if (empty($rewrites)) { $this->output('No class rewrites found'); } else { foreach ($rewrites as $type => $list) { $this->output(sprintf('%s (%s)', $this->bold($types[$type]), count($list))); foreach ($list as $key => $modules) { if (count($modules) === 1) { $this->output($this->bold($key), 2); } else { $this->error(sprintf('%s (possible classes conflicts)', $this->bold($key)), 2); } foreach ($modules as $module => $class) { $this->start($this->pad($module, 40), 4); $this->output($class); } } $this->br(); } } return $this; }
/** * Overwrite bootstrap file with new XML data. * * @param Mage_Core_Model_Config_Base $moduleConfig * @return bool */ protected function _saveMigrationModuleConfig($moduleConfig) { /** * Overwrite the bootstrap file. */ return (bool) file_put_contents($this->_getMigrationModuleBootstrapFile(), $moduleConfig->getNode()->asNiceXml('', 0)); }
/** * Returns assets and handles * * @param Mage_Core_Model_Config_Base $xml * @param string $type * @param string $area * @param string $package * @param string $theme * @return array */ protected function _getAssetData(Mage_Core_Model_Config_Base $xml, $type, $area, $package, $theme) { $assets = array(); $handles = array(); /** @var $node Mage_Core_Model_Config_Element */ foreach ($xml->getNode($type)->children() as $node) { $assetName = $node->getName(); $assetMethod = $node->method ? (string) $node->method : 'default'; $assetType = $node->type ? (string) $node->type : 'skin'; $assetArea = $node->area ? (string) $node->area : 'frontend'; $assetPackage = $node->package ? (string) $node->package : 'base'; $assetTheme = $node->theme ? (string) $node->theme : 'default'; $assetIf = $node->if ? (string) $node->if : null; $assetParams = $node->params ? (string) $node->params : null; if ($type == self::TYPE_CSS) { $assetPath = $node->path ? rtrim((string) $node->path, '/') : 'css'; $assetFile = sprintf('/%s.css', $node->filename); $assetFileMin = sprintf('/%s.min.css', $node->filename); } else { if ($type == self::TYPE_JS) { $assetPath = $node->path ? rtrim((string) $node->path, '/') : 'css'; $assetFile = sprintf('/%s.js', $node->filename); $assetFileMin = sprintf('/%s.min.js', $node->filename); } else { continue; } } if ($assetType != 'skin') { continue; } if ($assetArea != $area || $assetPackage != $package || $assetTheme != $theme) { continue; } if (!$node->handles || !$node->files) { continue; } $removeList = array(); /** @var $fileNode Mage_Core_Model_Config_Element */ foreach ($node->files->children() as $fileNode) { if (!$fileNode->remove) { continue; } $sourceName = $fileNode->getName(); $attributes = $fileNode->remove->attributes(); if (empty($attributes['type']) || empty($attributes['file'])) { continue; } $removeList[$sourceName] = array((string) $attributes['type'], (string) $attributes['file']); } /** @var $handle Mage_Core_Model_Config_Element */ foreach ($node->handles->children() as $handle) { $handles[$handle->getName()][] = $assetName; } $assets[$assetName] = array('file' => $assetPath . $assetFile, 'file_min' => $assetPath . $assetFileMin, 'if' => $assetIf, 'params' => $assetParams, 'remove' => $removeList, 'behavior' => $assetMethod); } return array($assets, $handles); }
/** * Check base url settings, if not set it rises an exception * * @param Mage_Core_Model_Config_Base $original * @param Mage_Core_Model_Config_Base $test * @return Mage_Test_Model_Config * @throws RuntimeException */ protected function _checkBaseUrl($original, $test) { $baseUrlSecure = (string) $test->getNode(self::XML_PATH_SECURE_BASE_URL); $baseUrlUnsecure = (string) $test->getNode(self::XML_PATH_UNSECURE_BASE_URL); if (empty($baseUrlSecure) || empty($baseUrlUnsecure) || $baseUrlSecure == self::CHANGE_ME || $baseUrlUnsecure == self::CHANGE_ME) { echo sprintf('Please change values in %s file for nodes %s and %s. ' . 'It will help in setting up proper controller test cases', 'app/etc/local.xml.phpunit', self::XML_PATH_SECURE_BASE_URL, self::XML_PATH_UNSECURE_BASE_URL); exit; } }
public function isModuleEnabled($module) { $fileConfig = new Mage_Core_Model_Config_Base(); $fileConfig->loadFile(Mage::getBaseDir('etc') . DS . 'modules' . DS . $module . '.xml'); $isActive = $fileConfig->getNode('modules/' . $module . '/active'); if (!$isActive || !in_array((string) $isActive, array('true', '1'))) { return false; } return true; }
public function mergeConfig($mergeToObject, $extensions) { foreach ($extensions as $extension) { if ($extension) { $mergeModel = new Mage_Core_Model_Config_Base(); if ($mergeModel->loadString($extension)) { $mergeToObject->extend($mergeModel->getNode(), true); } } } return $mergeToObject; }
/** * Returns node found by the $path and scope info * * @param string $path * @param string $scope * @param string $scopeCode * @return Mage_Core_Model_Config_Element */ public function getNode($path = null, $scope = '', $scopeCode = null) { if (!empty($scope)) { if ('store' === $scope || 'website' === $scope) { $scope .= 's'; } if ('default' !== $scope && is_int($scopeCode)) { if ('stores' == $scope) { $scopeCode = Mage::app()->getStore($scopeCode)->getCode(); } elseif ('websites' == $scope) { $scopeCode = Mage::app()->getWebsite($scopeCode)->getCode(); } else { Mage::throwException(Mage::helper('core')->__('Unknown scope "%s"', $scope)); } } $path = $scope . ($scopeCode ? '/' . $scopeCode : '') . (empty($path) ? '' : '/' . $path); } return parent::getNode($path); }
/** * Load declared modules configuration * * @param null $mergeConfig depricated * @return Mage_Core_Model_Config */ protected function _loadDeclaredModules($mergeConfig = null) { $moduleFiles = $this->_getDeclaredModuleFiles(); if (!$moduleFiles) { return; } Varien_Profiler::start('config/load-modules-declaration'); $unsortedConfig = new Mage_Core_Model_Config_Base(); $unsortedConfig->loadString('<config/>'); $fileConfig = new Mage_Core_Model_Config_Base(); // load modules declarations foreach ($moduleFiles as $file) { $fileConfig->loadFile($file); $unsortedConfig->extend($fileConfig); } $moduleDepends = array(); foreach ($unsortedConfig->getNode('modules')->children() as $moduleName => $moduleNode) { if (!$this->_isAllowedModule($moduleName)) { continue; } $depends = array(); if ($moduleNode->depends) { foreach ($moduleNode->depends->children() as $depend) { $depends[$depend->getName()] = true; } } $moduleDepends[$moduleName] = array('module' => $moduleName, 'depends' => $depends, 'active' => 'true' === (string) $moduleNode->active ? true : false); } // check and sort module dependence $moduleDepends = $this->_sortModuleDepends($moduleDepends); // create sorted config $sortedConfig = new Mage_Core_Model_Config_Base(); $sortedConfig->loadString('<config><modules/></config>'); foreach ($unsortedConfig->getNode()->children() as $nodeName => $node) { if ($nodeName != 'modules') { $sortedConfig->getNode()->appendChild($node); } } foreach ($moduleDepends as $moduleProp) { $node = $unsortedConfig->getNode('modules/' . $moduleProp['module']); $sortedConfig->getNode('modules')->appendChild($node); } $this->extend($sortedConfig); Varien_Profiler::stop('config/load-modules-declaration'); return $this; }
/** * Returns node found by the $path and scope info * * @param string $path * @param string $scope * @param string|int $scopeCode * @return Mage_Core_Model_Config_Element */ public function getNode($path = null, $scope = '', $scopeCode = null) { $path = $this->_getPathInScope($path, $scope, $scopeCode); /** * Check path cache loading */ if ($this->_useCache && $path !== null) { $path = explode('/', $path); $section = $path[0]; if (isset($this->_cacheSections[$section])) { $res = $this->getSectionNode($path); if ($res !== false) { return $res; } } } return parent::getNode($path); }