Package.php 12 KB

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