00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035 class Varien_Convert_Profile_Collection
00036 {
00037 protected $_xml;
00038 protected $_containers;
00039 protected $_profiles = array();
00040
00041 protected $_simplexmlDefaultClass = 'Varien_Simplexml_Element';
00042 protected $_profileDefaultClass = 'Varien_Convert_Profile';
00043 protected $_profileCollectionDefaultClass = 'Varien_Convert_Profile_Collection';
00044 protected $_containerDefaultClass = 'Varien_Convert_Container_Generic';
00045 protected $_containerCollectionDefaultClass = 'Varien_Convert_Container_Collection';
00046
00047 public function getContainers()
00048 {
00049 if (!$this->_containers) {
00050 $this->_containers = new $this->_containerCollectionDefaultClass();
00051 $this->_containers->setDefaultClass($this->_containerDefaultClass);
00052 }
00053 return $this->_containers;
00054 }
00055
00056 public function getContainer($name)
00057 {
00058 return $this->getContainers()->getItem($name);
00059 }
00060
00061
00062 public function addContainer($name, Varien_Convert_Container_Interface $container)
00063 {
00064 $container = $this->getContainers()->addItem($name, $container);
00065 return $container;
00066 }
00067
00068 public function getProfiles()
00069 {
00070 return $this->_profiles;
00071 }
00072
00073 public function getProfile($name)
00074 {
00075 if (!isset($this->_profiles[$name])) {
00076 $this->importProfileXml($name);
00077 }
00078 return $this->_profiles[$name];
00079 }
00080
00081 public function addProfile($name, Varien_Convert_Profile_Abstract $profile=null)
00082 {
00083 if (is_null($profile)) {
00084 $profile = new $this->_profileDefaultClass();
00085 }
00086 $this->_profiles[$name] = $profile;
00087 return $profile;
00088 }
00089
00090 public function run($profile)
00091 {
00092 $this->getProfile($profile)->run();
00093 return $this;
00094 }
00095
00096 public function getClassNameByType($type)
00097 {
00098 return $type;
00099 }
00100
00101 public function importXml($xml)
00102 {
00103 if (is_string($xml)) {
00104 $xml = simplexml_load_string($xml, $this->_simplexmlDefaultClass);
00105 }
00106 if (!$xml instanceof SimpleXMLElement) {
00107 return $this;
00108 }
00109 $this->_xml = $xml;
00110
00111 foreach ($xml->container as $containerNode) {
00112 if (!$containerNode['name'] || !$containerNode['type']) {
00113 continue;
00114 }
00115 $class = $this->getClassNameByType((string)$containerNode['type']);
00116 $container = $this->addContainer((string)$containerNode['name'], new $class());
00117 foreach ($containerNode->var as $varNode) {
00118 $container->setVar((string)$varNode['name'], (string)$varNode);
00119 }
00120 }
00121 return $this;
00122 }
00123
00124 public function importProfileXml($name)
00125 {
00126 if (!$this->_xml) {
00127 return $this;
00128 }
00129 $nodes = $this->_xml->xpath("//profile[@name='".$name."']");
00130 if (!$nodes) {
00131 return $this;
00132 }
00133 $profileNode = $nodes[0];
00134
00135 $profile = $this->addProfile($name);
00136 $profile->setContainers($this->getContainers());
00137 foreach ($profileNode->action as $actionNode) {
00138 $action = $profile->addAction();
00139 foreach ($actionNode->attributes() as $key=>$value) {
00140 $action->setParam($key, (string)$value);
00141 }
00142
00143 if ($actionNode['use']) {
00144 $container = $profile->getContainer((string)$actionNode['use']);
00145 } else {
00146 $action->setParam('class', $this->getClassNameByType((string)$actionNode['type']));
00147 $container = $action->getContainer();
00148 }
00149 $action->setContainer($container);
00150 if ($action->getParam('name')) {
00151 $this->addContainer($action->getParam('name'), $container);
00152 }
00153 foreach ($actionNode->var as $varNode) {
00154 $container->setVar((string)$varNode['name'], (string)$varNode);
00155 }
00156 }
00157
00158 return $this;
00159 }
00160 }