PageRenderTime 39ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/app/code/core/Mage/Adminhtml/Model/Extension.php

https://bitbucket.org/claudiu_marginean/magento-hg-mirror
PHP | 397 lines | 300 code | 51 blank | 46 comment | 37 complexity | f868bdcc5b2dce911d2f71e48bf15b0f MD5 | raw file
Possible License(s): CC-BY-SA-3.0, LGPL-2.1, GPL-2.0, WTFPL
  1. <?php
  2. /**
  3. * Magento
  4. *
  5. * NOTICE OF LICENSE
  6. *
  7. * This source file is subject to the Open Software License (OSL 3.0)
  8. * that is bundled with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://opensource.org/licenses/osl-3.0.php
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@magentocommerce.com so we can send you a copy immediately.
  14. *
  15. * DISCLAIMER
  16. *
  17. * Do not edit or add to this file if you wish to upgrade Magento to newer
  18. * versions in the future. If you wish to customize Magento for your
  19. * needs please refer to http://www.magentocommerce.com for more information.
  20. *
  21. * @category Mage
  22. * @package Mage_Adminhtml
  23. * @copyright Copyright (c) 2010 Magento Inc. (http://www.magentocommerce.com)
  24. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  25. */
  26. require_once 'Varien/Pear/Package.php';
  27. class Mage_Adminhtml_Model_Extension extends Varien_Object
  28. {
  29. protected $_roles;
  30. public function getPear()
  31. {
  32. return Varien_Pear::getInstance();
  33. }
  34. public function generatePackageXml()
  35. {
  36. Mage::getSingleton('adminhtml/session')
  37. ->setLocalExtensionPackageFormData($this->getData());
  38. Varien_Pear::$reloadOnRegistryUpdate = false;
  39. $pkg = new Varien_Pear_Package;
  40. #$pkg->getPear()->runHtmlConsole(array('command'=>'list-channels'));
  41. $pfm = $pkg->getPfm();
  42. $pfm->setOptions(array(
  43. 'packagedirectory'=>'.',
  44. 'baseinstalldir'=>'.',
  45. 'simpleoutput'=>true,
  46. ));
  47. $this->_setPackage($pfm);
  48. $this->_setRelease($pfm);
  49. $this->_setMaintainers($pfm);
  50. $this->_setDependencies($pfm);
  51. $this->_setContents($pfm);
  52. #echo "<pre>".print_r($pfm,1)."</pre>";
  53. if (!$pfm->validate(PEAR_VALIDATE_NORMAL)) {
  54. //echo "<pre>".print_r($this->getData(),1)."</pre>";
  55. //echo "TEST:";
  56. //echo "<pre>".print_r($pfm->getValidationWarnings(), 1)."</pre>";
  57. $message = $pfm->getValidationWarnings();
  58. //$message = $message[0]['message'];
  59. throw Mage::exception('Mage_Adminhtml', Mage::helper('adminhtml')->__($message[0]['message']));
  60. return $this;
  61. }
  62. $this->setPackageXml($pfm->getDefaultGenerator()->toXml(PEAR_VALIDATE_NORMAL));
  63. return $this;
  64. }
  65. protected function _setPackage($pfm)
  66. {
  67. $pfm->setPackageType('php');
  68. $pfm->setChannel($this->getData('channel'));
  69. $pfm->setLicense($this->getData('license'), $this->getData('license_uri'));
  70. $pfm->setPackage($this->getData('name'));
  71. $pfm->setSummary($this->getData('summary'));
  72. $pfm->setDescription($this->getData('description'));
  73. }
  74. protected function _setRelease($pfm)
  75. {
  76. $pfm->addRelease();
  77. $pfm->setDate(date('Y-m-d'));
  78. $pfm->setAPIVersion($this->getData('api_version'));
  79. $pfm->setReleaseVersion($this->getData('release_version'));
  80. $pfm->setAPIStability($this->getData('api_stability'));
  81. $pfm->setReleaseStability($this->getData('release_stability'));
  82. $pfm->setNotes($this->getData('notes'));
  83. }
  84. protected function _setMaintainers($pfm)
  85. {
  86. $maintainers = $this->getData('maintainers');
  87. foreach ($maintainers['role'] as $i=>$role) {
  88. if (0===$i) {
  89. continue;
  90. }
  91. $handle = $maintainers['handle'][$i];
  92. $name = $maintainers['name'][$i];
  93. $email = $maintainers['email'][$i];
  94. $active = !empty($maintainers['active'][$i]) ? 'yes' : 'no';
  95. $pfm->addMaintainer($role, $handle, $name, $email, $active);
  96. }
  97. }
  98. protected function _setDependencies($pfm)
  99. {
  100. $pfm->clearDeps();
  101. $exclude = $this->getData('depends_php_exclude')!=='' ? explode(',', $this->getData('depends_php_exclude')) : false;
  102. $pfm->setPhpDep($this->getData('depends_php_min'), $this->getData('depends_php_max'), $exclude);
  103. $pfm->setPearinstallerDep('1.6.2');
  104. foreach ($this->getData('depends') as $deptype=>$deps) {
  105. foreach ($deps['type'] as $i=>$type) {
  106. if (0===$i) {
  107. continue;
  108. }
  109. $name = $deps['name'][$i];
  110. $min = !empty($deps['min'][$i]) ? $deps['min'][$i] : false;
  111. $max = !empty($deps['max'][$i]) ? $deps['max'][$i] : false;
  112. $recommended = !empty($deps['recommended'][$i]) ? $deps['recommended'][$i] : false;
  113. $exclude = !empty($deps['exclude'][$i]) ? explode(',', $deps['exclude'][$i]) : false;
  114. if ($deptype!=='extension') {
  115. $channel = !empty($deps['channel'][$i]) ? $deps['channel'][$i] : 'connect.magentocommerce.com/core';
  116. }
  117. switch ($deptype) {
  118. case 'package':
  119. if ($type==='conflicts') {
  120. $pfm->addConflictingPackageDepWithChannel(
  121. $name, $channel, false, $min, $max, $recommended, $exclude);
  122. } else {
  123. $pfm->addPackageDepWithChannel(
  124. $type, $name, $channel, $min, $max, $recommended, $exclude);
  125. }
  126. break;
  127. case 'subpackage':
  128. if ($type==='conflicts') {
  129. Mage::throwException(Mage::helper('adminhtml')->__("Subpackage cannot be conflicting."));
  130. }
  131. $pfm->addSubpackageDepWithChannel(
  132. $type, $name, $channel, $min, $max, $recommended, $exclude);
  133. break;
  134. case 'extension':
  135. $pfm->addExtensionDep(
  136. $type, $name, $min, $max, $recommended, $exclude);
  137. break;
  138. }
  139. }
  140. }
  141. }
  142. protected function _setContents($pfm)
  143. {
  144. $baseDir = $this->getRoleDir('mage').DS;
  145. $pfm->clearContents();
  146. $contents = $this->getData('contents');
  147. $usesRoles = array();
  148. foreach ($contents['role'] as $i=>$role) {
  149. if (0===$i) {
  150. continue;
  151. }
  152. $usesRoles[$role] = 1;
  153. $roleDir = $this->getRoleDir($role).DS;
  154. $fullPath = $roleDir.$contents['path'][$i];
  155. switch ($contents['type'][$i]) {
  156. case 'file':
  157. if (!is_file($fullPath)) {
  158. Mage::throwException(Mage::helper('adminhtml')->__("Invalid file: %s", $fullPath));
  159. }
  160. $pfm->addFile('/', $contents['path'][$i], array('role'=>$role, 'md5sum'=>md5_file($fullPath)));
  161. break;
  162. case 'dir':
  163. if (!is_dir($fullPath)) {
  164. Mage::throwException(Mage::helper('adminhtml')->__("Invalid directory: %s", $fullPath));
  165. }
  166. $path = $contents['path'][$i];
  167. $include = $contents['include'][$i];
  168. $ignore = $contents['ignore'][$i];
  169. $this->_addDir($pfm, $role, $roleDir, $path, $include, $ignore);
  170. break;
  171. }
  172. }
  173. $pearRoles = $this->getRoles();
  174. #echo "<pre>".print_r($usesRoles,1)."</pre>";
  175. foreach ($usesRoles as $role=>$dummy) {
  176. if (empty($pearRoles[$role]['package'])) {
  177. continue;
  178. }
  179. $pfm->addUsesrole($role, $pearRoles[$role]['package']);
  180. }
  181. }
  182. protected function _addDir($pfm, $role, $roleDir, $path, $include, $ignore)
  183. {
  184. $roleDirLen = strlen($roleDir);
  185. $entries = @glob($roleDir.$path.DS."*");
  186. if (!empty($entries)) {
  187. foreach ($entries as $entry) {
  188. $filePath = substr($entry, $roleDirLen);
  189. if (!empty($include) && !preg_match($include, $filePath)) {
  190. continue;
  191. }
  192. if (!empty($ignore) && preg_match($ignore, $filePath)) {
  193. continue;
  194. }
  195. if (is_dir($entry)) {
  196. $baseName = basename($entry);
  197. if ('.'===$baseName || '..'===$baseName) {
  198. continue;
  199. }
  200. $this->_addDir($pfm, $role, $roleDir, $filePath, $include, $ignore);
  201. } elseif (is_file($entry)) {
  202. $pfm->addFile('/', $filePath, array('role'=>$role, 'md5sum'=>md5_file($entry)));
  203. }
  204. }
  205. }
  206. }
  207. public function getRoles()
  208. {
  209. if (!$this->_roles) {
  210. $frontend = $this->getPear()->getFrontend();
  211. $config = $this->getPear()->getConfig();
  212. $pearMage = new PEAR_Command_Mage($frontend, $config);
  213. $this->_roles = $pearMage->getRoles();
  214. }
  215. return $this->_roles;
  216. }
  217. public function getRoleDir($role)
  218. {
  219. $roles = $this->getRoles();
  220. return Varien_Pear::getInstance()->getConfig()->get($roles[$role]['dir_config']);
  221. }
  222. public function getMaintainerRoles()
  223. {
  224. return array(
  225. 'lead'=>'Lead',
  226. 'developer'=>'Developer',
  227. 'contributor'=>'Contributor',
  228. 'helper'=>'Helper'
  229. );
  230. }
  231. public function savePackage()
  232. {
  233. if ($this->getData('file_name') != '') {
  234. $fileName = $this->getData('file_name');
  235. $this->unsetData('file_name');
  236. } else {
  237. $fileName = $this->getName();
  238. }
  239. if (!preg_match('/^[a-z0-9]+[a-z0-9\-\_\.]*([\/\\\\]{1}[a-z0-9]+[a-z0-9\-\_\.]*)*$/i', $fileName)) {
  240. return false;
  241. }
  242. if (!$this->getPackageXml()) {
  243. $this->generatePackageXml();
  244. }
  245. if (!$this->getPackageXml()) {
  246. return false;
  247. }
  248. $pear = Varien_Pear::getInstance();
  249. $dir = Mage::getBaseDir('var').DS.'pear';
  250. if (!@file_put_contents($dir.DS.'package.xml', $this->getPackageXml())) {
  251. return false;
  252. }
  253. $pkgver = $this->getName().'-'.$this->getReleaseVersion();
  254. $this->unsPackageXml();
  255. $this->unsRoles();
  256. $xml = Mage::helper('core')->assocToXml($this->getData());
  257. $xml = new Varien_Simplexml_Element($xml->asXML());
  258. // prepare dir to save
  259. $parts = explode(DS, $fileName);
  260. array_pop($parts);
  261. $newDir = implode(DS, $parts);
  262. if ((!empty($newDir)) && (!is_dir($dir . DS . $newDir))) {
  263. if (!@mkdir($dir . DS . $newDir, 0777, true)) {
  264. return false;
  265. }
  266. }
  267. if (!@file_put_contents($dir . DS . $fileName . '.xml', $xml->asNiceXml())) {
  268. return false;
  269. }
  270. return true;
  271. }
  272. public function createPackage()
  273. {
  274. $pear = Varien_Pear::getInstance();
  275. $dir = Mage::getBaseDir('var').DS.'pear';
  276. if (!Mage::getConfig()->createDirIfNotExists($dir)) {
  277. return false;
  278. }
  279. $curDir = getcwd();
  280. chdir($dir);
  281. $result = $pear->run('mage-package', array(), array('package.xml'));
  282. chdir($curDir);
  283. if ($result instanceof PEAR_Error) {
  284. return $result;
  285. }
  286. return true;
  287. }
  288. public function getStabilityOptions()
  289. {
  290. return array(
  291. 'devel'=>'Development',
  292. 'alpha'=>'Alpha',
  293. 'beta'=>'Beta',
  294. 'stable'=>'Stable',
  295. );
  296. }
  297. public function getKnownChannels()
  298. {
  299. /*
  300. $pear = Varien_Pear::getInstance();
  301. $pear->run('list-channels');
  302. $output = $pear->getOutput();
  303. $pear->getFrontend()->clear();
  304. $data = $output[0]['output']['data'];
  305. $arr = array();
  306. foreach ($data as $channel) {
  307. $arr[$channel[0]] = $channel[1].' ('.$channel[0].')';
  308. }
  309. */
  310. $arr = array(
  311. 'connect.magentocommerce.com/core' => 'Magento Core Team',
  312. 'connect.magentocommerce.com/community' => 'Magento Community',
  313. #'pear.php.net' => 'PEAR',
  314. #'pear.phpunit.de' => 'PHPUnit',
  315. );
  316. return $arr;
  317. }
  318. public function loadLocal($package, $options=array())
  319. {
  320. $pear = $this->getPear();
  321. $pear->getFrontend()->clear();
  322. $result = $pear->run('info', $options, array($package));
  323. if ($result instanceof PEAR_Error) {
  324. Mage::throwException($result->message);
  325. break;
  326. }
  327. $output = $pear->getOutput();
  328. $pkg = new PEAR_PackageFile_v2;
  329. $pkg->fromArray($output[0]['output']['raw']);
  330. return $pkg;
  331. }
  332. public function loadRemote($package, $options=array())
  333. {
  334. $pear = $this->getPear();
  335. $pear->getFrontend()->clear();
  336. $result = $pear->run('remote-info', $options, array($package));
  337. if ($result instanceof PEAR_Error) {
  338. Mage::throwException($result->message);
  339. break;
  340. }
  341. $output = $pear->getOutput();
  342. $this->setData($output[0]['output']);
  343. return $this;
  344. }
  345. }