bundler.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. <?php namespace Laravel\CLI\Tasks\Bundle; defined('DS') or die('No direct script access.');
  2. use Laravel\IoC;
  3. use Laravel\File;
  4. use Laravel\Cache;
  5. use Laravel\Bundle;
  6. use Laravel\Request;
  7. use Laravel\CLI\Tasks\Task;
  8. class Bundler extends Task {
  9. /**
  10. * The bundle API repository.
  11. *
  12. * @var Repository
  13. */
  14. protected $repository;
  15. /**
  16. * Create a new bundle manager task.
  17. *
  18. * @param Repository $repository
  19. * @return void
  20. */
  21. public function __construct($repository)
  22. {
  23. $this->repository = $repository;
  24. }
  25. /**
  26. * Install the given bundles into the application.
  27. *
  28. * @param array $bundles
  29. * @return void
  30. */
  31. public function install($bundles)
  32. {
  33. foreach ($this->get($bundles) as $bundle)
  34. {
  35. if (Bundle::exists($bundle['name']))
  36. {
  37. echo "Bundle {$bundle['name']} is already installed.";
  38. continue;
  39. }
  40. // Once we have the bundle information, we can resolve an instance
  41. // of a provider and install the bundle into the application and
  42. // all of its registered dependencies as well.
  43. //
  44. // Each bundle provider implements the Provider interface and
  45. // is responsible for retrieving the bundle source from its
  46. // hosting party and installing it into the application.
  47. $path = path('bundle').$this->path($bundle);
  48. echo "Fetching [{$bundle['name']}]...";
  49. $this->download($bundle, $path);
  50. echo "done! Bundle installed.".PHP_EOL;
  51. }
  52. }
  53. /**
  54. * Uninstall the given bundles from the application.
  55. *
  56. * @param array $bundles
  57. * @return void
  58. */
  59. public function uninstall($bundles)
  60. {
  61. if (count($bundles) == 0)
  62. {
  63. throw new \Exception("Tell me what bundle to uninstall.");
  64. }
  65. foreach ($bundles as $name)
  66. {
  67. if ( ! Bundle::exists($name))
  68. {
  69. echo "Bundle [{$name}] is not installed.";
  70. continue;
  71. }
  72. echo "Uninstalling [{$name}]...".PHP_EOL;
  73. $migrator = IoC::resolve('task: migrate');
  74. $migrator->reset($name);
  75. $publisher = IoC::resolve('bundle.publisher');
  76. $publisher->unpublish($name);
  77. $location = Bundle::path($name);
  78. File::rmdir($location);
  79. echo "Bundle [{$name}] has been uninstalled!".PHP_EOL;
  80. }
  81. echo "Now, you have to remove those bundle from your application/bundles.php".PHP_EOL;
  82. }
  83. /**
  84. * Upgrade the given bundles for the application.
  85. *
  86. * @param array $bundles
  87. * @return void
  88. */
  89. public function upgrade($bundles)
  90. {
  91. if (count($bundles) == 0) $bundles = Bundle::names();
  92. foreach ($bundles as $name)
  93. {
  94. if ( ! Bundle::exists($name))
  95. {
  96. echo "Bundle [{$name}] is not installed!";
  97. continue;
  98. }
  99. // First we want to retrieve the information for the bundle, such as
  100. // where it is currently installed. This will allow us to upgrade
  101. // the bundle into it's current installation path.
  102. $location = Bundle::path($name);
  103. // If the bundle exists, we will grab the data about the bundle from
  104. // the API so we can make the right bundle provider for the bundle,
  105. // since we don't know the provider used to install.
  106. $response = $this->retrieve($name);
  107. if ($response['status'] == 'not-found')
  108. {
  109. continue;
  110. }
  111. // Once we have the bundle information from the API, we'll simply
  112. // recursively delete the bundle and then re-download it using
  113. // the correct provider assigned to the bundle.
  114. File::rmdir($location);
  115. $this->download($response['bundle'], $location);
  116. echo "Bundle [{$name}] has been upgraded!".PHP_EOL;
  117. }
  118. }
  119. /**
  120. * Gather all of the bundles from the bundle repository.
  121. *
  122. * @param array $bundles
  123. * @return array
  124. */
  125. protected function get($bundles)
  126. {
  127. $responses = array();
  128. foreach ($bundles as $bundle)
  129. {
  130. // First we'll call the bundle repository to gather the bundle data
  131. // array, which contains all of the information needed to install
  132. // the bundle into the Laravel application.
  133. $response = $this->retrieve($bundle);
  134. if ($response['status'] == 'not-found')
  135. {
  136. throw new \Exception("There is no bundle named [$bundle].");
  137. }
  138. // If the bundle was retrieved successfully, we will add it to
  139. // our array of bundles, as well as merge all of the bundle's
  140. // dependencies into the array of responses.
  141. $bundle = $response['bundle'];
  142. $responses[] = $bundle;
  143. // We'll also get the bundle's declared dependencies so they
  144. // can be installed along with the bundle, making it easy
  145. // to install a group of bundles.
  146. $dependencies = $this->get($bundle['dependencies']);
  147. $responses = array_merge($responses, $dependencies);
  148. }
  149. return $responses;
  150. }
  151. /**
  152. * Publish bundle assets to the public directory.
  153. *
  154. * @param array $bundles
  155. * @return void
  156. */
  157. public function publish($bundles)
  158. {
  159. if (count($bundles) == 0) $bundles = Bundle::names();
  160. array_walk($bundles, array(IoC::resolve('bundle.publisher'), 'publish'));
  161. }
  162. /**
  163. * Delete bundle assets from the public directory.
  164. *
  165. * @param array $bundles
  166. * @return void
  167. */
  168. public function unpublish($bundles)
  169. {
  170. if (count($bundles) == 0) $bundles = Bundle::names();
  171. array_walk($bundles, array(IoC::resolve('bundle.publisher'), 'unpublish'));
  172. }
  173. /**
  174. * Install a bundle using a provider.
  175. *
  176. * @param string $bundle
  177. * @param string $path
  178. * @return void
  179. */
  180. protected function download($bundle, $path)
  181. {
  182. $provider = "bundle.provider: {$bundle['provider']}";
  183. IoC::resolve($provider)->install($bundle, $path);
  184. }
  185. /**
  186. * Retrieve a bundle from the repository.
  187. *
  188. * @param string $bundle
  189. * @return array
  190. */
  191. protected function retrieve($bundle)
  192. {
  193. $response = $this->repository->get($bundle);
  194. if ( ! $response)
  195. {
  196. throw new \Exception("The bundle API is not responding.");
  197. }
  198. return $response;
  199. }
  200. /**
  201. * Return the path for a given bundle.
  202. *
  203. * @param array $bundle
  204. * @return string
  205. */
  206. protected function path($bundle)
  207. {
  208. return array_get($bundle, 'path', $bundle['name']);
  209. }
  210. }