installer 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. #!/usr/bin/env php
  2. <?php
  3. /*
  4. * This file is part of Composer.
  5. *
  6. * (c) Nils Adermann <naderman@naderman.de>
  7. * Jordi Boggiano <j.boggiano@seld.be>
  8. *
  9. * For the full copyright and license information, please view the LICENSE
  10. * file that was distributed with this source code.
  11. */
  12. process($argv);
  13. /**
  14. * processes the installer
  15. */
  16. function process($argv)
  17. {
  18. $check = in_array('--check', $argv);
  19. $help = in_array('--help', $argv);
  20. $force = in_array('--force', $argv);
  21. if ($help) {
  22. displayHelp();
  23. exit(0);
  24. }
  25. $ok = checkPlatform();
  26. if ($check && !$ok) {
  27. exit(1);
  28. }
  29. if ($ok || $force) {
  30. installComposer();
  31. }
  32. exit(0);
  33. }
  34. /**
  35. * displays the help
  36. */
  37. function displayHelp()
  38. {
  39. echo <<<EOF
  40. Composer Installer
  41. ------------------
  42. Options
  43. --help this help
  44. --check for checking environment only
  45. --force forces the installation
  46. EOF;
  47. }
  48. /**
  49. * check the platform for possible issues on running composer
  50. */
  51. function checkPlatform()
  52. {
  53. $errors = array();
  54. if (false !== ini_get('detect_unicode')) {
  55. $errors['unicode'] = 'On';
  56. }
  57. if (ini_get('phar.readonly')) {
  58. $errors['readonly'] = 'On';
  59. }
  60. if (ini_get('phar.require_hash')) {
  61. $errors['require_hash'] = 'On';
  62. }
  63. if ($suhosin = ini_get('suhosin.executor.include.whitelist') && (isset($suhosin) && false === stripos($suhosin, 'phar'))) {
  64. $errors['suhosin'] = $suhosin;
  65. }
  66. if (PHP_VERSION < '5.3.2') {
  67. $errors['php'] = PHP_VERSION;
  68. }
  69. if (!empty($errors)) {
  70. out("Composer detected that you have enabled some settings in your `php.ini` file that can make Composer unable to work properly.".PHP_EOL, 'error');
  71. echo PHP_EOL.'Make sure that you have changed options listed below:'.PHP_EOL;
  72. foreach ($errors as $error => $actual) {
  73. switch ($error) {
  74. case 'unicode':
  75. $text = " detect_unicode = Off (actual: {$actual})".PHP_EOL;
  76. break;
  77. case 'readonly':
  78. $text = " phar.readonly = Off (actual: {$actual})".PHP_EOL;
  79. break;
  80. case 'require_hash':
  81. $text = " phar.require_hash = Off (actual: {$actual})".PHP_EOL;
  82. break;
  83. case 'suhosin':
  84. $text = " suhosin.executor.include.whitelist = phar (actual: {$actual})".PHP_EOL;
  85. break;
  86. case 'php':
  87. $text = " PHP_VERSION (actual: {$actual})".PHP_EOL;
  88. break;
  89. }
  90. out($text, 'info');
  91. }
  92. echo PHP_EOL;
  93. return false;
  94. }
  95. out("All settings correct for using Composer".PHP_EOL,'success');
  96. return true;
  97. }
  98. /**
  99. * installs composer to the current working directory
  100. */
  101. function installComposer()
  102. {
  103. $installDir = getcwd();
  104. $file = $installDir . DIRECTORY_SEPARATOR . 'composer.phar';
  105. if (is_readable($file)) {
  106. @unlink($file);
  107. }
  108. $download = copy('http://getcomposer.org/composer.phar', $installDir.DIRECTORY_SEPARATOR.'composer.phar');
  109. out(PHP_EOL."Composer successfully installed to: " . $file, 'success');
  110. out(PHP_EOL."Use it: php composer.phar".PHP_EOL, 'info');
  111. }
  112. /**
  113. * colorize output
  114. */
  115. function out($text, $color = null)
  116. {
  117. $styles = array(
  118. 'success' => "\033[0;32m%s\033[0m",
  119. 'error' => "\033[31;31m%s\033[0m",
  120. 'info' => "\033[33;33m%s\033[0m"
  121. );
  122. echo sprintf(isset($styles[$color]) ? $styles[$color] : "%s", $text);
  123. }