ICanBoogie/Event master
  • Namespace
  • Class

Namespaces

  • ICanBoogie

Classes

  • Event
  • EventCollection
  • EventCollectionProvider
  • EventHook
  • EventHookReflection
  • EventProfiler
  • EventReflection

Functions

  • get_events
  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;

/**
 * Creates unfired, initialized instance of events.
 */
class EventReflection
{
    static private $instances = [];

    /**
     * Returns the {@link EventReflection} instance associated with the specified event class.
     *
     * @param string $class
     *
     * @return EventReflection
     */
    static public function from($class)
    {
        if (isset(self::$instances[$class]))
        {
            return self::$instances[$class];
        }

        return self::$instances[$class] = new static($class);
    }

    /*
     * Instance
     */

    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;
    }

    /**
     * Makes unfired, initialized event instance.
     *
     * @param array $params
     *
     * @return Event
     */
    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;
    }

    /**
     * Asserts that no extraneous parameter is specified.
     *
     * @param array $params
     *
     * @throws \BadMethodCallException when an extraneous parameter is specified.
     */
    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)) . ".");
        }
    }

    /**
     * Asserts that no required parameter is missing.
     *
     * @param array $params
     *
     * @throws \BadMethodCallException when a required parameter is missing.
     */
    protected function assert_no_missing(array $params)
    {
        $missing = [];

        /* @var $reflection \ReflectionParameter */

        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)) . ".");
        }
    }

    /**
     * Asserts that no parameter is skipped.
     *
     * @param array $params
     *
     * @throws \BadMethodCallException when a parameter is skipped.
     */
    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)) . ".");
        }
    }

    /**
     * Makes event instance.
     *
     * @return Event
     */
    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;
    }

    /**
     * Makes construct arguments.
     *
     * @param array $params
     *
     * @return array
     */
    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;
    }
}
ICanBoogie/Event master API documentation generated by ApiGen