chengkun
2025-06-05 4080b5997b38ca84b3b203c7101dcadb97b76925
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
<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 刘志淳 <chun@engineer.com>
// +----------------------------------------------------------------------
 
namespace think\console\command;
 
use think\console\Command;
use think\console\Input;
use think\console\input\Argument;
use think\console\Output;
 
abstract class Make extends Command
{
    protected $type;
 
    abstract protected function getStub();
 
    protected function configure()
    {
        $this->addArgument('name', Argument::REQUIRED, "The name of the class");
    }
 
    protected function execute(Input $input, Output $output)
    {
        $name = trim($input->getArgument('name'));
 
        $classname = $this->getClassName($name);
 
        $pathname = $this->getPathName($classname);
 
        if (is_file($pathname)) {
            $output->writeln('<error>' . $this->type . ':' . $classname . ' already exists!</error>');
            return false;
        }
 
        if (!is_dir(dirname($pathname))) {
            mkdir(dirname($pathname), 0755, true);
        }
 
        file_put_contents($pathname, $this->buildClass($classname));
 
        $output->writeln('<info>' . $this->type . ':' . $classname . ' created successfully.</info>');
    }
 
    protected function buildClass(string $name)
    {
        $stub = file_get_contents($this->getStub());
 
        $namespace = trim(implode('\\', array_slice(explode('\\', $name), 0, -1)), '\\');
 
        $class = str_replace($namespace . '\\', '', $name);
 
        return str_replace(['{%className%}', '{%actionSuffix%}', '{%namespace%}', '{%app_namespace%}'], [
            $class,
            $this->app->config->get('route.action_suffix'),
            $namespace,
            $this->app->getNamespace(),
        ], $stub);
    }
 
    protected function getPathName(string $name): string
    {
        $name = substr($name, 4);
 
        return $this->app->getBasePath() . ltrim(str_replace('\\', '/', $name), '/') . '.php';
    }
 
    protected function getClassName(string $name): string
    {
        if (str_contains($name, '\\')) {
            return $name;
        }
 
        if (str_contains($name, '@')) {
            [$app, $name] = explode('@', $name);
        } else {
            $app = '';
        }
 
        if (str_contains($name, '/')) {
            $name = str_replace('/', '\\', $name);
        }
 
        return $this->getNamespace($app) . '\\' . $name;
    }
 
    protected function getNamespace(string $app): string
    {
        return 'app' . ($app ? '\\' . $app : '');
    }
 
}