-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathmanager.php
More file actions
244 lines (209 loc) · 4.69 KB
/
Copy pathmanager.php
File metadata and controls
244 lines (209 loc) · 4.69 KB
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
<?php namespace Feather\Components\Gear;
use Str;
use Event;
use Cache;
use Bundle;
use Request;
use FilesystemIterator;
use Feather\Core\Gear;
use InvalidArgumentException;
use Feather\Components\Foundation;
class Manager extends Foundation\Component {
/**
* Registered Gears.
*
* @var array
*/
public $gears = array();
/**
* Started Gears.
*
* @var array
*/
public $started = array();
/**
* Bootstraps the Gears, loads from database and registers with the manager.
*
* @param Feather\Components\Foundation\Application $feather
* @return void
*/
public function __construct(Foundation\Application $feather)
{
parent::__construct($feather);
Cache::forget('gears');
$manager = $this;
return Cache::sear('gears', function() use ($manager)
{
foreach(Gear::enabled() as $gear)
{
Bundle::register($gear->identifier, array(
'location' => 'path: ' . path('gears') . $gear->identifier,
'handles' => trim(str_replace('(:feather)', Bundle::option('feather', 'handles'), $gear->handles), '/'),
'auto' => (bool) $gear->auto,
'autoloads' => (array) json_decode($gear->loads)
));
if($gear->auto)
{
Bundle::start($gear->identifier);
}
$manager->register($gear);
}
});
}
/**
* Register a Gear with the manager.
*
* @param Feather\Models\Gear $gear
* @return bool
*/
public function register(Gear $gear)
{
if(!$gear instanceof Gear)
{
throw new InvalidArgumentException('Supplied Gear is a not supported type.');
}
if(starts_with($gear->location, 'path: '))
{
$path = substr($gear->location, 6);
}
else
{
$path = path('gears') . $gear->location;
}
if(file_exists($path))
{
$this->gears[$gear->identifier] = $gear;
// Start any gears that have been set to automatically start.
if($gear->auto)
{
return $this->start($gear->identifier);
}
}
return true;
}
/**
* Determines if a Gear has been registered.
*
* @param string $gear
* @return bool
*/
public function registered($gear)
{
return isset($this->gears[$gear]);
}
/**
* Start a registered Gear.
*
* @param string $gear
* @return bool
*/
public function start($gear)
{
if($this->started($gear))
{
return $this->started[$gear];
}
elseif(!$this->registered($gear))
{
return false;
}
$this->started[$gear] = new Container;
// Spin through all the files within the gears directory and those that have
// the .gear.php suffix we'll require and hopefully be able to instantiate
// a new object.
if(starts_with($this->gears[$gear]->location, 'path: '))
{
$path = substr($this->gears[$gear]->location, 6);
}
else
{
$path = path('gears') . $this->gears[$gear]->location;
}
foreach(new FilesystemIterator($path) as $file)
{
if(ends_with($file->getFilename(), '.gear.php'))
{
require_once $file->getPathname();
$name = str_replace('.gear.php', null, $file->getFilename());
$class = "\\Feather\\Gear\\{$this->gears[$gear]->identifier}\\{$name}";
if(class_exists($class))
{
$this->started[$gear][$name] = new $class;
$this->started[$gear][$name]->application($this->feather);
}
}
}
return $this->started[$gear];
}
/**
* Determines if a Gear has been started.
*
* @param string $gear
* @return bool
*/
public function started($gear)
{
return isset($this->started[$gear]);
}
/**
* Disable a gear for this run only.
*
* @param string $gear
* @return void
*/
public function disable($gear)
{
if($this->registered($gear))
{
unset($this->gears[$gear]);
}
}
/**
* Gets a Gears container.
*
* @param string $gear
* @return object
*/
public function get($gear)
{
if(!$this->started($gear) or !$this->registered($gear))
{
return false;
}
return $this->started[$gear];
}
/**
* Fire an event and implodes the returned results.
*
* @param string $event
* @param array $parameters
* @return string
*/
public function fire($event, $parameters = array())
{
return implode(PHP_EOL, array_filter(Event::fire($event, array($parameters))));
}
/**
* Fire the first event in the queue.
*
* @param string $event
* @param array $parameters
* @return string
*/
public function first($event, $parameters = array())
{
return Event::first($event, $parameters);
}
/**
* Fire the first controller event in the queue.
*
* @param object $controller
* @return mixed
*/
public function controller($event, $controller)
{
$route = Request::route();
$method = Str::lower($route->method);
return Event::first("controller: {$event} {$route->controller}@{$method}.{$route->controller_action}", array(array($controller)));
}
}