Package.php 14 KB

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