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
<?php
namespace ICanBoogie;
class EventReflection
{
static private $instances = [];
static public function from($class)
{
if (isset(self::$instances[$class]))
{
return self::$instances[$class];
}
return self::$instances[$class] = new static($class);
}
protected $class;
protected $parameters;
protected function __construct($class)
{
$construct_reflection = new \ReflectionMethod($class, '__construct');
$parameters_reflection = $construct_reflection->getParameters();
$parameters = [];
foreach ($parameters_reflection as $parameter)
{
$parameters[$parameter->name] = $parameter;
}
$this->class = new \ReflectionClass($class);
$this->parameters = $parameters;
}
public function with(array $params)
{
$this->assert_no_extraneous($params);
$this->assert_no_missing($params);
$this->assert_no_skipped($params);
$event = $this->make_instance();
call_user_func_array([ $event, '__construct' ], $this->make_args($params));
return $event;
}
protected function assert_no_extraneous(array $params)
{
$extraneous = array_diff_key($params, $this->parameters);
if ($extraneous)
{
throw new \BadMethodCallException("The following parameters are extraneous: " . implode(', ', array_keys($extraneous)) . ".");
}
}
protected function assert_no_missing(array $params)
{
$missing = [];
foreach (array_diff_key($this->parameters, $params) as $param => $reflection)
{
if ($reflection->isDefaultValueAvailable())
{
continue;
}
$missing[$param] = $reflection;
}
if ($missing)
{
throw new \BadMethodCallException("The following parameters are required: " . implode(', ', array_keys($missing)) . ".");
}
}
protected function assert_no_skipped(array $params)
{
$skipped = array_diff_key(array_slice($this->parameters, 0, count($params)), $params);
if ($skipped)
{
throw new \BadMethodCallException("The following parameters are skipped: " . implode(', ', array_keys($skipped)) . ".");
}
}
protected function make_instance()
{
static $no_immediate_fire;
if (!$no_immediate_fire)
{
$no_immediate_fire = new \ReflectionProperty(Event::class, 'no_immediate_fire');
$no_immediate_fire->setAccessible(true);
}
$event = $this->class->newInstanceWithoutConstructor();
$no_immediate_fire->setValue($event, true);
return $event;
}
protected function make_args(array $params)
{
$args = [];
foreach(array_keys($this->parameters) as $param)
{
if (!array_key_exists($param, $params))
{
break;
}
$args[] = &$params[$param];
}
return $args;
}
}