| 1 |
<?php |
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
stubClassLoader::load('net::stubbles::events::stubEvent', |
|---|
| 10 |
'net::stubbles::events::stubEventListener' |
|---|
| 11 |
); |
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
class stubLazyEventListener extends stubSerializableObject implements stubEventListener |
|---|
| 19 |
{ |
|---|
| 20 |
|
|---|
| 21 |
* the full qualified name of the class to execute in case the event happens |
|---|
| 22 |
* |
|---|
| 23 |
* @var string |
|---|
| 24 |
*/ |
|---|
| 25 |
protected $fqClassName; |
|---|
| 26 |
|
|---|
| 27 |
|
|---|
| 28 |
* constructor |
|---|
| 29 |
* |
|---|
| 30 |
* @param string $fqClassName full qualified name of the class to execute |
|---|
| 31 |
*/ |
|---|
| 32 |
public function __construct($fqClassName) |
|---|
| 33 |
{ |
|---|
| 34 |
$this->fqClassName = $fqClassName; |
|---|
| 35 |
} |
|---|
| 36 |
|
|---|
| 37 |
|
|---|
| 38 |
* returns the class name of the lazy loaded class |
|---|
| 39 |
* |
|---|
| 40 |
* @return string |
|---|
| 41 |
*/ |
|---|
| 42 |
public function getLazyClassName() |
|---|
| 43 |
{ |
|---|
| 44 |
return $this->fqClassName; |
|---|
| 45 |
} |
|---|
| 46 |
|
|---|
| 47 |
|
|---|
| 48 |
* handles an event |
|---|
| 49 |
* |
|---|
| 50 |
* @param stubEvent $event event that triggered this event listener |
|---|
| 51 |
* @throws stubException |
|---|
| 52 |
*/ |
|---|
| 53 |
public function handleEvent(stubEvent $event) |
|---|
| 54 |
{ |
|---|
| 55 |
$this->createInstance()->handleEvent($event); |
|---|
| 56 |
} |
|---|
| 57 |
|
|---|
| 58 |
|
|---|
| 59 |
* returns true if listener should be removed after first notification or not |
|---|
| 60 |
* |
|---|
| 61 |
* @return bool |
|---|
| 62 |
* @throws stubException |
|---|
| 63 |
*/ |
|---|
| 64 |
public function autoremove() |
|---|
| 65 |
{ |
|---|
| 66 |
return $this->createInstance()->autoremove(); |
|---|
| 67 |
} |
|---|
| 68 |
|
|---|
| 69 |
|
|---|
| 70 |
* helper method to create the concrete event listener instance |
|---|
| 71 |
* |
|---|
| 72 |
* @return stubEventListener |
|---|
| 73 |
* @throws stubRuntimeException |
|---|
| 74 |
*/ |
|---|
| 75 |
protected function createInstance() |
|---|
| 76 |
{ |
|---|
| 77 |
static $instance; |
|---|
| 78 |
if (null === $instance) { |
|---|
| 79 |
$className = stubClassLoader::getNonQualifiedClassName($this->fqClassName); |
|---|
| 80 |
if (class_exists($className, false) == false) { |
|---|
| 81 |
stubClassLoader::load($this->fqClassName); |
|---|
| 82 |
} |
|---|
| 83 |
|
|---|
| 84 |
$instance = new $className(); |
|---|
| 85 |
} |
|---|
| 86 |
|
|---|
| 87 |
if (($instance instanceof stubEventListener) == false) { |
|---|
| 88 |
throw new stubRuntimeException('Lazy loaded event listener ' . $this->fqClassName . ' is not an instance of ' . $this->getPackageName() . '.stubEventListener.'); |
|---|
| 89 |
} |
|---|
| 90 |
|
|---|
| 91 |
return $instance; |
|---|
| 92 |
} |
|---|
| 93 |
} |
|---|
| 94 |
?> |
|---|