bundler.php 3.2 KB

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