Code Coverage
 
Classes and Traits
Functions and Methods
Lines
Total
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
17 / 17
Amf\Proxy\Singleton
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
2 / 2
7
100.00% covered (success)
100.00%
17 / 17
 instance
100.00% covered (success)
100.00%
1 / 1
4
100.00% covered (success)
100.00%
8 / 8
 __callStatic
100.00% covered (success)
100.00%
1 / 1
3
100.00% covered (success)
100.00%
9 / 9
<?php
/**
 * AMF (http://amf.artera.it/).
 *
 * @link      http://amf.artera.it
 *
 * @copyright Copyright (c) 2012-2013 Artera
 * @license   http://amf.artera.it/LICENSE.txt MIT License
 */
namespace Amf\Proxy;
use ReflectionClass;
/**
 * Singleton proxy class.
 *
 * This class can be used to proxy function calls from static methods to
 * a given object.
 * When extending this class you must specify a fully qualified class name
 * in the static $classname property
 *
 * @author  Massimiliano Torromeo
 */
abstract class Singleton
{
    protected static $classname;
    protected static $instance = null;
    public static function instance($instance = false, $args = [])
    {
        if ($instance !== false) {
            static::$instance = $instance;
        }
        if (static::$instance === null) {
            if (!empty($args)) {
                $reflection = new ReflectionClass(static::$classname);
                static::$instance = $reflection->newInstanceArgs($args);
            } else {
                static::$instance = new static::$classname();
            }
        }
        return static::$instance;
    }
    /**
     * Function calls proxy method.
     *
     * This function should never be called manually. Just call the original
     * function names as static methods instead.
     *
     * @param string $name      The method name
     * @param array  $arguments The method arguments
     *
     * @return mixed The proxied method result
     */
    public static function __callStatic($name, $arguments)
    {
        $instance = static::instance();
        if (!method_exists($instance, $name) && !method_exists($instance, '__call')) {
            $class = get_class($instance);
            $trace = debug_backtrace();
            $file = $trace[0]['file'];
            $line = $trace[0]['line'];
            trigger_error("Call to undefined method $class::$name() in $file on line $line", E_USER_ERROR);
        } else {
            return call_user_func_array([$instance, $name], $arguments);
        }
    }
}