| 1 |
<?php |
| 2 |
/** |
| 3 |
* |
| 4 |
*/ |
| 5 |
|
| 6 |
/** |
| 7 |
* |
| 8 |
*/ |
| 9 |
class CJTEventsDefinition { |
| 10 |
|
| 11 |
/** |
| 12 |
* put your comment there... |
| 13 |
* |
| 14 |
* @var mixed |
| 15 |
*/ |
| 16 |
protected $bases = array(); |
| 17 |
|
| 18 |
/** |
| 19 |
* put your comment there... |
| 20 |
* |
| 21 |
* @var mixed |
| 22 |
*/ |
| 23 |
protected $definitions = array(); |
| 24 |
|
| 25 |
/** |
| 26 |
* put your comment there... |
| 27 |
* |
| 28 |
*/ |
| 29 |
public function __construct() {} |
| 30 |
|
| 31 |
/** |
| 32 |
* put your comment there... |
| 33 |
* |
| 34 |
* @param mixed $className |
| 35 |
*/ |
| 36 |
public function addBaseClass($className, $options = array()) { |
| 37 |
$this->bases[$className] = $options; |
| 38 |
// Chaining! |
| 39 |
return $this; |
| 40 |
} |
| 41 |
|
| 42 |
/** |
| 43 |
* put your comment there... |
| 44 |
* |
| 45 |
* @param mixed $class |
| 46 |
* @param mixed $options |
| 47 |
*/ |
| 48 |
public function define($className, $options = array()) { |
| 49 |
$definition = array(); |
| 50 |
$definition['options'] = $options; |
| 51 |
// Get class properties to find out the events! |
| 52 |
$class = new ReflectionClass($className); |
| 53 |
$properties = $class->getProperties(ReflectionProperty::IS_PROTECTED); |
| 54 |
$values = $class->getDefaultProperties(); |
| 55 |
// protected (static + non-static) represent an event! |
| 56 |
foreach ($properties as $property) { |
| 57 |
$propertyName = $property->getName(); |
| 58 |
$propertyClass = $property->getDeclaringClass()->getName(); |
| 59 |
if (strpos($propertyName, $options['prefix']) === 0) { |
| 60 |
// Get event properties! |
| 61 |
$eventName = substr($propertyName, strlen($options['prefix'])); |
| 62 |
// If the event is already defined inherits the options! |
| 63 |
$inheritedOptions = array(); |
| 64 |
if (isset($this->definitions[$propertyClass]['events'][$propertyName])) { |
| 65 |
$inheritedOptions = $this->definitions[$propertyClass]['events'][$propertyName]['type']; |
| 66 |
} |
| 67 |
$definition['events'][$propertyName] = array( |
| 68 |
'fullName' => $propertyName, |
| 69 |
'name' => $eventName, |
| 70 |
'class' => $propertyClass, |
| 71 |
'id' => $eventName, |
| 72 |
'type' => array_merge($options, $inheritedOptions, ((array) $values[$propertyName])) |
| 73 |
); |
| 74 |
} |
| 75 |
} |
| 76 |
$this->definitions[$className] = $this->extend($definition); |
| 77 |
return $this; |
| 78 |
} |
| 79 |
|
| 80 |
/** |
| 81 |
* put your comment there... |
| 82 |
* |
| 83 |
* @param mixed $definition |
| 84 |
*/ |
| 85 |
protected function & extend(& $definition) { |
| 86 |
// Extend events from all base classes! |
| 87 |
foreach ($this->bases as $baseClassName => $options) { |
| 88 |
$baseClass = $this->get($baseClassName); |
| 89 |
foreach (((array) $baseClass['events']) as $name => $event) { |
| 90 |
$event['type'] = array_merge($event['type'], $options); |
| 91 |
$definition['events'][$name] = $event; |
| 92 |
} |
| 93 |
} |
| 94 |
return $definition; |
| 95 |
} |
| 96 |
|
| 97 |
/** |
| 98 |
* put your comment there... |
| 99 |
* |
| 100 |
* @param mixed $class |
| 101 |
*/ |
| 102 |
public function get($class) { |
| 103 |
return $this->definitions[$class]; |
| 104 |
} |
| 105 |
|
| 106 |
} // End class. |
| 107 |
|