• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

miaoxing / plugin / 5712595623

pending completion
5712595623

push

github

twinh
feat(plugin): 安装插件时,同时安装依赖的插件

0 of 30 new or added lines in 3 files covered. (0.0%)

2 existing lines in 2 files now uncovered.

918 of 2349 relevant lines covered (39.08%)

18.3 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

4.26
/src/BasePlugin.php
1
<?php
2

3
namespace Miaoxing\Plugin;
4

5
use Miaoxing\Plugin\Service\Ret;
6

7
/**
8
 * @mixin \EnvMixin
9
 * @mixin \ViewMixin
10
 * @mixin \EventMixin
11
 * @mixin \LoggerMixin
12
 * @mixin \ReqMixin
13
 * @mixin \StrMixin
14
 * @mixin \AppMixin
15
 * @mixin \PluginMixin
16
 */
17
abstract class BasePlugin extends \Miaoxing\Plugin\BaseService
18
{
19
    /**
20
     * 插件的简短名称
21
     *
22
     * @var string
23
     */
24
    protected $name = '';
25

26
    /**
27
     * 插件的版本号
28
     *
29
     * @var string
30
     * @see http://semver.org/lang/zh-CN/ 语义化版本2.0.0
31
     */
32
    protected $version = '1.0.0';
33

34
    /**
35
     * 插件的功能,使用说明描述
36
     *
37
     * @var string
38
     */
39
    protected $description = '';
40

41
    /**
42
     * 插件的唯一数字代码
43
     *
44
     * 用于错误码等
45
     *
46
     * @var int
47
     */
48
    protected $code;
49

50
    /**
51
     * The web path for this plugin
52
     *
53
     * @var string
54
     */
55
    protected $basePath;
56

57
    /**
58
     * 安装当前插件
59
     *
60
     * @return Ret
61
     */
62
    public function install()
63
    {
64
        return suc('Install success');
×
65
    }
66

67
    /**
68
     * 卸载当前插件
69
     *
70
     * @return Ret
71
     */
72
    public function uninstall()
73
    {
74
        return suc('Uninstall success');
×
75
    }
76

77
    /**
78
     * 获取插件基本信息
79
     *
80
     * @return array
81
     */
82
    public function toArray()
83
    {
84
        return [
85
            'id' => $this->getId(),
×
86
            'name' => $this->name,
×
87
            'version' => $this->version,
×
88
            'description' => $this->description,
×
89
        ];
90
    }
91

92
    /**
93
     * @return int
94
     */
95
    public function getCode()
96
    {
97
        return $this->code;
×
98
    }
99

100
    /**
101
     * 获取插件ID
102
     *
103
     * @return string
104
     */
105
    public function getId()
106
    {
107
        $class = static::class;
×
108
        $id = lcfirst(explode('\\', $class)[1]);
×
109
        $id = $this->str->dash($id);
×
110

111
        return $id;
×
112
    }
113

114
    /**
115
     * Returns the web path for this plugin
116
     *
117
     * @return string
118
     */
119
    public function getBasePath()
120
    {
121
        if (!$this->basePath) {
3✔
122
            $class = new \ReflectionClass($this);
×
123
            $this->basePath = substr(dirname($class->getFileName()), strlen(getcwd()) + 1);
×
124

125
            // TODO 改为默认
126
            if ('Miaoxing' == substr($class->getName(), 0, 8)) {
×
127
                $this->basePath = dirname($this->basePath);
×
128
            }
129
        }
130

131
        return $this->basePath;
3✔
132
    }
133

134
    /**
135
     * @return array
136
     */
137
    public function getControllerMap()
138
    {
139
        $basePath = $this->getBasePath() . '/src';
×
140

141
        return wei()->classMap->generate([$basePath], '/Controller/{*,*/*}.php', 'Controller', false);
×
142
    }
143

144
    public function getDepIds(): array
145
    {
NEW
146
        $ids = [];
×
NEW
147
        $composer = json_decode(file_get_contents($this->getBasePath() . '/composer.json'), true);
×
NEW
148
        $require = $composer['require'] ?? [];
×
149

NEW
150
        foreach ($require as $name => $version) {
×
NEW
151
            $id = explode('/', $name)[1] ?? '';
×
NEW
152
            if (!$id) {
×
NEW
153
                continue;
×
154
            }
155

NEW
156
            $plugin = $this->plugin->getById($id);
×
NEW
157
            if ($plugin) {
×
NEW
158
                array_unshift($ids, $id);
×
NEW
159
                $ids = array_merge($plugin->getDepIds(), $ids);
×
160
            }
161
        }
162

NEW
163
        return array_unique($ids);
×
164
    }
165

166
    /**
167
     * 加载插件的各项资源
168
     *
169
     * @param string $id
170
     */
171
    protected function initResources($id = null)
172
    {
173
        $id || $id = $this->getId();
×
174
        $plugin = $this->plugin->getById($id);
×
175
        $basePath = $plugin->getBasePath();
×
176

177
        $class = static::class;
×
178
        $namespace = substr($class, 0, strrpos($class, '\\'));
×
179

180
        // 1. 加载项目配置
181
        $this->env->loadConfigDir($basePath . '/configs');
×
182

183
        // 2. 加载项目服务类
184
        $serviceDir = $basePath . '/src/Service';
×
185
        if (is_dir($serviceDir)) {
×
186
            $this->wei->import($serviceDir, $namespace . '\Service');
×
187
        }
188

189
        // 3. 控制器继承
190
        $this->app->setControllerFormat($namespace . '\Controller\%controller%');
×
191

192
        // 4. 视图继承
193
        $this->view->setDirs([$basePath . '/views'] + $this->view->getDirs());
×
194
    }
195

196
    /**
197
     * Output a rendered template by current event template
198
     *
199
     * @param array $data
200
     */
201
    protected function display($data = [])
202
    {
203
        // eg onScript
204
        $function = debug_backtrace(\DEBUG_BACKTRACE_IGNORE_ARGS, 2)[1]['function'];
×
205
        $event = lcfirst(substr($function, 2));
×
206
        $id = $this->getId();
×
207

208
        // eg @plugin/plugin/script.php
209
        $name = '@' . $id . '/' . $id . '/' . $event . $this->view->getExtension();
×
210

211
        $this->view->display($name, $data);
×
212
    }
213
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc