bundler.php 4.7 KB
Newer Older
Taylor Otwell committed
1
<?php namespace Laravel\CLI\Tasks\Bundle; defined('DS') or die('No direct script access.');
2 3

use Laravel\IoC;
4
use Laravel\File;
5
use Laravel\Cache;
6
use Laravel\Bundle;
7
use Laravel\Request;
8 9 10 11 12
use Laravel\CLI\Tasks\Task;

class Bundler extends Task {

	/**
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
	 * The bundle API repository.
	 *
	 * @var Repository
	 */
	protected $repository;

	/**
	 * Create a new bundle manager task.
	 *
	 * @param  Repository  $repository
	 * @return void
	 */
	public function __construct($repository)
	{
		$this->repository = $repository;
	}

	/**
31 32 33 34 35 36 37 38 39
	 * Install the given bundles into the application.
	 *
	 * @param  array  $bundles
	 * @return void
	 */
	public function install($bundles)
	{
		foreach ($this->get($bundles) as $bundle)
		{
40
			if (Bundle::exists($bundle['name']))
41 42 43 44 45 46 47 48 49 50 51 52 53
			{
				echo "Bundle {$bundle['name']} is already installed.";

				continue;
			}

			// Once we have the bundle information, we can resolve an instance
			// of a provider and install the bundle into the application and
			// all of its registered dependencies as well.
			//
			// Each bundle provider implements the Provider interface and
			// is repsonsible for retrieving the bundle source from its
			// hosting party and installing it into the application.
54 55
			$path = path('bundle').$this->path($bundle);

56
			echo "Fetching [{$bundle['name']}]...";
Taylor Otwell committed
57

58
			$this->download($bundle, $path);
59

60
			echo "done! Bundle installed.".PHP_EOL;
61 62 63 64 65 66 67 68 69 70 71
		}
	}

	/**
	 * Upgrade the given bundles for the application.
	 *
	 * @param  array  $bundles
	 * @return void
	 */
	public function upgrade($bundles)
	{
72 73
		if (count($bundles) == 0) $bundles = Bundle::names();

74 75
		foreach ($bundles as $name)
		{
76
			if ( ! Bundle::exists($name))
77
			{
78 79 80
				echo "Bundle [{$name}] is not installed!";

				continue;
81 82
			}

83 84 85
			// First we want to retrieve the information for the bundle, such as
			// where it is currently installed. This will allow us to upgrade
			// the bundle into it's current installation path.
86
			$location = Bundle::path($name);
87

88 89 90
			// If the bundle exists, we will grab the data about the bundle from
			// the API so we can make the right bundle provider for the bundle,
			// since we don't know the provider used to install.
91
			$response = $this->retrieve($name);
92 93 94 95 96

			if ($response['status'] == 'not-found')
			{
				continue;
			}
97

98 99 100
			// Once we have the bundle information from the API, we'll simply
			// recursively delete the bundle and then re-download it using
			// the correct provider assigned to the bundle.
101
			File::rmdir($location);
102

103
			$this->download($response['bundle'], $location);
104

105
			echo "Bundle [{$name}] has been upgraded!".PHP_EOL;
106
		}
107 108 109
	}

	/**
110 111 112 113 114 115 116 117 118 119 120 121 122
	 * Gather all of the bundles from the bundle repository.
	 *
	 * @param  array  $bundles
	 * @return array
	 */
	protected function get($bundles)
	{
		$responses = array();

		foreach ($bundles as $bundle)
		{
			// First we'll call the bundle repository to gather the bundle data
			// array, which contains all of the information needed to install
123
			// the bundle into the Laravel application.
124
			$response = $this->retrieve($bundle);
125 126 127 128 129 130 131 132

			if ($response['status'] == 'not-found')
			{
				throw new \Exception("There is not a bundle named [$bundle].");
			}

			// If the bundle was retrieved successfully, we will add it to
			// our array of bundles, as well as merge all of the bundle's
133
			// dependencies into the array of responses.
134 135 136 137
			$bundle = $response['bundle'];

			$responses[] = $bundle;

138 139 140
			// We'll also get the bundle's declared dependenceis so they
			// can be installed along with the bundle, making it easy
			// to install a group of bundles.
141 142 143
			$dependencies = $this->get($bundle['dependencies']);

			$responses = array_merge($responses, $dependencies);
144 145 146 147 148
		}

		return $responses;
	}

149
	/**
150 151 152 153 154 155 156 157 158 159 160 161 162
	 * Publish bundle assets to the public directory.
	 *
	 * @param  array  $bundles
	 * @return void
	 */
	public function publish($bundles)
	{
		if (count($bundles) == 0) $bundles = Bundle::names();

		array_walk($bundles, array(IoC::resolve('bundle.publisher'), 'publish'));
	}

	/**
163 164 165 166 167 168
	 * Install a bundle using a provider.
	 *
	 * @param  string  $bundle
	 * @param  string  $path
	 * @return void
	 */
169
	protected function download($bundle, $path)
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
	{
		$provider = "bundle.provider: {$bundle['provider']}";

		IoC::resolve($provider)->install($bundle, $path);
	}

	/**
	 * Retrieve a bundle from the repository.
	 *
	 * @param  string  $bundle
	 * @return array
	 */
	protected function retrieve($bundle)
	{
		$response = $this->repository->get($bundle);

		if ( ! $response)
		{
			throw new \Exception("The bundle API is not responding.");
		}

		return $response;
	}

	/**
	 * Return the path for a given bundle.
	 *
	 * @param  array   $bundle
	 * @return string
	 */
	protected function path($bundle)
	{
		return array_get($bundle, 'path', $bundle['name']);
	}

205
}