bundler.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php namespace Laravel\CLI\Tasks\Bundle; defined('APP_PATH') or die('No direct script access.');
  2. use Laravel\IoC;
  3. use Laravel\Bundle;
  4. use Laravel\CLI\Tasks\Task;
  5. class Bundler extends Task {
  6. /**
  7. * Install the given bundles into the application.
  8. *
  9. * @param array $bundles
  10. * @return void
  11. */
  12. public function install($bundles)
  13. {
  14. $publisher = IoC::resolve('bundle.publisher');
  15. foreach ($this->get($bundles) as $bundle)
  16. {
  17. if (is_dir(BUNDLE_PATH.$bundle['name']))
  18. {
  19. echo "Bundle {$bundle['name']} is already installed.";
  20. continue;
  21. }
  22. // Once we have the bundle information, we can resolve an instance
  23. // of a provider and install the bundle into the application and
  24. // all of its registered dependencies as well.
  25. //
  26. // Each bundle provider implements the Provider interface and
  27. // is repsonsible for retrieving the bundle source from its
  28. // hosting party and installing it into the application.
  29. $provider = "bundle.provider: {$bundle['provider']}";
  30. IoC::resolve($provider)->install($bundle);
  31. $publisher->publish($bundle['name']);
  32. }
  33. }
  34. /**
  35. * Publish bundle assets to the public directory.
  36. *
  37. * @param array $bundles
  38. * @return void
  39. */
  40. public function publish($bundles)
  41. {
  42. // If no bundles are passed to the command, we'll just gather all
  43. // of the installed bundle names and publish the assets for each
  44. // of the bundles to the public directory.
  45. if (count($bundles) == 0) $bundles = Bundle::all();
  46. $publisher = IoC::resolve('bundle.publisher');
  47. foreach ($bundles as $bundle)
  48. {
  49. $publisher->publish($bundle['name']);
  50. }
  51. }
  52. /**
  53. * Gather all of the bundles from the bundle repository.
  54. *
  55. * @param array $bundles
  56. * @return array
  57. */
  58. protected function get($bundles)
  59. {
  60. $responses = array();
  61. $repository = IoC::resolve('bundle.repository');
  62. // This method is primarily responsible for gathering the data
  63. // for all bundles that need to be installed. This allows us
  64. // to verify the existence of the bundle before even getting
  65. // started on the actual installation process.
  66. foreach ($bundles as $bundle)
  67. {
  68. // First we'll call the bundle repository to gather the bundle data
  69. // array, which contains all of the information needed to install
  70. // the bundle into the application. We'll verify that the bundle
  71. // exists and the API is responding for each bundle.
  72. $response = $repository->get($bundle);
  73. if ( ! $response)
  74. {
  75. throw new \Exception("The bundle API is not responding.");
  76. }
  77. if ($response['status'] == 'not-found')
  78. {
  79. throw new \Exception("There is not a bundle named [$bundle].");
  80. }
  81. // If the bundle was retrieved successfully, we will add it to
  82. // our array of bundles, as well as merge all of the bundle's
  83. // dependencies into the array of responses so that they are
  84. // installed along with the consuming dependency.
  85. $bundle = $response['bundle'];
  86. $responses[] = $bundle;
  87. $responses = array_merge($responses, $this->get($bundle['dependencies']));
  88. }
  89. return $responses;
  90. }
  91. }