-
Notifications
You must be signed in to change notification settings - Fork 13
/
bundler.php
210 lines (174 loc) · 4.87 KB
/
bundler.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
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
205
206
207
208
209
210
<?php namespace Laravel\CLI\Tasks\Bundle; defined('DS') or die('No direct script access.');
use Laravel\IoC;
use Laravel\File;
use Laravel\Bundle;
use Laravel\CLI\Tasks\Task;
class Bundler extends Task {
/**
* 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;
}
/**
* Install the given bundles into the application.
*
* @param array $bundles
* @return void
*/
public function install($bundles)
{
foreach ($this->get($bundles) as $bundle)
{
if (Bundle::exists($bundle['name']))
{
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.
$path = path('bundle').$this->path($bundle);
$this->download($bundle, $path);
echo "Bundle [{$bundle['name']}] has been installed!".PHP_EOL;
}
}
/**
* Upgrade the given bundles for the application.
*
* @param array $bundles
* @return void
*/
public function upgrade($bundles)
{
if (count($bundles) == 0) $bundles = Bundle::names();
foreach ($bundles as $name)
{
if ( ! Bundle::exists($name))
{
echo "Bundle [{$name}] is not installed!";
continue;
}
// First we want to retrieve the information for the bundle,
// such as where it is currently installed. This will let
// us upgrade the bundle into the same path in which it
// is already installed.
$bundle = Bundle::get($name);
// 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 have no way of
// knowing which provider was used to install.
$response = $this->retrieve($name);
if ($response['status'] == 'not-found')
{
continue;
}
// Once we have the bundle information from the API,
// we'll simply recursively delete the bundle and
// then re-download it using the provider.
File::rmdir($bundle->location);
$this->download($response['bundle'], $bundle->location);
echo "Bundle [{$name}] has been upgraded!".PHP_EOL;
}
}
/**
* Publish bundle assets to the public directory.
*
* @param array $bundles
* @return void
*/
public function publish($bundles)
{
// If no bundles are passed to the command, we'll just gather
// all of the installed bundle names and publish the assets
// for each of the bundles to the public directory.
if (count($bundles) == 0) $bundles = Bundle::names();
$publisher = IoC::resolve('bundle.publisher');
foreach ($bundles as $bundle)
{
$publisher->publish($bundle);
}
}
/**
* 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
// the bundle into the application. We'll verify that the bundle
// exists and the API is responding for each bundle.
$response = $this->retrieve($bundle);
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
// dependencies into the array of responses so that they are
// installed along with the consuming dependency.
$bundle = $response['bundle'];
$responses[] = $bundle;
$dependencies = $this->get($bundle['dependencies']);
$responses = array_merge($responses, $dependencies);
}
return $responses;
}
/**
* Install a bundle using a provider.
*
* @param string $bundle
* @param string $path
* @return void
*/
protected function download($bundle, $path)
{
$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']);
}
}