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 Mage_Dataflow_Model_Convert_Profile_Collection
00036 {
00037
00038 protected $_xml;
00039
00040 protected $_containers;
00041
00042 protected $_profiles = array();
00043
00044 protected $_simplexmlDefaultClass = 'Varien_Simplexml_Element';
00045
00046 protected $_profileDefaultClass = 'Mage_Dataflow_Model_Convert_Profile';
00047
00048 protected $_profileCollectionDefaultClass = 'Mage_Dataflow_Model_Convert_Profile_Collection';
00049
00050 protected $_containerDefaultClass = 'Mage_Dataflow_Model_Convert_Container_Generic';
00051
00052 protected $_containerCollectionDefaultClass = 'Mage_Dataflow_Model_Convert_Container_Collection';
00053
00054 public function getContainers()
00055 {
00056 if (!$this->_containers) {
00057 $this->_containers = new $this->_containerCollectionDefaultClass();
00058 $this->_containers->setDefaultClass($this->_containerDefaultClass);
00059 }
00060 return $this->_containers;
00061 }
00062
00063 public function getContainer($name)
00064 {
00065 return $this->getContainers()->getItem($name);
00066 }
00067
00068
00069 public function addContainer($name, Mage_Dataflow_Model_Convert_Container_Interface $container)
00070 {
00071 $container = $this->getContainers()->addItem($name, $container);
00072 return $container;
00073 }
00074
00075 public function getProfiles()
00076 {
00077 return $this->_profiles;
00078 }
00079
00080 public function getProfile($name)
00081 {
00082 if (!isset($this->_profiles[$name])) {
00083 $this->importProfileXml($name);
00084 }
00085 return $this->_profiles[$name];
00086 }
00087
00088 public function addProfile($name, Mage_Dataflow_Model_Convert_Profile_Interface $profile=null)
00089 {
00090 if (is_null($profile)) {
00091 $profile = new $this->_profileDefaultClass();
00092 }
00093 $this->_profiles[$name] = $profile;
00094 return $profile;
00095 }
00096
00097 public function run($profile)
00098 {
00099 $this->getProfile($profile)->run();
00100 return $this;
00101 }
00102
00103 public function getClassNameByType($type)
00104 {
00105 return $type;
00106 }
00107
00108 public function importXml($xml)
00109 {
00110 if (is_string($xml)) {
00111 $xml = @simplexml_load_string($xml, $this->_simplexmlDefaultClass);
00112 }
00113 if (!$xml instanceof SimpleXMLElement) {
00114 return $this;
00115 }
00116 $this->_xml = $xml;
00117
00118 foreach ($xml->container as $containerNode) {
00119 if (!$containerNode['name'] || !$containerNode['type']) {
00120 continue;
00121 }
00122 $class = $this->getClassNameByType((string)$containerNode['type']);
00123 $container = $this->addContainer((string)$containerNode['name'], new $class());
00124 foreach ($containerNode->var as $varNode) {
00125 $container->setVar((string)$varNode['name'], (string)$varNode);
00126 }
00127 }
00128 return $this;
00129 }
00130
00131 public function importProfileXml($name)
00132 {
00133 if (!$this->_xml) {
00134 return $this;
00135 }
00136 $nodes = $this->_xml->xpath("//profile[@name='".$name."']");
00137 if (!$nodes) {
00138 return $this;
00139 }
00140 $profileNode = $nodes[0];
00141
00142 $profile = $this->addProfile($name);
00143 $profile->setContainers($this->getContainers());
00144 foreach ($profileNode->action as $actionNode) {
00145 $action = $profile->addAction();
00146 foreach ($actionNode->attributes() as $key=>$value) {
00147 $action->setParam($key, (string)$value);
00148 }
00149
00150 if ($actionNode['use']) {
00151 $container = $profile->getContainer((string)$actionNode['use']);
00152 } else {
00153 $action->setParam('class', $this->getClassNameByType((string)$actionNode['type']));
00154 $container = $action->getContainer();
00155 }
00156 $action->setContainer($container);
00157 if ($action->getParam('name')) {
00158 $this->addContainer($action->getParam('name'), $container);
00159 }
00160 foreach ($actionNode->var as $varNode) {
00161 if ($varNode['name'] == 'map') {
00162 $mapData = array();
00163 foreach ($varNode->map as $mapNode) {
00164 $mapData[(string)$mapNode['name']] = (string)$mapNode;
00165 }
00166 $container->setVar((string)$varNode['name'], $mapData);
00167 }
00168 else {
00169 $container->setVar((string)$varNode['name'], (string)$varNode);
00170 }
00171 }
00172 }
00173
00174 return $this;
00175 }
00176
00177 }