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

miaoxing / plugin / 3766633246

pending completion
3766633246

push

github

twinh
feat(plugin): `BasePage` 增加 `pageInit` 事件

0 of 1 new or added line in 1 file covered. (0.0%)

84 existing lines in 24 files now uncovered.

902 of 2275 relevant lines covered (39.65%)

19.29 hits per line

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

0.0
/src/Service/Seeder.php
1
<?php
2

3
namespace Miaoxing\Plugin\Service;
4

5
use Miaoxing\Plugin\BaseService;
6
use Miaoxing\Plugin\Seeder\BaseSeeder;
7
use Symfony\Component\Console\Output\OutputInterface;
8

9
/**
10
 * Seeder
11
 *
12
 * @mixin \DbMixin
13
 * @mixin \SchemaMixin
14
 * @mixin \ClassMapMixin
15
 */
16
class Seeder extends BaseService
17
{
18
    /**
19
     * @var array
20
     */
21
    protected $paths = [
22
        'src',
23
        'plugins/*/src',
24
    ];
25

26
    /**
27
     * @var string
28
     */
29
    protected $defaultPath = 'src/Seeder';
30

31
    /**
32
     * @var string
33
     */
34
    protected $defaultNamespace = 'App\Seeder';
35

36
    /**
37
     * @var string
38
     */
39
    protected $table = 'seeders';
40

41
    /**
42
     * @var OutputInterface|null
43
     */
44
    protected $output;
45

46
    /**
47
     * {@inheritdoc}
48
     */
49
    public function __construct(array $options = [])
50
    {
51
        parent::__construct($options);
×
52
        $this->prepareTable();
×
53
    }
54

55
    /**
56
     * Output the seeder status table
57
     */
58
    public function status()
59
    {
60
        $status = $this->getStatus();
×
61
        if (!$status) {
×
62
            $this->writeln('No seeders found.');
×
63

64
            return;
×
65
        }
66

67
        $this->writeln(' Ran?    Name ');
×
68
        $this->writeln('--------------');
×
69

70
        foreach ($status as $row) {
×
71
            if ($row['ran']) {
×
72
                $mark = '<info>Y</info>';
×
73
            } else {
74
                $mark = '<error>N</error>';
×
75
            }
76
            $this->writeln(' ' . $mark . '       ' . $row['id']);
×
77
        }
78
    }
79

80
    /**
81
     * @return array
82
     */
83
    public function getStatus(): array
84
    {
85
        $data = [];
×
86
        $ranIds = $this->getRanIds();
×
87
        $classes = $this->getSeederClasses();
×
88

89
        foreach ($classes as $id => $class) {
×
90
            $data[] = [
×
UNCOV
91
                'id' => $id,
×
92
                'ran' => in_array($id, $ranIds, true),
×
93
            ];
94
        }
95

96
        return $data;
×
97
    }
98

99
    /**
100
     * @param OutputInterface $output
101
     * @return $this
102
     * @svc
103
     */
104
    protected function setOutput(OutputInterface $output): self
105
    {
106
        $this->output = $output;
×
107
        return $this;
×
108
    }
109

110
    /**
111
     * @svc
112
     */
113
    protected function run(array $options = [])
114
    {
115
        if (isset($options['name'])) {
×
116
            $this->runOne($options['name']);
×
117
            return;
×
118
        }
119

120
        if (isset($options['from'])) {
×
121
            $this->runFrom($options['from']);
×
122
            return;
×
123
        }
124

125
        $classes = $this->getSeederClasses();
×
126

127
        $seeders = $this->db->init($this->table)
×
128
            ->desc('id')
×
129
            ->indexBy('id')
×
130
            ->fetchAll();
×
131

132
        foreach ($seeders as $id => $seeder) {
×
133
            if (isset($classes[$id])) {
×
134
                unset($classes[$id]);
×
135
            }
136
        }
137

138
        if (!$classes) {
×
139
            $this->writeln('<info>Nothing to run.</info>');
×
140
            return;
×
141
        }
142

143
        foreach ($classes as $name => $class) {
×
144
            $this->runByName($name, $classes);
×
145
        }
146
    }
147

148
    protected function runFrom($name)
149
    {
150
        $classes = $this->getSeederClasses();
×
151

152
        if ('root' !== $name) {
×
153
            if (!isset($classes[$name])) {
×
154
                $this->writeln(sprintf('<error>Seeder "%s" not found</error>', $name));
×
155
                return;
×
156
            }
157
            $classes = array_slice($classes, array_search($name, array_keys($classes), true));
×
158
        }
159

160
        foreach ($classes as $name => $class) {
×
161
            $this->runByName($name, $classes);
×
162
        }
163
    }
164

165
    protected function runOne($name)
166
    {
167
        $classes = $this->getSeederClasses();
×
168
        $this->runByName($name, $classes);
×
169
    }
170

171
    protected function runByName($name, $classes)
172
    {
173
        if (!isset($classes[$name])) {
×
174
            $this->writeln(sprintf('<error>Seeder "%s" not found</error>', $name));
×
175
            return;
×
176
        }
177

178
        $seeder = $this->newInstance($classes[$name]);
×
179
        $seeder->run();
×
180

181
        if ($this->db->select($this->table, ['id' => $name])) {
×
182
            $this->db->update($this->table, [
×
183
                'updated_at' => date('Y-m-d H:i:s'),
×
UNCOV
184
            ], ['id' => $name]);
×
185
        } else {
186
            $this->db->insert($this->table, [
×
UNCOV
187
                'id' => $name,
×
188
                'created_at' => date('Y-m-d H:i:s'),
×
189
                'updated_at' => date('Y-m-d H:i:s'),
×
190
            ]);
191
        }
192

193
        $this->writeln('<info>Ran: </info>' . $name);
×
194
    }
195

196
    protected function getSeederClasses(): array
197
    {
198
        return $this->classMap->generate($this->paths, '/Seeder/*.php', 'Seeder', false);
×
199
    }
200

201
    /**
202
     * @param array $options
203
     * @throws \Exception
204
     * @svc
205
     */
206
    protected function create(array $options)
207
    {
208
        $class = 'V' . date('YmdHis') . ucfirst($options['name']);
×
209
        $path = $options['path'] ?? $this->defaultPath;
×
210
        $namespace = $options['namespace'] ?? $this->defaultNamespace;
×
211

212
        if (!$path) {
×
213
            $this->writeln('<error>Path should not be empty</error>');
×
214
            return;
×
215
        }
216

217
        $content = $this->generateContent(compact('namespace', 'class'));
×
218

219
        $this->makeDir($path);
×
220
        $file = $path . '/' . $class . '.php';
×
221
        file_put_contents($file, $content);
×
222
        $this->writeln(sprintf('<info>Created the file: %s</info>', $file));
×
223
    }
224

225
    protected function generateContent($vars): string
226
    {
227
        extract($vars);
×
228

229
        ob_start();
×
230
        require __DIR__ . '/../Seeder/stubs/seeder.php';
×
231
        return ob_get_clean();
×
232
    }
233

234
    protected function getRanIds(): array
235
    {
236
        $seeders = $this->db->init($this->table)
×
237
            ->desc('id')
×
238
            ->indexBy('id')
×
239
            ->fetchAll();
×
240

241
        return array_keys($seeders);
×
242
    }
243

244
    /**
245
     * Create a instance from seeder class
246
     *
247
     * @param string $class
248
     * @return BaseSeeder
249
     */
250
    protected function newInstance(string $class): BaseSeeder
251
    {
252
        return new $class([
×
253
            'wei' => $this->wei,
×
254
        ]);
255
    }
256

257
    /**
258
     * Check if table exists, if not exists, create table
259
     */
260
    protected function prepareTable()
261
    {
262
        if (!$this->schema->hasTable($this->table)) {
×
263
            $this->schema->table($this->table)
×
264
                ->string('id', 128)
×
265
                ->timestamp('created_at')
×
266
                ->timestamp('updated_at')
×
267
                ->exec();
×
268
        }
269
    }
270

271
    protected function writeln($output)
272
    {
273
        if ($this->output) {
×
274
            $this->output->writeln($output);
×
275
        }
276
    }
277

278
    protected function makeDir($path)
279
    {
280
        if (!is_dir($path)) {
×
281
            mkdir($path, 0777, true);
×
282
            chmod($path, 0777);
×
283
        }
284
    }
285
}
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

© 2026 Coveralls, Inc