package.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. <?php
  2. /**
  3. * Fuel is a fast, lightweight, community driven PHP5 framework.
  4. *
  5. * @package Fuel
  6. * @version 1.5
  7. * @author Fuel Development Team
  8. * @license MIT License
  9. * @copyright 2010 - 2013 Fuel Development Team
  10. * @link http://fuelphp.com
  11. */
  12. namespace Oil;
  13. /**
  14. * Oil\Package Class
  15. *
  16. * @package Fuel
  17. * @subpackage Oil
  18. * @category Core
  19. * @author Phil Sturgeon
  20. */
  21. class Package
  22. {
  23. protected static $protected = array('oil');
  24. protected static $git = 'git';
  25. public static function install($package = null)
  26. {
  27. // Make sure something is set
  28. if ($package === null)
  29. {
  30. static::help();
  31. return;
  32. }
  33. $config = \Config::load('package');
  34. $version = \Cli::option('version', 'master');
  35. // Check to see if this package is already installed
  36. if (is_dir(PKGPATH . $package))
  37. {
  38. throw new Exception('Package "' . $package . '" is already installed.');
  39. return;
  40. }
  41. foreach ($config['sources'] as $source)
  42. {
  43. $packages = array('fuel-'.$package, $package);
  44. foreach ($packages as $package)
  45. {
  46. $zip_url = 'http://' . rtrim($source, '/').'/'.$package.'/zipball/'.$version;
  47. if ($fp = @fopen($zip_url, 'r'))
  48. {
  49. // We don't actually need this, just checking the file is there
  50. fclose($fp);
  51. // Now, lets get this package
  52. // If a direct download is requested, or git is unavailable, download it!
  53. if (\Cli::option('direct') OR static::_use_git() === false)
  54. {
  55. static::_download_package_zip($zip_url, $package, $version);
  56. exit;
  57. }
  58. // Otherwise, get your clone on baby!
  59. else
  60. {
  61. static::_clone_package_repo($source, $package, $version);
  62. exit;
  63. }
  64. }
  65. }
  66. }
  67. throw new Exception('Could not find package "' . $package . '".');
  68. }
  69. public static function uninstall($package)
  70. {
  71. $package_folder = PKGPATH . $package;
  72. // Check to see if this package is already installed
  73. if (in_array($package, static::$protected))
  74. {
  75. throw new Exception('Package "' . $package . '" cannot be uninstalled.');
  76. return false;
  77. }
  78. // Check to see if this package is already installed
  79. if ( ! is_dir($package_folder))
  80. {
  81. throw new Exception('Package "' . $package . '" is not installed.');
  82. return false;
  83. }
  84. \Cli::write('Package "' . $package . '" was uninstalled.', 'yellow');
  85. \File::delete_dir($package_folder);
  86. }
  87. public static function help()
  88. {
  89. $output = <<<HELP
  90. Usage:
  91. php oil [p|package] <packagename>
  92. Description:
  93. Packages containing extra functionality can be downloaded (or git cloned) simply with
  94. the following commands.
  95. Runtime options:
  96. --direct # Download direct from ZIP even if Git is installed
  97. Examples:
  98. php oil package install <packagename>
  99. php oil package uninstall <packagename>
  100. Documentation:
  101. http://fuelphp.com/docs/packages/oil/package.html
  102. HELP;
  103. \Cli::write($output);
  104. }
  105. private static function _use_git()
  106. {
  107. exec('which git', $output);
  108. // If this is a valid path to git, use it instead of just "git"
  109. if (file_exists($line = trim(current($output))))
  110. {
  111. static::$git = $line;
  112. }
  113. unset($output);
  114. // Double check git is installed (windows will fail step 1)
  115. exec(static::$git . ' --version', $output);
  116. preg_match('#^(git version)#', current($output), $matches);
  117. // If we have a match, use Git!
  118. return ! empty($matches[0]);
  119. }
  120. private static function _download_package_zip($zip_url, $package, $version)
  121. {
  122. \Cli::write('Downloading package: ' . $zip_url);
  123. // Make the folder so we can extract the ZIP to it
  124. mkdir($tmp_folder = APPPATH . 'tmp/' . $package . '-' . time());
  125. $zip_file = $tmp_folder . '.zip';
  126. @copy($zip_url, $zip_file);
  127. if (file_exists($zip_file))
  128. {
  129. $unzip = new \Unzip;
  130. $files = $unzip->extract($zip_file, $tmp_folder);
  131. // Grab the first folder out of it (we dont know what it's called)
  132. list($tmp_package_folder) = glob($tmp_folder.'/*', GLOB_ONLYDIR);
  133. $package_folder = PKGPATH . $package;
  134. // Move that folder into the packages folder
  135. rename($tmp_package_folder, $package_folder);
  136. unlink($zip_file);
  137. rmdir($tmp_folder);
  138. foreach ($files as $file)
  139. {
  140. $path = str_replace($tmp_package_folder, $package_folder, $file);
  141. chmod($path, octdec(755));
  142. \Cli::write("\t" . $path);
  143. }
  144. }
  145. else
  146. {
  147. \Cli::write('Package could not be found', 'red');
  148. }
  149. }
  150. public static function _clone_package_repo($source, $package, $version)
  151. {
  152. $repo_url = 'git://' . rtrim($source, '/').'/'.$package . '.git';
  153. \Cli::write('Downloading package: ' . $repo_url);
  154. $package_folder = PKGPATH . $package;
  155. // Clone to the package path
  156. passthru(static::$git . ' clone ' . $repo_url . ' ' . $package_folder);
  157. passthru(static::$git .' add ' . $package_folder . '/');
  158. \Cli::write('');
  159. }
  160. }
  161. /* End of file package.php */