github.com/goravel/framework@v1.13.9/http/console/request_make_command.go (about)

     1  package console
     2  
     3  import (
     4  	"errors"
     5  	"os"
     6  	"path/filepath"
     7  	"strings"
     8  
     9  	"github.com/goravel/framework/contracts/console"
    10  	"github.com/goravel/framework/contracts/console/command"
    11  	"github.com/goravel/framework/support/file"
    12  	"github.com/goravel/framework/support/str"
    13  
    14  	"github.com/gookit/color"
    15  )
    16  
    17  type RequestMakeCommand struct {
    18  }
    19  
    20  // Signature The name and signature of the console command.
    21  func (receiver *RequestMakeCommand) Signature() string {
    22  	return "make:request"
    23  }
    24  
    25  // Description The console command description.
    26  func (receiver *RequestMakeCommand) Description() string {
    27  	return "Create a new request class"
    28  }
    29  
    30  // Extend The console command extend.
    31  func (receiver *RequestMakeCommand) Extend() command.Extend {
    32  	return command.Extend{
    33  		Category: "make",
    34  	}
    35  }
    36  
    37  // Handle Execute the console command.
    38  func (receiver *RequestMakeCommand) Handle(ctx console.Context) error {
    39  	name := ctx.Argument(0)
    40  	if name == "" {
    41  		return errors.New("Not enough arguments (missing: name) ")
    42  	}
    43  
    44  	if err := file.Create(receiver.getPath(name), receiver.populateStub(receiver.getStub(), name)); err != nil {
    45  		return err
    46  	}
    47  
    48  	color.Greenln("Request created successfully")
    49  
    50  	return nil
    51  }
    52  
    53  func (receiver *RequestMakeCommand) getStub() string {
    54  	return Stubs{}.Request()
    55  }
    56  
    57  // populateStub Populate the place-holders in the command stub.
    58  func (receiver *RequestMakeCommand) populateStub(stub string, name string) string {
    59  	requestName, packageName, _ := receiver.parseName(name)
    60  
    61  	stub = strings.ReplaceAll(stub, "DummyRequest", str.Case2Camel(requestName))
    62  	stub = strings.ReplaceAll(stub, "DummyField", "Name string `form:\"name\" json:\"name\"`")
    63  	stub = strings.ReplaceAll(stub, "DummyPackage", packageName)
    64  
    65  	return stub
    66  }
    67  
    68  // getPath Get the full path to the command.
    69  func (receiver *RequestMakeCommand) getPath(name string) string {
    70  	pwd, _ := os.Getwd()
    71  
    72  	requestName, _, folderPath := receiver.parseName(name)
    73  
    74  	return filepath.Join(pwd, "app", "http", "requests", folderPath, str.Camel2Case(requestName)+".go")
    75  }
    76  
    77  // parseName Parse the name to get the request name, package name and folder path.
    78  func (receiver *RequestMakeCommand) parseName(name string) (string, string, string) {
    79  	name = strings.TrimSuffix(name, ".go")
    80  
    81  	segments := strings.Split(name, "/")
    82  
    83  	requestName := segments[len(segments)-1]
    84  
    85  	packageName := "requests"
    86  	folderPath := ""
    87  
    88  	if len(segments) > 1 {
    89  		folderPath = filepath.Join(segments[:len(segments)-1]...)
    90  		packageName = segments[len(segments)-2]
    91  	}
    92  
    93  	return requestName, packageName, folderPath
    94  }