bundler.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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 repsonsible 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. * Upgrade the given bundles for the application.
  55. *
  56. * @param array $bundles
  57. * @return void
  58. */
  59. public function upgrade($bundles)
  60. {
  61. if (count($bundles) == 0) $bundles = Bundle::names();
  62. foreach ($bundles as $name)
  63. {
  64. if ( ! Bundle::exists($name))
  65. {
  66. echo "Bundle [{$name}] is not installed!";
  67. continue;
  68. }
  69. // First we want to retrieve the information for the bundle, such as
  70. // where it is currently installed. This will allow us to upgrade
  71. // the bundle into it's current installation path.
  72. $location = Bundle::path($name);
  73. // If the bundle exists, we will grab the data about the bundle from
  74. // the API so we can make the right bundle provider for the bundle,
  75. // since we don't know the provider used to install.
  76. $response = $this->retrieve($name);
  77. if ($response['status'] == 'not-found')
  78. {
  79. continue;
  80. }
  81. // Once we have the bundle information from the API, we'll simply
  82. // recursively delete the bundle and then re-download it using
  83. // the correct provider assigned to the bundle.
  84. File::rmdir($location);
  85. $this->download($response['bundle'], $location);
  86. echo "Bundle [{$name}] has been upgraded!".PHP_EOL;
  87. }
  88. }
  89. /**
  90. * Gather all of the bundles from the bundle repository.
  91. *
  92. * @param array $bundles
  93. * @return array
  94. */
  95. protected function get($bundles)
  96. {
  97. $responses = array();
  98. foreach ($bundles as $bundle)
  99. {
  100. // First we'll call the bundle repository to gather the bundle data
  101. // array, which contains all of the information needed to install
  102. // the bundle into the Laravel application.
  103. $response = $this->retrieve($bundle);
  104. if ($response['status'] == 'not-found')
  105. {
  106. throw new \Exception("There is not a bundle named [$bundle].");
  107. }
  108. // If the bundle was retrieved successfully, we will add it to
  109. // our array of bundles, as well as merge all of the bundle's
  110. // dependencies into the array of responses.
  111. $bundle = $response['bundle'];
  112. $responses[] = $bundle;
  113. // We'll also get the bundle's declared dependenceis so they
  114. // can be installed along with the bundle, making it easy
  115. // to install a group of bundles.
  116. $dependencies = $this->get($bundle['dependencies']);
  117. $responses = array_merge($responses, $dependencies);
  118. }
  119. return $responses;
  120. }
  121. /**
  122. * Publish bundle assets to the public directory.
  123. *
  124. * @param array $bundles
  125. * @return void
  126. */
  127. public function publish($bundles)
  128. {
  129. if (count($bundles) == 0) $bundles = Bundle::names();
  130. array_walk($bundles, array(IoC::resolve('bundle.publisher'), 'publish'));
  131. }
  132. /**
  133. * Install a bundle using a provider.
  134. *
  135. * @param string $bundle
  136. * @param string $path
  137. * @return void
  138. */
  139. protected function download($bundle, $path)
  140. {
  141. $provider = "bundle.provider: {$bundle['provider']}";
  142. IoC::resolve($provider)->install($bundle, $path);
  143. }
  144. /**
  145. * Retrieve a bundle from the repository.
  146. *
  147. * @param string $bundle
  148. * @return array
  149. */
  150. protected function retrieve($bundle)
  151. {
  152. $response = $this->repository->get($bundle);
  153. if ( ! $response)
  154. {
  155. throw new \Exception("The bundle API is not responding.");
  156. }
  157. return $response;
  158. }
  159. /**
  160. * Return the path for a given bundle.
  161. *
  162. * @param array $bundle
  163. * @return string
  164. */
  165. protected function path($bundle)
  166. {
  167. return array_get($bundle, 'path', $bundle['name']);
  168. }
  169. }