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

miaoxing / plugin / 11490866867

21 Oct 2024 01:18AM UTC coverage: 42.957%. Remained the same
11490866867

push

github

twinh
feat(plugin): `g:relation` 命令支持传入首字母大写的模型名称,例如粘贴复制的类名

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

1235 of 2875 relevant lines covered (42.96%)

38.11 hits per line

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

0.0
/src/Command/GRelation.php
1
<?php
2

3
namespace Miaoxing\Plugin\Command;
4

5
use Miaoxing\Plugin\Service\Fs;
6
use Nette\PhpGenerator\PhpFile;
7
use Nette\PhpGenerator\PsrPrinter;
8
use Symfony\Component\Console\Input\InputArgument;
9
use Wei\BaseModel;
10

11
/**
12
 * @mixin \StrMixin
13
 */
14
class GRelation extends BaseCommand
15
{
16
    protected function configure()
17
    {
18
        $this->setDescription('Generate relation traits')
×
19
            ->addArgument('model', InputArgument::REQUIRED, 'The name of the model')
×
20
            ->addArgument('belongs-to-many', InputArgument::OPTIONAL, 'Whether generate belongs to many trait', false);
×
21
    }
22

23
    protected function handle()
24
    {
NEW
25
        $name = lcfirst($this->getArgument('model'));
×
26
        if (!$this->wei->has($name)) {
×
27
            return $this->err(sprintf('模型 %s 不存在', $name));
×
28
        }
29

30
        $class = $this->wei->getClass($name);
×
31
        if (!is_subclass_of($class, BaseModel::class)) {
×
32
            return $this->err(sprintf('类 %s 不是模型类', $class));
×
33
        }
34

35
        $this->createTrait($name, 'belongsTo');
×
36
        $this->createTrait($name, 'hasOne');
×
37
        $this->createTrait($name, 'hasMany', true);
×
38
        $this->getArgument('belongs-to-many') && $this->createTrait($name, 'belongsToMany', true);
×
39
    }
40

41
    protected function createTrait($name, $relation, $isPluralize = false)
42
    {
43
        // Prepare variables
44
        $methodName = substr($name, 0, -strlen('Model'));
×
45
        if ($isPluralize) {
×
46
            $methodName = $this->str->pluralize($methodName);
×
47
        }
48

49
        $class = $this->wei->getClass($name);
×
50
        $pluginNamespace = explode('\\', $class)[1];
×
51
        $classBaseName = ucfirst($name);
×
52
        $fileName = ucfirst($relation) . ucfirst($methodName) . 'Trait';
×
53

54
        // Generate file
55
        $phpFile = new PhpFile();
×
56
        $namespace = $phpFile->addNamespace('Miaoxing\\' . $pluginNamespace . '\Model');
×
57
        $namespace->addUse($class);
×
58

59
        // Generate trait
60
        $trait = $namespace->addTrait($fileName);
×
61
        $propertyType = $isPluralize ? ($classBaseName . '|' . $classBaseName . '[]') : $classBaseName;
×
62
        $trait->setComment('@property ' . $propertyType . ' $' . $methodName);
×
63
        $trait->addMethod($methodName)
×
64
            ->setBody('return $this->' . $relation . '(' . $classBaseName . '::class);')
×
65
            ->setReturnType($class);
×
66

67
        // Write to file
68
        $pluginId = $this->str->dash($pluginNamespace);
×
69
        $file = 'plugins/' . $pluginId . '/src/Model/' . $fileName . '.php';
×
70

71
        $printer = new PsrPrinter();
×
72
        $content = $printer->printFile($phpFile);
×
73

74
        $this->createFile($file, $content);
×
75
    }
76

77
    protected function createFile($file, $content)
78
    {
79
        $this->suc('生成文件 ' . $file);
×
80

81
        Fs::ensureDir(dirname($file));
×
82

83
        file_put_contents($file, $content);
×
84
        chmod($file, 0777);
×
85
    }
86
}
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