ICanBoogie/Prototype v2.3.0
  • Namespace
  • Class

Namespaces

  • ICanBoogie
    • Prototype

Classes

  • Prototype
  • Prototyped

Traits

  • PrototypeTrait
  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 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 
<?php

/*
 * This file is part of the ICanBoogie package.
 *
 * (c) Olivier Laviale <olivier.laviale@gmail.com>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace ICanBoogie;

use ICanBoogie\Prototype\MethodNotDefined;

/**
 * Manages the prototype methods that may be bound to classes using {@link PrototypeTrait}.
 */
class Prototype implements \ArrayAccess, \IteratorAggregate
{
    /**
     * Prototypes built per class.
     *
     * @var Prototype[]
     */
    static protected $prototypes = [];

    /**
     * Pool of prototype methods per class.
     *
     * @var array
     */
    static protected $pool;

    /**
     * Returns the prototype associated with the specified class or object.
     *
     * @param string|object $class Class name or instance.
     *
     * @return Prototype
     */
    static public function from($class)
    {
        if (is_object($class))
        {
            $class = get_class($class);
        }

        if (empty(self::$prototypes[$class]))
        {
            self::$prototypes[$class] = new static($class);
        }

        return self::$prototypes[$class];
    }

    /**
     * Defines many prototype methods in a single call.
     *
     * @param array $config
     */
    static public function configure(array $config)
    {
        if (!$config)
        {
            return;
        }

        self::update_pool($config);

        foreach (self::$prototypes as $class => $prototype)
        {
            $prototype->consolidated_methods = null;

            if (empty($config[$class]))
            {
                continue;
            }

            $prototype->methods = $config[$class] + $prototype->methods;
        }
    }

    /**
     * Updates the method pool with additional bindings.
     *
     * @param array $bindings
     *
     * @return array
     */
    static private function update_pool(array $bindings)
    {
        $pool = &self::$pool;

        if (!$pool)
        {
            $pool = $bindings;

            return;
        }

        $intersect = array_intersect_key($bindings, $pool);
        $pool += array_diff_key($bindings, $pool);

        foreach ($intersect as $class => $methods)
        {
            $pool[$class] = array_merge($pool[$class], $methods);
        }
    }

    /**
     * Class associated with the prototype.
     *
     * @var string
     */
    protected $class;

    /**
     * Parent prototype.
     *
     * @var Prototype
     */
    protected $parent;

    /**
     * Methods defined by the prototype.
     *
     * @var callable[]
     */
    protected $methods = [];

    /**
     * Methods defined by the prototypes chain.
     *
     * @var callable[]|null
     */
    protected $consolidated_methods;

    /**
     * Creates a prototype for the specified class.
     *
     * @param string $class
     */
    protected function __construct($class)
    {
        $this->class = $class;

        $parent_class = get_parent_class($class);

        if ($parent_class)
        {
            $this->parent = static::from($parent_class);
        }

        if (isset(self::$pool[$class]))
        {
            $this->methods = self::$pool[$class];
        }
    }

    /**
     * Consolidate the methods of the prototype.
     *
     * The method creates a single array from the prototype methods and those of its parents.
     *
     * @return callable[]
     */
    protected function get_consolidated_methods()
    {
        if ($this->consolidated_methods !== null)
        {
            return $this->consolidated_methods;
        }

        $methods = $this->methods;

        if ($this->parent)
        {
            $methods += $this->parent->get_consolidated_methods();
        }

        return $this->consolidated_methods = $methods;
    }

    /**
     * Revokes the consolidated methods of the prototype.
     *
     * The method must be invoked when prototype methods are modified.
     */
    protected function revoke_consolidated_methods()
    {
        $class = $this->class;

        foreach (self::$prototypes as $prototype)
        {
            if (!is_subclass_of($prototype->class, $class))
            {
                continue;
            }

            $prototype->consolidated_methods = null;
        }

        $this->consolidated_methods = null;
    }

    /**
     * Adds or replaces the specified method of the prototype.
     *
     * @param string $method The name of the method.
     *
     * @param callable $callback
     */
    public function offsetSet($method, $callback)
    {
        self::$prototypes[$this->class]->methods[$method] = $callback;

        $this->revoke_consolidated_methods();
    }

    /**
     * Removed the specified method from the prototype.
     *
     * @param string $method The name of the method.
     */
    public function offsetUnset($method)
    {
        unset(self::$prototypes[$this->class]->methods[$method]);

        $this->revoke_consolidated_methods();
    }

    /**
     * Checks if the prototype defines the specified method.
     *
     * @param string $method The name of the method.
     *
     * @return bool
     */
    public function offsetExists($method)
    {
        $methods = $this->consolidated_methods !== null
            ? $this->consolidated_methods
            : $this->get_consolidated_methods();

        return isset($methods[$method]);
    }

    /**
     * Returns the callback associated with the specified method.
     *
     * @param string $method The name of the method.
     *
     * @throws MethodNotDefined if the method is not defined.
     *
     * @return callable
     */
    public function offsetGet($method)
    {
        $methods = $this->consolidated_methods !== null
            ? $this->consolidated_methods
            : $this->get_consolidated_methods();

        if (!isset($methods[$method]))
        {
            throw new MethodNotDefined($method, $this->class);
        }

        return $methods[$method];
    }

    /**
     * Returns an iterator for the prototype methods.
     */
    public function getIterator()
    {
        $methods = $this->consolidated_methods !== null
            ? $this->consolidated_methods
            : $this->get_consolidated_methods();

        return new \ArrayIterator($methods);
    }
}
ICanBoogie/Prototype v2.3.0 API documentation generated by ApiGen