forked from featherforums/feather
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExtension.php
More file actions
77 lines (67 loc) · 1.66 KB
/
Copy pathExtension.php
File metadata and controls
77 lines (67 loc) · 1.66 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
<?php namespace Feather\Extensions;
use Illuminate\Container;
class Extension {
/**
* Laravel application instance.
*
* @var Illuminate\Container
*/
protected $app;
/**
* Create a new extension instance.
*
* @param Illuminate\Container $app
* @return void
*/
public function __construct(Container $app)
{
$this->app = $app;
}
/**
* Listen for an event and fire a method or closure handler.
*
* @param string $event
* @param string|Closure $handler
* @return void
*/
public function listen($event, $handler)
{
$this->bindEvent('listen', $event, $handler);
}
/**
* Overrides existing events listening for an event and fire
* a method or closure handler.
*
* @param string $event
* @param string|Closure $handler
* @return void
*/
public function override($event, $handler)
{
$this->bindEvent('override', $event, $handler);
}
/**
* Binds events for the extension.
*
* @param string $type
* @param string $event
* @param string|Closure $handler
* @return void
*/
private function bindEvent($type, $event, $handler)
{
// Set the current extension instance so we can use it within the event closure.
$extension = $this;
$this->app['events']->$type($event, function($parameters = array()) use ($extension, $handler)
{
// If the handler is a callable closure then we'll execute the closure
// and return its result.
if (is_callable($handler))
{
return $handler($parameters);
}
// Finally assume that the handler is a method on the extension itself.
return call_user_func_array(array($extension, $handler), $parameters);
});
}
}