ICanBoogie/Prototype 3.0.x
  • 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 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 
<?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 instances per class.
     *
     * @var Prototype[]
     */
    static private $prototypes = [];

    /**
     * Prototype methods per class.
     *
     * @var array
     */
    static private $bindings;

    /**
     * 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);
        }

        $prototype = &self::$prototypes[$class];

        return $prototype ?: $prototype = new static($class);
    }

    /**
     * Defines prototype methods.
     *
     * @param array $bindings
     */
    static public function bind(array $bindings)
    {
        if (!$bindings)
        {
            return;
        }

        self::update_bindings($bindings);
        self::update_instances($bindings);
    }

    /**
     * Defines prototype methods.
     *
     * @param array $bindings
     *
     * @deprecated
     * @see bind()
     */
    static public function configure(array $bindings)
    {
        self::bind($bindings);
    }

    /**
     * Updates prototype methods with bindings.
     *
     * @param array $bindings
     */
    static private function update_bindings(array $bindings)
    {
        $current = &self::$bindings;

        if (!$current)
        {
            $current = $bindings;
        }

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

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

    /**
     * Updates instances with bindings.
     *
     * @param array $bindings
     */
    static private function update_instances(array $bindings)
    {
        foreach (self::$prototypes as $class => $prototype)
        {
            $prototype->consolidated_methods = null;

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

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

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

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

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

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

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

        $parent_class = get_parent_class($class);

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

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

    /**
     * Returns the consolidated methods of the prototype.
     *
     * @return callable[]
     */
    private function get_consolidated_methods()
    {
        $consolidated_methods = &$this->consolidated_methods;

        if ($consolidated_methods !== null)
        {
            return $consolidated_methods;
        }

        return $consolidated_methods = $this->consolidate_methods();
    }

    /**
     * Consolidate the methods of the prototype.
     *
     * The method creates a single array from the prototype methods and those of its parents.
     *
     * @return callable[]
     */
    private function consolidate_methods()
    {
        $methods = $this->methods;

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

        return $methods;
    }

    /**
     * Revokes the consolidated methods of the prototype.
     *
     * The method must be invoked when prototype methods are modified.
     */
    private 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;

        if ($methods === null) {
            $methods = $this->consolidate_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;

        if ($methods === null) {
            $methods = $this->consolidate_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;

        if ($methods === null) {
            $methods = $this->consolidate_methods();
        }

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