github.com/goravel/framework@v1.13.9/database/console/factory_make_command.go (about)

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