/src/Pyrus/PackageFile/v2/Release.php

https://github.com/CloCkWeRX/Pyrus · PHP · 474 lines · 333 code · 76 blank · 65 comment · 89 complexity · 349431c44607d45530dcf6099a2eb79c MD5 · raw file

  1. <?php
  2. /**
  3. * \Pyrus\PackageFile\v2\Release
  4. *
  5. * PHP version 5
  6. *
  7. * @category Pyrus
  8. * @package Pyrus
  9. * @author Greg Beaver <cellog@php.net>
  10. * @copyright 2010 The PEAR Group
  11. * @license http://www.opensource.org/licenses/bsd-license.php New BSD License
  12. * @link https://github.com/pyrus/Pyrus
  13. */
  14. /**
  15. * Manage a release in package.xml
  16. *
  17. * To be used like:
  18. *
  19. * <code>
  20. * // release is a PHP package
  21. * $pf->type = 'php';
  22. * // release is an extension source package
  23. * $pf->type = 'extsrc';
  24. * $pf->release[0]->installconditions['php']->min('5.2.0');
  25. * // defaults to "min"
  26. * $pf->release[0]->installconditions['php'] = '5.2.0';
  27. * // defaults to "pattern"
  28. * $pf->release[0]->installconditions['arch'] = 'i386';
  29. * $pf->release[0]->installconditions['arch']->pattern('i386')->conflicts();
  30. * // defaults to "name"
  31. * $pf->release[0]->installconditions['os'] = 'windows';
  32. * // defaults to existing
  33. * $pf->release[0]->installconditions['extension'][0]->name('PDO');
  34. * $pf->release[0]->installconditions['extension'][0]->name('PDO')->min('1.0');
  35. * $pf->release[0]->ignore('path/to/file.ext');
  36. * $pf->release[0]->installAs('path/to/anotherfile.ext', 'new/name.php');
  37. * // add another release
  38. * $i = count($pf->release);
  39. * $pf->release[$i]->ignore('path/to/anotherfile.ext');
  40. * $pf->release[$i]->installAs('path/to/file.ext', 'new/name.php');
  41. * // remove release
  42. * unset($pf->release[1]);
  43. * // remove all releases
  44. * $pf->release = null;
  45. * </code>
  46. *
  47. * @category Pyrus
  48. * @package Pyrus
  49. * @author Greg Beaver <cellog@php.net>
  50. * @copyright 2010 The PEAR Group
  51. * @license http://www.opensource.org/licenses/bsd-license.php New BSD License
  52. * @link https://github.com/pyrus/Pyrus
  53. */
  54. namespace Pyrus\PackageFile\v2;
  55. class Release implements \ArrayAccess, \Countable
  56. {
  57. private $_parent;
  58. private $_packageInfo;
  59. private $_filelist;
  60. protected $index;
  61. protected $info = array();
  62. function __construct($parent, $packageInfo, array $filelist, $index = null)
  63. {
  64. $this->_parent = $parent;
  65. $this->_packageInfo = $packageInfo;
  66. $this->_filelist = $filelist;
  67. if ($index !== null) {
  68. $this->index = $index;
  69. $this->info = $packageInfo;
  70. } else {
  71. if (isset($packageInfo[0])) {
  72. $this->info = $packageInfo;
  73. } else if (count($packageInfo)) {
  74. $this->info = array($packageInfo);
  75. }
  76. }
  77. }
  78. function __isset($var)
  79. {
  80. if (!isset($this->index)) {
  81. return false;
  82. }
  83. return isset($this->info[$var]);
  84. }
  85. function __get($var)
  86. {
  87. if (!isset($this->index)) {
  88. return null;
  89. }
  90. if ($var === 'configureoption') {
  91. if (!isset($this->info['configureoption'])) {
  92. $info = array();
  93. } else {
  94. $info = $this->info['configureoption'];
  95. if (!is_array($info) || !isset($info[0])) {
  96. $info = array($info);
  97. }
  98. }
  99. return new Release\ConfigureOption($this, $info);
  100. }
  101. if ($var === 'binarypackage') {
  102. if (!isset($this->info['binarypackage'])) {
  103. $info = array();
  104. } else {
  105. $info = $this->info['binarypackage'];
  106. if (!is_array($info)) {
  107. $info = array($info);
  108. }
  109. }
  110. return new Release\BinaryPackage($this, $info);
  111. }
  112. if ($var === 'installconditions') {
  113. if (!isset($this->info['installcondition'])) {
  114. $this->info['installcondition'] = array();
  115. }
  116. $conditions = new Release\InstallCondition($this, $this->info['installcondition']);
  117. return $conditions;
  118. }
  119. throw new Release\Exception('Unknown variable ' . $var . ', installconditions is the only supported variable');
  120. }
  121. function setBinaryPackage($info)
  122. {
  123. $this->info['binarypackage'] = $info;
  124. }
  125. function setConfigureOption($info)
  126. {
  127. $this->info['configureoption'] = $info;
  128. }
  129. function count()
  130. {
  131. if (isset($this->index)) {
  132. return 1;
  133. }
  134. return count($this->info);
  135. }
  136. function offsetGet($var)
  137. {
  138. if (is_int($var) && !isset($this->index)) {
  139. if (isset($this->info[0])) {
  140. if (!isset($this->info[$var])) {
  141. if ($var != count($this)) {
  142. throw new Release\Exception('Can only set the next highest release index ' .
  143. count($this) . ', not ' . $var);
  144. }
  145. $this->info[$var] = array();
  146. }
  147. return new Release($this, $this->info[$var], $this->_filelist, $var);
  148. }
  149. if ($var !== 0) {
  150. throw new Release\Exception('Can only set the ' .
  151. 'next highest release index 0, not ' . $var);
  152. }
  153. $this->info[$var] = array();
  154. return new Release($this, $this->info[$var], $this->_filelist, $var);
  155. }
  156. }
  157. /**
  158. * @return array
  159. */
  160. function getInstallCondition()
  161. {
  162. if (!isset($this->index)) {
  163. return null;
  164. }
  165. if (!isset($this->info['installcondition'])) {
  166. return false;
  167. }
  168. return $this->info['installcondition'];
  169. }
  170. function getInstallAs()
  171. {
  172. if (!isset($this->index)) {
  173. return null;
  174. }
  175. if (!isset($this->info['filelist'])) {
  176. return false;
  177. }
  178. if (!isset($this->info['filelist']['install'])) {
  179. return false;
  180. }
  181. return $this->info['filelist']['install'];
  182. }
  183. function getIgnore()
  184. {
  185. if (!isset($this->index)) {
  186. return null;
  187. }
  188. if (!isset($this->info['filelist'])) {
  189. return false;
  190. }
  191. if (!isset($this->info['filelist']['ignore'])) {
  192. return false;
  193. }
  194. return $this->info['filelist']['ignore'];
  195. }
  196. function ignores($file)
  197. {
  198. $ignore = $this->getIgnore();
  199. if (!$ignore) {
  200. return false;
  201. }
  202. if (!isset($ignore[0])) {
  203. $ignore = array($ignore);
  204. }
  205. foreach ($ignore as $ignored) {
  206. if ($ignored['attribs']['name'] == $file) {
  207. return true;
  208. }
  209. }
  210. return false;
  211. }
  212. function installsAs($file)
  213. {
  214. $install = $this->getInstallAs();
  215. if (!$install) {
  216. return $file;
  217. }
  218. if (!isset($install[0])) {
  219. $install = array($install);
  220. }
  221. foreach ($install as $as) {
  222. if ($as['attribs']['name'] == $file) {
  223. return $as['attribs']['as'];
  224. }
  225. }
  226. return $file;
  227. }
  228. function offsetSet($var, $value)
  229. {
  230. if ($var === null) {
  231. $var = count($this->info);
  232. }
  233. if (is_int($var) === false) {
  234. throw new Release\Exception('Cannot set ' . $var);
  235. }
  236. if ($value instanceof Release) {
  237. $this->info[$var] = array('installcondition' => $value->getInstallCondition(),
  238. 'install' => $value->getInstallAs(),
  239. 'ignore' => $value->getIgnore());
  240. $this->save();
  241. return;
  242. }
  243. throw new Release\Exception('Cannot set ' . $var . ' to non-\Pyrus\PackageFile\v2\Release');
  244. }
  245. /**
  246. * @param string $var
  247. */
  248. function offsetUnset($var)
  249. {
  250. unset($this->info[$var]);
  251. $this->save();
  252. }
  253. /**
  254. * @param string $var
  255. * @return bool
  256. */
  257. function offsetExists($var)
  258. {
  259. return isset($this->info[$var]);
  260. }
  261. function ignore($file)
  262. {
  263. if (!isset($this->index)) {
  264. throw new Release\Exception('Cannot ignore file ' .
  265. $file . ' without specifying which release section to ignore it in');
  266. }
  267. if (isset($this->_filelist[$file])) {
  268. if (!isset($this->info['filelist'])) {
  269. $this->info['filelist'] = array();
  270. }
  271. if (!isset($this->info['filelist']['ignore'])) {
  272. $this->info['filelist']['ignore'] = array(array('attribs' => array('name' => $file)));
  273. } else {
  274. if (!isset($this->info['filelist']['ignore'][0])) {
  275. $this->info['filelist']['ignore'] = array($this->info['filelist']['ignore']);
  276. }
  277. $this->info['filelist']['ignore'][] = array('attribs' => array('name' => $file));
  278. }
  279. $this->save();
  280. return;
  281. }
  282. throw new Release\Exception('Unknown file ' . $file . ' - add to filelist before ignoring');
  283. }
  284. function installAs($file, $newname)
  285. {
  286. if (!isset($this->index)) {
  287. throw new Release\Exception('Cannot install file ' . $file . ' to ' . $newname .
  288. ' without specifying which release section to install it in');
  289. }
  290. if (isset($this->_filelist[$file])) {
  291. if (!isset($this->info['filelist'])) {
  292. $this->info['filelist'] = array();
  293. }
  294. if (!isset($this->info['filelist']['install'])) {
  295. $this->info['filelist']['install'] = array(array('attribs' =>
  296. array('name' => $file, 'as' => $newname)));
  297. } else {
  298. if (!isset($this->info['filelist']['install'][0])) {
  299. $this->info['filelist']['install'] = array($this->info['filelist']['install']);
  300. }
  301. $this->info['filelist']['install'][] = array('attribs' =>
  302. array('name' => $file, 'as' => $newname));
  303. }
  304. $this->save();
  305. return;
  306. }
  307. throw new Release\Exception('Unknown file ' . $file .
  308. ' - add to filelist before adding install as tag');
  309. }
  310. function setInstallCondition(Release\InstallCondition $c)
  311. {
  312. $this->info['installcondition'] = $c->getInfo();
  313. $this->save();
  314. }
  315. function setReleaseInfo($index, $info)
  316. {
  317. $this->info[$index] = $info;
  318. }
  319. /**
  320. * Saves results to the parent packagefile object
  321. */
  322. function save()
  323. {
  324. if (isset($this->index)) {
  325. $this->_parent->setReleaseInfo($this->index, $this->info);
  326. return $this->_parent->save();
  327. }
  328. $newXml = $this->info;
  329. foreach ($newXml as $index => $info) {
  330. if (isset($info['filelist'])) {
  331. if (isset($info['filelist']['ignore']) && count($info['filelist']['ignore']) == 1 && !isset($info['filelist']['ignore']['attribs'])) {
  332. $newXml[$index]['filelist']['ignore'] = $newXml[$index]['filelist']['ignore'][0];
  333. }
  334. if (isset($info['filelist']['install']) && count($info['filelist']['install']) == 1 && !isset($info['filelist']['install']['attribs'])) {
  335. $newXml[$index]['filelist']['install'] = $newXml[$index]['filelist']['install'][0];
  336. }
  337. if (array_key_exists('ignore', $info['filelist']) && !count($info['filelist']['ignore'])) {
  338. unset($newXml[$index]['filelist']['ignore']);
  339. }
  340. if (array_key_exists('install', $info['filelist']) && !count($info['filelist']['install'])) {
  341. unset($newXml[$index]['filelist']['install']);
  342. }
  343. }
  344. if (array_key_exists('installcondition', $info) && count($info['installcondition'])) {
  345. foreach (array('php', 'os', 'arch') as $key) {
  346. if (!isset($info['installcondition'][$key])) {
  347. continue;
  348. }
  349. foreach (array_keys($info['installcondition'][$key]) as $ikey) {
  350. if ($info['installcondition'][$key][$ikey] === null) {
  351. unset($newXml[$index]['installcondition'][$key][$ikey]);
  352. }
  353. }
  354. if (!count($newXml[$index]['installcondition'][$key])) {
  355. unset($newXml[$index]['installcondition'][$key]);
  356. }
  357. }
  358. if (array_key_exists('extension', $info['installcondition']) && !count($info['installcondition']['extension'])) {
  359. unset($newXml[$index]['installcondition']['extension']);
  360. }
  361. if (isset($info['installcondition']['extension'])) {
  362. if (!isset($info['installcondition']['extension'][0])) {
  363. $newXml[$index]['installcondition']['extension'] = $info['installcondition']['extension'] =
  364. array($info['installcondition']['extension']);
  365. }
  366. foreach ($info['installcondition']['extension'] as $extkey => $ext) {
  367. foreach (array_keys($ext) as $key) {
  368. if ($ext[$key] === null) {
  369. unset($newXml[$index]['installcondition']['extension'][$extkey][$key]);
  370. }
  371. }
  372. if (!count($newXml[$index]['installcondition']['extension'][$extkey])) {
  373. unset($newXml[$index]['installcondition']['extension'][$extkey]);
  374. }
  375. }
  376. if (count($newXml[$index]['installcondition']['extension']) == 1) {
  377. $newXml[$index]['installcondition']['extension'] =
  378. array_values($newXml[$index]['installcondition']['extension']);
  379. $newXml[$index]['installcondition']['extension'] =
  380. $newXml[$index]['installcondition']['extension'][0];
  381. } elseif (count($newXml[$index]['installcondition']['extension']) == 0) {
  382. unset($newXml[$index]['installcondition']['extension']);
  383. }
  384. }
  385. if (!count($newXml[$index]['installcondition'])) {
  386. unset($newXml[$index]['installcondition']);
  387. continue;
  388. }
  389. } else {
  390. unset($newXml[$index]['installcondition']);
  391. }
  392. }
  393. if (count($newXml) == 1) {
  394. $newXml = $newXml[0];
  395. }
  396. $this->_parent->rawrelease = $newXml;
  397. }
  398. }