bundler.php 4.7 KB

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