Completed
Push — master ( 62bec2...5bc1e5 )
by Hong
02:42
created

ExtensionAwareTrait::hasExtensionMethod()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
/**
3
 * Phossa Project
4
 *
5
 * PHP version 5.4
6
 *
7
 * @category  Library
8
 * @package   Phossa2\Shared
9
 * @copyright Copyright (c) 2016 phossa.com
10
 * @license   http://mit-license.org/ MIT License
11
 * @link      http://www.phossa.com/
12
 */
13
/*# declare(strict_types=1); */
14
15
namespace Phossa2\Shared\Extension;
16
17
use Phossa2\Shared\Message\Message;
18
use Phossa2\Shared\Exception\LogicException;
19
use Phossa2\Shared\Exception\BadMethodCallException;
20
21
/**
22
 * ExtensionAwareTrait
23
 *
24
 * @package Phossa2\Shared
25
 * @author  Hong Zhang <[email protected]>
26
 * @see     ExtensionAwareInterface
27
 * @version 2.0.0
28
 * @since   2.0.0 added
29
 */
30
trait ExtensionAwareTrait
31
{
32
    /**
33
     * Methods provided by extensions
34
     *
35
     * @var    array
36
     * @access protected
37
     */
38
    protected $extension_methods = [];
39
40
    /**
41
     * @param  string $method
42
     * @param  array $arguments
43
     * @return mixed
44
     * @throws BadMethodCallException if method not found
45
     * @access public
46
     */
47
    public function __call($method, array $arguments)
48
    {
49
        if ($this->hasExtensionMethod($method)) {
50
            return $this->runExtension($method, $arguments);
51
        }
52
        throw new BadMethodCallException(
53
            Message::get(Message::MSG_METHOD_NOTFOUND, $method),
54
            Message::MSG_METHOD_NOTFOUND
55
        );
56
    }
57
58
    /**
59
     * {@inheritDoc}
60
     */
61
    public function addExtension(
62
        ExtensionInterface $ext,
63
        /*# bool */ $forceOverride = false
64
    ) {
65
        foreach ($ext->methodsAvailable() as $method) {
66
            if (isset($this->extension_methods[$method]) &&
67
                !$forceOverride) {
68
                throw new LogicException(
69
                    Message::get(Message::MSG_EXTENSION_METHOD, $method),
70
                    Message::MSG_EXTENSION_METHOD
71
                );
72
            }
73
            $this->extension_methods[$method] = $ext;
74
        }
75
        return $this;
76
    }
77
78
    /**
79
     * Is this method from extensions ?
80
     *
81
     * @param  string $method
82
     * @return bool
83
     * @access protected
84
     */
85
    protected function hasExtensionMethod(/*# string */ $method)/*# : bool */
86
    {
87
        return isset($this->extension_methods[$method]);
88
    }
89
90
    /**
91
     * Include this in server's __call()
92
     *
93
     * @param  string $method
94
     * @param  array $args
95
     * @return mixed
96
     * @access protected
97
     */
98
    protected function runExtension(/*# string */ $method, array $args)
99
    {
100
        /* @var $ext ExtensionInterface */
101
        $ext = $this->extension_methods[$method];
102
103
        // boot extension if not yet
104
        $ext->boot($this);
105
106
        // execute extesion method
107
        return call_user_func_array([$ext, $method], $args);
108
    }
109
}
110