cell.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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 Cell
  22. {
  23. protected static $_protected = array('auth', 'email', 'oil', 'orm', 'parser');
  24. protected static $_api_url = 'http://cells.fuelphp.com/api/';
  25. protected static $_git_binary = 'git';
  26. protected static $_hg_binary = 'hg';
  27. public static function install($package = null)
  28. {
  29. // Make sure something is set
  30. if ($package === null)
  31. {
  32. static::help();
  33. return;
  34. }
  35. $version = \Cli::option('version', 'master');
  36. // Check to see if this package is already installed
  37. if (is_dir(PKGPATH . $package))
  38. {
  39. throw new Exception('Package "' . $package . '" is already installed.');
  40. return;
  41. }
  42. $request_url = static::$_api_url.'cells/show.json?name='.urlencode($package);
  43. $response = json_decode(@file_get_contents($request_url), true);
  44. if ( ! $response)
  45. {
  46. throw new Exception('No response from the API. Perhaps check your internet connection?');
  47. }
  48. if (empty($response['cell']))
  49. {
  50. throw new Exception('Could not find the cell "' . $package . '".');
  51. }
  52. $cell = $response['cell'];
  53. // Now, lets get this package
  54. // If it is git and (they have git installed (TODO) and they havent asked for a zip)
  55. if ($cell['repository_type'] == 'git' and ! \Cli::option('via-zip'))
  56. {
  57. \Cli::write('Downloading package: ' . $package);
  58. static::_clone_package_repo($cell['repository_url'], $package, $version);
  59. }
  60. // Fallback to shoving a ZIP file in place
  61. else
  62. {
  63. \Cli::write('Downloading package: ' . $package);
  64. static::_download_package_zip($zip_url, $package, $version);
  65. }
  66. }
  67. public static function all()
  68. {
  69. $request_url = static::$_api_url.'cells/list.json';
  70. $response = json_decode(@file_get_contents($request_url), true);
  71. if (empty($response['cells']))
  72. {
  73. throw new Exception('No cells were found with this name.');
  74. }
  75. foreach ($response['cells'] as $cell)
  76. {
  77. \Cli::write(\Cli::color($cell['name'], 'yellow').' '.\Cli::color($cell['current_version'], 'white').' - '.$cell['summary']);
  78. }
  79. }
  80. public static function search($name)
  81. {
  82. $request_url = static::$_api_url.'cells/search.json?name='.urlencode($name);
  83. $response = json_decode(file_get_contents($request_url), true);
  84. if (empty($response['cells']))
  85. {
  86. throw new Exception('No cells were found with this name.');
  87. }
  88. foreach ($response['cells'] as $cell)
  89. {
  90. \Cli::write(\Cli::color($cell['name'], 'yellow').' '.\Cli::color($cell['current_version'], 'white').' - '.$cell['summary']);
  91. }
  92. }
  93. public static function uninstall($package)
  94. {
  95. // Check to see if this package is already installed
  96. if ( ! $package_folder = \Package::exists($package))
  97. {
  98. throw new Exception('Package "' . $package . '" is not installed.');
  99. return false;
  100. }
  101. // Check to see if this package is already installed
  102. if (in_array($package, static::$_protected))
  103. {
  104. throw new Exception('Package "' . $package . '" cannot be uninstalled.');
  105. return false;
  106. }
  107. \Cli::write('Package "' . $package . '" was uninstalled.', 'yellow');
  108. \File::delete_dir($package_folder);
  109. }
  110. public static function info($cell = null)
  111. {
  112. // Make sure something is set
  113. if ($cell === null)
  114. {
  115. static::help();
  116. return;
  117. }
  118. $request_url = static::$_api_url.'cells/show.json?name='.urlencode($cell);
  119. $response = json_decode(@file_get_contents($request_url), true);
  120. if ( ! $response)
  121. {
  122. throw new Exception('No response from the API. Perhaps check your internet connection?');
  123. }
  124. else if (empty($response['cell']))
  125. {
  126. throw new Exception('Could not find the cell "' . $cell . '".');
  127. }
  128. var_dump($response);
  129. }
  130. public static function help()
  131. {
  132. $output = <<<HELP
  133. Usage:
  134. oil cell <sub-command> <cell-name>
  135. Description:
  136. Packages containing extra functionality can be downloaded (or git cloned) simply with
  137. the following commands.
  138. Runtime options:
  139. --via-zip # Download a ZIP file instead of using Git or Hg.
  140. Examples:
  141. oil cell list
  142. oil cell search <keyword>
  143. oil cell show <cell-name>
  144. oil cell install <cell-name>
  145. oil cell uninstall <cell-name>
  146. Documentation:
  147. http://docs.fuelphp.com/packages/oil/cell.html
  148. HELP;
  149. \Cli::write($output);
  150. }
  151. protected static function _use_git()
  152. {
  153. exec('which git', $output);
  154. // If this is a valid path to git, use it instead of just "git"
  155. if (file_exists($line = trim(current($output))))
  156. {
  157. static::$git = $line;
  158. }
  159. unset($output);
  160. // Double check git is installed (windows will fail step 1)
  161. exec(static::$git . ' --version', $output);
  162. preg_match('#^(git version)#', current($output), $matches);
  163. // If we have a match, use Git!
  164. return ! empty($matches[0]);
  165. }
  166. protected static function _download_package_zip($zip_url, $package, $version)
  167. {
  168. // Make the folder so we can extract the ZIP to it
  169. mkdir($tmp_folder = APPPATH . 'tmp/' . $package . '-' . time());
  170. $zip_file = $tmp_folder . '.zip';
  171. @copy($zip_url, $zip_file);
  172. $unzip = new \Unzip;
  173. $files = $unzip->extract($zip_file, $tmp_folder);
  174. // Grab the first folder out of it (we dont know what it's called)
  175. list($tmp_package_folder) = glob($tmp_folder.'/*', GLOB_ONLYDIR);
  176. $package_folder = PKGPATH . $package;
  177. // Move that folder into the packages folder
  178. rename($tmp_package_folder, $package_folder);
  179. unlink($zip_file);
  180. rmdir($tmp_folder);
  181. foreach ($files as $file)
  182. {
  183. $path = str_replace($tmp_package_folder, $package_folder, $file);
  184. chmod($path, octdec(755));
  185. \Cli::write("\t" . $path);
  186. }
  187. }
  188. public static function _clone_package_repo($repo_url, $package, $version)
  189. {
  190. // TODO Make this magic
  191. // $package_folder = str_replace(realpath(__DIR__.'/').'/', '', PKGPATH.$package);
  192. $package_folder = 'fuel/packages/'.$package;
  193. // Clone to the package path
  194. passthru(static::$_git_binary.' submodule add '.$repo_url.' '.$package_folder);
  195. passthru(static::$_git_binary.' submodule update');
  196. \Cli::write('');
  197. }
  198. }
  199. /* End of file package.php */