zhaojs
2023-08-02 21512703df5ddf962893003a460e75af10d4757f
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
<?php
 
declare(strict_types=1);
 
/*
 * This file is part of the EasyWeChatComposer.
 *
 * (c) 张铭阳 <mingyoungcheung@gmail.com>
 *
 * This source file is subject to the MIT license that is bundled
 * with this source code in the file LICENSE.
 */
 
namespace EasyWeChatComposer;
 
use EasyWeChat\Kernel\Contracts\EventHandlerInterface;
use Pimple\Container;
use ReflectionClass;
 
class Extension
{
    /**
     * @var \Pimple\Container
     */
    protected $app;
 
    /**
     * @var string
     */
    protected $manifestPath;
 
    /**
     * @var array|null
     */
    protected $manifest;
 
    /**
     * @param \Pimple\Container $app
     */
    public function __construct(Container $app)
    {
        $this->app = $app;
        $this->manifestPath = __DIR__.'/../extensions.php';
    }
 
    /**
     * Get observers.
     *
     * @return array
     */
    public function observers(): array
    {
        if ($this->shouldIgnore()) {
            return [];
        }
 
        $observers = [];
 
        foreach ($this->getManifest() as $name => $extra) {
            $observers = array_merge($observers, $extra['observers'] ?? []);
        }
 
        return array_map([$this, 'listObserver'], array_filter($observers, [$this, 'validateObserver']));
    }
 
    /**
     * @param mixed $observer
     *
     * @return bool
     */
    protected function isDisable($observer): bool
    {
        return in_array($observer, $this->app->config->get('disable_observers', []));
    }
 
    /**
     * Get the observers should be ignore.
     *
     * @return bool
     */
    protected function shouldIgnore(): bool
    {
        return !file_exists($this->manifestPath) || $this->isDisable('*');
    }
 
    /**
     * Validate the given observer.
     *
     * @param mixed $observer
     *
     * @return bool
     *
     * @throws \ReflectionException
     */
    protected function validateObserver($observer): bool
    {
        return !$this->isDisable($observer)
            && (new ReflectionClass($observer))->implementsInterface(EventHandlerInterface::class)
            && $this->accessible($observer);
    }
 
    /**
     * Determine whether the given observer is accessible.
     *
     * @param string $observer
     *
     * @return bool
     */
    protected function accessible($observer): bool
    {
        if (!method_exists($observer, 'getAccessor')) {
            return true;
        }
 
        return in_array(get_class($this->app), (array) $observer::getAccessor());
    }
 
    /**
     * @param mixed $observer
     *
     * @return array
     */
    protected function listObserver($observer): array
    {
        $condition = method_exists($observer, 'onCondition') ? $observer::onCondition() : '*';
 
        return [$observer, $condition];
    }
 
    /**
     * Get the easywechat manifest.
     *
     * @return array
     */
    protected function getManifest(): array
    {
        if (!is_null($this->manifest)) {
            return $this->manifest;
        }
 
        return $this->manifest = file_exists($this->manifestPath) ? require $this->manifestPath : [];
    }
}