Package.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607
  1. <?php
  2. /*
  3. * This file is part of Composer.
  4. *
  5. * (c) Nils Adermann <naderman@naderman.de>
  6. * Jordi Boggiano <j.boggiano@seld.be>
  7. *
  8. * For the full copyright and license information, please view the LICENSE
  9. * file that was distributed with this source code.
  10. */
  11. namespace Composer\Package;
  12. use Composer\Package\Version\VersionParser;
  13. use Composer\Util\ComposerMirror;
  14. /**
  15. * Core package definitions that are needed to resolve dependencies and install packages
  16. *
  17. * @author Nils Adermann <naderman@naderman.de>
  18. */
  19. class Package extends BasePackage
  20. {
  21. protected $type;
  22. protected $targetDir;
  23. protected $installationSource;
  24. protected $sourceType;
  25. protected $sourceUrl;
  26. protected $sourceReference;
  27. protected $sourceMirrors;
  28. protected $distType;
  29. protected $distUrl;
  30. protected $distReference;
  31. protected $distSha1Checksum;
  32. protected $distMirrors;
  33. protected $version;
  34. protected $prettyVersion;
  35. protected $releaseDate;
  36. protected $extra = array();
  37. protected $binaries = array();
  38. protected $dev;
  39. protected $stability;
  40. protected $notificationUrl;
  41. protected $requires = array();
  42. protected $conflicts = array();
  43. protected $provides = array();
  44. protected $replaces = array();
  45. protected $devRequires = array();
  46. protected $suggests = array();
  47. protected $autoload = array();
  48. protected $devAutoload = array();
  49. protected $includePaths = array();
  50. protected $archiveExcludes = array();
  51. /**
  52. * Creates a new in memory package.
  53. *
  54. * @param string $name The package's name
  55. * @param string $version The package's version
  56. * @param string $prettyVersion The package's non-normalized version
  57. */
  58. public function __construct($name, $version, $prettyVersion)
  59. {
  60. parent::__construct($name);
  61. $this->version = $version;
  62. $this->prettyVersion = $prettyVersion;
  63. $this->stability = VersionParser::parseStability($version);
  64. $this->dev = $this->stability === 'dev';
  65. }
  66. /**
  67. * {@inheritDoc}
  68. */
  69. public function isDev()
  70. {
  71. return $this->dev;
  72. }
  73. /**
  74. * @param string $type
  75. */
  76. public function setType($type)
  77. {
  78. $this->type = $type;
  79. }
  80. /**
  81. * {@inheritDoc}
  82. */
  83. public function getType()
  84. {
  85. return $this->type ?: 'library';
  86. }
  87. /**
  88. * {@inheritDoc}
  89. */
  90. public function getStability()
  91. {
  92. return $this->stability;
  93. }
  94. /**
  95. * @param string $targetDir
  96. */
  97. public function setTargetDir($targetDir)
  98. {
  99. $this->targetDir = $targetDir;
  100. }
  101. /**
  102. * {@inheritDoc}
  103. */
  104. public function getTargetDir()
  105. {
  106. if (null === $this->targetDir) {
  107. return;
  108. }
  109. return ltrim(preg_replace('{ (?:^|[\\\\/]+) \.\.? (?:[\\\\/]+|$) (?:\.\.? (?:[\\\\/]+|$) )*}x', '/', $this->targetDir), '/');
  110. }
  111. /**
  112. * @param array $extra
  113. */
  114. public function setExtra(array $extra)
  115. {
  116. $this->extra = $extra;
  117. }
  118. /**
  119. * {@inheritDoc}
  120. */
  121. public function getExtra()
  122. {
  123. return $this->extra;
  124. }
  125. /**
  126. * @param array $binaries
  127. */
  128. public function setBinaries(array $binaries)
  129. {
  130. $this->binaries = $binaries;
  131. }
  132. /**
  133. * {@inheritDoc}
  134. */
  135. public function getBinaries()
  136. {
  137. return $this->binaries;
  138. }
  139. /**
  140. * {@inheritDoc}
  141. */
  142. public function setInstallationSource($type)
  143. {
  144. $this->installationSource = $type;
  145. }
  146. /**
  147. * {@inheritDoc}
  148. */
  149. public function getInstallationSource()
  150. {
  151. return $this->installationSource;
  152. }
  153. /**
  154. * @param string $type
  155. */
  156. public function setSourceType($type)
  157. {
  158. $this->sourceType = $type;
  159. }
  160. /**
  161. * {@inheritDoc}
  162. */
  163. public function getSourceType()
  164. {
  165. return $this->sourceType;
  166. }
  167. /**
  168. * @param string $url
  169. */
  170. public function setSourceUrl($url)
  171. {
  172. $this->sourceUrl = $url;
  173. }
  174. /**
  175. * {@inheritDoc}
  176. */
  177. public function getSourceUrl()
  178. {
  179. return $this->sourceUrl;
  180. }
  181. /**
  182. * @param string $reference
  183. */
  184. public function setSourceReference($reference)
  185. {
  186. $this->sourceReference = $reference;
  187. }
  188. /**
  189. * {@inheritDoc}
  190. */
  191. public function getSourceReference()
  192. {
  193. return $this->sourceReference;
  194. }
  195. /**
  196. * @param array|null $mirrors
  197. */
  198. public function setSourceMirrors($mirrors)
  199. {
  200. $this->sourceMirrors = $mirrors;
  201. }
  202. /**
  203. * {@inheritDoc}
  204. */
  205. public function getSourceMirrors()
  206. {
  207. return $this->sourceMirrors;
  208. }
  209. /**
  210. * {@inheritDoc}
  211. */
  212. public function getSourceUrls()
  213. {
  214. return $this->getUrls($this->sourceUrl, $this->sourceMirrors, $this->sourceReference, $this->sourceType, 'source');
  215. }
  216. /**
  217. * @param string $type
  218. */
  219. public function setDistType($type)
  220. {
  221. $this->distType = $type;
  222. }
  223. /**
  224. * {@inheritDoc}
  225. */
  226. public function getDistType()
  227. {
  228. return $this->distType;
  229. }
  230. /**
  231. * @param string $url
  232. */
  233. public function setDistUrl($url)
  234. {
  235. $this->distUrl = $url;
  236. }
  237. /**
  238. * {@inheritDoc}
  239. */
  240. public function getDistUrl()
  241. {
  242. return $this->distUrl;
  243. }
  244. /**
  245. * @param string $reference
  246. */
  247. public function setDistReference($reference)
  248. {
  249. $this->distReference = $reference;
  250. }
  251. /**
  252. * {@inheritDoc}
  253. */
  254. public function getDistReference()
  255. {
  256. return $this->distReference;
  257. }
  258. /**
  259. * @param string $sha1checksum
  260. */
  261. public function setDistSha1Checksum($sha1checksum)
  262. {
  263. $this->distSha1Checksum = $sha1checksum;
  264. }
  265. /**
  266. * {@inheritDoc}
  267. */
  268. public function getDistSha1Checksum()
  269. {
  270. return $this->distSha1Checksum;
  271. }
  272. /**
  273. * @param array|null $mirrors
  274. */
  275. public function setDistMirrors($mirrors)
  276. {
  277. $this->distMirrors = $mirrors;
  278. }
  279. /**
  280. * {@inheritDoc}
  281. */
  282. public function getDistMirrors()
  283. {
  284. return $this->distMirrors;
  285. }
  286. /**
  287. * {@inheritDoc}
  288. */
  289. public function getDistUrls()
  290. {
  291. return $this->getUrls($this->distUrl, $this->distMirrors, $this->distReference, $this->distType, 'dist');
  292. }
  293. /**
  294. * {@inheritDoc}
  295. */
  296. public function getVersion()
  297. {
  298. return $this->version;
  299. }
  300. /**
  301. * {@inheritDoc}
  302. */
  303. public function getPrettyVersion()
  304. {
  305. return $this->prettyVersion;
  306. }
  307. /**
  308. * Set the releaseDate
  309. *
  310. * @param \DateTime $releaseDate
  311. */
  312. public function setReleaseDate(\DateTime $releaseDate)
  313. {
  314. $this->releaseDate = $releaseDate;
  315. }
  316. /**
  317. * {@inheritDoc}
  318. */
  319. public function getReleaseDate()
  320. {
  321. return $this->releaseDate;
  322. }
  323. /**
  324. * Set the required packages
  325. *
  326. * @param array $requires A set of package links
  327. */
  328. public function setRequires(array $requires)
  329. {
  330. $this->requires = $requires;
  331. }
  332. /**
  333. * {@inheritDoc}
  334. */
  335. public function getRequires()
  336. {
  337. return $this->requires;
  338. }
  339. /**
  340. * Set the conflicting packages
  341. *
  342. * @param array $conflicts A set of package links
  343. */
  344. public function setConflicts(array $conflicts)
  345. {
  346. $this->conflicts = $conflicts;
  347. }
  348. /**
  349. * {@inheritDoc}
  350. */
  351. public function getConflicts()
  352. {
  353. return $this->conflicts;
  354. }
  355. /**
  356. * Set the provided virtual packages
  357. *
  358. * @param array $provides A set of package links
  359. */
  360. public function setProvides(array $provides)
  361. {
  362. $this->provides = $provides;
  363. }
  364. /**
  365. * {@inheritDoc}
  366. */
  367. public function getProvides()
  368. {
  369. return $this->provides;
  370. }
  371. /**
  372. * Set the packages this one replaces
  373. *
  374. * @param array $replaces A set of package links
  375. */
  376. public function setReplaces(array $replaces)
  377. {
  378. $this->replaces = $replaces;
  379. }
  380. /**
  381. * {@inheritDoc}
  382. */
  383. public function getReplaces()
  384. {
  385. return $this->replaces;
  386. }
  387. /**
  388. * Set the recommended packages
  389. *
  390. * @param array $devRequires A set of package links
  391. */
  392. public function setDevRequires(array $devRequires)
  393. {
  394. $this->devRequires = $devRequires;
  395. }
  396. /**
  397. * {@inheritDoc}
  398. */
  399. public function getDevRequires()
  400. {
  401. return $this->devRequires;
  402. }
  403. /**
  404. * Set the suggested packages
  405. *
  406. * @param array $suggests A set of package names/comments
  407. */
  408. public function setSuggests(array $suggests)
  409. {
  410. $this->suggests = $suggests;
  411. }
  412. /**
  413. * {@inheritDoc}
  414. */
  415. public function getSuggests()
  416. {
  417. return $this->suggests;
  418. }
  419. /**
  420. * Set the autoload mapping
  421. *
  422. * @param array $autoload Mapping of autoloading rules
  423. */
  424. public function setAutoload(array $autoload)
  425. {
  426. $this->autoload = $autoload;
  427. }
  428. /**
  429. * {@inheritDoc}
  430. */
  431. public function getAutoload()
  432. {
  433. return $this->autoload;
  434. }
  435. /**
  436. * Set the dev autoload mapping
  437. *
  438. * @param array $devAutoload Mapping of dev autoloading rules
  439. */
  440. public function setDevAutoload(array $devAutoload)
  441. {
  442. $this->devAutoload = $devAutoload;
  443. }
  444. /**
  445. * {@inheritDoc}
  446. */
  447. public function getDevAutoload()
  448. {
  449. return $this->devAutoload;
  450. }
  451. /**
  452. * Sets the list of paths added to PHP's include path.
  453. *
  454. * @param array $includePaths List of directories.
  455. */
  456. public function setIncludePaths(array $includePaths)
  457. {
  458. $this->includePaths = $includePaths;
  459. }
  460. /**
  461. * {@inheritDoc}
  462. */
  463. public function getIncludePaths()
  464. {
  465. return $this->includePaths;
  466. }
  467. /**
  468. * Sets the notification URL
  469. *
  470. * @param string $notificationUrl
  471. */
  472. public function setNotificationUrl($notificationUrl)
  473. {
  474. $this->notificationUrl = $notificationUrl;
  475. }
  476. /**
  477. * {@inheritDoc}
  478. */
  479. public function getNotificationUrl()
  480. {
  481. return $this->notificationUrl;
  482. }
  483. /**
  484. * Sets a list of patterns to be excluded from archives
  485. *
  486. * @param array $excludes
  487. */
  488. public function setArchiveExcludes(array $excludes)
  489. {
  490. $this->archiveExcludes = $excludes;
  491. }
  492. /**
  493. * {@inheritDoc}
  494. */
  495. public function getArchiveExcludes()
  496. {
  497. return $this->archiveExcludes;
  498. }
  499. /**
  500. * Replaces current version and pretty version with passed values.
  501. * It also sets stability.
  502. *
  503. * @param string $version The package's normalized version
  504. * @param string $prettyVersion The package's non-normalized version
  505. */
  506. public function replaceVersion($version, $prettyVersion)
  507. {
  508. $this->version = $version;
  509. $this->prettyVersion = $prettyVersion;
  510. $this->stability = VersionParser::parseStability($version);
  511. $this->dev = $this->stability === 'dev';
  512. }
  513. protected function getUrls($url, $mirrors, $ref, $type, $urlType)
  514. {
  515. if (!$url) {
  516. return array();
  517. }
  518. $urls = array($url);
  519. if ($mirrors) {
  520. foreach ($mirrors as $mirror) {
  521. if ($urlType === 'dist') {
  522. $mirrorUrl = ComposerMirror::processUrl($mirror['url'], $this->name, $this->version, $ref, $type);
  523. } elseif ($urlType === 'source' && $type === 'git') {
  524. $mirrorUrl = ComposerMirror::processGitUrl($mirror['url'], $this->name, $url, $type);
  525. } elseif ($urlType === 'source' && $type === 'hg') {
  526. $mirrorUrl = ComposerMirror::processHgUrl($mirror['url'], $this->name, $url, $type);
  527. }
  528. if (!in_array($mirrorUrl, $urls)) {
  529. $func = $mirror['preferred'] ? 'array_unshift' : 'array_push';
  530. $func($urls, $mirrorUrl);
  531. }
  532. }
  533. }
  534. return $urls;
  535. }
  536. }