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

miaoxing / plugin / 9719022737

27 Jun 2024 12:34PM UTC coverage: 43.355%. Remained the same
9719022737

push

github

twinh
style: php-cs-fixer string_implicit_backslashes

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

1256 of 2897 relevant lines covered (43.36%)

7.68 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
    {
25
        $name = $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();
×
NEW
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